2 * GLIB Compatibility Functions
4 * Copyright IBM, Corp. 2013
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
21 /* GLIB version compatibility flags */
22 #if !GLIB_CHECK_VERSION(2, 26, 0)
23 #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
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
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()
42 #if defined(_WIN32) && !GLIB_CHECK_VERSION(2, 50, 0)
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
);
52 #if !GLIB_CHECK_VERSION(2, 30, 0)
53 /* Not a 100% compatible implementation, but good enough for most
54 * cases. Placeholders are only supported at the end of the
56 static inline gchar
*qemu_g_dir_make_tmp(gchar
const *tmpl
, GError
**error
)
58 gchar
*path
= g_build_filename(g_get_tmp_dir(), tmpl
?: ".XXXXXX", NULL
);
60 if (mkdtemp(path
) != NULL
) {
63 /* Error occurred, clean up. */
64 g_set_error(error
, G_FILE_ERROR
, g_file_error_from_errno(errno
),
70 #define g_dir_make_tmp(tmpl, error) qemu_g_dir_make_tmp(tmpl, error)
71 #endif /* glib 2.30 */
74 #if !GLIB_CHECK_VERSION(2, 31, 0)
75 /* before glib-2.31, GMutex and GCond was dynamic-only (there was a separate
76 * GStaticMutex, but it didn't work with condition variables).
78 * Our implementation uses GOnce to fake a static implementation that does
79 * not require separate initialization.
80 * We need to rename the types to avoid passing our CompatGMutex/CompatGCond
81 * by mistake to a function that expects GMutex/GCond. However, for ease
82 * of use we keep the GLib function names. GLib uses macros for the
83 * implementation, we use inline functions instead and undefine the macros.
86 typedef struct CompatGMutex
{
90 typedef struct CompatGCond
{
94 static inline gpointer
do_g_mutex_new(gpointer unused
)
96 return (gpointer
) g_mutex_new();
99 static inline void g_mutex_init(CompatGMutex
*mutex
)
101 mutex
->once
= (GOnce
) G_ONCE_INIT
;
104 static inline void g_mutex_clear(CompatGMutex
*mutex
)
106 g_assert(mutex
->once
.status
!= G_ONCE_STATUS_PROGRESS
);
107 if (mutex
->once
.retval
) {
108 g_mutex_free((GMutex
*) mutex
->once
.retval
);
110 mutex
->once
= (GOnce
) G_ONCE_INIT
;
113 static inline void (g_mutex_lock
)(CompatGMutex
*mutex
)
115 g_once(&mutex
->once
, do_g_mutex_new
, NULL
);
116 g_mutex_lock((GMutex
*) mutex
->once
.retval
);
120 static inline gboolean (g_mutex_trylock
)(CompatGMutex
*mutex
)
122 g_once(&mutex
->once
, do_g_mutex_new
, NULL
);
123 return g_mutex_trylock((GMutex
*) mutex
->once
.retval
);
125 #undef g_mutex_trylock
128 static inline void (g_mutex_unlock
)(CompatGMutex
*mutex
)
130 g_mutex_unlock((GMutex
*) mutex
->once
.retval
);
132 #undef g_mutex_unlock
134 static inline gpointer
do_g_cond_new(gpointer unused
)
136 return (gpointer
) g_cond_new();
139 static inline void g_cond_init(CompatGCond
*cond
)
141 cond
->once
= (GOnce
) G_ONCE_INIT
;
144 static inline void g_cond_clear(CompatGCond
*cond
)
146 g_assert(cond
->once
.status
!= G_ONCE_STATUS_PROGRESS
);
147 if (cond
->once
.retval
) {
148 g_cond_free((GCond
*) cond
->once
.retval
);
150 cond
->once
= (GOnce
) G_ONCE_INIT
;
153 static inline void (g_cond_wait
)(CompatGCond
*cond
, CompatGMutex
*mutex
)
155 g_assert(mutex
->once
.status
!= G_ONCE_STATUS_PROGRESS
);
156 g_once(&cond
->once
, do_g_cond_new
, NULL
);
157 g_cond_wait((GCond
*) cond
->once
.retval
, (GMutex
*) mutex
->once
.retval
);
161 static inline void (g_cond_broadcast
)(CompatGCond
*cond
)
163 g_once(&cond
->once
, do_g_cond_new
, NULL
);
164 g_cond_broadcast((GCond
*) cond
->once
.retval
);
166 #undef g_cond_broadcast
168 static inline void (g_cond_signal
)(CompatGCond
*cond
)
170 g_once(&cond
->once
, do_g_cond_new
, NULL
);
171 g_cond_signal((GCond
*) cond
->once
.retval
);
175 static inline gboolean (g_cond_timed_wait
)(CompatGCond
*cond
,
179 g_assert(mutex
->once
.status
!= G_ONCE_STATUS_PROGRESS
);
180 g_once(&cond
->once
, do_g_cond_new
, NULL
);
181 return g_cond_timed_wait((GCond
*) cond
->once
.retval
,
182 (GMutex
*) mutex
->once
.retval
, time
);
184 #undef g_cond_timed_wait
186 /* This is not a macro, because it didn't exist until 2.32. */
187 static inline gboolean
g_cond_wait_until(CompatGCond
*cond
, CompatGMutex
*mutex
,
192 /* Convert from monotonic to CLOCK_REALTIME. */
193 end_time
-= g_get_monotonic_time();
194 g_get_current_time(&time
);
195 end_time
+= time
.tv_sec
* G_TIME_SPAN_SECOND
+ time
.tv_usec
;
197 time
.tv_sec
= end_time
/ G_TIME_SPAN_SECOND
;
198 time
.tv_usec
= end_time
% G_TIME_SPAN_SECOND
;
199 return g_cond_timed_wait(cond
, mutex
, &time
);
202 /* before 2.31 there was no g_thread_new() */
203 static inline GThread
*g_thread_new(const char *name
,
204 GThreadFunc func
, gpointer data
)
206 GThread
*thread
= g_thread_create(func
, data
, TRUE
, NULL
);
208 g_error("creating thread");
213 #define CompatGMutex GMutex
214 #define CompatGCond GCond
215 #endif /* glib 2.31 */
217 #if !GLIB_CHECK_VERSION(2, 32, 0)
218 /* Beware, function returns gboolean since 2.39.2, see GLib commit 9101915 */
219 static inline void g_hash_table_add(GHashTable
*hash_table
, gpointer key
)
221 g_hash_table_replace(hash_table
, key
, key
);
225 #ifndef g_assert_true
226 #define g_assert_true(expr) \
228 if (G_LIKELY(expr)) { \
230 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
231 "'" #expr "' should be TRUE"); \
236 #ifndef g_assert_false
237 #define g_assert_false(expr) \
239 if (G_LIKELY(!(expr))) { \
241 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
242 "'" #expr "' should be FALSE"); \
247 #ifndef g_assert_null
248 #define g_assert_null(expr) \
250 if (G_LIKELY((expr) == NULL)) { \
252 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
253 "'" #expr "' should be NULL"); \
258 #ifndef g_assert_nonnull
259 #define g_assert_nonnull(expr) \
261 if (G_LIKELY((expr) != NULL)) { \
263 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
264 "'" #expr "' should not be NULL"); \
269 #ifndef g_assert_cmpmem
270 #define g_assert_cmpmem(m1, l1, m2, l2) \
272 gconstpointer __m1 = m1, __m2 = m2; \
273 int __l1 = l1, __l2 = l2; \
274 if (__l1 != __l2) { \
275 g_assertion_message_cmpnum( \
276 G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
277 #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", __l1, "==", \
279 } else if (memcmp(__m1, __m2, __l1) != 0) { \
280 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
281 "assertion failed (" #m1 " == " #m2 ")"); \
286 #if !GLIB_CHECK_VERSION(2, 28, 0)
287 static inline void g_list_free_full(GList
*list
, GDestroyNotify free_func
)
291 for (l
= list
; l
; l
= l
->next
) {
298 static inline void g_slist_free_full(GSList
*list
, GDestroyNotify free_func
)
302 for (l
= list
; l
; l
= l
->next
) {
310 #if !GLIB_CHECK_VERSION(2, 26, 0)
311 static inline void g_source_set_name(GSource
*source
, const char *name
)
313 /* This is just a debugging aid, so leaving it a no-op */
315 static inline void g_source_set_name_by_id(guint tag
, const char *name
)
317 /* This is just a debugging aid, so leaving it a no-op */
321 #if !GLIB_CHECK_VERSION(2, 36, 0)
322 /* Always fail. This will not include error_report output in the test log,
323 * sending it instead to stderr.
325 #define g_test_initialized() (0)
327 #if !GLIB_CHECK_VERSION(2, 38, 0)
328 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
329 #error schizophrenic detection of glib subprocess testing
331 #define g_test_subprocess() (0)
335 #if !GLIB_CHECK_VERSION(2, 34, 0)
337 g_test_add_data_func_full(const char *path
,
340 gpointer data_free_func
)
342 #if GLIB_CHECK_VERSION(2, 26, 0)
343 /* back-compat casts, remove this once we can require new-enough glib */
344 g_test_add_vtable(path
, 0, data
, NULL
,
345 (GTestFixtureFunc
)fn
, (GTestFixtureFunc
) data_free_func
);
347 /* back-compat casts, remove this once we can require new-enough glib */
348 g_test_add_vtable(path
, 0, data
, NULL
,
349 (void (*)(void)) fn
, (void (*)(void)) data_free_func
);