1 #ifndef QEMU_THREAD_H 2 #define QEMU_THREAD_H 3 4 #include "qemu/processor.h" 5 #include "qemu/atomic.h" 6 7 typedef struct QemuCond QemuCond; 8 typedef struct QemuSemaphore QemuSemaphore; 9 typedef struct QemuEvent QemuEvent; 10 typedef struct QemuLockCnt QemuLockCnt; 11 typedef struct QemuThread QemuThread; 12 13 #ifdef _WIN32 14 #include "qemu/thread-win32.h" 15 #else 16 #include "qemu/thread-posix.h" 17 #endif 18 19 /* include QSP header once QemuMutex, QemuCond etc. are defined */ 20 #include "qemu/qsp.h" 21 22 #define QEMU_THREAD_JOINABLE 0 23 #define QEMU_THREAD_DETACHED 1 24 25 void qemu_mutex_init(QemuMutex *mutex); 26 void qemu_mutex_destroy(QemuMutex *mutex); 27 int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line); 28 void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line); 29 void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line); 30 31 typedef void (*QemuMutexLockFunc)(QemuMutex *m, const char *f, int l); 32 typedef int (*QemuMutexTrylockFunc)(QemuMutex *m, const char *f, int l); 33 typedef void (*QemuRecMutexLockFunc)(QemuRecMutex *m, const char *f, int l); 34 typedef int (*QemuRecMutexTrylockFunc)(QemuRecMutex *m, const char *f, int l); 35 typedef void (*QemuCondWaitFunc)(QemuCond *c, QemuMutex *m, const char *f, 36 int l); 37 typedef bool (*QemuCondTimedWaitFunc)(QemuCond *c, QemuMutex *m, int ms, 38 const char *f, int l); 39 40 extern QemuMutexLockFunc qemu_bql_mutex_lock_func; 41 extern QemuMutexLockFunc qemu_mutex_lock_func; 42 extern QemuMutexTrylockFunc qemu_mutex_trylock_func; 43 extern QemuRecMutexLockFunc qemu_rec_mutex_lock_func; 44 extern QemuRecMutexTrylockFunc qemu_rec_mutex_trylock_func; 45 extern QemuCondWaitFunc qemu_cond_wait_func; 46 extern QemuCondTimedWaitFunc qemu_cond_timedwait_func; 47 48 /* convenience macros to bypass the profiler */ 49 #define qemu_mutex_lock__raw(m) \ 50 qemu_mutex_lock_impl(m, __FILE__, __LINE__) 51 #define qemu_mutex_trylock__raw(m) \ 52 qemu_mutex_trylock_impl(m, __FILE__, __LINE__) 53 54 #ifdef __COVERITY__ 55 /* 56 * Coverity is severely confused by the indirect function calls, 57 * hide them. 58 */ 59 #define qemu_mutex_lock(m) \ 60 qemu_mutex_lock_impl(m, __FILE__, __LINE__) 61 #define qemu_mutex_trylock(m) \ 62 qemu_mutex_trylock_impl(m, __FILE__, __LINE__) 63 #define qemu_rec_mutex_lock(m) \ 64 qemu_rec_mutex_lock_impl(m, __FILE__, __LINE__) 65 #define qemu_rec_mutex_trylock(m) \ 66 qemu_rec_mutex_trylock_impl(m, __FILE__, __LINE__) 67 #define qemu_cond_wait(c, m) \ 68 qemu_cond_wait_impl(c, m, __FILE__, __LINE__) 69 #define qemu_cond_timedwait(c, m, ms) \ 70 qemu_cond_timedwait_impl(c, m, ms, __FILE__, __LINE__) 71 #else 72 #define qemu_mutex_lock(m) ({ \ 73 QemuMutexLockFunc _f = atomic_read(&qemu_mutex_lock_func); \ 74 _f(m, __FILE__, __LINE__); \ 75 }) 76 77 #define qemu_mutex_trylock(m) ({ \ 78 QemuMutexTrylockFunc _f = atomic_read(&qemu_mutex_trylock_func); \ 79 _f(m, __FILE__, __LINE__); \ 80 }) 81 82 #define qemu_rec_mutex_lock(m) ({ \ 83 QemuRecMutexLockFunc _f = atomic_read(&qemu_rec_mutex_lock_func); \ 84 _f(m, __FILE__, __LINE__); \ 85 }) 86 87 #define qemu_rec_mutex_trylock(m) ({ \ 88 QemuRecMutexTrylockFunc _f; \ 89 _f = atomic_read(&qemu_rec_mutex_trylock_func); \ 90 _f(m, __FILE__, __LINE__); \ 91 }) 92 93 #define qemu_cond_wait(c, m) ({ \ 94 QemuCondWaitFunc _f = atomic_read(&qemu_cond_wait_func); \ 95 _f(c, m, __FILE__, __LINE__); \ 96 }) 97 98 #define qemu_cond_timedwait(c, m, ms) ({ \ 99 QemuCondTimedWaitFunc _f = atomic_read(&qemu_cond_timedwait_func); \ 100 _f(c, m, ms, __FILE__, __LINE__); \ 101 }) 102 #endif 103 104 #define qemu_mutex_unlock(mutex) \ 105 qemu_mutex_unlock_impl(mutex, __FILE__, __LINE__) 106 107 static inline void (qemu_mutex_lock)(QemuMutex *mutex) 108 { 109 qemu_mutex_lock(mutex); 110 } 111 112 static inline int (qemu_mutex_trylock)(QemuMutex *mutex) 113 { 114 return qemu_mutex_trylock(mutex); 115 } 116 117 static inline void (qemu_mutex_unlock)(QemuMutex *mutex) 118 { 119 qemu_mutex_unlock(mutex); 120 } 121 122 static inline void (qemu_rec_mutex_lock)(QemuRecMutex *mutex) 123 { 124 qemu_rec_mutex_lock(mutex); 125 } 126 127 static inline int (qemu_rec_mutex_trylock)(QemuRecMutex *mutex) 128 { 129 return qemu_rec_mutex_trylock(mutex); 130 } 131 132 /* Prototypes for other functions are in thread-posix.h/thread-win32.h. */ 133 void qemu_rec_mutex_init(QemuRecMutex *mutex); 134 135 void qemu_cond_init(QemuCond *cond); 136 void qemu_cond_destroy(QemuCond *cond); 137 138 /* 139 * IMPORTANT: The implementation does not guarantee that pthread_cond_signal 140 * and pthread_cond_broadcast can be called except while the same mutex is 141 * held as in the corresponding pthread_cond_wait calls! 142 */ 143 void qemu_cond_signal(QemuCond *cond); 144 void qemu_cond_broadcast(QemuCond *cond); 145 void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, 146 const char *file, const int line); 147 bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms, 148 const char *file, const int line); 149 150 static inline void (qemu_cond_wait)(QemuCond *cond, QemuMutex *mutex) 151 { 152 qemu_cond_wait(cond, mutex); 153 } 154 155 /* Returns true if timeout has not expired, and false otherwise */ 156 static inline bool (qemu_cond_timedwait)(QemuCond *cond, QemuMutex *mutex, 157 int ms) 158 { 159 return qemu_cond_timedwait(cond, mutex, ms); 160 } 161 162 void qemu_sem_init(QemuSemaphore *sem, int init); 163 void qemu_sem_post(QemuSemaphore *sem); 164 void qemu_sem_wait(QemuSemaphore *sem); 165 int qemu_sem_timedwait(QemuSemaphore *sem, int ms); 166 void qemu_sem_destroy(QemuSemaphore *sem); 167 168 void qemu_event_init(QemuEvent *ev, bool init); 169 void qemu_event_set(QemuEvent *ev); 170 void qemu_event_reset(QemuEvent *ev); 171 void qemu_event_wait(QemuEvent *ev); 172 void qemu_event_destroy(QemuEvent *ev); 173 174 void qemu_thread_create(QemuThread *thread, const char *name, 175 void *(*start_routine)(void *), 176 void *arg, int mode); 177 void *qemu_thread_join(QemuThread *thread); 178 void qemu_thread_get_self(QemuThread *thread); 179 bool qemu_thread_is_self(QemuThread *thread); 180 void qemu_thread_exit(void *retval) QEMU_NORETURN; 181 void qemu_thread_naming(bool enable); 182 183 struct Notifier; 184 /** 185 * qemu_thread_atexit_add: 186 * @notifier: Notifier to add 187 * 188 * Add the specified notifier to a list which will be run via 189 * notifier_list_notify() when this thread exits (either by calling 190 * qemu_thread_exit() or by returning from its start_routine). 191 * The usual usage is that the caller passes a Notifier which is 192 * a per-thread variable; it can then use the callback to free 193 * other per-thread data. 194 * 195 * If the thread exits as part of the entire process exiting, 196 * it is unspecified whether notifiers are called or not. 197 */ 198 void qemu_thread_atexit_add(struct Notifier *notifier); 199 /** 200 * qemu_thread_atexit_remove: 201 * @notifier: Notifier to remove 202 * 203 * Remove the specified notifier from the thread-exit notification 204 * list. It is not valid to try to remove a notifier which is not 205 * on the list. 206 */ 207 void qemu_thread_atexit_remove(struct Notifier *notifier); 208 209 struct QemuSpin { 210 int value; 211 }; 212 213 static inline void qemu_spin_init(QemuSpin *spin) 214 { 215 __sync_lock_release(&spin->value); 216 } 217 218 static inline void qemu_spin_lock(QemuSpin *spin) 219 { 220 while (unlikely(__sync_lock_test_and_set(&spin->value, true))) { 221 while (atomic_read(&spin->value)) { 222 cpu_relax(); 223 } 224 } 225 } 226 227 static inline bool qemu_spin_trylock(QemuSpin *spin) 228 { 229 return __sync_lock_test_and_set(&spin->value, true); 230 } 231 232 static inline bool qemu_spin_locked(QemuSpin *spin) 233 { 234 return atomic_read(&spin->value); 235 } 236 237 static inline void qemu_spin_unlock(QemuSpin *spin) 238 { 239 __sync_lock_release(&spin->value); 240 } 241 242 struct QemuLockCnt { 243 #ifndef CONFIG_LINUX 244 QemuMutex mutex; 245 #endif 246 unsigned count; 247 }; 248 249 /** 250 * qemu_lockcnt_init: initialize a QemuLockcnt 251 * @lockcnt: the lockcnt to initialize 252 * 253 * Initialize lockcnt's counter to zero and prepare its mutex 254 * for usage. 255 */ 256 void qemu_lockcnt_init(QemuLockCnt *lockcnt); 257 258 /** 259 * qemu_lockcnt_destroy: destroy a QemuLockcnt 260 * @lockcnt: the lockcnt to destruct 261 * 262 * Destroy lockcnt's mutex. 263 */ 264 void qemu_lockcnt_destroy(QemuLockCnt *lockcnt); 265 266 /** 267 * qemu_lockcnt_inc: increment a QemuLockCnt's counter 268 * @lockcnt: the lockcnt to operate on 269 * 270 * If the lockcnt's count is zero, wait for critical sections 271 * to finish and increment lockcnt's count to 1. If the count 272 * is not zero, just increment it. 273 * 274 * Because this function can wait on the mutex, it must not be 275 * called while the lockcnt's mutex is held by the current thread. 276 * For the same reason, qemu_lockcnt_inc can also contribute to 277 * AB-BA deadlocks. This is a sample deadlock scenario: 278 * 279 * thread 1 thread 2 280 * ------------------------------------------------------- 281 * qemu_lockcnt_lock(&lc1); 282 * qemu_lockcnt_lock(&lc2); 283 * qemu_lockcnt_inc(&lc2); 284 * qemu_lockcnt_inc(&lc1); 285 */ 286 void qemu_lockcnt_inc(QemuLockCnt *lockcnt); 287 288 /** 289 * qemu_lockcnt_dec: decrement a QemuLockCnt's counter 290 * @lockcnt: the lockcnt to operate on 291 */ 292 void qemu_lockcnt_dec(QemuLockCnt *lockcnt); 293 294 /** 295 * qemu_lockcnt_dec_and_lock: decrement a QemuLockCnt's counter and 296 * possibly lock it. 297 * @lockcnt: the lockcnt to operate on 298 * 299 * Decrement lockcnt's count. If the new count is zero, lock 300 * the mutex and return true. Otherwise, return false. 301 */ 302 bool qemu_lockcnt_dec_and_lock(QemuLockCnt *lockcnt); 303 304 /** 305 * qemu_lockcnt_dec_if_lock: possibly decrement a QemuLockCnt's counter and 306 * lock it. 307 * @lockcnt: the lockcnt to operate on 308 * 309 * If the count is 1, decrement the count to zero, lock 310 * the mutex and return true. Otherwise, return false. 311 */ 312 bool qemu_lockcnt_dec_if_lock(QemuLockCnt *lockcnt); 313 314 /** 315 * qemu_lockcnt_lock: lock a QemuLockCnt's mutex. 316 * @lockcnt: the lockcnt to operate on 317 * 318 * Remember that concurrent visits are not blocked unless the count is 319 * also zero. You can use qemu_lockcnt_count to check for this inside a 320 * critical section. 321 */ 322 void qemu_lockcnt_lock(QemuLockCnt *lockcnt); 323 324 /** 325 * qemu_lockcnt_unlock: release a QemuLockCnt's mutex. 326 * @lockcnt: the lockcnt to operate on. 327 */ 328 void qemu_lockcnt_unlock(QemuLockCnt *lockcnt); 329 330 /** 331 * qemu_lockcnt_inc_and_unlock: combined unlock/increment on a QemuLockCnt. 332 * @lockcnt: the lockcnt to operate on. 333 * 334 * This is the same as 335 * 336 * qemu_lockcnt_unlock(lockcnt); 337 * qemu_lockcnt_inc(lockcnt); 338 * 339 * but more efficient. 340 */ 341 void qemu_lockcnt_inc_and_unlock(QemuLockCnt *lockcnt); 342 343 /** 344 * qemu_lockcnt_count: query a LockCnt's count. 345 * @lockcnt: the lockcnt to query. 346 * 347 * Note that the count can change at any time. Still, while the 348 * lockcnt is locked, one can usefully check whether the count 349 * is non-zero. 350 */ 351 unsigned qemu_lockcnt_count(QemuLockCnt *lockcnt); 352 353 #endif 354