2013-03-25 16:23:56 +01:00
|
|
|
/*
|
|
|
|
* GLIB Compatibility Functions
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2013
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
2014-05-08 10:30:46 +02:00
|
|
|
* Michael Tokarev <mjt@tls.msk.ru>
|
|
|
|
* Paolo Bonzini <pbonzini@redhat.com>
|
2013-03-25 16:23:56 +01:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef QEMU_GLIB_COMPAT_H
|
|
|
|
#define QEMU_GLIB_COMPAT_H
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2014-10-15 14:29:30 +02:00
|
|
|
/* 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)
|
2015-04-02 17:17:45 +02:00
|
|
|
static inline gint64 qemu_g_get_monotonic_time(void)
|
2014-10-15 14:29:30 +02:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
2015-04-02 17:17:45 +02:00
|
|
|
/* work around distro backports of this interface */
|
|
|
|
#define g_get_monotonic_time() qemu_g_get_monotonic_time()
|
2014-10-15 14:29:30 +02:00
|
|
|
#endif
|
|
|
|
|
glib: fix g_poll early timeout on windows
g_poll has a problem on Windows when using
timeouts < 10ms, in glib/gpoll.c:
/* If not, and we have a significant timeout, poll again with
* timeout then. Note that this will return indication for only
* one event, or only for messages. We ignore timeouts less than
* ten milliseconds as they are mostly pointless on Windows, the
* MsgWaitForMultipleObjectsEx() call will timeout right away
* anyway.
*/
if (retval == 0 && (timeout == INFINITE || timeout >= 10))
retval = poll_rest (poll_msgs, handles, nhandles, fds, nfds, timeout);
so whenever g_poll is called with timeout < 10ms it does
a quick poll instead of wait, this causes significant performance
degradation of QEMU, thus we should use WaitForMultipleObjectsEx
directly
Signed-off-by: Stanislav Vorobiov <s.vorobiov@samsung.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2014-05-08 10:47:10 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
/*
|
|
|
|
* g_poll has a problem on Windows when using
|
|
|
|
* timeouts < 10ms, so use wrapper.
|
|
|
|
*/
|
|
|
|
#define g_poll(fds, nfds, timeout) g_poll_fixed(fds, nfds, timeout)
|
|
|
|
gint g_poll_fixed(GPollFD *fds, guint nfds, gint timeout);
|
2014-05-02 16:35:56 +02:00
|
|
|
#endif
|
|
|
|
|
2016-08-18 20:46:02 +02:00
|
|
|
#if !GLIB_CHECK_VERSION(2, 30, 0)
|
|
|
|
/* Not a 100% compatible implementation, but good enough for most
|
|
|
|
* cases. Placeholders are only supported at the end of the
|
|
|
|
* template. */
|
|
|
|
static inline gchar *qemu_g_dir_make_tmp(gchar const *tmpl, GError **error)
|
|
|
|
{
|
|
|
|
gchar *path = g_build_filename(g_get_tmp_dir(), tmpl ?: ".XXXXXX", NULL);
|
|
|
|
|
|
|
|
if (mkdtemp(path) != NULL) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
/* Error occurred, clean up. */
|
|
|
|
g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno),
|
|
|
|
"mkdtemp() failed");
|
|
|
|
g_free(path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#define g_dir_make_tmp(tmpl, error) qemu_g_dir_make_tmp(tmpl, error)
|
|
|
|
#endif /* glib 2.30 */
|
|
|
|
|
2014-05-08 10:30:46 +02:00
|
|
|
#if !GLIB_CHECK_VERSION(2, 31, 0)
|
|
|
|
/* before glib-2.31, GMutex and GCond was dynamic-only (there was a separate
|
|
|
|
* GStaticMutex, but it didn't work with condition variables).
|
|
|
|
*
|
|
|
|
* Our implementation uses GOnce to fake a static implementation that does
|
|
|
|
* not require separate initialization.
|
|
|
|
* We need to rename the types to avoid passing our CompatGMutex/CompatGCond
|
|
|
|
* by mistake to a function that expects GMutex/GCond. However, for ease
|
|
|
|
* of use we keep the GLib function names. GLib uses macros for the
|
|
|
|
* implementation, we use inline functions instead and undefine the macros.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct CompatGMutex {
|
|
|
|
GOnce once;
|
|
|
|
} CompatGMutex;
|
|
|
|
|
|
|
|
typedef struct CompatGCond {
|
|
|
|
GOnce once;
|
|
|
|
} CompatGCond;
|
|
|
|
|
|
|
|
static inline gpointer do_g_mutex_new(gpointer unused)
|
|
|
|
{
|
|
|
|
return (gpointer) g_mutex_new();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void g_mutex_init(CompatGMutex *mutex)
|
|
|
|
{
|
|
|
|
mutex->once = (GOnce) G_ONCE_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void g_mutex_clear(CompatGMutex *mutex)
|
|
|
|
{
|
2015-05-07 12:38:02 +02:00
|
|
|
g_assert(mutex->once.status != G_ONCE_STATUS_PROGRESS);
|
2014-05-08 10:30:46 +02:00
|
|
|
if (mutex->once.retval) {
|
|
|
|
g_mutex_free((GMutex *) mutex->once.retval);
|
|
|
|
}
|
|
|
|
mutex->once = (GOnce) G_ONCE_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void (g_mutex_lock)(CompatGMutex *mutex)
|
|
|
|
{
|
|
|
|
g_once(&mutex->once, do_g_mutex_new, NULL);
|
|
|
|
g_mutex_lock((GMutex *) mutex->once.retval);
|
|
|
|
}
|
|
|
|
#undef g_mutex_lock
|
|
|
|
|
|
|
|
static inline gboolean (g_mutex_trylock)(CompatGMutex *mutex)
|
|
|
|
{
|
|
|
|
g_once(&mutex->once, do_g_mutex_new, NULL);
|
|
|
|
return g_mutex_trylock((GMutex *) mutex->once.retval);
|
|
|
|
}
|
|
|
|
#undef g_mutex_trylock
|
|
|
|
|
|
|
|
|
|
|
|
static inline void (g_mutex_unlock)(CompatGMutex *mutex)
|
|
|
|
{
|
|
|
|
g_mutex_unlock((GMutex *) mutex->once.retval);
|
|
|
|
}
|
|
|
|
#undef g_mutex_unlock
|
|
|
|
|
|
|
|
static inline gpointer do_g_cond_new(gpointer unused)
|
|
|
|
{
|
|
|
|
return (gpointer) g_cond_new();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void g_cond_init(CompatGCond *cond)
|
|
|
|
{
|
|
|
|
cond->once = (GOnce) G_ONCE_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void g_cond_clear(CompatGCond *cond)
|
|
|
|
{
|
2015-05-07 12:38:02 +02:00
|
|
|
g_assert(cond->once.status != G_ONCE_STATUS_PROGRESS);
|
2014-05-08 10:30:46 +02:00
|
|
|
if (cond->once.retval) {
|
|
|
|
g_cond_free((GCond *) cond->once.retval);
|
|
|
|
}
|
|
|
|
cond->once = (GOnce) G_ONCE_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void (g_cond_wait)(CompatGCond *cond, CompatGMutex *mutex)
|
|
|
|
{
|
2015-05-07 12:38:02 +02:00
|
|
|
g_assert(mutex->once.status != G_ONCE_STATUS_PROGRESS);
|
2014-05-08 10:30:46 +02:00
|
|
|
g_once(&cond->once, do_g_cond_new, NULL);
|
|
|
|
g_cond_wait((GCond *) cond->once.retval, (GMutex *) mutex->once.retval);
|
|
|
|
}
|
|
|
|
#undef g_cond_wait
|
|
|
|
|
|
|
|
static inline void (g_cond_broadcast)(CompatGCond *cond)
|
|
|
|
{
|
|
|
|
g_once(&cond->once, do_g_cond_new, NULL);
|
|
|
|
g_cond_broadcast((GCond *) cond->once.retval);
|
|
|
|
}
|
|
|
|
#undef g_cond_broadcast
|
|
|
|
|
|
|
|
static inline void (g_cond_signal)(CompatGCond *cond)
|
|
|
|
{
|
|
|
|
g_once(&cond->once, do_g_cond_new, NULL);
|
|
|
|
g_cond_signal((GCond *) cond->once.retval);
|
|
|
|
}
|
|
|
|
#undef g_cond_signal
|
|
|
|
|
2016-06-28 18:32:42 +02:00
|
|
|
static inline gboolean (g_cond_timed_wait)(CompatGCond *cond,
|
|
|
|
CompatGMutex *mutex,
|
|
|
|
GTimeVal *time)
|
|
|
|
{
|
|
|
|
g_assert(mutex->once.status != G_ONCE_STATUS_PROGRESS);
|
|
|
|
g_once(&cond->once, do_g_cond_new, NULL);
|
|
|
|
return g_cond_timed_wait((GCond *) cond->once.retval,
|
|
|
|
(GMutex *) mutex->once.retval, time);
|
|
|
|
}
|
|
|
|
#undef g_cond_timed_wait
|
|
|
|
|
|
|
|
/* This is not a macro, because it didn't exist until 2.32. */
|
|
|
|
static inline gboolean g_cond_wait_until(CompatGCond *cond, CompatGMutex *mutex,
|
|
|
|
gint64 end_time)
|
|
|
|
{
|
|
|
|
GTimeVal time;
|
|
|
|
|
|
|
|
/* Convert from monotonic to CLOCK_REALTIME. */
|
|
|
|
end_time -= g_get_monotonic_time();
|
|
|
|
g_get_current_time(&time);
|
|
|
|
end_time += time.tv_sec * G_TIME_SPAN_SECOND + time.tv_usec;
|
|
|
|
|
|
|
|
time.tv_sec = end_time / G_TIME_SPAN_SECOND;
|
|
|
|
time.tv_usec = end_time % G_TIME_SPAN_SECOND;
|
|
|
|
return g_cond_timed_wait(cond, mutex, &time);
|
|
|
|
}
|
2014-05-08 10:30:46 +02:00
|
|
|
|
|
|
|
/* before 2.31 there was no g_thread_new() */
|
|
|
|
static inline GThread *g_thread_new(const char *name,
|
|
|
|
GThreadFunc func, gpointer data)
|
|
|
|
{
|
|
|
|
GThread *thread = g_thread_create(func, data, TRUE, NULL);
|
|
|
|
if (!thread) {
|
|
|
|
g_error("creating thread");
|
|
|
|
}
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define CompatGMutex GMutex
|
|
|
|
#define CompatGCond GCond
|
|
|
|
#endif /* glib 2.31 */
|
|
|
|
|
2015-10-27 15:44:00 +01:00
|
|
|
#if !GLIB_CHECK_VERSION(2, 32, 0)
|
|
|
|
/* Beware, function returns gboolean since 2.39.2, see GLib commit 9101915 */
|
|
|
|
static inline void g_hash_table_add(GHashTable *hash_table, gpointer key)
|
|
|
|
{
|
|
|
|
g_hash_table_replace(hash_table, key, key);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-02 14:58:17 +02:00
|
|
|
#ifndef g_assert_true
|
|
|
|
#define g_assert_true(expr) \
|
|
|
|
do { \
|
|
|
|
if (G_LIKELY(expr)) { \
|
|
|
|
} else { \
|
|
|
|
g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
|
|
|
|
"'" #expr "' should be TRUE"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef g_assert_false
|
|
|
|
#define g_assert_false(expr) \
|
|
|
|
do { \
|
|
|
|
if (G_LIKELY(!(expr))) { \
|
|
|
|
} else { \
|
|
|
|
g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
|
|
|
|
"'" #expr "' should be FALSE"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef g_assert_null
|
|
|
|
#define g_assert_null(expr) \
|
|
|
|
do { \
|
|
|
|
if (G_LIKELY((expr) == NULL)) { \
|
|
|
|
} else { \
|
|
|
|
g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
|
|
|
|
"'" #expr "' should be NULL"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef g_assert_nonnull
|
|
|
|
#define g_assert_nonnull(expr) \
|
|
|
|
do { \
|
|
|
|
if (G_LIKELY((expr) != NULL)) { \
|
|
|
|
} else { \
|
|
|
|
g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
|
|
|
|
"'" #expr "' should not be NULL"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef g_assert_cmpmem
|
|
|
|
#define g_assert_cmpmem(m1, l1, m2, l2) \
|
|
|
|
do { \
|
|
|
|
gconstpointer __m1 = m1, __m2 = m2; \
|
|
|
|
int __l1 = l1, __l2 = l2; \
|
|
|
|
if (__l1 != __l2) { \
|
|
|
|
g_assertion_message_cmpnum( \
|
|
|
|
G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
|
|
|
|
#l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", __l1, "==", \
|
|
|
|
__l2, 'i'); \
|
|
|
|
} else if (memcmp(__m1, __m2, __l1) != 0) { \
|
|
|
|
g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
|
|
|
|
"assertion failed (" #m1 " == " #m2 ")"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
2016-08-05 09:16:07 +02:00
|
|
|
#if !GLIB_CHECK_VERSION(2, 28, 0)
|
|
|
|
static inline void g_list_free_full(GList *list, GDestroyNotify free_func)
|
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
for (l = list; l; l = l->next) {
|
|
|
|
free_func(l->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_list_free(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void g_slist_free_full(GSList *list, GDestroyNotify free_func)
|
|
|
|
{
|
|
|
|
GSList *l;
|
|
|
|
|
|
|
|
for (l = list; l; l = l->next) {
|
|
|
|
free_func(l->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_slist_free(list);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-09-30 12:50:18 +02:00
|
|
|
#if !GLIB_CHECK_VERSION(2, 26, 0)
|
|
|
|
static inline void g_source_set_name(GSource *source, const char *name)
|
|
|
|
{
|
|
|
|
/* This is just a debugging aid, so leaving it a no-op */
|
|
|
|
}
|
2016-09-30 12:57:14 +02:00
|
|
|
static inline void g_source_set_name_by_id(guint tag, const char *name)
|
|
|
|
{
|
|
|
|
/* This is just a debugging aid, so leaving it a no-op */
|
|
|
|
}
|
2016-09-30 12:50:18 +02:00
|
|
|
#endif
|
|
|
|
|
2016-10-24 18:31:03 +02:00
|
|
|
#if !GLIB_CHECK_VERSION(2, 36, 0)
|
|
|
|
/* Always fail. This will not include error_report output in the test log,
|
|
|
|
* sending it instead to stderr.
|
|
|
|
*/
|
|
|
|
#define g_test_initialized() (0)
|
|
|
|
#endif
|
|
|
|
#if !GLIB_CHECK_VERSION(2, 38, 0)
|
|
|
|
#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
|
|
|
|
#error schizophrenic detection of glib subprocess testing
|
|
|
|
#endif
|
|
|
|
#define g_test_subprocess() (0)
|
|
|
|
#endif
|
|
|
|
|
2013-03-25 16:23:56 +01:00
|
|
|
#endif
|