glib: add compatibility interface for g_get_monotonic_time()
This patch fixes compilation errors when building against glib <2.28.0
due to the missing g_get_monotonic_time() function.
The compilation error in tests/libqos/virtio.c was introduced in commit
70556264a89a268efba1d7e8e341adcdd7881eb4 ("libqos: use microseconds
instead of iterations for virtio timeout").
Add a simple g_get_monotonic_time() implementation to glib-compat.h
based on code from vhost-user-test.c.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
[Igor: add G_TIME_SPAN_SECOND, include glib-compat.h in libqtest.h]
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
			
			
This commit is contained in:
		
							parent
							
								
									32d9c5613e
								
							
						
					
					
						commit
						89b516d8b9
					
				@ -18,6 +18,11 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include <glib.h>
 | 
					#include <glib.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* GLIB version compatibility flags */
 | 
				
			||||||
 | 
					#if !GLIB_CHECK_VERSION(2, 26, 0)
 | 
				
			||||||
 | 
					#define G_TIME_SPAN_SECOND              (G_GINT64_CONSTANT(1000000))
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if !GLIB_CHECK_VERSION(2, 14, 0)
 | 
					#if !GLIB_CHECK_VERSION(2, 14, 0)
 | 
				
			||||||
static inline guint g_timeout_add_seconds(guint interval, GSourceFunc function,
 | 
					static inline guint g_timeout_add_seconds(guint interval, GSourceFunc function,
 | 
				
			||||||
                                          gpointer data)
 | 
					                                          gpointer data)
 | 
				
			||||||
@ -26,6 +31,20 @@ static inline guint g_timeout_add_seconds(guint interval, GSourceFunc function,
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if !GLIB_CHECK_VERSION(2, 28, 0)
 | 
				
			||||||
 | 
					static inline gint64 g_get_monotonic_time(void)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    /* g_get_monotonic_time() is best-effort so we can use the wall clock as a
 | 
				
			||||||
 | 
					     * fallback.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    GTimeVal time;
 | 
				
			||||||
 | 
					    g_get_current_time(&time);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return time.tv_sec * G_TIME_SPAN_SECOND + time.tv_usec;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef _WIN32
 | 
					#ifdef _WIN32
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * g_poll has a problem on Windows when using
 | 
					 * g_poll has a problem on Windows when using
 | 
				
			||||||
 | 
				
			|||||||
@ -23,6 +23,7 @@
 | 
				
			|||||||
#include <stdarg.h>
 | 
					#include <stdarg.h>
 | 
				
			||||||
#include <sys/types.h>
 | 
					#include <sys/types.h>
 | 
				
			||||||
#include "qapi/qmp/qdict.h"
 | 
					#include "qapi/qmp/qdict.h"
 | 
				
			||||||
 | 
					#include "glib-compat.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct QTestState QTestState;
 | 
					typedef struct QTestState QTestState;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -21,15 +21,6 @@
 | 
				
			|||||||
#include <sys/vfs.h>
 | 
					#include <sys/vfs.h>
 | 
				
			||||||
#include <qemu/sockets.h>
 | 
					#include <qemu/sockets.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* GLIB version compatibility flags */
 | 
					 | 
				
			||||||
#if !GLIB_CHECK_VERSION(2, 26, 0)
 | 
					 | 
				
			||||||
#define G_TIME_SPAN_SECOND              (G_GINT64_CONSTANT(1000000))
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#if GLIB_CHECK_VERSION(2, 28, 0)
 | 
					 | 
				
			||||||
#define HAVE_MONOTONIC_TIME
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#if GLIB_CHECK_VERSION(2, 32, 0)
 | 
					#if GLIB_CHECK_VERSION(2, 32, 0)
 | 
				
			||||||
#define HAVE_MUTEX_INIT
 | 
					#define HAVE_MUTEX_INIT
 | 
				
			||||||
#define HAVE_COND_INIT
 | 
					#define HAVE_COND_INIT
 | 
				
			||||||
@ -116,18 +107,6 @@ static VhostUserMemory memory;
 | 
				
			|||||||
static GMutex *data_mutex;
 | 
					static GMutex *data_mutex;
 | 
				
			||||||
static GCond *data_cond;
 | 
					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)
 | 
					static GMutex *_mutex_new(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    GMutex *mutex;
 | 
					    GMutex *mutex;
 | 
				
			||||||
@ -210,7 +189,7 @@ static void read_guest_mem(void)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    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) {
 | 
					    while (!fds_num) {
 | 
				
			||||||
        if (!_cond_wait_until(data_cond, data_mutex, end_time)) {
 | 
					        if (!_cond_wait_until(data_cond, data_mutex, end_time)) {
 | 
				
			||||||
            /* timeout has passed */
 | 
					            /* timeout has passed */
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user