9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Russ Cox" <rsc@swtch.com>
To: 9fans@9fans.net
Subject: Re: [9fans] channel_lock
Date: Mon, 14 Jul 2008 17:04:14 -0400	[thread overview]
Message-ID: <20080714210249.08E4B1E8C1F@holo.morphisms.net> (raw)
In-Reply-To: <61d4bbe5c7dc2aec46ffb603ed219b88@quanstro.net>

> locks are not necessary between threads in the same proc
> because only one of them can run at a time and they are
> cooperatively scheduled.

It is important to note that using cooperatively scheduled threads
doesn't automatically mean you don't need locking.  If your
threads can go to sleep with some resources in intermediate states
that other threads should not observe, then you need locking.
It's just that instead of using Locks, which do not yield the current
thread, you should use QLocks, which do.

The precise version of Erik's statement is this:

    For resources shared only by threads in a single proc, Locks are either
    unnecessary (because there is no true concurrent access due to the
    cooperative scheduling) or incorrect (because spinning won't
    let the other thread run, so the lock will never be unlocked).

If the Lock would always protect a tiny critical section that
obviously doesn't yield or call any function that might yield
(qlock, alt, send, recv, etc.), then it's unnecessary.  Otherwise,
it's incorrect, and you should be using QLocks.

Russ



  reply	other threads:[~2008-07-14 21:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-14 18:47 palazzol
2008-07-14 19:07 ` erik quanstrom
2008-07-14 21:04   ` Russ Cox [this message]
2008-07-14 20:58 ` Russ Cox

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=20080714210249.08E4B1E8C1F@holo.morphisms.net \
    --to=rsc@swtch.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).