[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v2 2/6] Introduce qemu_cond_timedwait for POSIX
From: |
Jan Kiszka |
Subject: |
[Qemu-devel] [PATCH v2 2/6] Introduce qemu_cond_timedwait for POSIX |
Date: |
Tue, 20 Sep 2011 21:02:41 +0200 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686 (x86_64); de; rv:1.8.1.12) Gecko/20080226 SUSE/2.0.0.12-1.1 Thunderbird/2.0.0.12 Mnenhy/0.7.5.666 |
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<address@hidden>
>
> 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 <address@hidden>
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 <address@hidden>
---
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 <signal.h>
#include <stdint.h>
#include <string.h>
+#include <sys/time.h>
#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