9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Bakul Shah <bakul+plan9@bitblocks.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] sleep/wakeup bug?
Date: Fri, 25 Feb 2011 01:18:14 -0800	[thread overview]
Message-ID: <20110225091814.3387EB832@mail.bitblocks.com> (raw)
In-Reply-To: Your message of "Fri, 25 Feb 2011 09:37:39 +0100." <9e5ef05b5cfedd0faa831cc7c0d57f74@plan9.cs.bell-labs.com>

On Fri, 25 Feb 2011 09:37:39 +0100 Sape Mullender <sape@plan9.bell-labs.com>  wrote:
> I suppose the use of counting semaphores in sleep/wakeup could
> help in cases like this (but I'm sure there are still plenty of
> other scenarios where they might not help).  The value of the
> semaphore would represent something like "number of things to
> do", so acquire(sema) would (atomically) wait until the value
> of sema is greater than zero, then (using compare&swap, or
> doing the whole thing inside an ilock) decrement the semaphore
> and continue.
> Release(sema) will (atomically) increment the semaphore and, if the
> old value was zero, wake up any waiters.
>
> Now, at first glance that looks like a vast improvement over sleep/
> wakeup, but *inside* acquire and release, you'd still have sleep/wakeup
> and you'd still run the risk of waking up just when something else
> managed to grab the semaphore, or waking up something that hasn't
> actually gone to sleep yet.
>
> So, I think you can think of semaphores as a wrapper for sleep/wakeup
> that can be used in some case to make sure that you can indeed safely
> do a free() of some memory (this was, I think what started the whole
> discussion).

wait(sema) & signal(sema) in either order would do proper
synchronization. Not the case with sleep/wakeup -- they are cheaper
though.

> It's taken a long time to get sleep/wakeup bugfree in Plan 9 and
> some of the greatest minds in code verification (formerly at Bell Labs)
> have been called upon to help get it right.
>
> Russ is perfectly correct in the explanations below and it's a good
> exercise to read through it.  This stuff is really tricky.  Many
> optimization, all of them seemingly correct, failed because of subtle
> race conditions, some of them involving three or more processes.

Is it inherently tricky? Aren't semaphores easier to reason about
and get right?



  reply	other threads:[~2011-02-25  9:18 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-25  5:26 erik quanstrom
2011-02-25  5:47 ` Russ Cox
2011-02-25  5:53   ` erik quanstrom
2011-02-25  6:01     ` Russ Cox
2011-02-25  6:12       ` erik quanstrom
     [not found]       ` <2808a9fa079bea86380a8d52be67b980@coraid.com>
     [not found]         ` <AANLkTi=4_=++Tm2a9Jq9jSzqUSexkW-ZjM-38oD_bS1y@mail.gmail.com>
     [not found]           ` <40925e8f64489665bd5bd6ca743400ea@coraid.com>
2011-02-25  6:51             ` Russ Cox
2011-02-25  7:13               ` erik quanstrom
2011-02-25 14:44                 ` Russ Cox
2011-02-25  8:37               ` Sape Mullender
2011-02-25  9:18                 ` Bakul Shah [this message]
2011-02-25 14:57               ` Charles Forsyth
2011-02-25 16:09               ` Venkatesh Srinivas
  -- strict thread matches above, loose matches on Subject: below --
2011-02-24 22:01 erik quanstrom
2011-02-25  4:46 ` Russ Cox
2011-02-25  9:46 ` Richard Miller

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=20110225091814.3387EB832@mail.bitblocks.com \
    --to=bakul+plan9@bitblocks.com \
    --cc=9fans@9fans.net \
    /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).