From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/5805 Path: news.gmane.org!not-for-mail From: Szabolcs Nagy Newsgroups: gmane.linux.lib.musl.general Subject: Re: bug in pthread_cond_broadcast Date: Tue, 12 Aug 2014 18:50:34 +0200 Message-ID: <20140812165033.GM22308@port70.net> References: <1407801532.15134.96.camel@eris.loria.fr> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1407862254 20853 80.91.229.3 (12 Aug 2014 16:50:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 12 Aug 2014 16:50:54 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-5811-gllmg-musl=m.gmane.org@lists.openwall.com Tue Aug 12 18:50:47 2014 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1XHFHb-0005Gt-ED for gllmg-musl@plane.gmane.org; Tue, 12 Aug 2014 18:50:47 +0200 Original-Received: (qmail 26496 invoked by uid 550); 12 Aug 2014 16:50:46 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 26488 invoked from network); 12 Aug 2014 16:50:46 -0000 Mail-Followup-To: musl@lists.openwall.com Content-Disposition: inline In-Reply-To: <1407801532.15134.96.camel@eris.loria.fr> User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:5805 Archived-At: * Jens Gustedt [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)); > } > >