qsp: QEMU's Synchronization Profiler
[qemu/ar7.git] / include / qemu / thread.h
blobc90ea4783f71d16256e0e596871e1516f6d050fc
1 #ifndef QEMU_THREAD_H
2 #define QEMU_THREAD_H
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;
13 #ifdef _WIN32
14 #include "qemu/thread-win32.h"
15 #else
16 #include "qemu/thread-posix.h"
17 #endif
19 /* include QSP header once QemuMutex, QemuCond etc. are defined */
20 #include "qemu/qsp.h"
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,
36 int l);
38 extern QemuMutexLockFunc qemu_mutex_lock_func;
39 extern QemuMutexTrylockFunc qemu_mutex_trylock_func;
40 extern QemuRecMutexLockFunc qemu_rec_mutex_lock_func;
41 extern QemuRecMutexTrylockFunc qemu_rec_mutex_trylock_func;
42 extern QemuCondWaitFunc qemu_cond_wait_func;
44 /* convenience macros to bypass the profiler */
45 #define qemu_mutex_lock__raw(m) \
46 qemu_mutex_lock_impl(m, __FILE__, __LINE__)
47 #define qemu_mutex_trylock__raw(m) \
48 qemu_mutex_trylock_impl(m, __FILE__, __LINE__)
50 #define qemu_mutex_lock(m) ({ \
51 QemuMutexLockFunc _f = atomic_read(&qemu_mutex_lock_func); \
52 _f(m, __FILE__, __LINE__); \
55 #define qemu_mutex_trylock(m) ({ \
56 QemuMutexTrylockFunc _f = atomic_read(&qemu_mutex_trylock_func); \
57 _f(m, __FILE__, __LINE__); \
60 #define qemu_rec_mutex_lock(m) ({ \
61 QemuRecMutexLockFunc _f = atomic_read(&qemu_rec_mutex_lock_func); \
62 _f(m, __FILE__, __LINE__); \
65 #define qemu_rec_mutex_trylock(m) ({ \
66 QemuRecMutexTrylockFunc _f; \
67 _f = atomic_read(&qemu_rec_mutex_trylock_func); \
68 _f(m, __FILE__, __LINE__); \
71 #define qemu_cond_wait(c, m) ({ \
72 QemuCondWaitFunc _f = atomic_read(&qemu_cond_wait_func); \
73 _f(c, m, __FILE__, __LINE__); \
76 #define qemu_mutex_unlock(mutex) \
77 qemu_mutex_unlock_impl(mutex, __FILE__, __LINE__)
79 static inline void (qemu_mutex_lock)(QemuMutex *mutex)
81 qemu_mutex_lock(mutex);
84 static inline int (qemu_mutex_trylock)(QemuMutex *mutex)
86 return qemu_mutex_trylock(mutex);
89 static inline void (qemu_mutex_unlock)(QemuMutex *mutex)
91 qemu_mutex_unlock(mutex);
94 static inline void (qemu_rec_mutex_lock)(QemuRecMutex *mutex)
96 qemu_rec_mutex_lock(mutex);
99 static inline int (qemu_rec_mutex_trylock)(QemuRecMutex *mutex)
101 return qemu_rec_mutex_trylock(mutex);
104 /* Prototypes for other functions are in thread-posix.h/thread-win32.h. */
105 void qemu_rec_mutex_init(QemuRecMutex *mutex);
107 void qemu_cond_init(QemuCond *cond);
108 void qemu_cond_destroy(QemuCond *cond);
111 * IMPORTANT: The implementation does not guarantee that pthread_cond_signal
112 * and pthread_cond_broadcast can be called except while the same mutex is
113 * held as in the corresponding pthread_cond_wait calls!
115 void qemu_cond_signal(QemuCond *cond);
116 void qemu_cond_broadcast(QemuCond *cond);
117 void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex,
118 const char *file, const int line);
120 static inline void (qemu_cond_wait)(QemuCond *cond, QemuMutex *mutex)
122 qemu_cond_wait(cond, mutex);
125 void qemu_sem_init(QemuSemaphore *sem, int init);
126 void qemu_sem_post(QemuSemaphore *sem);
127 void qemu_sem_wait(QemuSemaphore *sem);
128 int qemu_sem_timedwait(QemuSemaphore *sem, int ms);
129 void qemu_sem_destroy(QemuSemaphore *sem);
131 void qemu_event_init(QemuEvent *ev, bool init);
132 void qemu_event_set(QemuEvent *ev);
133 void qemu_event_reset(QemuEvent *ev);
134 void qemu_event_wait(QemuEvent *ev);
135 void qemu_event_destroy(QemuEvent *ev);
137 void qemu_thread_create(QemuThread *thread, const char *name,
138 void *(*start_routine)(void *),
139 void *arg, int mode);
140 void *qemu_thread_join(QemuThread *thread);
141 void qemu_thread_get_self(QemuThread *thread);
142 bool qemu_thread_is_self(QemuThread *thread);
143 void qemu_thread_exit(void *retval);
144 void qemu_thread_naming(bool enable);
146 struct Notifier;
147 void qemu_thread_atexit_add(struct Notifier *notifier);
148 void qemu_thread_atexit_remove(struct Notifier *notifier);
150 struct QemuSpin {
151 int value;
154 static inline void qemu_spin_init(QemuSpin *spin)
156 __sync_lock_release(&spin->value);
159 static inline void qemu_spin_lock(QemuSpin *spin)
161 while (unlikely(__sync_lock_test_and_set(&spin->value, true))) {
162 while (atomic_read(&spin->value)) {
163 cpu_relax();
168 static inline bool qemu_spin_trylock(QemuSpin *spin)
170 return __sync_lock_test_and_set(&spin->value, true);
173 static inline bool qemu_spin_locked(QemuSpin *spin)
175 return atomic_read(&spin->value);
178 static inline void qemu_spin_unlock(QemuSpin *spin)
180 __sync_lock_release(&spin->value);
183 struct QemuLockCnt {
184 #ifndef CONFIG_LINUX
185 QemuMutex mutex;
186 #endif
187 unsigned count;
191 * qemu_lockcnt_init: initialize a QemuLockcnt
192 * @lockcnt: the lockcnt to initialize
194 * Initialize lockcnt's counter to zero and prepare its mutex
195 * for usage.
197 void qemu_lockcnt_init(QemuLockCnt *lockcnt);
200 * qemu_lockcnt_destroy: destroy a QemuLockcnt
201 * @lockcnt: the lockcnt to destruct
203 * Destroy lockcnt's mutex.
205 void qemu_lockcnt_destroy(QemuLockCnt *lockcnt);
208 * qemu_lockcnt_inc: increment a QemuLockCnt's counter
209 * @lockcnt: the lockcnt to operate on
211 * If the lockcnt's count is zero, wait for critical sections
212 * to finish and increment lockcnt's count to 1. If the count
213 * is not zero, just increment it.
215 * Because this function can wait on the mutex, it must not be
216 * called while the lockcnt's mutex is held by the current thread.
217 * For the same reason, qemu_lockcnt_inc can also contribute to
218 * AB-BA deadlocks. This is a sample deadlock scenario:
220 * thread 1 thread 2
221 * -------------------------------------------------------
222 * qemu_lockcnt_lock(&lc1);
223 * qemu_lockcnt_lock(&lc2);
224 * qemu_lockcnt_inc(&lc2);
225 * qemu_lockcnt_inc(&lc1);
227 void qemu_lockcnt_inc(QemuLockCnt *lockcnt);
230 * qemu_lockcnt_dec: decrement a QemuLockCnt's counter
231 * @lockcnt: the lockcnt to operate on
233 void qemu_lockcnt_dec(QemuLockCnt *lockcnt);
236 * qemu_lockcnt_dec_and_lock: decrement a QemuLockCnt's counter and
237 * possibly lock it.
238 * @lockcnt: the lockcnt to operate on
240 * Decrement lockcnt's count. If the new count is zero, lock
241 * the mutex and return true. Otherwise, return false.
243 bool qemu_lockcnt_dec_and_lock(QemuLockCnt *lockcnt);
246 * qemu_lockcnt_dec_if_lock: possibly decrement a QemuLockCnt's counter and
247 * lock it.
248 * @lockcnt: the lockcnt to operate on
250 * If the count is 1, decrement the count to zero, lock
251 * the mutex and return true. Otherwise, return false.
253 bool qemu_lockcnt_dec_if_lock(QemuLockCnt *lockcnt);
256 * qemu_lockcnt_lock: lock a QemuLockCnt's mutex.
257 * @lockcnt: the lockcnt to operate on
259 * Remember that concurrent visits are not blocked unless the count is
260 * also zero. You can use qemu_lockcnt_count to check for this inside a
261 * critical section.
263 void qemu_lockcnt_lock(QemuLockCnt *lockcnt);
266 * qemu_lockcnt_unlock: release a QemuLockCnt's mutex.
267 * @lockcnt: the lockcnt to operate on.
269 void qemu_lockcnt_unlock(QemuLockCnt *lockcnt);
272 * qemu_lockcnt_inc_and_unlock: combined unlock/increment on a QemuLockCnt.
273 * @lockcnt: the lockcnt to operate on.
275 * This is the same as
277 * qemu_lockcnt_unlock(lockcnt);
278 * qemu_lockcnt_inc(lockcnt);
280 * but more efficient.
282 void qemu_lockcnt_inc_and_unlock(QemuLockCnt *lockcnt);
285 * qemu_lockcnt_count: query a LockCnt's count.
286 * @lockcnt: the lockcnt to query.
288 * Note that the count can change at any time. Still, while the
289 * lockcnt is locked, one can usefully check whether the count
290 * is non-zero.
292 unsigned qemu_lockcnt_count(QemuLockCnt *lockcnt);
294 #endif