From: Jamie Lokier <jamie@shareable.org>
To: Avi Kivity <avi@redhat.com>
Cc: qemu-devel@nongnu.org, "Andreas Färber" <andreas.faerber@web.de>,
"Glauber Costa" <glommer@redhat.com>,
aliguori@us.ibm.com
Subject: Re: [Qemu-devel] [PATCH 2/7] store thread-specific env information
Date: Sun, 29 Nov 2009 22:29:24 +0000 [thread overview]
Message-ID: <20091129222924.GA12299@shareable.org> (raw)
In-Reply-To: <4B129661.1000808@redhat.com>
> On 11/29/2009 05:38 PM, Andreas Färber wrote:
>> Am 29.11.2009 um 16:29 schrieb Avi Kivity:
>>> Where is __thread not supported?
>> Apple, Sun.
Some flavours of uClinux :-)
Avi Kivity wrote:
> Well, pthread_getspecific is around 130 bytes of code, whereas __thread
> is just on instruction. Maybe we should support both.
It's easy enough, they are quite similar. Except that
pthread_key_create lets you provide a destructor which is called as
each thread is destroyed (unfortunately no constructor for new
threads; and you can use both methods if you need a destructor and
speed together).
It's not always one instruction - it's more complicated in shared
libraries, but it's always close to that.
Anyway, I decided to measure them both as I wondered about this for
another program.
On my 2.0GHz Core Duo (32-bit), tight unrolled loop, everything in cache:
Read void *__thread variable ~ 0.6 ns
Call pthread_getspecific(key) ~ 8.8 ns
__thread is preferable but it's not much overhead to call pthread_getspecific().
Imho, it's not worth making code less portable or more complicated to
handle both, but it's a nice touch.
However, I did notice that the compiler optimises away references to
__thread variables much better, such as hoisting from inside loops.
In my programs I have taken to wrapping everything inside a
thread_specific(var) macro, similar to the one in the kernel, which
expands to call pthread_getspecific() or use __thread[*], That keeps the
complexity in one place, which is where the macro is defined.
( [*] - Windows has __thread, but it sometimes crashes when used in a
DLL, so I use the Windows equivalent of pthread_getspecific() in the
same wrapper macro, which is fine. )
-- Jamie
next prev parent reply other threads:[~2009-11-29 22:29 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-26 17:24 [Qemu-devel] [PATCH 0/7] KVM SMP support, early version Glauber Costa
2009-11-26 17:24 ` [Qemu-devel] [PATCH 1/7] Don't mess with halted state Glauber Costa
2009-11-26 17:24 ` [Qemu-devel] [PATCH 2/7] store thread-specific env information Glauber Costa
2009-11-26 17:24 ` [Qemu-devel] [PATCH 3/7] update halted state on mp_state sync Glauber Costa
2009-11-26 17:24 ` [Qemu-devel] [PATCH 4/7] qemu_flush_work for remote vcpu execution Glauber Costa
2009-11-26 17:24 ` [Qemu-devel] [PATCH 5/7] tell kernel about all registers instead of just mp_state Glauber Costa
2009-11-26 17:24 ` [Qemu-devel] [PATCH 6/7] Don't call kvm cpu reset on initialization Glauber Costa
2009-11-26 17:25 ` [Qemu-devel] [PATCH 7/7] remove smp restriction from kvm Glauber Costa
2009-11-29 15:32 ` [Qemu-devel] [PATCH 5/7] tell kernel about all registers instead of just mp_state Avi Kivity
2009-11-30 11:45 ` Glauber Costa
2009-11-30 12:04 ` Gleb Natapov
2009-11-30 12:05 ` Avi Kivity
2009-11-30 13:31 ` Glauber Costa
2009-11-30 13:32 ` Avi Kivity
2009-11-29 15:32 ` [Qemu-devel] [PATCH 4/7] qemu_flush_work for remote vcpu execution Avi Kivity
2009-11-30 11:44 ` Glauber Costa
2009-11-30 12:06 ` Avi Kivity
2009-11-30 11:48 ` [Qemu-devel] " Paolo Bonzini
2009-11-29 15:29 ` [Qemu-devel] [PATCH 2/7] store thread-specific env information Avi Kivity
2009-11-29 15:38 ` Andreas Färber
2009-11-29 15:42 ` Avi Kivity
2009-11-29 16:00 ` Andreas Färber
2009-11-29 22:29 ` Jamie Lokier [this message]
2009-11-30 11:36 ` [Qemu-devel] " Paolo Bonzini
2009-11-30 11:41 ` Glauber Costa
2009-11-30 11:49 ` Paolo Bonzini
2009-11-30 12:07 ` Avi Kivity
2009-11-29 17:32 ` [Qemu-devel] Re: [PATCH 0/7] KVM SMP support, early version Jan Kiszka
2009-11-30 11:42 ` Glauber Costa
2009-11-30 15:55 ` Glauber Costa
2009-11-30 16:40 ` Avi Kivity
2009-11-30 16:47 ` Glauber Costa
2009-11-30 17:30 ` Jan Kiszka
2009-12-01 12:10 ` Avi Kivity
2009-12-01 12:17 ` Jan Kiszka
2009-12-01 12:20 ` Glauber Costa
2009-12-01 12:33 ` Jan Kiszka
2009-11-30 16:50 ` Avi Kivity
2009-12-01 0:31 ` [Qemu-devel] " Ed Swierk
2009-12-01 0:42 ` Ed Swierk
2009-12-01 12:20 ` Alexander Graf
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=20091129222924.GA12299@shareable.org \
--to=jamie@shareable.org \
--cc=aliguori@us.ibm.com \
--cc=andreas.faerber@web.de \
--cc=avi@redhat.com \
--cc=glommer@redhat.com \
--cc=qemu-devel@nongnu.org \
/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).