1#ifndef QEMU_THREAD_H
2#define QEMU_THREAD_H
3
4#include "qemu/processor.h"
5#include "qemu/atomic.h"
6
7typedef struct QemuMutex QemuMutex;
8typedef struct QemuCond QemuCond;
9typedef struct QemuSemaphore QemuSemaphore;
10typedef struct QemuEvent QemuEvent;
11typedef struct QemuLockCnt QemuLockCnt;
12typedef 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
23void qemu_mutex_init(QemuMutex *mutex);
24void qemu_mutex_destroy(QemuMutex *mutex);
25void qemu_mutex_lock(QemuMutex *mutex);
26int qemu_mutex_trylock(QemuMutex *mutex);
27void qemu_mutex_unlock(QemuMutex *mutex);
28
29
30void qemu_rec_mutex_init(QemuRecMutex *mutex);
31
32void qemu_cond_init(QemuCond *cond);
33void qemu_cond_destroy(QemuCond *cond);
34
35
36
37
38
39
40void qemu_cond_signal(QemuCond *cond);
41void qemu_cond_broadcast(QemuCond *cond);
42void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex);
43
44void qemu_sem_init(QemuSemaphore *sem, int init);
45void qemu_sem_post(QemuSemaphore *sem);
46void qemu_sem_wait(QemuSemaphore *sem);
47int qemu_sem_timedwait(QemuSemaphore *sem, int ms);
48void qemu_sem_destroy(QemuSemaphore *sem);
49
50void qemu_event_init(QemuEvent *ev, bool init);
51void qemu_event_set(QemuEvent *ev);
52void qemu_event_reset(QemuEvent *ev);
53void qemu_event_wait(QemuEvent *ev);
54void qemu_event_destroy(QemuEvent *ev);
55
56void qemu_thread_create(QemuThread *thread, const char *name,
57 void *(*start_routine)(void *),
58 void *arg, int mode);
59void *qemu_thread_join(QemuThread *thread);
60void qemu_thread_get_self(QemuThread *thread);
61bool qemu_thread_is_self(QemuThread *thread);
62void qemu_thread_exit(void *retval);
63void qemu_thread_naming(bool enable);
64
65struct Notifier;
66void qemu_thread_atexit_add(struct Notifier *notifier);
67void qemu_thread_atexit_remove(struct Notifier *notifier);
68
69typedef struct QemuSpin {
70 int value;
71} QemuSpin;
72
73static inline void qemu_spin_init(QemuSpin *spin)
74{
75 __sync_lock_release(&spin->value);
76}
77
78static inline void qemu_spin_lock(QemuSpin *spin)
79{
80 while (unlikely(__sync_lock_test_and_set(&spin->value, true))) {
81 while (atomic_read(&spin->value)) {
82 cpu_relax();
83 }
84 }
85}
86
87static inline bool qemu_spin_trylock(QemuSpin *spin)
88{
89 return __sync_lock_test_and_set(&spin->value, true);
90}
91
92static inline bool qemu_spin_locked(QemuSpin *spin)
93{
94 return atomic_read(&spin->value);
95}
96
97static inline void qemu_spin_unlock(QemuSpin *spin)
98{
99 __sync_lock_release(&spin->value);
100}
101
102struct QemuLockCnt {
103#ifndef CONFIG_LINUX
104 QemuMutex mutex;
105#endif
106 unsigned count;
107};
108
109
110
111
112
113
114
115
116void qemu_lockcnt_init(QemuLockCnt *lockcnt);
117
118
119
120
121
122
123
124void qemu_lockcnt_destroy(QemuLockCnt *lockcnt);
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146void qemu_lockcnt_inc(QemuLockCnt *lockcnt);
147
148
149
150
151
152void qemu_lockcnt_dec(QemuLockCnt *lockcnt);
153
154
155
156
157
158
159
160
161
162bool qemu_lockcnt_dec_and_lock(QemuLockCnt *lockcnt);
163
164
165
166
167
168
169
170
171
172bool qemu_lockcnt_dec_if_lock(QemuLockCnt *lockcnt);
173
174
175
176
177
178
179
180
181
182void qemu_lockcnt_lock(QemuLockCnt *lockcnt);
183
184
185
186
187
188void qemu_lockcnt_unlock(QemuLockCnt *lockcnt);
189
190
191
192
193
194
195
196
197
198
199
200
201void qemu_lockcnt_inc_and_unlock(QemuLockCnt *lockcnt);
202
203
204
205
206
207
208
209
210
211unsigned qemu_lockcnt_count(QemuLockCnt *lockcnt);
212
213#endif
214