9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] How it work?
@ 2005-03-22 19:00 Sergey Reva
  2005-03-23  0:40 ` Tim Newsham
  0 siblings, 1 reply; 8+ messages in thread
From: Sergey Reva @ 2005-03-22 19:00 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Hello Fans!

I try understand synchronization in Plan 9.
I find test-and-set routines (for PC) in l.s, but I can't find where
it used. Also I can't find any reason why it unused... so where it used?

And why rendezvous is syscall, but lock, qlock are library routines
which look like the same as kernel qlock.c

--
http://rs-rlab.narod.ru                          mailto:rs_rlab@mail.ru



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] How it work?
  2005-03-22 19:00 [9fans] How it work? Sergey Reva
@ 2005-03-23  0:40 ` Tim Newsham
  2005-03-23  6:57   ` Sergey Reva
  0 siblings, 1 reply; 8+ messages in thread
From: Tim Newsham @ 2005-03-23  0:40 UTC (permalink / raw)
  To: Sergey Reva, Fans of the OS Plan 9 from Bell Labs

> I try understand synchronization in Plan 9.
> I find test-and-set routines (for PC) in l.s, but I can't find where
> it used. Also I can't find any reason why it unused... so where it used?

The kernel's files are spread out over /sys/src/9/{pc,port,ip}.
Take a look at port/{qlock,taslock}.c.

> And why rendezvous is syscall, but lock, qlock are library routines
> which look like the same as kernel qlock.c

There are several types of locks.  Some (like spin locks) can operate
without any assistance from the kernel (not entirely, they yield with a
syscall).  Others (like rendezvous) use the kernel to mediate between the
processes. The lock(2) man page has more details.

Tim N.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] How it work?
  2005-03-23  0:40 ` Tim Newsham
@ 2005-03-23  6:57   ` Sergey Reva
  2005-03-23 17:34     ` Russ Cox
  0 siblings, 1 reply; 8+ messages in thread
From: Sergey Reva @ 2005-03-23  6:57 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Hello fans

Thanks Tim for advice!

Now I find:
1. "lock" used tas (test-and-set), in libc too
2. "queuing lock" use "lock", and also suspend task which wait locking
3. "rendezvous" can handle two task (and live in kernel)
4. "Rendez" (as subset of rsleep;rwakeup;rwakeupall) can handle multiple
   tasks (and live in libc)

Is it right?

TN> There are several types of locks.  Some (like spin locks) can operate
TN> without any assistance from the kernel (not entirely, they yield with a
TN> syscall).  Others (like rendezvous) use the kernel to mediate between the
TN> processes.
As I seen both rendezvous and Rendez(rsleep;rwakeup;rwakeupall)
change state of process but Rendez can do this even in libc, where is
different other than state of process: Rendezvous (in syscall) and
Sleep (in rendez)? why rendezvous need to be at kernel?

--
http://rs-rlab.narod.ru                            mailto:rs_rlab@mail.ru



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] How it work?
  2005-03-23  6:57   ` Sergey Reva
@ 2005-03-23 17:34     ` Russ Cox
  2005-03-24  8:46       ` Sergey Reva
  0 siblings, 1 reply; 8+ messages in thread
From: Russ Cox @ 2005-03-23 17:34 UTC (permalink / raw)
  To: Sergey Reva, Fans of the OS Plan 9 from Bell Labs

The rendezvous system call is the only synchronization
method the kernel provides to user space.  QLocks,
RWLocks, and Rendezes all use rendezvous() to make
a process go to sleep or to wake it up.

You asked why the rendezvous system needed to be in
the kernel.  Where else would it be?  How else could a
process arrange to go to sleep?

Russ


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] How it work?
  2005-03-23 17:34     ` Russ Cox
@ 2005-03-24  8:46       ` Sergey Reva
  2005-03-24  8:55         ` noselasd
       [not found]         ` <4439b95e268ad4987f174271ac554f28@smtp.songnetworks.no>
  0 siblings, 2 replies; 8+ messages in thread
From: Sergey Reva @ 2005-03-24  8:46 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Hello Russ,

Wednesday, March 23, 2005, 7:34:16 PM, you wrote:
RC> You asked why the rendezvous system needed to be in
RC> the kernel.  Where else would it be?  How else could a
RC> process arrange to go to sleep?
It's because understanding of code bring to me on next morning :)
--
http://rs-rlab.narod.ru                            mailto:rs_rlab@mail.ru



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] How it work?
  2005-03-24  8:46       ` Sergey Reva
@ 2005-03-24  8:55         ` noselasd
       [not found]         ` <4439b95e268ad4987f174271ac554f28@smtp.songnetworks.no>
  1 sibling, 0 replies; 8+ messages in thread
From: noselasd @ 2005-03-24  8:55 UTC (permalink / raw)
  To: rs_rlab, 9fans

> Hello Russ,
>
> Wednesday, March 23, 2005, 7:34:16 PM, you wrote:
> RC> You asked why the rendezvous system needed to be in
> RC> the kernel.  Where else would it be?  How else could a
> RC> process arrange to go to sleep?
> It's because understanding of code bring to me on next morning :)

Unless you already got it, peek at
http://plan9.escet.urjc.es/usr/nemo/9.txt.gz
It's for the 3.ed but nevertheless mostly helpful.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] How it work?
       [not found]         ` <4439b95e268ad4987f174271ac554f28@smtp.songnetworks.no>
@ 2005-03-25 10:33           ` Sergey Reva
  2005-03-25 17:08             ` Russ Cox
  0 siblings, 1 reply; 8+ messages in thread
From: Sergey Reva @ 2005-03-25 10:33 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Hello Fans!

Still need help with Rendez...
I can use lock, qlock and rendezvous without any problem but Rendez
don't work... if rsleep executed earlier than rwakeup, abort() cause
exception, stk() show:
rsleep ... /sys/src/libc/9sys/qlock.c:286
This line contain me = getqlp() in getqlp i find abort()...

in other way when rwakeup executed first, abort() ... stk() show:
rwakeup ... /sys/src/libc/9sys/qlock.c:329
which point me to another abort()

Maybe it wrong initialization? But I also try other way... or I wrong
use rendez?

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

QLock q={0};
Rendez r={.l=&q};

void testthread(void)
{
        switch(rfork(RFPROC|RFMEM))
        {
                case -1:
                        print("can't fork");
                        exits(0);
                case 0:
                        break;
                default:
                        return;
        }

        for(;;)
        {
                print("Sleep\n");
                rsleep(&r);
                print("Wake up!\n");
        }

        exits(0);
}

void main(int,char**)
{
        int x;

        testthread();

        for (x=0;x<10;)
        {
                if (rwakeup(&r))
                        x++;
        }
        exits(0);
}

Thanks for help!

--
http://rs-rlab.narod.ru                            mailto:rs_rlab@mail.ru



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [9fans] How it work?
  2005-03-25 10:33           ` Sergey Reva
@ 2005-03-25 17:08             ` Russ Cox
  0 siblings, 0 replies; 8+ messages in thread
From: Russ Cox @ 2005-03-25 17:08 UTC (permalink / raw)
  To: Sergey Reva, Fans of the OS Plan 9 from Bell Labs

lock(2):

          Rendezes are rendezvous points.  Each Rendez r is protected
          by a QLock r->l, which must be held by the callers of
          rsleep, rwakeup, and rwakeupall. Rsleep atomically releases
          r->l and suspends execution of the calling task.  After
          resuming execution, rsleep will reacquire r->l before
          returning.  If any processes are sleeping on r, rwakeup
          wakes one of them.  it returns 1 if a process was awakened,
          0 if not.  Rwakeupall wakes all processes sleeping on r,
          returning the number of processes awakened.  Rwakeup and
          rwakeupall do not release r->l and do not suspend execution
          of the current task.

          Before use, Rendezes should be initialized to all zeros
          except for r->l pointer, which should point at the QLock
          that will guard r.

You're not locking the qlock first.

> I can use lock, qlock and rendezvous without any problem but Rendez
> don't work... if rsleep executed earlier than rwakeup, abort() cause
> exception, stk() show:
> rsleep ... /sys/src/libc/9sys/qlock.c:286
> This line contain me = getqlp() in getqlp i find abort()...

You're being misled.  The line that stk() shows is the
code that the call to abort will return to, which is
sometimes the next line of code.  The actual
call is earlier, on line 283 or so:

	/* we should hold the qlock */
	if(!r->l->locked)
>		abort();

	/* add ourselves to the wait list */
	me = getqlp();

Since getqlp wasn't on your stack frame, you can be
pretty sure that's not what called abort.  (The Plan 9
compilers thankfully don't do any tail call elimination.)

> in other way when rwakeup executed first, abort() ... stk() show:
> rwakeup ... /sys/src/libc/9sys/qlock.c:329
> which point me to another abort()

This time the line ref is correct.

> Maybe it wrong initialization? But I also try other way... or I wrong
> use rendez?

Well, yes.  In addition to not locking the qlock before
calling rsleep or rwakeup, you're not actually checking
a condition.

>         for(;;)
>         {
>                 print("Sleep\n");
>                 rsleep(&r);
>                 print("Wake up!\n");
>         }

>         for (x=0;x<10;)
>         {
>                 if (rwakeup(&r))
>                         x++;
>         }

I guess this technically will work, but the standard usage
is that you're expecting some condition to happen, like
there's a shared queue and one thread is waiting for more
input and the other thread, which has just added to the queue,
wants to wake him.  Commonly you see:

    qlock(&l);
    while(bad condition)
        rsleep(&r);
    code depending on good condition
    qunlock(&l)

and

    qlock(&l)
    make condition good
    rwakeup(&r)
    qunlock(&l)

It's like the sleep and wakeup in the kernel except that
the locking is a little different.

See http://swtch.com/usr/local/plan9/src/libmux/queue.c
for a full example.  It's an arbitrarily buffered queue.
The Rendez q->r is slept on by receivers waiting for
something to be put into the queue.

Russ


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-03-25 17:08 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-22 19:00 [9fans] How it work? Sergey Reva
2005-03-23  0:40 ` Tim Newsham
2005-03-23  6:57   ` Sergey Reva
2005-03-23 17:34     ` Russ Cox
2005-03-24  8:46       ` Sergey Reva
2005-03-24  8:55         ` noselasd
     [not found]         ` <4439b95e268ad4987f174271ac554f28@smtp.songnetworks.no>
2005-03-25 10:33           ` Sergey Reva
2005-03-25 17:08             ` Russ Cox

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).