unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: "Julian Graham" <joolean@gmail.com>
To: "Ludovic Courtès" <ludo@gnu.org>
Cc: guile-devel@gnu.org
Subject: Re: Race condition in threading code?
Date: Sun, 31 Aug 2008 11:05:17 -0400	[thread overview]
Message-ID: <2bc5f8210808310805p34f88f9em8f8d9c16fbf42df@mail.gmail.com> (raw)
In-Reply-To: <877i9x9w8j.fsf@gnu.org>

Hi Ludovic,

> Let me rephrase it: what can happen is that, during the tick, another
> thread could actually take M, increase `M->level' and mark itself as the
> owner.  After the tick, our primary thread takes `M->lock' back,
> thinking it now owns M, and goes to sleep; but M is actually already
> taken by that other thread, so our primary thread never wakes up.  (Not
> sure this description is any clearer...)

Almost, but not quite.  Let me try again:

Thread A wants to lock fat_mutex M.  It seizes the administrative lock
M->lock and examines the state of M.  M is held by thread B, so thread
A prepares to put itself onto the blocking queue for M by calling
`SCM_TICK'.  In order to call `SCM_TICK', thread A must temporarily
release M->lock.

When it does this, thread B, the owner of M, seizes M->lock and
releases M, which involves waking up the next waiting thread on the
blocking queue for M -- but thread A hasn't finished doing the tick
and so isn't on the blocking queue.  Thread B releases M->lock and
goes about its business.

Thread A finishes the tick and seizes M->lock again and adds itself to
the blocking queue for M without re-examining M's state.  The only way
thread A can ever wake up after this is if another thread locks and
releases M.


> I guess it can be applied to 1.8 as well?

I would say so, yes.  I'll make a patch against it if you tell me how
to do that with git.  :)


> Another question: why is there this mixture of `scm_i_pthread' and
> `scm_i_scm_pthread' calls?

The scm_i_pthread_* functions are actually preprocessor #defines that
map directly onto pthreads API functions.  The scm_i_scm_pthread_*
functions are wrappers around pthreads functions that could block --
the wrappers leave Guile mode before calling into pthreads.
pthread_mutex_lock can block, so from Guile mode (e.g., from
fat_mutex_lock), it needs to be called via
scm_i_scm_pthread_mutex_lock; but pthread_mutex_unlock can't block, so
it can be called directly via scm_i_pthread_mutex_unlock.

Is that what you were asking?


Regards,
Julian




  reply	other threads:[~2008-08-31 15:05 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-16 18:21 Race condition in threading code? Han-Wen Nienhuys
2008-08-16 18:42 ` Julian Graham
2008-08-16 18:45   ` Han-Wen Nienhuys
2008-08-26 20:23     ` Andy Wingo
2008-08-27  0:41       ` Han-Wen Nienhuys
2008-08-27  2:36       ` Han-Wen Nienhuys
2008-08-27  7:46       ` Ludovic Courtès
2008-08-27 13:14         ` Julian Graham
2008-08-30 23:05           ` Julian Graham
2008-08-31  1:49             ` Han-Wen Nienhuys
2008-08-31  2:54               ` Julian Graham
2008-08-31 13:52                 ` Han-Wen Nienhuys
2008-08-31 10:34               ` Ludovic Courtès
2008-08-31 12:58             ` Ludovic Courtès
2008-08-31 15:05               ` Julian Graham [this message]
2008-08-31 19:39                 ` Ludovic Courtès
2008-09-07  0:12                   ` Julian Graham
2008-09-08 12:37                     ` Ludovic Courtès
2008-08-31 20:08       ` Ludovic Courtès
2008-08-31 23:59         ` Han-Wen Nienhuys
2008-09-01  8:11           ` Ludovic Courtès
2008-09-01  0:18         ` Han-Wen Nienhuys
2008-09-03  4:56           ` Han-Wen Nienhuys
2008-09-04 18:12             ` Andy Wingo

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

  List information: https://www.gnu.org/software/guile/

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

  git send-email \
    --in-reply-to=2bc5f8210808310805p34f88f9em8f8d9c16fbf42df@mail.gmail.com \
    --to=joolean@gmail.com \
    --cc=guile-devel@gnu.org \
    --cc=ludo@gnu.org \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).