38a3d0d5331347bd310f269659206b575899bd5f
[sliver-openvswitch.git] / datapath / linux / compat / include / linux / mutex.h
1 #ifndef __LINUX_MUTEX_WRAPPER_H
2 #define __LINUX_MUTEX_WRAPPER_H
3
4
5 #include <linux/version.h>
6 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
7
8 #include <linux/semaphore.h>
9
10 struct mutex {
11         struct semaphore sema;
12 };
13
14 #define mutex_init(mutex) init_MUTEX(&(mutex)->sema)
15 #define mutex_destroy(mutex) do { } while (0)
16
17 #define __MUTEX_INITIALIZER(name) \
18                         __SEMAPHORE_INITIALIZER(name, 1)
19
20 #define DEFINE_MUTEX(mutexname) \
21         struct mutex mutexname = { __MUTEX_INITIALIZER(mutexname.sema) }
22
23 /*
24  * See kernel/mutex.c for detailed documentation of these APIs.
25  * Also see Documentation/mutex-design.txt.
26  */
27 static inline void mutex_lock(struct mutex *lock)
28 {
29         down(&lock->sema);
30 }
31
32 static inline int mutex_lock_interruptible(struct mutex *lock)
33 {
34         return down_interruptible(&lock->sema);
35 }
36
37 #define mutex_lock_nested(lock, subclass) mutex_lock(lock)
38 #define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
39
40 /*
41  * NOTE: mutex_trylock() follows the spin_trylock() convention,
42  *       not the down_trylock() convention!
43  */
44 static inline int mutex_trylock(struct mutex *lock)
45 {
46         return !down_trylock(&lock->sema);
47 }
48
49 static inline void mutex_unlock(struct mutex *lock)
50 {
51         up(&lock->sema);
52 }
53 #else
54
55 #include_next <linux/mutex.h>
56
57 #endif /* linux version < 2.6.16 */
58
59 #endif