4 #include "qemu/processor.h"
5 #include "qemu/atomic.h"
6 #include "qemu/clang-tsa.h"
8 typedef struct QemuCond QemuCond
;
9 typedef struct QemuSemaphore QemuSemaphore
;
10 typedef struct QemuEvent QemuEvent
;
11 typedef struct QemuLockCnt QemuLockCnt
;
12 typedef struct QemuThread QemuThread
;
15 #include "qemu/thread-win32.h"
17 #include "qemu/thread-posix.h"
20 /* include QSP header once QemuMutex, QemuCond etc. are defined */
23 #define QEMU_THREAD_JOINABLE 0
24 #define QEMU_THREAD_DETACHED 1
26 void qemu_mutex_init(QemuMutex
*mutex
);
27 void qemu_mutex_destroy(QemuMutex
*mutex
);
28 int TSA_NO_TSA
qemu_mutex_trylock_impl(QemuMutex
*mutex
, const char *file
,
30 void TSA_NO_TSA
qemu_mutex_lock_impl(QemuMutex
*mutex
, const char *file
,
32 void TSA_NO_TSA
qemu_mutex_unlock_impl(QemuMutex
*mutex
, const char *file
,
35 void qemu_rec_mutex_init(QemuRecMutex
*mutex
);
36 void qemu_rec_mutex_destroy(QemuRecMutex
*mutex
);
37 void qemu_rec_mutex_lock_impl(QemuRecMutex
*mutex
, const char *file
, int line
);
38 int qemu_rec_mutex_trylock_impl(QemuRecMutex
*mutex
, const char *file
, int line
);
39 void qemu_rec_mutex_unlock_impl(QemuRecMutex
*mutex
, const char *file
, int line
);
41 typedef void (*QemuMutexLockFunc
)(QemuMutex
*m
, const char *f
, int l
);
42 typedef int (*QemuMutexTrylockFunc
)(QemuMutex
*m
, const char *f
, int l
);
43 typedef void (*QemuRecMutexLockFunc
)(QemuRecMutex
*m
, const char *f
, int l
);
44 typedef int (*QemuRecMutexTrylockFunc
)(QemuRecMutex
*m
, const char *f
, int l
);
45 typedef void (*QemuCondWaitFunc
)(QemuCond
*c
, QemuMutex
*m
, const char *f
,
47 typedef bool (*QemuCondTimedWaitFunc
)(QemuCond
*c
, QemuMutex
*m
, int ms
,
48 const char *f
, int l
);
50 extern QemuMutexLockFunc qemu_bql_mutex_lock_func
;
51 extern QemuMutexLockFunc qemu_mutex_lock_func
;
52 extern QemuMutexTrylockFunc qemu_mutex_trylock_func
;
53 extern QemuRecMutexLockFunc qemu_rec_mutex_lock_func
;
54 extern QemuRecMutexTrylockFunc qemu_rec_mutex_trylock_func
;
55 extern QemuCondWaitFunc qemu_cond_wait_func
;
56 extern QemuCondTimedWaitFunc qemu_cond_timedwait_func
;
58 /* convenience macros to bypass the profiler */
59 #define qemu_mutex_lock__raw(m) \
60 qemu_mutex_lock_impl(m, __FILE__, __LINE__)
61 #define qemu_mutex_trylock__raw(m) \
62 qemu_mutex_trylock_impl(m, __FILE__, __LINE__)
66 * Coverity is severely confused by the indirect function calls,
69 #define qemu_mutex_lock(m) \
70 qemu_mutex_lock_impl(m, __FILE__, __LINE__)
71 #define qemu_mutex_trylock(m) \
72 qemu_mutex_trylock_impl(m, __FILE__, __LINE__)
73 #define qemu_rec_mutex_lock(m) \
74 qemu_rec_mutex_lock_impl(m, __FILE__, __LINE__)
75 #define qemu_rec_mutex_trylock(m) \
76 qemu_rec_mutex_trylock_impl(m, __FILE__, __LINE__)
77 #define qemu_cond_wait(c, m) \
78 qemu_cond_wait_impl(c, m, __FILE__, __LINE__)
79 #define qemu_cond_timedwait(c, m, ms) \
80 qemu_cond_timedwait_impl(c, m, ms, __FILE__, __LINE__)
82 #define qemu_mutex_lock(m) ({ \
83 QemuMutexLockFunc _f = qatomic_read(&qemu_mutex_lock_func); \
84 _f(m, __FILE__, __LINE__); \
87 #define qemu_mutex_trylock(m) ({ \
88 QemuMutexTrylockFunc _f = qatomic_read(&qemu_mutex_trylock_func); \
89 _f(m, __FILE__, __LINE__); \
92 #define qemu_rec_mutex_lock(m) ({ \
93 QemuRecMutexLockFunc _f = qatomic_read(&qemu_rec_mutex_lock_func);\
94 _f(m, __FILE__, __LINE__); \
97 #define qemu_rec_mutex_trylock(m) ({ \
98 QemuRecMutexTrylockFunc _f; \
99 _f = qatomic_read(&qemu_rec_mutex_trylock_func); \
100 _f(m, __FILE__, __LINE__); \
103 #define qemu_cond_wait(c, m) ({ \
104 QemuCondWaitFunc _f = qatomic_read(&qemu_cond_wait_func); \
105 _f(c, m, __FILE__, __LINE__); \
108 #define qemu_cond_timedwait(c, m, ms) ({ \
109 QemuCondTimedWaitFunc _f = qatomic_read(&qemu_cond_timedwait_func);\
110 _f(c, m, ms, __FILE__, __LINE__); \
114 #define qemu_mutex_unlock(mutex) \
115 qemu_mutex_unlock_impl(mutex, __FILE__, __LINE__)
117 #define qemu_rec_mutex_unlock(mutex) \
118 qemu_rec_mutex_unlock_impl(mutex, __FILE__, __LINE__)
120 static inline void (qemu_mutex_lock
)(QemuMutex
*mutex
)
122 qemu_mutex_lock(mutex
);
125 static inline int (qemu_mutex_trylock
)(QemuMutex
*mutex
)
127 return qemu_mutex_trylock(mutex
);
130 static inline void (qemu_mutex_unlock
)(QemuMutex
*mutex
)
132 qemu_mutex_unlock(mutex
);
135 static inline void (qemu_rec_mutex_lock
)(QemuRecMutex
*mutex
)
137 qemu_rec_mutex_lock(mutex
);
140 static inline int (qemu_rec_mutex_trylock
)(QemuRecMutex
*mutex
)
142 return qemu_rec_mutex_trylock(mutex
);
145 static inline void (qemu_rec_mutex_unlock
)(QemuRecMutex
*mutex
)
147 qemu_rec_mutex_unlock(mutex
);
150 void qemu_cond_init(QemuCond
*cond
);
151 void qemu_cond_destroy(QemuCond
*cond
);
154 * IMPORTANT: The implementation does not guarantee that pthread_cond_signal
155 * and pthread_cond_broadcast can be called except while the same mutex is
156 * held as in the corresponding pthread_cond_wait calls!
158 void qemu_cond_signal(QemuCond
*cond
);
159 void qemu_cond_broadcast(QemuCond
*cond
);
160 void TSA_NO_TSA
qemu_cond_wait_impl(QemuCond
*cond
, QemuMutex
*mutex
,
161 const char *file
, const int line
);
162 bool qemu_cond_timedwait_impl(QemuCond
*cond
, QemuMutex
*mutex
, int ms
,
163 const char *file
, const int line
);
165 static inline void (qemu_cond_wait
)(QemuCond
*cond
, QemuMutex
*mutex
)
167 qemu_cond_wait(cond
, mutex
);
170 /* Returns true if timeout has not expired, and false otherwise */
171 static inline bool (qemu_cond_timedwait
)(QemuCond
*cond
, QemuMutex
*mutex
,
174 return qemu_cond_timedwait(cond
, mutex
, ms
);
177 void qemu_sem_init(QemuSemaphore
*sem
, int init
);
178 void qemu_sem_post(QemuSemaphore
*sem
);
179 void qemu_sem_wait(QemuSemaphore
*sem
);
180 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
);
181 void qemu_sem_destroy(QemuSemaphore
*sem
);
183 void qemu_event_init(QemuEvent
*ev
, bool init
);
184 void qemu_event_set(QemuEvent
*ev
);
185 void qemu_event_reset(QemuEvent
*ev
);
186 void qemu_event_wait(QemuEvent
*ev
);
187 void qemu_event_destroy(QemuEvent
*ev
);
189 void qemu_thread_create(QemuThread
*thread
, const char *name
,
190 void *(*start_routine
)(void *),
191 void *arg
, int mode
);
192 int qemu_thread_set_affinity(QemuThread
*thread
, unsigned long *host_cpus
,
193 unsigned long nbits
);
194 int qemu_thread_get_affinity(QemuThread
*thread
, unsigned long **host_cpus
,
195 unsigned long *nbits
);
196 void *qemu_thread_join(QemuThread
*thread
);
197 void qemu_thread_get_self(QemuThread
*thread
);
198 bool qemu_thread_is_self(QemuThread
*thread
);
199 G_NORETURN
void qemu_thread_exit(void *retval
);
200 void qemu_thread_naming(bool enable
);
204 * qemu_thread_atexit_add:
205 * @notifier: Notifier to add
207 * Add the specified notifier to a list which will be run via
208 * notifier_list_notify() when this thread exits (either by calling
209 * qemu_thread_exit() or by returning from its start_routine).
210 * The usual usage is that the caller passes a Notifier which is
211 * a per-thread variable; it can then use the callback to free
212 * other per-thread data.
214 * If the thread exits as part of the entire process exiting,
215 * it is unspecified whether notifiers are called or not.
217 void qemu_thread_atexit_add(struct Notifier
*notifier
);
219 * qemu_thread_atexit_remove:
220 * @notifier: Notifier to remove
222 * Remove the specified notifier from the thread-exit notification
223 * list. It is not valid to try to remove a notifier which is not
226 void qemu_thread_atexit_remove(struct Notifier
*notifier
);
229 #include <sanitizer/tsan_interface.h>
236 static inline void qemu_spin_init(QemuSpin
*spin
)
238 qatomic_set(&spin
->value
, 0);
240 __tsan_mutex_create(spin
, __tsan_mutex_not_static
);
244 static inline void qemu_spin_destroy(QemuSpin
*spin
)
247 __tsan_mutex_destroy(spin
, __tsan_mutex_not_static
);
251 static inline void qemu_spin_lock(QemuSpin
*spin
)
254 __tsan_mutex_pre_lock(spin
, 0);
256 while (unlikely(qatomic_xchg(&spin
->value
, 1))) {
257 while (qatomic_read(&spin
->value
)) {
262 __tsan_mutex_post_lock(spin
, 0, 0);
266 static inline bool qemu_spin_trylock(QemuSpin
*spin
)
269 __tsan_mutex_pre_lock(spin
, __tsan_mutex_try_lock
);
271 bool busy
= qatomic_xchg(&spin
->value
, true);
273 unsigned flags
= __tsan_mutex_try_lock
;
274 flags
|= busy
? __tsan_mutex_try_lock_failed
: 0;
275 __tsan_mutex_post_lock(spin
, flags
, 0);
280 static inline bool qemu_spin_locked(QemuSpin
*spin
)
282 return qatomic_read(&spin
->value
);
285 static inline void qemu_spin_unlock(QemuSpin
*spin
)
288 __tsan_mutex_pre_unlock(spin
, 0);
290 qatomic_store_release(&spin
->value
, 0);
292 __tsan_mutex_post_unlock(spin
, 0);
304 * qemu_lockcnt_init: initialize a QemuLockcnt
305 * @lockcnt: the lockcnt to initialize
307 * Initialize lockcnt's counter to zero and prepare its mutex
310 void qemu_lockcnt_init(QemuLockCnt
*lockcnt
);
313 * qemu_lockcnt_destroy: destroy a QemuLockcnt
314 * @lockcnt: the lockcnt to destruct
316 * Destroy lockcnt's mutex.
318 void qemu_lockcnt_destroy(QemuLockCnt
*lockcnt
);
321 * qemu_lockcnt_inc: increment a QemuLockCnt's counter
322 * @lockcnt: the lockcnt to operate on
324 * If the lockcnt's count is zero, wait for critical sections
325 * to finish and increment lockcnt's count to 1. If the count
326 * is not zero, just increment it.
328 * Because this function can wait on the mutex, it must not be
329 * called while the lockcnt's mutex is held by the current thread.
330 * For the same reason, qemu_lockcnt_inc can also contribute to
331 * AB-BA deadlocks. This is a sample deadlock scenario:
334 * -------------------------------------------------------
335 * qemu_lockcnt_lock(&lc1);
336 * qemu_lockcnt_lock(&lc2);
337 * qemu_lockcnt_inc(&lc2);
338 * qemu_lockcnt_inc(&lc1);
340 void qemu_lockcnt_inc(QemuLockCnt
*lockcnt
);
343 * qemu_lockcnt_dec: decrement a QemuLockCnt's counter
344 * @lockcnt: the lockcnt to operate on
346 void qemu_lockcnt_dec(QemuLockCnt
*lockcnt
);
349 * qemu_lockcnt_dec_and_lock: decrement a QemuLockCnt's counter and
351 * @lockcnt: the lockcnt to operate on
353 * Decrement lockcnt's count. If the new count is zero, lock
354 * the mutex and return true. Otherwise, return false.
356 bool qemu_lockcnt_dec_and_lock(QemuLockCnt
*lockcnt
);
359 * qemu_lockcnt_dec_if_lock: possibly decrement a QemuLockCnt's counter and
361 * @lockcnt: the lockcnt to operate on
363 * If the count is 1, decrement the count to zero, lock
364 * the mutex and return true. Otherwise, return false.
366 bool qemu_lockcnt_dec_if_lock(QemuLockCnt
*lockcnt
);
369 * qemu_lockcnt_lock: lock a QemuLockCnt's mutex.
370 * @lockcnt: the lockcnt to operate on
372 * Remember that concurrent visits are not blocked unless the count is
373 * also zero. You can use qemu_lockcnt_count to check for this inside a
376 void qemu_lockcnt_lock(QemuLockCnt
*lockcnt
);
379 * qemu_lockcnt_unlock: release a QemuLockCnt's mutex.
380 * @lockcnt: the lockcnt to operate on.
382 void qemu_lockcnt_unlock(QemuLockCnt
*lockcnt
);
385 * qemu_lockcnt_inc_and_unlock: combined unlock/increment on a QemuLockCnt.
386 * @lockcnt: the lockcnt to operate on.
388 * This is the same as
390 * qemu_lockcnt_unlock(lockcnt);
391 * qemu_lockcnt_inc(lockcnt);
393 * but more efficient.
395 void qemu_lockcnt_inc_and_unlock(QemuLockCnt
*lockcnt
);
398 * qemu_lockcnt_count: query a LockCnt's count.
399 * @lockcnt: the lockcnt to query.
401 * Note that the count can change at any time. Still, while the
402 * lockcnt is locked, one can usefully check whether the count
405 unsigned qemu_lockcnt_count(QemuLockCnt
*lockcnt
);