mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Szabolcs Nagy <nsz@port70.net>
To: musl@lists.openwall.com
Subject: Re: bug in pthread_cond_broadcast
Date: Tue, 12 Aug 2014 18:50:34 +0200	[thread overview]
Message-ID: <20140812165033.GM22308@port70.net> (raw)
In-Reply-To: <1407801532.15134.96.camel@eris.loria.fr>

* Jens Gustedt <jens.gustedt@inria.fr> [2014-08-12 01:58:52 +0200]:
> thread_ret client(void *arg) {
>   unsigned * number = arg;
>   for (unsigned i = 0; i < phases; ++i) {
>     trace("thread %u in phase %u\n", *number, i);
>     mutex_lock(&mut[i]);
>     ++inside[i];
>     if (inside[i] == threads) {
>       trace("thread %u is last, signalling main\n", *number);
>       int ret = condition_signal(&cond_main);

the last client at the end of phase 0 wakes the main thread here

the main thead is waiting on cond_main using mut[0]

>       trace("thread %u is last, signalling main, %s\n", *number, errorstring(ret));
>     }
>     while (i == phase) {
>       tell("thread %u in phase %u (%u), waiting\n", *number, i, phase);
>       int ret = condition_wait(&cond_client, &mut[i]);
>       trace("thread %u in phase %u (%u), finished, %s\n", *number, i, phase, errorstring(ret));

the last client thread will wait here unlocking mut[0] so
the main thread can continue

the main thread broadcast wakes all clients while holding both
mut[0] and mut[1] then unlocks mut[0] and starts waiting on
cond_main using mut[1]

the awaken clients will go into the next phase locking mut[1]
and waiting on cond_client using mut[1]

however there might be still clients waiting on cond_client
using mut[0] (eg. the broadcast is not yet finished)

i see logs where one thread is already in phase 1 (using mut[1])
while another is not yet out of condition_wait (using mut[0]):

pthread_cond_smasher.c:120: thread 3 in phase 1 (1), waiting
pthread_cond_smasher.c:122: thread 6 in phase 0 (1), finished, No error information

"When a thread waits on a condition variable, having specified a particular
 mutex to either the pthread_cond_timedwait() or the pthread_cond_wait()
 operation, a dynamic binding is formed between that mutex and condition
 variable that remains in effect as long as at least one thread is blocked
 on the condition variable. During this time, the effect of an attempt by
 any thread to wait on that condition variable using a different mutex is
 undefined. "

so are all clients considered unblocked after a broadcast?

>     }
>     int ret = mutex_unlock(&mut[i]);
>     trace("thread %u in phase %u (%u), has unlocked mutex: %s\n", *number, i, phase, errorstring(ret));
>   }
>   return 0;
> }
> 
> 
> int main(void) {
>   tell("start up of main, using %s, library %s\n", VERSION, LIBRARY);
>   condition_init(&cond_client);
>   condition_init(&cond_main);
>   for (unsigned i = 0; i < phases; ++i) {
>     mutex_init(&mut[i]);
>   }
>   mutex_lock(&mut[0]);
> 
>   for (unsigned i = 0; i < threads; ++i) {
>     args[i] = i;
>     thread_create(&id[i], client, &args[i]);
>   }
> 
>   while (phase < phases) {
>     while (inside[phase] < threads) {
>       trace("main seeing %u threads in phase %u, waiting\n", inside[phase], phase);
>       int ret = condition_wait(&cond_main, &mut[phase]);
>       tell("main seeing %u threads in phase %u, %s\n", inside[phase], phase, errorstring(ret));
>     }
>     /* now we know that everybody is waiting inside, lock the next
>        mutex, if any, such that nobody can enter the next phase
>        without our permission. */
>     if (phase < phases-1)
>       mutex_lock(&mut[phase+1]);
>     /* Now signal all clients, update the phase count and release the
>        mutex they are waiting for. */
>     int ret = condition_broadcast(&cond_client);
>     trace("main has broadcast to %u: %s\n", phase, errorstring(ret));
>     ++phase;
>     ret = mutex_unlock(&mut[phase-1]);
>     trace("main has unlocked mutex %u: %s\n", phase-1, errorstring(ret));
>   }
> 
> 


  reply	other threads:[~2014-08-12 16:50 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-11 23:58 Jens Gustedt
2014-08-12 16:50 ` Szabolcs Nagy [this message]
2014-08-12 17:19   ` Rich Felker
2014-08-12 18:18     ` Jens Gustedt
2014-08-12 21:20       ` Rich Felker
2014-08-12 22:50         ` Jens Gustedt
2014-08-12 23:33           ` Rich Felker
2014-08-13  4:11             ` Rich Felker
2014-08-13  0:30           ` Rich Felker
2014-08-13  7:00             ` Jens Gustedt
2014-08-13 12:34               ` Rich Felker

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=20140812165033.GM22308@port70.net \
    --to=nsz@port70.net \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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