@@ -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,28 @@ void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
error_exit(err, __func__);
}
+/* Returns true if condition was signals, false if timed out. */
+bool 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)
@@ -1,5 +1,6 @@
#ifndef __QEMU_THREAD_POSIX_H
#define __QEMU_THREAD_POSIX_H 1
+#include <stdbool.h>
#include "pthread.h"
struct QemuMutex {
@@ -14,4 +15,8 @@ struct QemuThread {
pthread_t thread;
};
+/* only provided for posix so far */
+bool qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex,
+ unsigned int timeout_ms);
+
#endif
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 <jan.kiszka@siemens.com> --- qemu-thread-posix.c | 23 +++++++++++++++++++++++ qemu-thread-posix.h | 5 +++++ 2 files changed, 28 insertions(+), 0 deletions(-)