From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53810) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zi0eo-0003Z6-0P for qemu-devel@nongnu.org; Fri, 02 Oct 2015 09:45:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zi0ed-0000lR-Sl for qemu-devel@nongnu.org; Fri, 02 Oct 2015 09:45:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38001) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zi0ed-0000lN-Lw for qemu-devel@nongnu.org; Fri, 02 Oct 2015 09:45:43 -0400 Date: Fri, 2 Oct 2015 16:45:40 +0300 From: "Michael S. Tsirkin" Message-ID: <1443793405-15190-11-git-send-email-mst@redhat.com> References: <1443793405-15190-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <1443793405-15190-1-git-send-email-mst@redhat.com> Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 10/15] vhost-user-test: do not reinvent glib-compat.h List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Peter Maydell , Yuanhan Liu , Igor Mammedov , =?us-ascii?B?PT9VVEYtOD9xP01hcmMtQW5kcj1DMz1BOT0yMEx1cmVhdT89?= , Paolo Bonzini From: Paolo Bonzini 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: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin Reviewed-by: Marc-Andr=E9 Lureau Tested-by: Marc-Andr=E9 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 @@ * */ =20 -#define QEMU_GLIB_COMPAT_H #include =20 #include "libqtest.h" @@ -30,12 +29,6 @@ #define HAVE_MONOTONIC_TIME #endif =20 -#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=3Dtcg" #define QEMU_CMD_MEM " -m 512 -object memory-backend-file,id=3Dmem,si= ze=3D512M,"\ "mem-path=3D%s,share=3Don -numa node,memdev=3Dme= m" @@ -113,93 +106,21 @@ static VhostUserMsg m __attribute__ ((unused)); =20 int fds_num =3D 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 =3D g_new(GMutex, 1); - g_mutex_init(mutex); -#else - mutex =3D g_mutex_new(); -#endif - - return mutex; -} +static CompatGMutex data_mutex; +static CompatGCond data_cond; =20 -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 =3D g_new(GCond, 1); - g_cond_init(cond); -#else - cond =3D 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 =3D FALSE; -#ifdef HAVE_COND_INIT - ret =3D g_cond_wait_until(cond, mutex, end_time); -#else + end_time -=3D g_get_monotonic_time(); GTimeVal time =3D { end_time / G_TIME_SPAN_SECOND, end_time % G_TIME_SPAN_SECOND }; ret =3D 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, gpointe= r data) -{ - GThread *thread =3D NULL; - GError *error =3D NULL; -#ifdef HAVE_THREAD_NEW - thread =3D g_thread_try_new(name, func, data, &error); -#else - thread =3D g_thread_create(func, data, TRUE, &error); -#endif - return thread; -} =20 static void read_guest_mem(void) { @@ -208,11 +129,11 @@ static void read_guest_mem(void) int i, j; size_t size; =20 - g_mutex_lock(data_mutex); + g_mutex_lock(&data_mutex); =20 - end_time =3D _get_time() + 5 * G_TIME_SPAN_SECOND; + end_time =3D 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) } =20 g_assert_cmpint(1, =3D=3D, 1); - g_mutex_unlock(data_mutex); + g_mutex_unlock(&data_mutex); } =20 static void *thread_function(void *data) @@ -280,7 +201,7 @@ static void chr_read(void *opaque, const uint8_t *buf= , int size) return; } =20 - g_mutex_lock(data_mutex); + g_mutex_lock(&data_mutex); memcpy(p, buf, VHOST_USER_HDR_SIZE); =20 if (msg.size) { @@ -313,7 +234,7 @@ static void chr_read(void *opaque, const uint8_t *buf= , int size) fds_num =3D qemu_chr_fe_get_msgfds(chr, fds, sizeof(fds) / sizeo= f(int)); =20 /* signal the test that it can continue */ - g_cond_signal(data_cond); + g_cond_signal(&data_cond); break; =20 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); } =20 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); =20 /* run the main loop thread so the chardev may operate */ - data_mutex =3D _mutex_new(); - data_cond =3D _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); =20 qemu_cmd =3D g_strdup_printf(QEMU_CMD, hugefs, socket_path); s =3D 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); =20 return ret; } --=20 MST