block: Change bitmap truncate conditional to assertion
[qemu/kevin.git] / include / glib-compat.h
blob318e0003689ddf76e00a18a4e6572d0bb1b1be6b
1 /*
2 * GLIB Compatibility Functions
4 * Copyright IBM, Corp. 2013
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 * Michael Tokarev <mjt@tls.msk.ru>
9 * Paolo Bonzini <pbonzini@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
16 #ifndef QEMU_GLIB_COMPAT_H
17 #define QEMU_GLIB_COMPAT_H
19 #include <glib.h>
21 /* GLIB version compatibility flags */
22 #if !GLIB_CHECK_VERSION(2, 26, 0)
23 #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
24 #endif
26 #if !GLIB_CHECK_VERSION(2, 28, 0)
27 static inline gint64 qemu_g_get_monotonic_time(void)
29 /* g_get_monotonic_time() is best-effort so we can use the wall clock as a
30 * fallback.
33 GTimeVal time;
34 g_get_current_time(&time);
36 return time.tv_sec * G_TIME_SPAN_SECOND + time.tv_usec;
38 /* work around distro backports of this interface */
39 #define g_get_monotonic_time() qemu_g_get_monotonic_time()
40 #endif
42 #ifdef _WIN32
44 * g_poll has a problem on Windows when using
45 * timeouts < 10ms, so use wrapper.
47 #define g_poll(fds, nfds, timeout) g_poll_fixed(fds, nfds, timeout)
48 gint g_poll_fixed(GPollFD *fds, guint nfds, gint timeout);
49 #endif
51 #if !GLIB_CHECK_VERSION(2, 31, 0)
52 /* before glib-2.31, GMutex and GCond was dynamic-only (there was a separate
53 * GStaticMutex, but it didn't work with condition variables).
55 * Our implementation uses GOnce to fake a static implementation that does
56 * not require separate initialization.
57 * We need to rename the types to avoid passing our CompatGMutex/CompatGCond
58 * by mistake to a function that expects GMutex/GCond. However, for ease
59 * of use we keep the GLib function names. GLib uses macros for the
60 * implementation, we use inline functions instead and undefine the macros.
63 typedef struct CompatGMutex {
64 GOnce once;
65 } CompatGMutex;
67 typedef struct CompatGCond {
68 GOnce once;
69 } CompatGCond;
71 static inline gpointer do_g_mutex_new(gpointer unused)
73 return (gpointer) g_mutex_new();
76 static inline void g_mutex_init(CompatGMutex *mutex)
78 mutex->once = (GOnce) G_ONCE_INIT;
81 static inline void g_mutex_clear(CompatGMutex *mutex)
83 g_assert(mutex->once.status != G_ONCE_STATUS_PROGRESS);
84 if (mutex->once.retval) {
85 g_mutex_free((GMutex *) mutex->once.retval);
87 mutex->once = (GOnce) G_ONCE_INIT;
90 static inline void (g_mutex_lock)(CompatGMutex *mutex)
92 g_once(&mutex->once, do_g_mutex_new, NULL);
93 g_mutex_lock((GMutex *) mutex->once.retval);
95 #undef g_mutex_lock
97 static inline gboolean (g_mutex_trylock)(CompatGMutex *mutex)
99 g_once(&mutex->once, do_g_mutex_new, NULL);
100 return g_mutex_trylock((GMutex *) mutex->once.retval);
102 #undef g_mutex_trylock
105 static inline void (g_mutex_unlock)(CompatGMutex *mutex)
107 g_mutex_unlock((GMutex *) mutex->once.retval);
109 #undef g_mutex_unlock
111 static inline gpointer do_g_cond_new(gpointer unused)
113 return (gpointer) g_cond_new();
116 static inline void g_cond_init(CompatGCond *cond)
118 cond->once = (GOnce) G_ONCE_INIT;
121 static inline void g_cond_clear(CompatGCond *cond)
123 g_assert(cond->once.status != G_ONCE_STATUS_PROGRESS);
124 if (cond->once.retval) {
125 g_cond_free((GCond *) cond->once.retval);
127 cond->once = (GOnce) G_ONCE_INIT;
130 static inline void (g_cond_wait)(CompatGCond *cond, CompatGMutex *mutex)
132 g_assert(mutex->once.status != G_ONCE_STATUS_PROGRESS);
133 g_once(&cond->once, do_g_cond_new, NULL);
134 g_cond_wait((GCond *) cond->once.retval, (GMutex *) mutex->once.retval);
136 #undef g_cond_wait
138 static inline void (g_cond_broadcast)(CompatGCond *cond)
140 g_once(&cond->once, do_g_cond_new, NULL);
141 g_cond_broadcast((GCond *) cond->once.retval);
143 #undef g_cond_broadcast
145 static inline void (g_cond_signal)(CompatGCond *cond)
147 g_once(&cond->once, do_g_cond_new, NULL);
148 g_cond_signal((GCond *) cond->once.retval);
150 #undef g_cond_signal
153 /* before 2.31 there was no g_thread_new() */
154 static inline GThread *g_thread_new(const char *name,
155 GThreadFunc func, gpointer data)
157 GThread *thread = g_thread_create(func, data, TRUE, NULL);
158 if (!thread) {
159 g_error("creating thread");
161 return thread;
163 #else
164 #define CompatGMutex GMutex
165 #define CompatGCond GCond
166 #endif /* glib 2.31 */
168 #endif