From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34551) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WIy9I-0004hf-1A for qemu-devel@nongnu.org; Thu, 27 Feb 2014 05:25:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WIy99-0008OG-Jz for qemu-devel@nongnu.org; Thu, 27 Feb 2014 05:25:03 -0500 Received: from mail-wi0-x22a.google.com ([2a00:1450:400c:c05::22a]:47834) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WIy99-0008O6-CH for qemu-devel@nongnu.org; Thu, 27 Feb 2014 05:24:55 -0500 Received: by mail-wi0-f170.google.com with SMTP id hi5so443159wib.1 for ; Thu, 27 Feb 2014 02:24:54 -0800 (PST) Date: Thu, 27 Feb 2014 11:24:51 +0100 From: Stefan Hajnoczi Message-ID: <20140227102451.GD30387@stefanha-thinkpad.redhat.com> References: <1393348774-14663-1-git-send-email-stefanha@redhat.com> <1393348774-14663-2-git-send-email-stefanha@redhat.com> <530CD3FD.4030609@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <530CD3FD.4030609@redhat.com> Subject: Re: [Qemu-devel] [PATCH v2 1/2] iothread: stash thread ID away List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: Kevin Wolf , "Shergill, Gurinder" , qemu-devel@nongnu.org, Luiz Capitulino , Stefan Hajnoczi , Paolo Bonzini , "Vinod, Chegu" On Tue, Feb 25, 2014 at 10:33:49AM -0700, Eric Blake wrote: > On 02/25/2014 10:19 AM, Stefan Hajnoczi wrote: > > Keep the thread ID around so we can report it via QMP. > > > > There's only one problem: qemu_get_thread_id() (gettid() wrapper on > > Linux) must be called from the thread itself. There is no way to get > > the thread ID outside the thread. > > > > This patch uses a condvar to wait for iothread_run() to populate the > > thread_id inside the thread. > > > > > + > > + /* Wait for initialization to complete */ > > + qemu_mutex_lock(&iothread->init_done_lock); > > + qemu_cond_wait(&iothread->init_done_cond, > > + &iothread->init_done_lock); > > + qemu_mutex_unlock(&iothread->init_done_lock); > > Generally, cond_wait needs to be done in a loop, where you also check > the condition (in this case, that thread_id was actually set) - a > pthread implementation is allowed to do spurious wakeups even if no > other thread has managed to change the condition that you were waiting for. You are right, POSIX is explicit about this: "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 figured no other thread issues pthread_cond_signal/broadcast so spurious wakeups cannot occur but I was wrong. Stefan