From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:51710) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R65av-0004Hq-WF for qemu-devel@nongnu.org; Tue, 20 Sep 2011 15:03:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1R65au-0005O3-Sj for qemu-devel@nongnu.org; Tue, 20 Sep 2011 15:03:01 -0400 Received: from fmmailgate01.web.de ([217.72.192.221]:44573) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R65au-0005Nw-GR for qemu-devel@nongnu.org; Tue, 20 Sep 2011 15:03:00 -0400 Message-ID: <4E78E351.3070408@web.de> Date: Tue, 20 Sep 2011 21:02:41 +0200 From: Jan Kiszka MIME-Version: 1.0 References: <4E78DA03.3040809@redhat.com> In-Reply-To: <4E78DA03.3040809@redhat.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Sender: jan.kiszka@web.de Subject: [Qemu-devel] [PATCH v2 2/6] Introduce qemu_cond_timedwait for POSIX List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: Anthony Liguori , qemu-devel@nongnu.org On 2011-09-20 20:22, Paolo Bonzini wrote: > On 09/20/2011 06:53 PM, Jan Kiszka wrote: >> First user will be posix compat aio. >> >> Signed-off-by: Jan Kiszka > > I'm pretty sure the win32 version is not thread-safe, Yeah, I would even say it's completely broken. Was a naive hack. > but posix compat > aio is currently POSIX only. Just leave it out. > -------8<------- From: Jan Kiszka First user will be POSIX compat aio. Windows use cases aren't in sight, so this remains a POSIX-only service for now. Signed-off-by: Jan Kiszka --- qemu-thread-posix.c | 22 ++++++++++++++++++++++ qemu-thread-posix.h | 2 ++ 2 files changed, 24 insertions(+), 0 deletions(-) diff --git a/qemu-thread-posix.c b/qemu-thread-posix.c index f76427e..1d970fb 100644 --- a/qemu-thread-posix.c +++ b/qemu-thread-posix.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "qemu-thread.h" static void error_exit(int err, const char *msg) @@ -115,6 +116,27 @@ void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex) error_exit(err, __func__); } +int qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex, + unsigned int timeout_ms) +{ + struct timespec ts; + struct timeval tv; + int err; + + gettimeofday(&tv, NULL); + ts.tv_sec = tv.tv_sec + timeout_ms / 1000; + ts.tv_nsec = tv.tv_usec * 1000 + timeout_ms % 1000; + if (ts.tv_nsec > 1000000000) { + ts.tv_sec++; + ts.tv_nsec -= 1000000000; + } + err = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts); + if (err && err != ETIMEDOUT) { + error_exit(err, __func__); + } + return err == 0; +} + void qemu_thread_create(QemuThread *thread, void *(*start_routine)(void*), void *arg, int mode) diff --git a/qemu-thread-posix.h b/qemu-thread-posix.h index 540fa0b..b4ae5ad 100644 --- a/qemu-thread-posix.h +++ b/qemu-thread-posix.h @@ -16,5 +16,7 @@ struct QemuThread { /* only provided for posix so far */ void qemu_thread_join(QemuThread *thread); +int qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex, + unsigned int timeout_ms); #endif