Fixes for thread pool patches.
cover Fixes for thread pool patches.
Three fixes: 1) Darwin does not support weak aliases, use weak references instead. 2) Darwin, NetBSD and OpenBSD do not have sem_timedwait, implement counting semaphores with a mutex and cv there. 3) Daemonize was broken, fixes are in patches 3-5.
Paolo Bonzini (5):
compiler: support Darwin weak references
semaphore: implement fallback counting semaphores with mutex+condvar
qemu-timer: reinitialize timers after fork
vl: unify calls to init_timer_alarm
vl: delay thread initialization after daemonization
compiler.h | 9 ++++++- main-loop.c | 6 +++-- osdep.c | 56 +++++++++++++++++++++++----------------- oslib-win32.c | 12 +++++---- qemu-sockets.c | 40 ++++++++++++++++------------- qemu-thread-posix.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-thread-posix.h | 6 +++++ qemu-timer.c | 15 ++++++++++- qmp.c | 2 ++ vl.c | 9 +++----
10 file modificati, 172 inserzioni(+), 57 rimozioni(-)
Signature · 1 line
1.7.12.1
PATCH 1/5 compiler: support Darwin weak references
Weakrefs only tell you if the symbol was defined elsewhere, so you need a further check at runtime to pick the default definition when needed.
This could be automated by the compiler, but it does not do it.
--- compiler.h | 9 ++++++++- osdep.c | 56 ++++++++++++++++++++++++++++++++------------------------ oslib-win32.c | 12 +++++++----- qemu-sockets.c | 40 ++++++++++++++++++++++------------------ qmp.c | 2 ++
5 file modificati, 71 inserzioni(+), 48 rimozioni(-)
compiler.h +8 -1
@@ -50,8 +50,15 @@# define __printf__ __gnu_printf__# endif # endif -# define QEMU_WEAK_ALIAS (newname, oldname) \ +# if defined(__APPLE__) +# define QEMU_WEAK_ALIAS (newname, oldname) \ + static typeof(oldname) weak_##newname __attribute__((weakref(#oldname))) +# define QEMU_WEAK_REF (newname, oldname) (weak_##newname ? weak_##newname : oldname) +# else +# define QEMU_WEAK_ALIAS (newname, oldname) \ typeof(oldname) newname __attribute__((weak, alias (#oldname))) +# define QEMU_WEAK_REF (newname, oldname) newname +# endif #else #define GCC_ATTR /**/ #define GCC_FMT_ATTR (n, m)
osdep.c +32 -24
65 lines not on this page · raw message
oslib-win32.c +7 -5
@@ -32,6 +32,13 @@#include "trace.h" #include "qemu_socket.h" +static void default_qemu_fd_register (int fd) +{ +} +QEMU_WEAK_ALIAS (qemu_fd_register, default_qemu_fd_register); +#define qemu_fd_register \ + QEMU_WEAK_REF(qemu_fd_register, default_qemu_fd_register) +void *qemu_oom_check (void *ptr) {if (ptr ==NULL ) {
@@ -150,8 +157,3 @@ int qemu_get_thread_id(void) {return GetCurrentThreadId (); } - -static void default_qemu_fd_register (int fd) -{ -} -QEMU_WEAK_ALIAS (qemu_fd_register, default_qemu_fd_register);
qemu-sockets.c +22 -18
@@ -61,6 +61,28 @@ static QemuOptsList dummy_opts = { }, }; +static int default_monitor_get_fd (Monitor *mon,const char *name,Error **errp) +{ +error_setg (errp,"only QEMU supports file descriptor passing" ); +return -1 ; +} +QEMU_WEAK_ALIAS (monitor_get_fd, default_monitor_get_fd); +#define monitor_get_fd \ + QEMU_WEAK_REF(monitor_get_fd, default_monitor_get_fd) + +static int default_qemu_set_fd_handler2 (int fd, +IOCanReadHandler *fd_read_poll, +IOHandler *fd_read, +IOHandler *fd_write, +void *opaque) + +{ +abort (); +} +QEMU_WEAK_ALIAS (qemu_set_fd_handler2, default_qemu_set_fd_handler2); +#define qemu_set_fd_handler2 \ + QEMU_WEAK_REF(qemu_set_fd_handler2, default_qemu_set_fd_handler2) +static int inet_getport (struct addrinfo *e) {struct sockaddr_in *i4;
@@ -967,21 +989,3 @@ int socket_init(void)#endif return 0 ; } - -static int default_monitor_get_fd (Monitor *mon,const char *name,Error **errp) -{ -error_setg (errp,"only QEMU supports file descriptor passing" ); -return -1 ; -} -QEMU_WEAK_ALIAS (monitor_get_fd, default_monitor_get_fd); - -static int default_qemu_set_fd_handler2 (int fd, -IOCanReadHandler *fd_read_poll, -IOHandler *fd_read, -IOHandler *fd_write, -void *opaque) - -{ -abort (); -} -QEMU_WEAK_ALIAS (qemu_set_fd_handler2, default_qemu_set_fd_handler2);
qmp.c +2 -0
@@ -477,6 +477,8 @@ static CpuDefinitionInfoList *default_arch_query_cpu_definitions(Error **errp)return NULL ; }QEMU_WEAK_ALIAS (arch_query_cpu_definitions, default_arch_query_cpu_definitions); +#define arch_query_cpu_definitions \ + QEMU_WEAK_REF(arch_query_cpu_definitions, default_arch_query_cpu_definitions)CpuDefinitionInfoList *qmp_query_cpu_definitions (Error **errp) {
Signature · 1 line
1.7.12.1
Re: [Qemu-devel] [PATCH 1/5] compiler: support Darwin weak references
Quoted 5 lines · Paolo Bonzini
Weakrefs only tell you if the symbol was defined elsewhere, so you need a further check at runtime to pick the default definition when needed.
This could be automated by the compiler, but it does not do it.
clang doesn't error out anymore, but this version still provokes a warning:
CC osdep.o
osdep.c:85:1: warning: unused function 'weak_monitor_fdset_dup_fd_find' [-Wunused-function] QEMU_WEAK_ALIAS(monitor_fdset_dup_fd_find, default_fdset_dup_fd_find); ^
./compiler.h:55:32: note: expanded from macro 'QEMU_WEAK_ALIAS'
static typeof(oldname) weak_##newname __attribute__((weakref(#oldname)))
^
<scratch space>:147:1: note: expanded from macro 'weak_' weak_monitor_fdset_dup_fd_find
^
1 warning generated.
-- PMM
PATCH 2/5 semaphore: implement fallback counting semaphores with mutex+condvar
In reply to Paolo BonziniOpenBSD and Darwin do not have sem_timedwait. Implement a fallback for them.
--- qemu-thread-posix.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-thread-posix.h | 6 +++++
2 file modificati, 80 inserzioni(+)
qemu-thread-posix.c +74 -0
@@ -122,36 +122,100 @@ void qemu_sem_init(QemuSemaphore *sem, int init) {int rc; +#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__) + rc =pthread_mutex_init (&sem->lock,NULL ); +if (rc !=0 ) { +error_exit (rc, __func__); + } + rc =pthread_cond_init (&sem->cond,NULL ); +if (rc !=0 ) { +error_exit (rc, __func__); + } +if (init <0 ) { +error_exit (EINVAL , __func__); + } + sem->count = init; +#else rc =sem_init (&sem->sem,0 , init);if (rc <0 ) {error_exit (errno, __func__); } +#endif }void qemu_sem_destroy (QemuSemaphore *sem) {int rc; +#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__) + rc =pthread_cond_destroy (&sem->cond); +if (rc <0 ) { +error_exit (rc, __func__); + } + rc =pthread_mutex_destroy (&sem->lock); +if (rc <0 ) { +error_exit (rc, __func__); + } +#else rc =sem_destroy (&sem->sem);if (rc <0 ) {error_exit (errno, __func__); } +#endif }void qemu_sem_post (QemuSemaphore *sem) {int rc; +#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__) +pthread_mutex_lock (&sem->lock); +if (sem->count ==INT_MAX ) { + rc =EINVAL ; + }else if (sem->count++ <0 ) { + rc =pthread_cond_signal (&sem->cond); + }else { + rc =0 ; + } +pthread_mutex_unlock (&sem->lock); +if (rc !=0 ) { +error_exit (rc, __func__); + } +#else rc =sem_post (&sem->sem);if (rc <0 ) {error_exit (errno, __func__); } +#endif } int qemu_sem_timedwait(QemuSemaphore *sem, int ms) { int rc; +#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__) + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + if (ms) { + int nsec = ts.tv_nsec + (ms % 1000) * 1000000; + ts.tv_sec += ms / 1000 + nsec / 1000000000; + ts.tv_nsec = nsec % 1000000000; + } + + pthread_mutex_lock(&sem->lock); + --sem->count; + while (sem->count < 0) { + rc = pthread_cond_timedwait(&sem->cond, &sem->lock, &ts); + if (rc == ETIMEDOUT) { + break; + } + if (rc != 0) { + error_exit(rc, __func__); + } + } + pthread_mutex_unlock(&sem->lock); + return (rc == ETIMEDOUT ? -1 : 0); +#else if (ms <= 0) { /* This is cheaper than sem_timedwait. */ do {
@@ -181,10 +245,19 @@ int qemu_sem_timedwait(QemuSemaphore *sem, int ms)error_exit (errno, __func__); }return 0 ; +#endif }void qemu_sem_wait (QemuSemaphore *sem) { +#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__) + pthread_mutex_lock(&sem->lock); + --sem->count; + while (sem->count < 0) { + pthread_cond_wait(&sem->cond, &sem->lock); + } + pthread_mutex_unlock(&sem->lock); +#else int rc; do {
@@ -193,6 +266,7 @@ void qemu_sem_wait(QemuSemaphore *sem)if (rc <0 ) {error_exit (errno, __func__); } +#endif } void qemu_thread_create(QemuThread *thread,
qemu-thread-posix.h +6 -0
@@ -12,7 +12,13 @@ struct QemuCond { };struct QemuSemaphore { +#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__) +pthread_mutex_t lock; +pthread_cond_t cond; +int count; +#else sem_t sem; +#endif };struct QemuThread {
Signature · 1 line
1.7.12.1
Re: [Qemu-devel] [PATCH 2/5] semaphore: implement fallback counting semaphores with mutex+condvar
qemu-thread-posix.c void qemu_sem_init(QemuSemaphore *sem, int init) · line 198
Quoted 3 lines · Paolo Bonzini
qemu-thread-posix.c +3
-0+#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__) +struct timespec ts; +clock_gettime (CLOCK_REALTIME , &ts);
qemu-thread-posix.c:198:5: warning: implicit declaration of function 'clock_gettime' is invalid in C99
[-Wimplicit-function-declaration]
clock_gettime(CLOCK_REALTIME, &ts);
^
qemu-thread-posix.c:198:19: error: use of undeclared identifier 'CLOCK_REALTIME'
clock_gettime(CLOCK_REALTIME, &ts);
^1 warning and 1 error generated.
make: *** [qemu-thread-posix.o] Error 1
MacOS doesn't implement clock_gettime()...
-- PMM
Re: [Qemu-devel] [PATCH 2/5] semaphore: implement fallback counting semaphores with mutex+condvar
In reply to Paolo BonziniQuoted 14 lines · Paolo Bonzini
OpenBSD and Darwin do not have
sem_timedwait. Implement a fallback for them.Signed-off-by Paolo Bonzini <[email protected]>--- qemu-thread-posix.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-thread-posix.h | 6 +++++2 file modificati, 80 inserzioni(+)
qemu-thread-posix.c +0
-0
qemu-thread-posix.c void qemu_sem_init(QemuSemaphore *sem, int init) · line 125
Quoted 4 lines
qemu-thread-posix.c +1
-0@@ -122,36 +122,100 @@ void qemu_sem_init(QemuSemaphore *sem, int init) {int rc; +#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)
OpenBSD 5.2 & -current (libpthread) / NetBSD -current (librt) have supported sem_timedwait() for roughly 8 months now. Please change this to properly test for the presence of sem_timedwait() within the configure script.
Signature · 3 lines
This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
Re: [Qemu-devel] [PATCH 2/5] semaphore: implement fallback counting semaphores with mutex+condvar
Il 18/11/2012 10:09, Brad Smith ha scritto:
Quoted 19 lines · Paolo Bonzini
Quoted 18 lines
OpenBSD and Darwin do not have
sem_timedwait. Implement a fallback for them.Signed-off-by Paolo Bonzini <[email protected]>--- qemu-thread-posix.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-thread-posix.h | 6 +++++2 file modificati, 80 inserzioni(+)
qemu-thread-posix.c +0
-0@@ -122,36 +122,100 @@ void qemu_sem_init(QemuSemaphore *sem, int init) {int rc;
Quoted 6 lines
qemu-thread-posix.c void qemu_sem_init(QemuSemaphore *sem, int init) · line 125
Quoted 1 line
qemu-thread-posix.c +1
-0+#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)OpenBSD 5.2 & -current (libpthread) / NetBSD -current (librt) have supported
sem_timedwait()for roughly 8 months now. Please change this to properly test for the presence ofsem_timedwait()within the configure script.
Please submit a patch. The patched code works, and it's not even suboptimal because *BSD use a mutex/condvar to implement semaphores. We end up executing the very same code.
Paolo
Re: [Qemu-devel] [PATCH 2/5] semaphore: implement fallback counting semaphores with mutex+condvar
Quoted 27 lines · Paolo Bonzini
Il 18/11/2012 10:09, Brad Smith ha scritto:
Quoted 19 lines · Paolo Bonzini
Quoted 18 lines
OpenBSD and Darwin do not have
sem_timedwait. Implement a fallback for them.Signed-off-by Paolo Bonzini <[email protected]>--- qemu-thread-posix.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-thread-posix.h | 6 +++++2 file modificati, 80 inserzioni(+)
qemu-thread-posix.c +0
-0@@ -122,36 +122,100 @@ void qemu_sem_init(QemuSemaphore *sem, int init) {int rc;Quoted 6 lines
qemu-thread-posix.c void qemu_sem_init(QemuSemaphore *sem, int init) · line 125
Quoted 1 line
qemu-thread-posix.c +1
-0+#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)OpenBSD 5.2 & -current (libpthread) / NetBSD -current (librt) have supported
sem_timedwait()for roughly 8 months now. Please change this to properly test for the presence ofsem_timedwait()within the configure script.
Quoted 3 lines
Please submit a patch. The patched code works, and it's not even suboptimal because *BSD use a mutex/condvar to implement semaphores. We end up executing the very same code.
I understand what you mean. It's more so out of principle to try and remove workarounds wherever possible when it is possible to do so.
Signature · 3 lines
This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
PATCH 3/5 qemu-timer: reinitialize timers after fork
In reply to Paolo BonziniTimers are not inherited by the child of a fork(2), so just use pthread_atfork to reinstate them after daemonize.
--- qemu-timer.c | 15 ++++++++++++++-
1 file modificato, 14 inserzioni(+). 1 rimozione(-)
qemu-timer.c +14 -0
@@ -742,6 +742,17 @@ static void quit_timers(void) t->stop (t); } +static void reinit_timers (void ) +{ +struct qemu_alarm_timer *t = alarm_timer; + t->stop (t); +if (t->start (t)) { +fprintf (stderr,"Internal timer error: aborting\n" ); +exit (1 ); + } +qemu_rearm_alarm_timer (t); +} +int init_timer_alarm (void ) {struct qemu_alarm_timer *t =NULL ;
@@ -765,6 +776,9 @@ int init_timer_alarm(void) }atexit (quit_timers); +#ifdef CONFIG_POSIX +pthread_atfork (NULL ,NULL , reinit_timers); +#endif alarm_timer = t;return 0 ;
Signature · 1 line
1.7.12.1
PATCH 4/5 vl: unify calls to init_timer_alarm
In reply to Paolo Bonziniinit_timer_alarm was being called twice. This is not needed.
--- main-loop.c | 5 ++++- vl.c | 5 -----
2 file modificati, 4 inserzioni(+), 6 rimozioni(-)
main-loop.c +4 -1
@@ -123,7 +123,10 @@ int qemu_init_main_loop(void)GSource *src;init_clocks (); -init_timer_alarm (); +if (init_timer_alarm () <0 ) { +fprintf (stderr,"could not initialize alarm timer\n" ); +exit (1 ); + }qemu_mutex_lock_iothread (); ret =qemu_signal_init ();
vl.c +0 -5
@@ -3616,11 +3616,6 @@ int main(int argc, char **argv, char **envp)add_device_config (DEV_VIRTCON ,"vc:80Cx24C" ); } -if (init_timer_alarm () <0 ) { -fprintf (stderr,"could not initialize alarm timer\n" ); -exit (1 ); - } -socket_init ();if (qemu_opts_foreach (qemu_find_opts ("chardev" ), chardev_init_func,NULL ,1 ) !=0 )
Signature · 1 line
1.7.12.1
PATCH 5/5 vl: delay thread initialization after daemonization
In reply to Paolo BonziniCommit ac4119c (chardev: Use timer instead of bottom-half to postpone open event, 2012-10-12) moved the alarm timer initialization to an earlier point but failed to consider that it depends on qemu_init_main_loop.
Later, commit 1c53786 (vl: init main loop earlier, 2012-10-30) fixed this, but left -daemonize in two different ways. First, timers need to be reinitialized after forking. Second, the global mutex was being held by the parent, and thus dropped after forking.
The first is now fixed using pthread_atfork. For the second part, make sure that the global mutex is not taken before daemonization, and similarly delay qemu_thread_self.
--- main-loop.c | 1 - vl.c | 4 +++-
2 file modificati, 3 inserzioni(+), 2 rimozioni(-)
main-loop.c +0 -1
@@ -128,7 +128,6 @@ int qemu_init_main_loop(void)exit (1 ); } -qemu_mutex_lock_iothread (); ret =qemu_signal_init ();if (ret) {return ret;
vl.c +3 -1
@@ -3477,7 +3477,6 @@ int main(int argc, char **argv, char **envp) }loc_set_none (); -qemu_init_cpu_loop ();if (qemu_init_main_loop ()) {fprintf (stderr,"qemu_init_main_loop failed\n" );exit (1 );
@@ -3677,6 +3676,9 @@ int main(int argc, char **argv, char **envp)os_set_line_buffering (); +qemu_init_cpu_loop (); +qemu_mutex_lock_iothread (); +#ifdef CONFIG_SPICE /* spice needs the timers to be initialized by this point */ qemu_spice_init ();
Signature · 1 line
1.7.12.1