1 /* Public domain. */ 2 3 #ifndef _LINUX_MUTEX_H 4 #define _LINUX_MUTEX_H 5 6 #include <sys/stdint.h> 7 #include <sys/rwlock.h> 8 #include <linux/list.h> 9 #include <linux/spinlock_types.h> 10 #include <linux/lockdep.h> 11 12 #define DEFINE_MUTEX(x) struct rwlock x = RWLOCK_INITIALIZER(#x) 13 14 #define mutex_lock_interruptible_nested(rwl, subc) \ 15 mutex_lock_interruptible(rwl) 16 #define mutex_lock(rwl) rw_enter_write(rwl) 17 #define mutex_lock_nest_lock(rwl, sub) rw_enter_write(rwl) 18 #define mutex_lock_nested(rwl, sub) rw_enter_write(rwl) 19 #define mutex_trylock(rwl) (rw_enter(rwl, RW_WRITE | RW_NOSLEEP) == 0) 20 #define mutex_unlock(rwl) rw_exit_write(rwl) 21 #define mutex_is_locked(rwl) (rw_status(rwl) != 0) 22 #define mutex_destroy(rwl) 23 24 static inline int mutex_lock_interruptible(struct rwlock * rwl)25mutex_lock_interruptible(struct rwlock *rwl) 26 { 27 if (rw_enter(rwl, RW_WRITE | RW_INTR) != 0) 28 return -EINTR; 29 return 0; 30 } 31 32 enum mutex_trylock_recursive_result { 33 MUTEX_TRYLOCK_FAILED, 34 MUTEX_TRYLOCK_SUCCESS, 35 MUTEX_TRYLOCK_RECURSIVE 36 }; 37 38 static inline enum mutex_trylock_recursive_result mutex_trylock_recursive(struct rwlock * rwl)39mutex_trylock_recursive(struct rwlock *rwl) 40 { 41 if (rw_status(rwl) == RW_WRITE) 42 return MUTEX_TRYLOCK_RECURSIVE; 43 if (mutex_trylock(rwl)) 44 return MUTEX_TRYLOCK_SUCCESS; 45 return MUTEX_TRYLOCK_FAILED; 46 } 47 48 int atomic_dec_and_mutex_lock(volatile int *, struct rwlock *); 49 50 #endif 51