]> git.proxmox.com Git - mirror_qemu.git/blob - include/glib-compat.h
Merge remote-tracking branch 'remotes/kraxel/tags/pull-vga-20141015-1' into staging
[mirror_qemu.git] / include / glib-compat.h
1 /*
2 * GLIB Compatibility Functions
3 *
4 * Copyright IBM, Corp. 2013
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 * Michael Tokarev <mjt@tls.msk.ru>
9 * Paolo Bonzini <pbonzini@redhat.com>
10 *
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.
13 *
14 */
15
16 #ifndef QEMU_GLIB_COMPAT_H
17 #define QEMU_GLIB_COMPAT_H
18
19 #include <glib.h>
20
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
25
26 #if !GLIB_CHECK_VERSION(2, 14, 0)
27 static inline guint g_timeout_add_seconds(guint interval, GSourceFunc function,
28 gpointer data)
29 {
30 return g_timeout_add(interval * 1000, function, data);
31 }
32 #endif
33
34 #if !GLIB_CHECK_VERSION(2, 28, 0)
35 static inline gint64 g_get_monotonic_time(void)
36 {
37 /* g_get_monotonic_time() is best-effort so we can use the wall clock as a
38 * fallback.
39 */
40
41 GTimeVal time;
42 g_get_current_time(&time);
43
44 return time.tv_sec * G_TIME_SPAN_SECOND + time.tv_usec;
45 }
46 #endif
47
48 #ifdef _WIN32
49 /*
50 * g_poll has a problem on Windows when using
51 * timeouts < 10ms, so use wrapper.
52 */
53 #define g_poll(fds, nfds, timeout) g_poll_fixed(fds, nfds, timeout)
54 gint g_poll_fixed(GPollFD *fds, guint nfds, gint timeout);
55 #elif !GLIB_CHECK_VERSION(2, 20, 0)
56 /*
57 * Glib before 2.20.0 doesn't implement g_poll, so wrap it to compile properly
58 * on older systems.
59 */
60 static inline gint g_poll(GPollFD *fds, guint nfds, gint timeout)
61 {
62 GMainContext *ctx = g_main_context_default();
63 return g_main_context_get_poll_func(ctx)(fds, nfds, timeout);
64 }
65 #endif
66
67 #if !GLIB_CHECK_VERSION(2, 31, 0)
68 /* before glib-2.31, GMutex and GCond was dynamic-only (there was a separate
69 * GStaticMutex, but it didn't work with condition variables).
70 *
71 * Our implementation uses GOnce to fake a static implementation that does
72 * not require separate initialization.
73 * We need to rename the types to avoid passing our CompatGMutex/CompatGCond
74 * by mistake to a function that expects GMutex/GCond. However, for ease
75 * of use we keep the GLib function names. GLib uses macros for the
76 * implementation, we use inline functions instead and undefine the macros.
77 */
78
79 typedef struct CompatGMutex {
80 GOnce once;
81 } CompatGMutex;
82
83 typedef struct CompatGCond {
84 GOnce once;
85 } CompatGCond;
86
87 static inline gpointer do_g_mutex_new(gpointer unused)
88 {
89 return (gpointer) g_mutex_new();
90 }
91
92 static inline void g_mutex_init(CompatGMutex *mutex)
93 {
94 mutex->once = (GOnce) G_ONCE_INIT;
95 }
96
97 static inline void g_mutex_clear(CompatGMutex *mutex)
98 {
99 assert(mutex->once.status != G_ONCE_STATUS_PROGRESS);
100 if (mutex->once.retval) {
101 g_mutex_free((GMutex *) mutex->once.retval);
102 }
103 mutex->once = (GOnce) G_ONCE_INIT;
104 }
105
106 static inline void (g_mutex_lock)(CompatGMutex *mutex)
107 {
108 g_once(&mutex->once, do_g_mutex_new, NULL);
109 g_mutex_lock((GMutex *) mutex->once.retval);
110 }
111 #undef g_mutex_lock
112
113 static inline gboolean (g_mutex_trylock)(CompatGMutex *mutex)
114 {
115 g_once(&mutex->once, do_g_mutex_new, NULL);
116 return g_mutex_trylock((GMutex *) mutex->once.retval);
117 }
118 #undef g_mutex_trylock
119
120
121 static inline void (g_mutex_unlock)(CompatGMutex *mutex)
122 {
123 g_mutex_unlock((GMutex *) mutex->once.retval);
124 }
125 #undef g_mutex_unlock
126
127 static inline gpointer do_g_cond_new(gpointer unused)
128 {
129 return (gpointer) g_cond_new();
130 }
131
132 static inline void g_cond_init(CompatGCond *cond)
133 {
134 cond->once = (GOnce) G_ONCE_INIT;
135 }
136
137 static inline void g_cond_clear(CompatGCond *cond)
138 {
139 assert(cond->once.status != G_ONCE_STATUS_PROGRESS);
140 if (cond->once.retval) {
141 g_cond_free((GCond *) cond->once.retval);
142 }
143 cond->once = (GOnce) G_ONCE_INIT;
144 }
145
146 static inline void (g_cond_wait)(CompatGCond *cond, CompatGMutex *mutex)
147 {
148 assert(mutex->once.status != G_ONCE_STATUS_PROGRESS);
149 g_once(&cond->once, do_g_cond_new, NULL);
150 g_cond_wait((GCond *) cond->once.retval, (GMutex *) mutex->once.retval);
151 }
152 #undef g_cond_wait
153
154 static inline void (g_cond_broadcast)(CompatGCond *cond)
155 {
156 g_once(&cond->once, do_g_cond_new, NULL);
157 g_cond_broadcast((GCond *) cond->once.retval);
158 }
159 #undef g_cond_broadcast
160
161 static inline void (g_cond_signal)(CompatGCond *cond)
162 {
163 g_once(&cond->once, do_g_cond_new, NULL);
164 g_cond_signal((GCond *) cond->once.retval);
165 }
166 #undef g_cond_signal
167
168
169 /* before 2.31 there was no g_thread_new() */
170 static inline GThread *g_thread_new(const char *name,
171 GThreadFunc func, gpointer data)
172 {
173 GThread *thread = g_thread_create(func, data, TRUE, NULL);
174 if (!thread) {
175 g_error("creating thread");
176 }
177 return thread;
178 }
179 #else
180 #define CompatGMutex GMutex
181 #define CompatGCond GCond
182 #endif /* glib 2.31 */
183
184 #endif