xref: /kvmtool/include/kvm/mutex.h (revision 15542bab78ec91a6ccf03d4fedd165c8867c22da)
1 #ifndef KVM__MUTEX_H
2 #define KVM__MUTEX_H
3 
4 #include <pthread.h>
5 
6 #include "kvm/util.h"
7 
8 /*
9  * Kernel-alike mutex API - to make it easier for kernel developers
10  * to write user-space code! :-)
11  */
12 
13 struct mutex {
14 	pthread_mutex_t mutex;
15 };
16 #define MUTEX_INITIALIZER { .mutex = PTHREAD_MUTEX_INITIALIZER }
17 
18 #define DEFINE_MUTEX(mtx) struct mutex mtx = MUTEX_INITIALIZER
19 
mutex_init(struct mutex * lock)20 static inline void mutex_init(struct mutex *lock)
21 {
22 	if (pthread_mutex_init(&lock->mutex, NULL) != 0)
23 		die("unexpected pthread_mutex_init() failure!");
24 }
25 
mutex_lock(struct mutex * lock)26 static inline void mutex_lock(struct mutex *lock)
27 {
28 	if (pthread_mutex_lock(&lock->mutex) != 0)
29 		die("unexpected pthread_mutex_lock() failure!");
30 
31 }
32 
mutex_unlock(struct mutex * lock)33 static inline void mutex_unlock(struct mutex *lock)
34 {
35 	if (pthread_mutex_unlock(&lock->mutex) != 0)
36 		die("unexpected pthread_mutex_unlock() failure!");
37 }
38 
39 #endif /* KVM__MUTEX_H */
40