From owner-freebsd-hackers@FreeBSD.ORG Mon Feb 28 02:00:58 2011 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 41856106566B; Mon, 28 Feb 2011 02:00:58 +0000 (UTC) (envelope-from davidxu@freebsd.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 2FEBC8FC15; Mon, 28 Feb 2011 02:00:58 +0000 (UTC) Received: from xyf.my.dom (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.4/8.14.4) with ESMTP id p1S20u5i087494; Mon, 28 Feb 2011 02:00:57 GMT (envelope-from davidxu@freebsd.org) Message-ID: <4D6B01DB.9090909@freebsd.org> Date: Mon, 28 Feb 2011 10:00:59 +0800 From: David Xu User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.9.2.13) Gecko/20110127 Thunderbird/3.1.7 MIME-Version: 1.0 To: Yuri References: <4D6ABA14.80208@rawbw.com> <4D6AC17A.7020505@rawbw.com> In-Reply-To: <4D6AC17A.7020505@rawbw.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Cc: Garrett Cooper , freebsd-hackers@freebsd.org, standards@freebsd.org Subject: Re: Is pthread_cond_signal(3) man page correct? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 28 Feb 2011 02:00:58 -0000 On 2011/02/28 05:26, Yuri wrote: > Forwarding to standards@ and davidxu@ per Garrett Cooper suggestion. > > Also I want to add that I came to this question while observing behavior > consistent with multiple wakeup on FreeBSD-8.1. The heavily > multi-threaded code that assumes that only one thread can be woken up by > one pthread_cond_signal call crashes, and the only reasonable > explanation so far is that more than one threads are actually being > woken up. > > Yuri > > > On 02/27/2011 12:54, Yuri wrote: >> On FreeBSD-8.1 this page says: >> The pthread_cond_signal() function unblocks one thread waiting for the >> condition variable cond. >> >> On Linux it says: >> The /pthread_cond_signal/() function shall unblock at least one of the >> threads that are blocked on the specified condition variable /cond/ >> (if any threads are blocked on /cond/). >> >> Also HP page >> (http://docs.hp.com/en/B2355-90130/pthread_cond_signal.3T.html) says: >> "If there are no threads blocked on /cond/, this function has no >> effect." And later it says: "It is possible that more than one thread >> can be unblocked due to a spurious wakeup." >> >> This is quite confusing: in case nobody is waiting does it block or >> not? In case other threads are waiting it's really "any arbitrary >> number of threads are woken up"? Or on FreeBSD it's strictly 1? >> Shouldn't this be defined in one and only way by POSIX and all >> POSIX-compliant systems should work exactly the same. >> >> I think man page should be expanded to give more comprehensive >> explanation. >> >> Yuri >> _______________________________________________ >> freebsd-hackers@freebsd.org mailing list >> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers >> To unsubscribe, send any mail to >> "freebsd-hackers-unsubscribe@freebsd.org" >> It is really not important if pthread_cond_signal wake up one thread or multiple threads in corner case, because POSIX said: --- When using condition variables there is always a Boolean predicate involving shared variables associated with each condition wait that is true if the thread should proceed. Spurious wakeups from the pthread_cond_timedwait() or pthread_cond_wait() functions may occur. Since the return from pthread_cond_timedwait() or pthread_cond_wait() does not imply anything about the value of this predicate, the predicate should be re-evaluated upon such return. --- I think in normal case, pthread_cond_signal will wake up one thread, but other events for example, UNIX signal and fork() may interrupt a thread sleeping in kernel, and cause pthread_cond_wait to return to userland, this is called spurious wakeup, and other events, I can not think of yet, but I believe they exist. Regards, David Xu