]> Git Repo - qemu.git/blob - include/qemu/thread.h
Merge remote-tracking branch 'remotes/xtensa/tags/20180122-xtensa' into staging
[qemu.git] / include / qemu / thread.h
1 #ifndef QEMU_THREAD_H
2 #define QEMU_THREAD_H
3
4 #include "qemu/processor.h"
5 #include "qemu/atomic.h"
6
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;
13
14 #ifdef _WIN32
15 #include "qemu/thread-win32.h"
16 #else
17 #include "qemu/thread-posix.h"
18 #endif
19
20 #define QEMU_THREAD_JOINABLE 0
21 #define QEMU_THREAD_DETACHED 1
22
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);
28
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__)
35
36 static inline void (qemu_mutex_lock)(QemuMutex *mutex)
37 {
38     qemu_mutex_lock(mutex);
39 }
40
41 static inline int (qemu_mutex_trylock)(QemuMutex *mutex)
42 {
43     return qemu_mutex_trylock(mutex);
44 }
45
46 static inline void (qemu_mutex_unlock)(QemuMutex *mutex)
47 {
48     qemu_mutex_unlock(mutex);
49 }
50
51 /* Prototypes for other functions are in thread-posix.h/thread-win32.h.  */
52 void qemu_rec_mutex_init(QemuRecMutex *mutex);
53
54 void qemu_cond_init(QemuCond *cond);
55 void qemu_cond_destroy(QemuCond *cond);
56
57 /*
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!
61  */
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);
66
67 #define qemu_cond_wait(cond, mutex) \
68         qemu_cond_wait_impl(cond, mutex, __FILE__, __LINE__)
69
70 static inline void (qemu_cond_wait)(QemuCond *cond, QemuMutex *mutex)
71 {
72     qemu_cond_wait(cond, mutex);
73 }
74
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);
80
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);
86
87 void qemu_thread_create(QemuThread *thread, const char *name,
88                         void *(*start_routine)(void *),
89                         void *arg, int mode);
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);
95
96 struct Notifier;
97 void qemu_thread_atexit_add(struct Notifier *notifier);
98 void qemu_thread_atexit_remove(struct Notifier *notifier);
99
100 typedef struct QemuSpin {
101     int value;
102 } QemuSpin;
103
104 static inline void qemu_spin_init(QemuSpin *spin)
105 {
106     __sync_lock_release(&spin->value);
107 }
108
109 static inline void qemu_spin_lock(QemuSpin *spin)
110 {
111     while (unlikely(__sync_lock_test_and_set(&spin->value, true))) {
112         while (atomic_read(&spin->value)) {
113             cpu_relax();
114         }
115     }
116 }
117
118 static inline bool qemu_spin_trylock(QemuSpin *spin)
119 {
120     return __sync_lock_test_and_set(&spin->value, true);
121 }
122
123 static inline bool qemu_spin_locked(QemuSpin *spin)
124 {
125     return atomic_read(&spin->value);
126 }
127
128 static inline void qemu_spin_unlock(QemuSpin *spin)
129 {
130     __sync_lock_release(&spin->value);
131 }
132
133 struct QemuLockCnt {
134 #ifndef CONFIG_LINUX
135     QemuMutex mutex;
136 #endif
137     unsigned count;
138 };
139
140 /**
141  * qemu_lockcnt_init: initialize a QemuLockcnt
142  * @lockcnt: the lockcnt to initialize
143  *
144  * Initialize lockcnt's counter to zero and prepare its mutex
145  * for usage.
146  */
147 void qemu_lockcnt_init(QemuLockCnt *lockcnt);
148
149 /**
150  * qemu_lockcnt_destroy: destroy a QemuLockcnt
151  * @lockcnt: the lockcnt to destruct
152  *
153  * Destroy lockcnt's mutex.
154  */
155 void qemu_lockcnt_destroy(QemuLockCnt *lockcnt);
156
157 /**
158  * qemu_lockcnt_inc: increment a QemuLockCnt's counter
159  * @lockcnt: the lockcnt to operate on
160  *
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.
164  *
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:
169  *
170  *            thread 1                      thread 2
171  *            -------------------------------------------------------
172  *            qemu_lockcnt_lock(&lc1);
173  *                                          qemu_lockcnt_lock(&lc2);
174  *            qemu_lockcnt_inc(&lc2);
175  *                                          qemu_lockcnt_inc(&lc1);
176  */
177 void qemu_lockcnt_inc(QemuLockCnt *lockcnt);
178
179 /**
180  * qemu_lockcnt_dec: decrement a QemuLockCnt's counter
181  * @lockcnt: the lockcnt to operate on
182  */
183 void qemu_lockcnt_dec(QemuLockCnt *lockcnt);
184
185 /**
186  * qemu_lockcnt_dec_and_lock: decrement a QemuLockCnt's counter and
187  * possibly lock it.
188  * @lockcnt: the lockcnt to operate on
189  *
190  * Decrement lockcnt's count.  If the new count is zero, lock
191  * the mutex and return true.  Otherwise, return false.
192  */
193 bool qemu_lockcnt_dec_and_lock(QemuLockCnt *lockcnt);
194
195 /**
196  * qemu_lockcnt_dec_if_lock: possibly decrement a QemuLockCnt's counter and
197  * lock it.
198  * @lockcnt: the lockcnt to operate on
199  *
200  * If the count is 1, decrement the count to zero, lock
201  * the mutex and return true.  Otherwise, return false.
202  */
203 bool qemu_lockcnt_dec_if_lock(QemuLockCnt *lockcnt);
204
205 /**
206  * qemu_lockcnt_lock: lock a QemuLockCnt's mutex.
207  * @lockcnt: the lockcnt to operate on
208  *
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
211  * critical section.
212  */
213 void qemu_lockcnt_lock(QemuLockCnt *lockcnt);
214
215 /**
216  * qemu_lockcnt_unlock: release a QemuLockCnt's mutex.
217  * @lockcnt: the lockcnt to operate on.
218  */
219 void qemu_lockcnt_unlock(QemuLockCnt *lockcnt);
220
221 /**
222  * qemu_lockcnt_inc_and_unlock: combined unlock/increment on a QemuLockCnt.
223  * @lockcnt: the lockcnt to operate on.
224  *
225  * This is the same as
226  *
227  *     qemu_lockcnt_unlock(lockcnt);
228  *     qemu_lockcnt_inc(lockcnt);
229  *
230  * but more efficient.
231  */
232 void qemu_lockcnt_inc_and_unlock(QemuLockCnt *lockcnt);
233
234 /**
235  * qemu_lockcnt_count: query a LockCnt's count.
236  * @lockcnt: the lockcnt to query.
237  *
238  * Note that the count can change at any time.  Still, while the
239  * lockcnt is locked, one can usefully check whether the count
240  * is non-zero.
241  */
242 unsigned qemu_lockcnt_count(QemuLockCnt *lockcnt);
243
244 #endif
This page took 0.038821 seconds and 4 git commands to generate.