qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
	Nikolay Nikolaev <n.nikolaev@virtualopensystems.com>,
	Anthony Liguori <aliguori@amazon.com>,
	Antonios Motakis <a.motakis@virtualopensystems.com>
Subject: [Qemu-devel] [PULL 05/23] qtest: fix qtest for vhost-user
Date: Mon, 23 Jun 2014 18:53:18 +0300	[thread overview]
Message-ID: <1403538745-18622-6-git-send-email-mst@redhat.com> (raw)
In-Reply-To: <1403538745-18622-1-git-send-email-mst@redhat.com>

From: Nikolay Nikolaev <n.nikolaev@virtualopensystems.com>

Fix compile for older glib, provide conditionally compiled versions of the
used glib APIs.

Signed-off-by: Nikolay Nikolaev <n.nikolaev@virtualopensystems.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 tests/vhost-user-test.c | 128 ++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 113 insertions(+), 15 deletions(-)

diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c
index 7c826b4..2934379 100644
--- a/tests/vhost-user-test.c
+++ b/tests/vhost-user-test.c
@@ -8,17 +8,30 @@
  *
  */
 
+#define QEMU_GLIB_COMPAT_H
+#include <glib.h>
+
 #include "libqtest.h"
 #include "qemu/option.h"
 #include "sysemu/char.h"
 #include "sysemu/sysemu.h"
 
-#include <glib.h>
 #include <linux/vhost.h>
 #include <sys/mman.h>
 #include <sys/vfs.h>
 #include <qemu/sockets.h>
 
+/* GLIB version compatibility flags */
+#if GLIB_CHECK_VERSION(2, 28, 0)
+#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"
@@ -95,8 +108,93 @@ 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 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 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)
+{
+    gboolean ret = FALSE;
+#ifdef HAVE_COND_INIT
+    ret = g_cond_wait_until(cond, mutex, end_time);
+#else
+    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)
 {
@@ -104,11 +202,11 @@ static void read_guest_mem(void)
     gint64 end_time;
     int i, j;
 
-    g_mutex_lock(&data_mutex);
+    g_mutex_lock(data_mutex);
 
-    end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
+    end_time = _get_time() + 5 * G_TIME_SPAN_SECOND;
     while (!fds_num) {
-        if (!g_cond_wait_until(&data_cond, &data_mutex, end_time)) {
+        if (!_cond_wait_until(data_cond, data_mutex, end_time)) {
             /* timeout has passed */
             g_assert(fds_num);
             break;
@@ -143,7 +241,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)
@@ -203,8 +301,8 @@ 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_mutex_unlock(&data_mutex);
+        g_cond_signal(data_cond);
+        g_mutex_unlock(data_mutex);
         break;
 
     case VHOST_USER_SET_VRING_KICK:
@@ -285,10 +383,10 @@ 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 */
-    g_mutex_init(&data_mutex);
-    g_cond_init(&data_cond);
-    g_mutex_lock(&data_mutex);
-    g_thread_new(NULL, thread_function, NULL);
+    data_mutex = _mutex_new();
+    data_cond = _cond_new();
+    g_mutex_lock(data_mutex);
+    _thread_new(NULL, thread_function, NULL);
 
     qemu_cmd = g_strdup_printf(QEMU_CMD, hugefs, socket_path);
     s = qtest_start(qemu_cmd);
@@ -305,8 +403,8 @@ int main(int argc, char **argv)
     /* cleanup */
     unlink(socket_path);
     g_free(socket_path);
-    g_cond_clear(&data_cond);
-    g_mutex_clear(&data_mutex);
+    _cond_free(data_cond);
+    _mutex_free(data_mutex);
 
     return ret;
 }
-- 
MST

  parent reply	other threads:[~2014-06-23 15:53 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-23 15:53 [Qemu-devel] [PULL 00/23] pc,pci,vhost,net fixes, enhancements Michael S. Tsirkin
2014-06-23 15:53 ` [Qemu-devel] [PULL 01/23] vhost: block migration if backend does not log memory Michael S. Tsirkin
2014-06-23 15:53 ` [Qemu-devel] [PULL 02/23] vhost: fix resource leak in error handling Michael S. Tsirkin
2014-06-23 15:53 ` [Qemu-devel] [PULL 03/23] qapi/hmp: use 'backend' instead of 'device' with memory backend Michael S. Tsirkin
2014-06-23 15:53 ` [Qemu-devel] [PULL 04/23] libqemustub: add more stubs for qemu-char Michael S. Tsirkin
2014-06-23 15:53 ` Michael S. Tsirkin [this message]
2014-06-23 15:53 ` [Qemu-devel] [PULL 06/23] qtest: fix vhost-user-test unbalanced mutex locks Michael S. Tsirkin
2014-06-23 15:53 ` [Qemu-devel] [PULL 07/23] e1000: emulate auto-negotiation during external link status change Michael S. Tsirkin
2014-06-23 15:53 ` [Qemu-devel] [PULL 08/23] e1000: improve auto-negotiation reporting via mii-tool Michael S. Tsirkin
2014-06-23 15:53 ` [Qemu-devel] [PULL 09/23] e1000: signal guest on successful link auto-negotiation Michael S. Tsirkin
2014-06-23 15:53 ` [Qemu-devel] [PULL 10/23] e1000: move e1000_autoneg_timer() to after set_ics() Michael S. Tsirkin
2014-06-23 15:53 ` [Qemu-devel] [PULL 11/23] e1000: factor out checking for auto-negotiation availability Michael S. Tsirkin
2014-06-23 15:53 ` [Qemu-devel] [PULL 12/23] qapi/string-output-visitor: fix human output Michael S. Tsirkin
2014-06-23 15:53 ` [Qemu-devel] [PULL 13/23] qemu-char: fix qemu_chr_fe_get_msgfd() Michael S. Tsirkin
2014-06-23 15:53 ` [Qemu-devel] [PULL 14/23] qemu-char: avoid leaking unused fds in tcp_get_msgfds() Michael S. Tsirkin
2014-06-23 15:53 ` [Qemu-devel] [PULL 15/23] virtio-pci: Report an error when msix vectors init fails Michael S. Tsirkin
2014-06-23 15:54 ` [Qemu-devel] [PULL 16/23] q35: Use PC_Q35_COMPAT_1_4 on pc-q35-1.4 compat_props Michael S. Tsirkin
2014-06-23 15:54 ` [Qemu-devel] [PULL 17/23] hw/pcie: correct debug message Michael S. Tsirkin
2014-06-23 15:54 ` [Qemu-devel] [PULL 18/23] hw/pcie: implement power controller functionality Michael S. Tsirkin
2014-06-23 15:54 ` [Qemu-devel] [PULL 19/23] hw/pcie: better hotplug/hotunplug support Michael S. Tsirkin
2014-06-23 15:54 ` [Qemu-devel] [PULL 20/23] pcie: coding style tweak Michael S. Tsirkin
2014-06-23 15:54 ` [Qemu-devel] [PULL 21/23] xen-hvm: Fix xen_hvm_init() to adjust pc memory layout Michael S. Tsirkin
2014-06-23 15:54 ` [Qemu-devel] [PULL 22/23] pc & q35: Add new machine opt max-ram-below-4g Michael S. Tsirkin
2014-06-23 15:54 ` [Qemu-devel] [PULL 23/23] xen-hvm: Handle " Michael S. Tsirkin
2014-06-24 11:21 ` [Qemu-devel] [PULL 00/23] pc,pci,vhost,net fixes, enhancements Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1403538745-18622-6-git-send-email-mst@redhat.com \
    --to=mst@redhat.com \
    --cc=a.motakis@virtualopensystems.com \
    --cc=aliguori@amazon.com \
    --cc=n.nikolaev@virtualopensystems.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).