From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, ming.lei@canonical.com, pl@kamp.de,
stefanha@redhat.com
Subject: [Qemu-devel] [PATCH 1/7] coroutine-ucontext: use __thread
Date: Fri, 28 Nov 2014 15:12:15 +0100 [thread overview]
Message-ID: <1417183941-26329-2-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1417183941-26329-1-git-send-email-pbonzini@redhat.com>
ELF thread local storage is about 10% faster on tests/test-coroutine's
perf/cost test. The timing on my machine is 160ns per iteration with
pthread TLS, 145 with ELF TLS.
Based on a patch by Kevin Wolf and Peter Lieven, but redone to follow
the model of coroutine-win32.c (including the important "noinline"
attribute!!!).
Platforms without thread-local storage (OpenBSD probably?) will need
a new-enough GCC for this to compile, in order to use the same emutls
support that Windows already relies on.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
coroutine-ucontext.c | 64 +++++++++++++---------------------------------------
1 file changed, 16 insertions(+), 48 deletions(-)
diff --git a/coroutine-ucontext.c b/coroutine-ucontext.c
index 4bf2cde..d86e3e1 100644
--- a/coroutine-ucontext.c
+++ b/coroutine-ucontext.c
@@ -25,7 +25,6 @@
#include <stdlib.h>
#include <setjmp.h>
#include <stdint.h>
-#include <pthread.h>
#include <ucontext.h>
#include "qemu-common.h"
#include "block/coroutine_int.h"
@@ -48,15 +47,8 @@ typedef struct {
/**
* Per-thread coroutine bookkeeping
*/
-typedef struct {
- /** Currently executing coroutine */
- Coroutine *current;
-
- /** The default coroutine */
- CoroutineUContext leader;
-} CoroutineThreadState;
-
-static pthread_key_t thread_state_key;
+static __thread CoroutineUContext leader;
+static __thread Coroutine *current;
/*
* va_args to makecontext() must be type 'int', so passing
@@ -68,36 +60,6 @@ union cc_arg {
int i[2];
};
-static CoroutineThreadState *coroutine_get_thread_state(void)
-{
- CoroutineThreadState *s = pthread_getspecific(thread_state_key);
-
- if (!s) {
- s = g_malloc0(sizeof(*s));
- s->current = &s->leader.base;
- pthread_setspecific(thread_state_key, s);
- }
- return s;
-}
-
-static void qemu_coroutine_thread_cleanup(void *opaque)
-{
- CoroutineThreadState *s = opaque;
-
- g_free(s);
-}
-
-static void __attribute__((constructor)) coroutine_init(void)
-{
- int ret;
-
- ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
- if (ret != 0) {
- fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
- abort();
- }
-}
-
static void coroutine_trampoline(int i0, int i1)
{
union cc_arg arg;
@@ -193,15 +155,22 @@ void qemu_coroutine_delete(Coroutine *co_)
g_free(co);
}
+/* This function is marked noinline to prevent GCC from inlining it
+ * into coroutine_trampoline(). If we allow it to do that then it
+ * hoists the code to get the address of the TLS variable "current"
+ * out of the while() loop. This is an invalid transformation because
+ * the SwitchToFiber() call may be called when running thread A but
+ * return in thread B, and so we might be in a different thread
+ * context each time round the loop.
+ */
CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
CoroutineAction action)
{
CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
- CoroutineThreadState *s = coroutine_get_thread_state();
int ret;
- s->current = to_;
+ current = to_;
ret = sigsetjmp(from->env, 0);
if (ret == 0) {
@@ -212,14 +181,13 @@ CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
Coroutine *qemu_coroutine_self(void)
{
- CoroutineThreadState *s = coroutine_get_thread_state();
-
- return s->current;
+ if (!current) {
+ current = &leader.base;
+ }
+ return current;
}
bool qemu_in_coroutine(void)
{
- CoroutineThreadState *s = pthread_getspecific(thread_state_key);
-
- return s && s->current->caller;
+ return current && current->caller;
}
--
2.1.0
next prev parent reply other threads:[~2014-11-28 14:12 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-28 14:12 [Qemu-devel] [PATCH 0/7] coroutine: optimizations Paolo Bonzini
2014-11-28 14:12 ` Paolo Bonzini [this message]
2014-11-28 14:28 ` [Qemu-devel] [PATCH 1/7] coroutine-ucontext: use __thread Peter Maydell
2014-11-28 14:45 ` Markus Armbruster
2014-11-28 15:36 ` Kevin Wolf
2014-11-28 14:12 ` [Qemu-devel] [PATCH 2/7] qemu-thread: add per-thread atexit functions Paolo Bonzini
2014-11-28 14:12 ` [Qemu-devel] [PATCH 3/7] test-coroutine: avoid overflow on 32-bit systems Paolo Bonzini
2014-12-01 1:28 ` Ming Lei
2014-12-01 12:41 ` Paolo Bonzini
2014-12-02 1:20 ` Ming Lei
2014-11-28 14:12 ` [Qemu-devel] [PATCH 4/7] QSLIST: add lock-free operations Paolo Bonzini
2014-11-28 14:12 ` [Qemu-devel] [PATCH 5/7] coroutine: rewrite pool to avoid mutex Paolo Bonzini
2014-11-28 16:40 ` Kevin Wolf
2014-11-28 17:30 ` Paolo Bonzini
2014-11-28 17:31 ` Paolo Bonzini
2014-11-28 18:34 ` Kevin Wolf
2014-11-28 19:57 ` Paolo Bonzini
2014-11-28 14:12 ` [Qemu-devel] [PATCH 6/7] coroutine: drop qemu_coroutine_adjust_pool_size Paolo Bonzini
2014-11-28 14:12 ` [Qemu-devel] [PATCH 7/7] coroutine: try harder not to delete coroutines Paolo Bonzini
2014-11-28 20:52 ` Peter Lieven
2014-11-29 14:27 ` Paolo Bonzini
2014-11-29 21:28 ` Peter Lieven
2014-11-29 14:28 ` Paolo Bonzini
2014-12-01 5:55 ` [Qemu-devel] [PATCH 0/7] coroutine: optimizations Ming Lei
2014-12-01 7:05 ` Peter Lieven
2014-12-01 7:46 ` Ming Lei
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=1417183941-26329-2-git-send-email-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=kwolf@redhat.com \
--cc=ming.lei@canonical.com \
--cc=pl@kamp.de \
--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).