From patchwork Tue Sep 29 12:12:03 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 524079 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 60D521402B6 for ; Wed, 30 Sep 2015 10:42:38 +1000 (AEST) Received: from localhost ([::1]:55979 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zh5Tg-0001w6-Ad for incoming@patchwork.ozlabs.org; Tue, 29 Sep 2015 20:42:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47303) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZgtlR-00041j-TA for qemu-devel@nongnu.org; Tue, 29 Sep 2015 08:12:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZgtlN-0001uj-R2 for qemu-devel@nongnu.org; Tue, 29 Sep 2015 08:12:09 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45126) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZgtlN-0001uX-JG for qemu-devel@nongnu.org; Tue, 29 Sep 2015 08:12:05 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 30E4D8E6FF for ; Tue, 29 Sep 2015 12:12:05 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-19.ams2.redhat.com [10.36.112.19]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t8TCC2gE021962; Tue, 29 Sep 2015 08:12:03 -0400 From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 29 Sep 2015 14:12:03 +0200 Message-Id: <1443528723-10645-1-git-send-email-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: marcandre.lureau@redhat.com, mst@redhat.com Subject: [Qemu-devel] [PATCH v2] vhost-user-test: do not reinvent glib-compat.h 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 glib-compat.h has the gunk to support both old-style and new-style gthread functions. Use it instead of reinventing it. Signed-off-by: Paolo Bonzini Reviewed-by: Marc-André Lureau Tested-by: Marc-André Lureau --- tests/vhost-user-test.c | 113 +++++++----------------------------------------- 1 file changed, 16 insertions(+), 97 deletions(-) diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c index e301db7..0e04f06 100644 --- a/tests/vhost-user-test.c +++ b/tests/vhost-user-test.c @@ -8,7 +8,6 @@ * */ -#define QEMU_GLIB_COMPAT_H #include #include "libqtest.h" @@ -30,12 +29,6 @@ #define HAVE_MONOTONIC_TIME #endif -#if GLIB_CHECK_VERSION(2, 32, 0) -#define HAVE_MUTEX_INIT -#define HAVE_COND_INIT -#define HAVE_THREAD_NEW -#endif - #define QEMU_CMD_ACCEL " -machine accel=tcg" #define QEMU_CMD_MEM " -m 512 -object memory-backend-file,id=mem,size=512M,"\ "mem-path=%s,share=on -numa node,memdev=mem" @@ -113,93 +106,21 @@ static VhostUserMsg m __attribute__ ((unused)); int fds_num = 0, fds[VHOST_MEMORY_MAX_NREGIONS]; static VhostUserMemory memory; -static GMutex *data_mutex; -static GCond *data_cond; - -static gint64 _get_time(void) -{ -#ifdef HAVE_MONOTONIC_TIME - return g_get_monotonic_time(); -#else - GTimeVal time; - g_get_current_time(&time); - - return time.tv_sec * G_TIME_SPAN_SECOND + time.tv_usec; -#endif -} - -static GMutex *_mutex_new(void) -{ - GMutex *mutex; - -#ifdef HAVE_MUTEX_INIT - mutex = g_new(GMutex, 1); - g_mutex_init(mutex); -#else - mutex = g_mutex_new(); -#endif - - return mutex; -} +static CompatGMutex data_mutex; +static CompatGCond data_cond; -static void _mutex_free(GMutex *mutex) -{ -#ifdef HAVE_MUTEX_INIT - g_mutex_clear(mutex); - g_free(mutex); -#else - g_mutex_free(mutex); -#endif -} - -static GCond *_cond_new(void) -{ - GCond *cond; - -#ifdef HAVE_COND_INIT - cond = g_new(GCond, 1); - g_cond_init(cond); -#else - cond = g_cond_new(); -#endif - - return cond; -} - -static gboolean _cond_wait_until(GCond *cond, GMutex *mutex, gint64 end_time) +#if !GLIB_CHECK_VERSION(2, 32, 0) +static gboolean g_cond_wait_until(CompatGCond cond, CompatGMutex mutex, + gint64 end_time) { gboolean ret = FALSE; -#ifdef HAVE_COND_INIT - ret = g_cond_wait_until(cond, mutex, end_time); -#else + end_time -= g_get_monotonic_time(); GTimeVal time = { end_time / G_TIME_SPAN_SECOND, end_time % G_TIME_SPAN_SECOND }; ret = g_cond_timed_wait(cond, mutex, &time); -#endif return ret; } - -static void _cond_free(GCond *cond) -{ -#ifdef HAVE_COND_INIT - g_cond_clear(cond); - g_free(cond); -#else - g_cond_free(cond); #endif -} - -static GThread *_thread_new(const gchar *name, GThreadFunc func, gpointer data) -{ - GThread *thread = NULL; - GError *error = NULL; -#ifdef HAVE_THREAD_NEW - thread = g_thread_try_new(name, func, data, &error); -#else - thread = g_thread_create(func, data, TRUE, &error); -#endif - return thread; -} static void read_guest_mem(void) { @@ -208,11 +129,11 @@ static void read_guest_mem(void) int i, j; size_t size; - g_mutex_lock(data_mutex); + g_mutex_lock(&data_mutex); - end_time = _get_time() + 5 * G_TIME_SPAN_SECOND; + end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; while (!fds_num) { - if (!_cond_wait_until(data_cond, data_mutex, end_time)) { + if (!g_cond_wait_until(&data_cond, &data_mutex, end_time)) { /* timeout has passed */ g_assert(fds_num); break; @@ -252,7 +173,7 @@ static void read_guest_mem(void) } g_assert_cmpint(1, ==, 1); - g_mutex_unlock(data_mutex); + g_mutex_unlock(&data_mutex); } static void *thread_function(void *data) @@ -280,7 +201,7 @@ static void chr_read(void *opaque, const uint8_t *buf, int size) return; } - g_mutex_lock(data_mutex); + g_mutex_lock(&data_mutex); memcpy(p, buf, VHOST_USER_HDR_SIZE); if (msg.size) { @@ -313,7 +234,7 @@ static void chr_read(void *opaque, const uint8_t *buf, int size) fds_num = qemu_chr_fe_get_msgfds(chr, fds, sizeof(fds) / sizeof(int)); /* signal the test that it can continue */ - g_cond_signal(data_cond); + g_cond_signal(&data_cond); break; case VHOST_USER_SET_VRING_KICK: @@ -330,7 +251,7 @@ static void chr_read(void *opaque, const uint8_t *buf, int size) default: break; } - g_mutex_unlock(data_mutex); + g_mutex_unlock(&data_mutex); } static const char *init_hugepagefs(void) @@ -395,9 +316,9 @@ int main(int argc, char **argv) qemu_chr_add_handlers(chr, chr_can_read, chr_read, NULL, chr); /* run the main loop thread so the chardev may operate */ - data_mutex = _mutex_new(); - data_cond = _cond_new(); - _thread_new(NULL, thread_function, NULL); + g_mutex_init(&data_mutex); + g_cond_init(&data_cond); + g_thread_new(NULL, thread_function, NULL); qemu_cmd = g_strdup_printf(QEMU_CMD, hugefs, socket_path); s = qtest_start(qemu_cmd); @@ -414,8 +335,6 @@ int main(int argc, char **argv) /* cleanup */ unlink(socket_path); g_free(socket_path); - _cond_free(data_cond); - _mutex_free(data_mutex); return ret; }