From: Stefan Hajnoczi <stefanha@gmail.com>
To: Eric Blake <eblake@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>,
"Shergill, Gurinder" <gurinder.shergill@hp.com>,
qemu-devel@nongnu.org, Luiz Capitulino <lcapitulino@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>,
"Vinod, Chegu" <chegu_vinod@hp.com>
Subject: Re: [Qemu-devel] [PATCH v2 1/2] iothread: stash thread ID away
Date: Thu, 27 Feb 2014 11:24:51 +0100 [thread overview]
Message-ID: <20140227102451.GD30387@stefanha-thinkpad.redhat.com> (raw)
In-Reply-To: <530CD3FD.4030609@redhat.com>
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
next prev parent reply other threads:[~2014-02-27 10:25 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-02-25 17:19 [Qemu-devel] [PATCH v2 0/2] dataplane: add query-iothreads QMP command Stefan Hajnoczi
2014-02-25 17:19 ` [Qemu-devel] [PATCH v2 1/2] iothread: stash thread ID away Stefan Hajnoczi
2014-02-25 17:33 ` Eric Blake
2014-02-27 10:24 ` Stefan Hajnoczi [this message]
2014-02-25 17:19 ` [Qemu-devel] [PATCH v2 2/2] qmp: add query-iothreads command Stefan Hajnoczi
2014-02-25 17:36 ` Eric Blake
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=20140227102451.GD30387@stefanha-thinkpad.redhat.com \
--to=stefanha@gmail.com \
--cc=chegu_vinod@hp.com \
--cc=eblake@redhat.com \
--cc=gurinder.shergill@hp.com \
--cc=kwolf@redhat.com \
--cc=lcapitulino@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.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.
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).