4 #include "qemu/processor.h"
5 #include "qemu/atomic.h"
7 typedef struct QemuMutex QemuMutex
;
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 #define QEMU_THREAD_JOINABLE 0
21 #define QEMU_THREAD_DETACHED 1
23 void qemu_mutex_init(QemuMutex
*mutex
);
24 void qemu_mutex_destroy(QemuMutex
*mutex
);
25 int qemu_mutex_trylock_impl(QemuMutex
*mutex
, const char *file
, const int line
);
26 void qemu_mutex_lock_impl(QemuMutex
*mutex
, const char *file
, const int line
);
27 void qemu_mutex_unlock_impl(QemuMutex
*mutex
, const char *file
, const int line
);
29 #define qemu_mutex_lock(mutex) \
30 qemu_mutex_lock_impl(mutex, __FILE__, __LINE__)
31 #define qemu_mutex_trylock(mutex) \
32 qemu_mutex_trylock_impl(mutex, __FILE__, __LINE__)
33 #define qemu_mutex_unlock(mutex) \
34 qemu_mutex_unlock_impl(mutex, __FILE__, __LINE__)
36 static inline void (qemu_mutex_lock
)(QemuMutex
*mutex
)
38 qemu_mutex_lock(mutex
);
41 static inline int (qemu_mutex_trylock
)(QemuMutex
*mutex
)
43 return qemu_mutex_trylock(mutex
);
46 static inline void (qemu_mutex_unlock
)(QemuMutex
*mutex
)
48 qemu_mutex_unlock(mutex
);
51 /* Prototypes for other functions are in thread-posix.h/thread-win32.h. */
52 void qemu_rec_mutex_init(QemuRecMutex
*mutex
);
54 void qemu_cond_init(QemuCond
*cond
);
55 void qemu_cond_destroy(QemuCond
*cond
);
58 * IMPORTANT: The implementation does not guarantee that pthread_cond_signal
59 * and pthread_cond_broadcast can be called except while the same mutex is
60 * held as in the corresponding pthread_cond_wait calls!
62 void qemu_cond_signal(QemuCond
*cond
);
63 void qemu_cond_broadcast(QemuCond
*cond
);
64 void qemu_cond_wait_impl(QemuCond
*cond
, QemuMutex
*mutex
,
65 const char *file
, const int line
);
67 #define qemu_cond_wait(cond, mutex) \
68 qemu_cond_wait_impl(cond, mutex, __FILE__, __LINE__)
70 static inline void (qemu_cond_wait
)(QemuCond
*cond
, QemuMutex
*mutex
)
72 qemu_cond_wait(cond
, mutex
);
75 void qemu_sem_init(QemuSemaphore
*sem
, int init
);
76 void qemu_sem_post(QemuSemaphore
*sem
);
77 void qemu_sem_wait(QemuSemaphore
*sem
);
78 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
);
79 void qemu_sem_destroy(QemuSemaphore
*sem
);
81 void qemu_event_init(QemuEvent
*ev
, bool init
);
82 void qemu_event_set(QemuEvent
*ev
);
83 void qemu_event_reset(QemuEvent
*ev
);
84 void qemu_event_wait(QemuEvent
*ev
);
85 void qemu_event_destroy(QemuEvent
*ev
);
87 void qemu_thread_create(QemuThread
*thread
, const char *name
,
88 void *(*start_routine
)(void *),
90 void *qemu_thread_join(QemuThread
*thread
);
91 void qemu_thread_get_self(QemuThread
*thread
);
92 bool qemu_thread_is_self(QemuThread
*thread
);
93 void qemu_thread_exit(void *retval
);
94 void qemu_thread_naming(bool enable
);
97 void qemu_thread_atexit_add(struct Notifier
*notifier
);
98 void qemu_thread_atexit_remove(struct Notifier
*notifier
);
100 typedef struct QemuSpin
{
104 static inline void qemu_spin_init(QemuSpin
*spin
)
106 __sync_lock_release(&spin
->value
);
109 static inline void qemu_spin_lock(QemuSpin
*spin
)
111 while (unlikely(__sync_lock_test_and_set(&spin
->value
, true))) {
112 while (atomic_read(&spin
->value
)) {
118 static inline bool qemu_spin_trylock(QemuSpin
*spin
)
120 return __sync_lock_test_and_set(&spin
->value
, true);
123 static inline bool qemu_spin_locked(QemuSpin
*spin
)
125 return atomic_read(&spin
->value
);
128 static inline void qemu_spin_unlock(QemuSpin
*spin
)
130 __sync_lock_release(&spin
->value
);
141 * qemu_lockcnt_init: initialize a QemuLockcnt
142 * @lockcnt: the lockcnt to initialize
144 * Initialize lockcnt's counter to zero and prepare its mutex
147 void qemu_lockcnt_init(QemuLockCnt
*lockcnt
);
150 * qemu_lockcnt_destroy: destroy a QemuLockcnt
151 * @lockcnt: the lockcnt to destruct
153 * Destroy lockcnt's mutex.
155 void qemu_lockcnt_destroy(QemuLockCnt
*lockcnt
);
158 * qemu_lockcnt_inc: increment a QemuLockCnt's counter
159 * @lockcnt: the lockcnt to operate on
161 * If the lockcnt's count is zero, wait for critical sections
162 * to finish and increment lockcnt's count to 1. If the count
163 * is not zero, just increment it.
165 * Because this function can wait on the mutex, it must not be
166 * called while the lockcnt's mutex is held by the current thread.
167 * For the same reason, qemu_lockcnt_inc can also contribute to
168 * AB-BA deadlocks. This is a sample deadlock scenario:
171 * -------------------------------------------------------
172 * qemu_lockcnt_lock(&lc1);
173 * qemu_lockcnt_lock(&lc2);
174 * qemu_lockcnt_inc(&lc2);
175 * qemu_lockcnt_inc(&lc1);
177 void qemu_lockcnt_inc(QemuLockCnt
*lockcnt
);
180 * qemu_lockcnt_dec: decrement a QemuLockCnt's counter
181 * @lockcnt: the lockcnt to operate on
183 void qemu_lockcnt_dec(QemuLockCnt
*lockcnt
);
186 * qemu_lockcnt_dec_and_lock: decrement a QemuLockCnt's counter and
188 * @lockcnt: the lockcnt to operate on
190 * Decrement lockcnt's count. If the new count is zero, lock
191 * the mutex and return true. Otherwise, return false.
193 bool qemu_lockcnt_dec_and_lock(QemuLockCnt
*lockcnt
);
196 * qemu_lockcnt_dec_if_lock: possibly decrement a QemuLockCnt's counter and
198 * @lockcnt: the lockcnt to operate on
200 * If the count is 1, decrement the count to zero, lock
201 * the mutex and return true. Otherwise, return false.
203 bool qemu_lockcnt_dec_if_lock(QemuLockCnt
*lockcnt
);
206 * qemu_lockcnt_lock: lock a QemuLockCnt's mutex.
207 * @lockcnt: the lockcnt to operate on
209 * Remember that concurrent visits are not blocked unless the count is
210 * also zero. You can use qemu_lockcnt_count to check for this inside a
213 void qemu_lockcnt_lock(QemuLockCnt
*lockcnt
);
216 * qemu_lockcnt_unlock: release a QemuLockCnt's mutex.
217 * @lockcnt: the lockcnt to operate on.
219 void qemu_lockcnt_unlock(QemuLockCnt
*lockcnt
);
222 * qemu_lockcnt_inc_and_unlock: combined unlock/increment on a QemuLockCnt.
223 * @lockcnt: the lockcnt to operate on.
225 * This is the same as
227 * qemu_lockcnt_unlock(lockcnt);
228 * qemu_lockcnt_inc(lockcnt);
230 * but more efficient.
232 void qemu_lockcnt_inc_and_unlock(QemuLockCnt
*lockcnt
);
235 * qemu_lockcnt_count: query a LockCnt's count.
236 * @lockcnt: the lockcnt to query.
238 * Note that the count can change at any time. Still, while the
239 * lockcnt is locked, one can usefully check whether the count
242 unsigned qemu_lockcnt_count(QemuLockCnt
*lockcnt
);