fpu: Bound increment for scalbn
[qemu.git] / include / qemu / thread.h
blobef7bd16123b7910522d92be32c36799836aa5804
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 #define QEMU_THREAD_JOINABLE 0
20 #define QEMU_THREAD_DETACHED 1
22 void qemu_mutex_init(QemuMutex *mutex);
23 void qemu_mutex_destroy(QemuMutex *mutex);
24 int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line);
25 void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line);
26 void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line);
28 #define qemu_mutex_lock(mutex) \
29 qemu_mutex_lock_impl(mutex, __FILE__, __LINE__)
30 #define qemu_mutex_trylock(mutex) \
31 qemu_mutex_trylock_impl(mutex, __FILE__, __LINE__)
32 #define qemu_mutex_unlock(mutex) \
33 qemu_mutex_unlock_impl(mutex, __FILE__, __LINE__)
35 static inline void (qemu_mutex_lock)(QemuMutex *mutex)
37 qemu_mutex_lock(mutex);
40 static inline int (qemu_mutex_trylock)(QemuMutex *mutex)
42 return qemu_mutex_trylock(mutex);
45 static inline void (qemu_mutex_unlock)(QemuMutex *mutex)
47 qemu_mutex_unlock(mutex);
50 /* Prototypes for other functions are in thread-posix.h/thread-win32.h. */
51 void qemu_rec_mutex_init(QemuRecMutex *mutex);
53 void qemu_cond_init(QemuCond *cond);
54 void qemu_cond_destroy(QemuCond *cond);
57 * IMPORTANT: The implementation does not guarantee that pthread_cond_signal
58 * and pthread_cond_broadcast can be called except while the same mutex is
59 * held as in the corresponding pthread_cond_wait calls!
61 void qemu_cond_signal(QemuCond *cond);
62 void qemu_cond_broadcast(QemuCond *cond);
63 void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex,
64 const char *file, const int line);
66 #define qemu_cond_wait(cond, mutex) \
67 qemu_cond_wait_impl(cond, mutex, __FILE__, __LINE__)
69 static inline void (qemu_cond_wait)(QemuCond *cond, QemuMutex *mutex)
71 qemu_cond_wait(cond, mutex);
74 void qemu_sem_init(QemuSemaphore *sem, int init);
75 void qemu_sem_post(QemuSemaphore *sem);
76 void qemu_sem_wait(QemuSemaphore *sem);
77 int qemu_sem_timedwait(QemuSemaphore *sem, int ms);
78 void qemu_sem_destroy(QemuSemaphore *sem);
80 void qemu_event_init(QemuEvent *ev, bool init);
81 void qemu_event_set(QemuEvent *ev);
82 void qemu_event_reset(QemuEvent *ev);
83 void qemu_event_wait(QemuEvent *ev);
84 void qemu_event_destroy(QemuEvent *ev);
86 void qemu_thread_create(QemuThread *thread, const char *name,
87 void *(*start_routine)(void *),
88 void *arg, int mode);
89 void *qemu_thread_join(QemuThread *thread);
90 void qemu_thread_get_self(QemuThread *thread);
91 bool qemu_thread_is_self(QemuThread *thread);
92 void qemu_thread_exit(void *retval);
93 void qemu_thread_naming(bool enable);
95 struct Notifier;
96 void qemu_thread_atexit_add(struct Notifier *notifier);
97 void qemu_thread_atexit_remove(struct Notifier *notifier);
99 struct QemuSpin {
100 int value;
103 static inline void qemu_spin_init(QemuSpin *spin)
105 __sync_lock_release(&spin->value);
108 static inline void qemu_spin_lock(QemuSpin *spin)
110 while (unlikely(__sync_lock_test_and_set(&spin->value, true))) {
111 while (atomic_read(&spin->value)) {
112 cpu_relax();
117 static inline bool qemu_spin_trylock(QemuSpin *spin)
119 return __sync_lock_test_and_set(&spin->value, true);
122 static inline bool qemu_spin_locked(QemuSpin *spin)
124 return atomic_read(&spin->value);
127 static inline void qemu_spin_unlock(QemuSpin *spin)
129 __sync_lock_release(&spin->value);
132 struct QemuLockCnt {
133 #ifndef CONFIG_LINUX
134 QemuMutex mutex;
135 #endif
136 unsigned count;
140 * qemu_lockcnt_init: initialize a QemuLockcnt
141 * @lockcnt: the lockcnt to initialize
143 * Initialize lockcnt's counter to zero and prepare its mutex
144 * for usage.
146 void qemu_lockcnt_init(QemuLockCnt *lockcnt);
149 * qemu_lockcnt_destroy: destroy a QemuLockcnt
150 * @lockcnt: the lockcnt to destruct
152 * Destroy lockcnt's mutex.
154 void qemu_lockcnt_destroy(QemuLockCnt *lockcnt);
157 * qemu_lockcnt_inc: increment a QemuLockCnt's counter
158 * @lockcnt: the lockcnt to operate on
160 * If the lockcnt's count is zero, wait for critical sections
161 * to finish and increment lockcnt's count to 1. If the count
162 * is not zero, just increment it.
164 * Because this function can wait on the mutex, it must not be
165 * called while the lockcnt's mutex is held by the current thread.
166 * For the same reason, qemu_lockcnt_inc can also contribute to
167 * AB-BA deadlocks. This is a sample deadlock scenario:
169 * thread 1 thread 2
170 * -------------------------------------------------------
171 * qemu_lockcnt_lock(&lc1);
172 * qemu_lockcnt_lock(&lc2);
173 * qemu_lockcnt_inc(&lc2);
174 * qemu_lockcnt_inc(&lc1);
176 void qemu_lockcnt_inc(QemuLockCnt *lockcnt);
179 * qemu_lockcnt_dec: decrement a QemuLockCnt's counter
180 * @lockcnt: the lockcnt to operate on
182 void qemu_lockcnt_dec(QemuLockCnt *lockcnt);
185 * qemu_lockcnt_dec_and_lock: decrement a QemuLockCnt's counter and
186 * possibly lock it.
187 * @lockcnt: the lockcnt to operate on
189 * Decrement lockcnt's count. If the new count is zero, lock
190 * the mutex and return true. Otherwise, return false.
192 bool qemu_lockcnt_dec_and_lock(QemuLockCnt *lockcnt);
195 * qemu_lockcnt_dec_if_lock: possibly decrement a QemuLockCnt's counter and
196 * lock it.
197 * @lockcnt: the lockcnt to operate on
199 * If the count is 1, decrement the count to zero, lock
200 * the mutex and return true. Otherwise, return false.
202 bool qemu_lockcnt_dec_if_lock(QemuLockCnt *lockcnt);
205 * qemu_lockcnt_lock: lock a QemuLockCnt's mutex.
206 * @lockcnt: the lockcnt to operate on
208 * Remember that concurrent visits are not blocked unless the count is
209 * also zero. You can use qemu_lockcnt_count to check for this inside a
210 * critical section.
212 void qemu_lockcnt_lock(QemuLockCnt *lockcnt);
215 * qemu_lockcnt_unlock: release a QemuLockCnt's mutex.
216 * @lockcnt: the lockcnt to operate on.
218 void qemu_lockcnt_unlock(QemuLockCnt *lockcnt);
221 * qemu_lockcnt_inc_and_unlock: combined unlock/increment on a QemuLockCnt.
222 * @lockcnt: the lockcnt to operate on.
224 * This is the same as
226 * qemu_lockcnt_unlock(lockcnt);
227 * qemu_lockcnt_inc(lockcnt);
229 * but more efficient.
231 void qemu_lockcnt_inc_and_unlock(QemuLockCnt *lockcnt);
234 * qemu_lockcnt_count: query a LockCnt's count.
235 * @lockcnt: the lockcnt to query.
237 * Note that the count can change at any time. Still, while the
238 * lockcnt is locked, one can usefully check whether the count
239 * is non-zero.
241 unsigned qemu_lockcnt_count(QemuLockCnt *lockcnt);
243 #endif