xref: /qemu/include/qemu/thread.h (revision 52ea63de)
1 #ifndef __QEMU_THREAD_H
2 #define __QEMU_THREAD_H 1
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 QemuThread QemuThread;
12 
13 #ifdef _WIN32
14 #include "qemu/thread-win32.h"
15 #else
16 #include "qemu/thread-posix.h"
17 #endif
18 
19 #define QEMU_THREAD_JOINABLE 0
20 #define QEMU_THREAD_DETACHED 1
21 
22 void qemu_mutex_init(QemuMutex *mutex);
23 void qemu_mutex_destroy(QemuMutex *mutex);
24 void qemu_mutex_lock(QemuMutex *mutex);
25 int qemu_mutex_trylock(QemuMutex *mutex);
26 void qemu_mutex_unlock(QemuMutex *mutex);
27 
28 void qemu_cond_init(QemuCond *cond);
29 void qemu_cond_destroy(QemuCond *cond);
30 
31 /*
32  * IMPORTANT: The implementation does not guarantee that pthread_cond_signal
33  * and pthread_cond_broadcast can be called except while the same mutex is
34  * held as in the corresponding pthread_cond_wait calls!
35  */
36 void qemu_cond_signal(QemuCond *cond);
37 void qemu_cond_broadcast(QemuCond *cond);
38 void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex);
39 
40 void qemu_sem_init(QemuSemaphore *sem, int init);
41 void qemu_sem_post(QemuSemaphore *sem);
42 void qemu_sem_wait(QemuSemaphore *sem);
43 int qemu_sem_timedwait(QemuSemaphore *sem, int ms);
44 void qemu_sem_destroy(QemuSemaphore *sem);
45 
46 void qemu_event_init(QemuEvent *ev, bool init);
47 void qemu_event_set(QemuEvent *ev);
48 void qemu_event_reset(QemuEvent *ev);
49 void qemu_event_wait(QemuEvent *ev);
50 void qemu_event_destroy(QemuEvent *ev);
51 
52 void qemu_thread_create(QemuThread *thread, const char *name,
53                         void *(*start_routine)(void *),
54                         void *arg, int mode);
55 void *qemu_thread_join(QemuThread *thread);
56 void qemu_thread_get_self(QemuThread *thread);
57 bool qemu_thread_is_self(QemuThread *thread);
58 void qemu_thread_exit(void *retval);
59 void qemu_thread_naming(bool enable);
60 
61 struct Notifier;
62 void qemu_thread_atexit_add(struct Notifier *notifier);
63 void qemu_thread_atexit_remove(struct Notifier *notifier);
64 
65 typedef struct QemuSpin {
66     int value;
67 } QemuSpin;
68 
69 static inline void qemu_spin_init(QemuSpin *spin)
70 {
71     __sync_lock_release(&spin->value);
72 }
73 
74 static inline void qemu_spin_lock(QemuSpin *spin)
75 {
76     while (unlikely(__sync_lock_test_and_set(&spin->value, true))) {
77         while (atomic_read(&spin->value)) {
78             cpu_relax();
79         }
80     }
81 }
82 
83 static inline bool qemu_spin_trylock(QemuSpin *spin)
84 {
85     return __sync_lock_test_and_set(&spin->value, true);
86 }
87 
88 static inline bool qemu_spin_locked(QemuSpin *spin)
89 {
90     return atomic_read(&spin->value);
91 }
92 
93 static inline void qemu_spin_unlock(QemuSpin *spin)
94 {
95     __sync_lock_release(&spin->value);
96 }
97 
98 #endif
99