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, 28, 0) 27 static inline gint64 qemu_g_get_monotonic_time(void) 28 { 29 /* g_get_monotonic_time() is best-effort so we can use the wall clock as a 30 * fallback. 31 */ 32 33 GTimeVal time; 34 g_get_current_time(&time); 35 36 return time.tv_sec * G_TIME_SPAN_SECOND + time.tv_usec; 37 } 38 /* work around distro backports of this interface */ 39 #define g_get_monotonic_time() qemu_g_get_monotonic_time() 40 #endif 41 42 #ifdef _WIN32 43 /* 44 * g_poll has a problem on Windows when using 45 * timeouts < 10ms, so use wrapper. 46 */ 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 50 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). 54 * 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. 61 */ 62 63 typedef struct CompatGMutex { 64 GOnce once; 65 } CompatGMutex; 66 67 typedef struct CompatGCond { 68 GOnce once; 69 } CompatGCond; 70 71 static inline gpointer do_g_mutex_new(gpointer unused) 72 { 73 return (gpointer) g_mutex_new(); 74 } 75 76 static inline void g_mutex_init(CompatGMutex *mutex) 77 { 78 mutex->once = (GOnce) G_ONCE_INIT; 79 } 80 81 static inline void g_mutex_clear(CompatGMutex *mutex) 82 { 83 g_assert(mutex->once.status != G_ONCE_STATUS_PROGRESS); 84 if (mutex->once.retval) { 85 g_mutex_free((GMutex *) mutex->once.retval); 86 } 87 mutex->once = (GOnce) G_ONCE_INIT; 88 } 89 90 static inline void (g_mutex_lock)(CompatGMutex *mutex) 91 { 92 g_once(&mutex->once, do_g_mutex_new, NULL); 93 g_mutex_lock((GMutex *) mutex->once.retval); 94 } 95 #undef g_mutex_lock 96 97 static inline gboolean (g_mutex_trylock)(CompatGMutex *mutex) 98 { 99 g_once(&mutex->once, do_g_mutex_new, NULL); 100 return g_mutex_trylock((GMutex *) mutex->once.retval); 101 } 102 #undef g_mutex_trylock 103 104 105 static inline void (g_mutex_unlock)(CompatGMutex *mutex) 106 { 107 g_mutex_unlock((GMutex *) mutex->once.retval); 108 } 109 #undef g_mutex_unlock 110 111 static inline gpointer do_g_cond_new(gpointer unused) 112 { 113 return (gpointer) g_cond_new(); 114 } 115 116 static inline void g_cond_init(CompatGCond *cond) 117 { 118 cond->once = (GOnce) G_ONCE_INIT; 119 } 120 121 static inline void g_cond_clear(CompatGCond *cond) 122 { 123 g_assert(cond->once.status != G_ONCE_STATUS_PROGRESS); 124 if (cond->once.retval) { 125 g_cond_free((GCond *) cond->once.retval); 126 } 127 cond->once = (GOnce) G_ONCE_INIT; 128 } 129 130 static inline void (g_cond_wait)(CompatGCond *cond, CompatGMutex *mutex) 131 { 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); 135 } 136 #undef g_cond_wait 137 138 static inline void (g_cond_broadcast)(CompatGCond *cond) 139 { 140 g_once(&cond->once, do_g_cond_new, NULL); 141 g_cond_broadcast((GCond *) cond->once.retval); 142 } 143 #undef g_cond_broadcast 144 145 static inline void (g_cond_signal)(CompatGCond *cond) 146 { 147 g_once(&cond->once, do_g_cond_new, NULL); 148 g_cond_signal((GCond *) cond->once.retval); 149 } 150 #undef g_cond_signal 151 152 static inline gboolean (g_cond_timed_wait)(CompatGCond *cond, 153 CompatGMutex *mutex, 154 GTimeVal *time) 155 { 156 g_assert(mutex->once.status != G_ONCE_STATUS_PROGRESS); 157 g_once(&cond->once, do_g_cond_new, NULL); 158 return g_cond_timed_wait((GCond *) cond->once.retval, 159 (GMutex *) mutex->once.retval, time); 160 } 161 #undef g_cond_timed_wait 162 163 /* This is not a macro, because it didn't exist until 2.32. */ 164 static inline gboolean g_cond_wait_until(CompatGCond *cond, CompatGMutex *mutex, 165 gint64 end_time) 166 { 167 GTimeVal time; 168 169 /* Convert from monotonic to CLOCK_REALTIME. */ 170 end_time -= g_get_monotonic_time(); 171 g_get_current_time(&time); 172 end_time += time.tv_sec * G_TIME_SPAN_SECOND + time.tv_usec; 173 174 time.tv_sec = end_time / G_TIME_SPAN_SECOND; 175 time.tv_usec = end_time % G_TIME_SPAN_SECOND; 176 return g_cond_timed_wait(cond, mutex, &time); 177 } 178 179 /* before 2.31 there was no g_thread_new() */ 180 static inline GThread *g_thread_new(const char *name, 181 GThreadFunc func, gpointer data) 182 { 183 GThread *thread = g_thread_create(func, data, TRUE, NULL); 184 if (!thread) { 185 g_error("creating thread"); 186 } 187 return thread; 188 } 189 #else 190 #define CompatGMutex GMutex 191 #define CompatGCond GCond 192 #endif /* glib 2.31 */ 193 194 #if !GLIB_CHECK_VERSION(2, 32, 0) 195 /* Beware, function returns gboolean since 2.39.2, see GLib commit 9101915 */ 196 static inline void g_hash_table_add(GHashTable *hash_table, gpointer key) 197 { 198 g_hash_table_replace(hash_table, key, key); 199 } 200 #endif 201 202 #ifndef g_assert_true 203 #define g_assert_true(expr) \ 204 do { \ 205 if (G_LIKELY(expr)) { \ 206 } else { \ 207 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ 208 "'" #expr "' should be TRUE"); \ 209 } \ 210 } while (0) 211 #endif 212 213 #ifndef g_assert_false 214 #define g_assert_false(expr) \ 215 do { \ 216 if (G_LIKELY(!(expr))) { \ 217 } else { \ 218 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ 219 "'" #expr "' should be FALSE"); \ 220 } \ 221 } while (0) 222 #endif 223 224 #ifndef g_assert_null 225 #define g_assert_null(expr) \ 226 do { \ 227 if (G_LIKELY((expr) == NULL)) { \ 228 } else { \ 229 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ 230 "'" #expr "' should be NULL"); \ 231 } \ 232 } while (0) 233 #endif 234 235 #ifndef g_assert_nonnull 236 #define g_assert_nonnull(expr) \ 237 do { \ 238 if (G_LIKELY((expr) != NULL)) { \ 239 } else { \ 240 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ 241 "'" #expr "' should not be NULL"); \ 242 } \ 243 } while (0) 244 #endif 245 246 #ifndef g_assert_cmpmem 247 #define g_assert_cmpmem(m1, l1, m2, l2) \ 248 do { \ 249 gconstpointer __m1 = m1, __m2 = m2; \ 250 int __l1 = l1, __l2 = l2; \ 251 if (__l1 != __l2) { \ 252 g_assertion_message_cmpnum( \ 253 G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ 254 #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", __l1, "==", \ 255 __l2, 'i'); \ 256 } else if (memcmp(__m1, __m2, __l1) != 0) { \ 257 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ 258 "assertion failed (" #m1 " == " #m2 ")"); \ 259 } \ 260 } while (0) 261 #endif 262 263 #endif 264