4 #include "qemu/processor.h"
5 #include "qemu/atomic.h"
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
;
14 #include "qemu/thread-win32.h"
16 #include "qemu/thread-posix.h"
19 /* include QSP header once QemuMutex, QemuCond etc. are defined */
22 #define QEMU_THREAD_JOINABLE 0
23 #define QEMU_THREAD_DETACHED 1
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
);
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
,
37 typedef bool (*QemuCondTimedWaitFunc
)(QemuCond
*c
, QemuMutex
*m
, int ms
,
38 const char *f
, int l
);
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
;
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__)
56 * Coverity is severely confused by the indirect function calls,
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__)
72 #define qemu_mutex_lock(m) ({ \
73 QemuMutexLockFunc _f = qatomic_read(&qemu_mutex_lock_func); \
74 _f(m, __FILE__, __LINE__); \
77 #define qemu_mutex_trylock(m) ({ \
78 QemuMutexTrylockFunc _f = qatomic_read(&qemu_mutex_trylock_func); \
79 _f(m, __FILE__, __LINE__); \
82 #define qemu_rec_mutex_lock(m) ({ \
83 QemuRecMutexLockFunc _f = qatomic_read(&qemu_rec_mutex_lock_func);\
84 _f(m, __FILE__, __LINE__); \
87 #define qemu_rec_mutex_trylock(m) ({ \
88 QemuRecMutexTrylockFunc _f; \
89 _f = qatomic_read(&qemu_rec_mutex_trylock_func); \
90 _f(m, __FILE__, __LINE__); \
93 #define qemu_cond_wait(c, m) ({ \
94 QemuCondWaitFunc _f = qatomic_read(&qemu_cond_wait_func); \
95 _f(c, m, __FILE__, __LINE__); \
98 #define qemu_cond_timedwait(c, m, ms) ({ \
99 QemuCondTimedWaitFunc _f = qatomic_read(&qemu_cond_timedwait_func);\
100 _f(c, m, ms, __FILE__, __LINE__); \
104 #define qemu_mutex_unlock(mutex) \
105 qemu_mutex_unlock_impl(mutex, __FILE__, __LINE__)
107 static inline void (qemu_mutex_lock
)(QemuMutex
*mutex
)
109 qemu_mutex_lock(mutex
);
112 static inline int (qemu_mutex_trylock
)(QemuMutex
*mutex
)
114 return qemu_mutex_trylock(mutex
);
117 static inline void (qemu_mutex_unlock
)(QemuMutex
*mutex
)
119 qemu_mutex_unlock(mutex
);
122 static inline void (qemu_rec_mutex_lock
)(QemuRecMutex
*mutex
)
124 qemu_rec_mutex_lock(mutex
);
127 static inline int (qemu_rec_mutex_trylock
)(QemuRecMutex
*mutex
)
129 return qemu_rec_mutex_trylock(mutex
);
132 /* Prototypes for other functions are in thread-posix.h/thread-win32.h. */
133 void qemu_rec_mutex_init(QemuRecMutex
*mutex
);
135 void qemu_cond_init(QemuCond
*cond
);
136 void qemu_cond_destroy(QemuCond
*cond
);
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!
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
);
150 static inline void (qemu_cond_wait
)(QemuCond
*cond
, QemuMutex
*mutex
)
152 qemu_cond_wait(cond
, mutex
);
155 /* Returns true if timeout has not expired, and false otherwise */
156 static inline bool (qemu_cond_timedwait
)(QemuCond
*cond
, QemuMutex
*mutex
,
159 return qemu_cond_timedwait(cond
, mutex
, ms
);
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
);
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
);
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
);
185 * qemu_thread_atexit_add:
186 * @notifier: Notifier to add
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.
195 * If the thread exits as part of the entire process exiting,
196 * it is unspecified whether notifiers are called or not.
198 void qemu_thread_atexit_add(struct Notifier
*notifier
);
200 * qemu_thread_atexit_remove:
201 * @notifier: Notifier to remove
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
207 void qemu_thread_atexit_remove(struct Notifier
*notifier
);
210 #include <sanitizer/tsan_interface.h>
217 static inline void qemu_spin_init(QemuSpin
*spin
)
219 __sync_lock_release(&spin
->value
);
221 __tsan_mutex_create(spin
, __tsan_mutex_not_static
);
225 /* const parameter because the only purpose here is the TSAN annotation */
226 static inline void qemu_spin_destroy(const QemuSpin
*spin
)
229 __tsan_mutex_destroy((void *)spin
, __tsan_mutex_not_static
);
233 static inline void qemu_spin_lock(QemuSpin
*spin
)
236 __tsan_mutex_pre_lock(spin
, 0);
238 while (unlikely(__sync_lock_test_and_set(&spin
->value
, true))) {
239 while (qatomic_read(&spin
->value
)) {
244 __tsan_mutex_post_lock(spin
, 0, 0);
248 static inline bool qemu_spin_trylock(QemuSpin
*spin
)
251 __tsan_mutex_pre_lock(spin
, __tsan_mutex_try_lock
);
253 bool busy
= __sync_lock_test_and_set(&spin
->value
, true);
255 unsigned flags
= __tsan_mutex_try_lock
;
256 flags
|= busy
? __tsan_mutex_try_lock_failed
: 0;
257 __tsan_mutex_post_lock(spin
, flags
, 0);
262 static inline bool qemu_spin_locked(QemuSpin
*spin
)
264 return qatomic_read(&spin
->value
);
267 static inline void qemu_spin_unlock(QemuSpin
*spin
)
270 __tsan_mutex_pre_unlock(spin
, 0);
272 __sync_lock_release(&spin
->value
);
274 __tsan_mutex_post_unlock(spin
, 0);
286 * qemu_lockcnt_init: initialize a QemuLockcnt
287 * @lockcnt: the lockcnt to initialize
289 * Initialize lockcnt's counter to zero and prepare its mutex
292 void qemu_lockcnt_init(QemuLockCnt
*lockcnt
);
295 * qemu_lockcnt_destroy: destroy a QemuLockcnt
296 * @lockcnt: the lockcnt to destruct
298 * Destroy lockcnt's mutex.
300 void qemu_lockcnt_destroy(QemuLockCnt
*lockcnt
);
303 * qemu_lockcnt_inc: increment a QemuLockCnt's counter
304 * @lockcnt: the lockcnt to operate on
306 * If the lockcnt's count is zero, wait for critical sections
307 * to finish and increment lockcnt's count to 1. If the count
308 * is not zero, just increment it.
310 * Because this function can wait on the mutex, it must not be
311 * called while the lockcnt's mutex is held by the current thread.
312 * For the same reason, qemu_lockcnt_inc can also contribute to
313 * AB-BA deadlocks. This is a sample deadlock scenario:
316 * -------------------------------------------------------
317 * qemu_lockcnt_lock(&lc1);
318 * qemu_lockcnt_lock(&lc2);
319 * qemu_lockcnt_inc(&lc2);
320 * qemu_lockcnt_inc(&lc1);
322 void qemu_lockcnt_inc(QemuLockCnt
*lockcnt
);
325 * qemu_lockcnt_dec: decrement a QemuLockCnt's counter
326 * @lockcnt: the lockcnt to operate on
328 void qemu_lockcnt_dec(QemuLockCnt
*lockcnt
);
331 * qemu_lockcnt_dec_and_lock: decrement a QemuLockCnt's counter and
333 * @lockcnt: the lockcnt to operate on
335 * Decrement lockcnt's count. If the new count is zero, lock
336 * the mutex and return true. Otherwise, return false.
338 bool qemu_lockcnt_dec_and_lock(QemuLockCnt
*lockcnt
);
341 * qemu_lockcnt_dec_if_lock: possibly decrement a QemuLockCnt's counter and
343 * @lockcnt: the lockcnt to operate on
345 * If the count is 1, decrement the count to zero, lock
346 * the mutex and return true. Otherwise, return false.
348 bool qemu_lockcnt_dec_if_lock(QemuLockCnt
*lockcnt
);
351 * qemu_lockcnt_lock: lock a QemuLockCnt's mutex.
352 * @lockcnt: the lockcnt to operate on
354 * Remember that concurrent visits are not blocked unless the count is
355 * also zero. You can use qemu_lockcnt_count to check for this inside a
358 void qemu_lockcnt_lock(QemuLockCnt
*lockcnt
);
361 * qemu_lockcnt_unlock: release a QemuLockCnt's mutex.
362 * @lockcnt: the lockcnt to operate on.
364 void qemu_lockcnt_unlock(QemuLockCnt
*lockcnt
);
367 * qemu_lockcnt_inc_and_unlock: combined unlock/increment on a QemuLockCnt.
368 * @lockcnt: the lockcnt to operate on.
370 * This is the same as
372 * qemu_lockcnt_unlock(lockcnt);
373 * qemu_lockcnt_inc(lockcnt);
375 * but more efficient.
377 void qemu_lockcnt_inc_and_unlock(QemuLockCnt
*lockcnt
);
380 * qemu_lockcnt_count: query a LockCnt's count.
381 * @lockcnt: the lockcnt to query.
383 * Note that the count can change at any time. Still, while the
384 * lockcnt is locked, one can usefully check whether the count
387 unsigned qemu_lockcnt_count(QemuLockCnt
*lockcnt
);