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 void qemu_mutex_lock(QemuMutex
*mutex
);
26 int qemu_mutex_trylock(QemuMutex
*mutex
);
27 void qemu_mutex_unlock(QemuMutex
*mutex
);
29 /* Prototypes for other functions are in thread-posix.h/thread-win32.h. */
30 void qemu_rec_mutex_init(QemuRecMutex
*mutex
);
32 void qemu_cond_init(QemuCond
*cond
);
33 void qemu_cond_destroy(QemuCond
*cond
);
36 * IMPORTANT: The implementation does not guarantee that pthread_cond_signal
37 * and pthread_cond_broadcast can be called except while the same mutex is
38 * held as in the corresponding pthread_cond_wait calls!
40 void qemu_cond_signal(QemuCond
*cond
);
41 void qemu_cond_broadcast(QemuCond
*cond
);
42 void qemu_cond_wait(QemuCond
*cond
, QemuMutex
*mutex
);
44 void qemu_sem_init(QemuSemaphore
*sem
, int init
);
45 void qemu_sem_post(QemuSemaphore
*sem
);
46 void qemu_sem_wait(QemuSemaphore
*sem
);
47 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
);
48 void qemu_sem_destroy(QemuSemaphore
*sem
);
50 void qemu_event_init(QemuEvent
*ev
, bool init
);
51 void qemu_event_set(QemuEvent
*ev
);
52 void qemu_event_reset(QemuEvent
*ev
);
53 void qemu_event_wait(QemuEvent
*ev
);
54 void qemu_event_destroy(QemuEvent
*ev
);
56 void qemu_thread_create(QemuThread
*thread
, const char *name
,
57 void *(*start_routine
)(void *),
59 void *qemu_thread_join(QemuThread
*thread
);
60 void qemu_thread_get_self(QemuThread
*thread
);
61 bool qemu_thread_is_self(QemuThread
*thread
);
62 void qemu_thread_exit(void *retval
);
63 void qemu_thread_naming(bool enable
);
66 void qemu_thread_atexit_add(struct Notifier
*notifier
);
67 void qemu_thread_atexit_remove(struct Notifier
*notifier
);
69 typedef struct QemuSpin
{
73 static inline void qemu_spin_init(QemuSpin
*spin
)
75 __sync_lock_release(&spin
->value
);
78 static inline void qemu_spin_lock(QemuSpin
*spin
)
80 while (unlikely(__sync_lock_test_and_set(&spin
->value
, true))) {
81 while (atomic_read(&spin
->value
)) {
87 static inline bool qemu_spin_trylock(QemuSpin
*spin
)
89 return __sync_lock_test_and_set(&spin
->value
, true);
92 static inline bool qemu_spin_locked(QemuSpin
*spin
)
94 return atomic_read(&spin
->value
);
97 static inline void qemu_spin_unlock(QemuSpin
*spin
)
99 __sync_lock_release(&spin
->value
);
110 * qemu_lockcnt_init: initialize a QemuLockcnt
111 * @lockcnt: the lockcnt to initialize
113 * Initialize lockcnt's counter to zero and prepare its mutex
116 void qemu_lockcnt_init(QemuLockCnt
*lockcnt
);
119 * qemu_lockcnt_destroy: destroy a QemuLockcnt
120 * @lockcnt: the lockcnt to destruct
122 * Destroy lockcnt's mutex.
124 void qemu_lockcnt_destroy(QemuLockCnt
*lockcnt
);
127 * qemu_lockcnt_inc: increment a QemuLockCnt's counter
128 * @lockcnt: the lockcnt to operate on
130 * If the lockcnt's count is zero, wait for critical sections
131 * to finish and increment lockcnt's count to 1. If the count
132 * is not zero, just increment it.
134 * Because this function can wait on the mutex, it must not be
135 * called while the lockcnt's mutex is held by the current thread.
136 * For the same reason, qemu_lockcnt_inc can also contribute to
137 * AB-BA deadlocks. This is a sample deadlock scenario:
140 * -------------------------------------------------------
141 * qemu_lockcnt_lock(&lc1);
142 * qemu_lockcnt_lock(&lc2);
143 * qemu_lockcnt_inc(&lc2);
144 * qemu_lockcnt_inc(&lc1);
146 void qemu_lockcnt_inc(QemuLockCnt
*lockcnt
);
149 * qemu_lockcnt_dec: decrement a QemuLockCnt's counter
150 * @lockcnt: the lockcnt to operate on
152 void qemu_lockcnt_dec(QemuLockCnt
*lockcnt
);
155 * qemu_lockcnt_dec_and_lock: decrement a QemuLockCnt's counter and
157 * @lockcnt: the lockcnt to operate on
159 * Decrement lockcnt's count. If the new count is zero, lock
160 * the mutex and return true. Otherwise, return false.
162 bool qemu_lockcnt_dec_and_lock(QemuLockCnt
*lockcnt
);
165 * qemu_lockcnt_dec_if_lock: possibly decrement a QemuLockCnt's counter and
167 * @lockcnt: the lockcnt to operate on
169 * If the count is 1, decrement the count to zero, lock
170 * the mutex and return true. Otherwise, return false.
172 bool qemu_lockcnt_dec_if_lock(QemuLockCnt
*lockcnt
);
175 * qemu_lockcnt_lock: lock a QemuLockCnt's mutex.
176 * @lockcnt: the lockcnt to operate on
178 * Remember that concurrent visits are not blocked unless the count is
179 * also zero. You can use qemu_lockcnt_count to check for this inside a
182 void qemu_lockcnt_lock(QemuLockCnt
*lockcnt
);
185 * qemu_lockcnt_unlock: release a QemuLockCnt's mutex.
186 * @lockcnt: the lockcnt to operate on.
188 void qemu_lockcnt_unlock(QemuLockCnt
*lockcnt
);
191 * qemu_lockcnt_inc_and_unlock: combined unlock/increment on a QemuLockCnt.
192 * @lockcnt: the lockcnt to operate on.
194 * This is the same as
196 * qemu_lockcnt_unlock(lockcnt);
197 * qemu_lockcnt_inc(lockcnt);
199 * but more efficient.
201 void qemu_lockcnt_inc_and_unlock(QemuLockCnt
*lockcnt
);
204 * qemu_lockcnt_count: query a LockCnt's count.
205 * @lockcnt: the lockcnt to query.
207 * Note that the count can change at any time. Still, while the
208 * lockcnt is locked, one can usefully check whether the count
211 unsigned qemu_lockcnt_count(QemuLockCnt
*lockcnt
);