From patchwork Thu Apr 5 10:59:08 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kiszka X-Patchwork-Id: 151181 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 63E92B7060 for ; Fri, 6 Apr 2012 22:35:43 +1000 (EST) Received: from localhost ([::1]:40738 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SFkSv-000820-LA for incoming@patchwork.ozlabs.org; Thu, 05 Apr 2012 07:02:57 -0400 Received: from eggs.gnu.org ([208.118.235.92]:45176) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SFkQZ-0007iv-MY for qemu-devel@nongnu.org; Thu, 05 Apr 2012 07:02:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SFkPR-00074Z-F1 for qemu-devel@nongnu.org; Thu, 05 Apr 2012 07:00:31 -0400 Received: from goliath.siemens.de ([192.35.17.28]:16871) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SFkPR-00073o-4m for qemu-devel@nongnu.org; Thu, 05 Apr 2012 06:59:21 -0400 Received: from mail1.siemens.de (localhost [127.0.0.1]) by goliath.siemens.de (8.13.6/8.13.6) with ESMTP id q35AxIit020058; Thu, 5 Apr 2012 12:59:18 +0200 Received: from mchn199C.mchp.siemens.de ([139.25.109.49]) by mail1.siemens.de (8.13.6/8.13.6) with ESMTP id q35AxIc7027596; Thu, 5 Apr 2012 12:59:18 +0200 From: Jan Kiszka To: Anthony Liguori , qemu-devel@nongnu.org Date: Thu, 5 Apr 2012 12:59:08 +0200 Message-Id: <6e10c02cbb87fe30703de848455593df41ec7f4b.1333623555.git.jan.kiszka@siemens.com> X-Mailer: git-send-email 1.7.3.4 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 192.35.17.28 Cc: Kevin Wolf , Paolo Bonzini Subject: [Qemu-devel] [PATCH v3 01/10] Introduce qemu_cond_timedwait for POSIX X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 | 23 +++++++++++++++++++++++ qemu-thread-posix.h | 5 +++++ 2 files changed, 28 insertions(+), 0 deletions(-) diff --git a/qemu-thread-posix.c b/qemu-thread-posix.c index 9e1b5fb..cd65df2 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,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) diff --git a/qemu-thread-posix.h b/qemu-thread-posix.h index ee4618e..9f00524 100644 --- a/qemu-thread-posix.h +++ b/qemu-thread-posix.h @@ -1,5 +1,6 @@ #ifndef __QEMU_THREAD_POSIX_H #define __QEMU_THREAD_POSIX_H 1 +#include #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