9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: erik quanstrom <quanstro@coraid.com>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] does qlock(2) block all threads on a proc?
Date: Thu,  9 Aug 2007 21:56:38 -0400	[thread overview]
Message-ID: <03466c823678888c6b62537ec193157b@coraid.com> (raw)
In-Reply-To: <c58b317a0708091714n4deef948g47b7f2e0cada34e0@mail.gmail.com>

qlock(2) doesn't block all threads in a proc.  i'm terrible
at explaining code, but here it goes....

from the source, you'll see that the thread library sets
up (/sys/src/libthread/main.c:35)

	_qlockinit(_threadrendezvous);

what does this do?  in /sys/src/libc/9sys/qlock.c we see
that this just sets a function pointer used in qlock.

qlock returns if unlocked otherwise calls the function
ptr set by _qlockinit() until it returns a magic value.

back in the thread library (/sys/src/libthread/rendez.c)
we find _threadrendezvous.  _threadrendezvous has
two cases.  there is a matching tag (somebody's waiting
to rendezvous with us), and there isn't.  the first case
is qunlock.  the second is qlock when the lock is locked.

in that case, we set set up for going to sleep and then
let _sched give the processor to another thread.

here's some code that shows how this works:

#include<u.h>
#include<libc.h>
#include<thread.h>

QLock q;

void
lockthread(void*)
{
	qlock(&q);
	for(;;){
		print("lockthread\n");
		sleep(100);
		yield();
	}
	qunlock(&q);
}

void
noisethread(void*)
{
	for(;;){
		print("hi mom\n");
		sleep(100);
		yield();
	}
}

void
threadmain(int, char**)
{
	threadcreate(noisethread, 0, 32*1024);
	threadcreate(lockthread, 0, 32*1024);
	yield();

	while(sleep(100) != -1){
		qlock(&q);

		/* never gets here */
		print("thread library confused\n");
		yield();
		qunlock(&q);
	}

	threadexitsall("");
}

perhaps qlock should be added to the man page in the
list of yielding functions.

- erik


  parent reply	other threads:[~2007-08-10  1:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-10  0:14 david jeannot
2007-08-10  0:26 ` Kris Maglione
2007-08-10  1:56 ` erik quanstrom [this message]
2007-08-10 16:16 ` Francisco J Ballesteros
2007-08-10 16:30   ` erik quanstrom
2007-08-10 18:37     ` Francisco J Ballesteros

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=03466c823678888c6b62537ec193157b@coraid.com \
    --to=quanstro@coraid.com \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).