1 /* Public domain. */ 2 3 #ifndef _LINUX_REFCOUNT_H 4 #define _LINUX_REFCOUNT_H 5 6 #include <sys/types.h> 7 #include <linux/atomic.h> 8 9 typedef atomic_t refcount_t; 10 11 static inline bool 12 refcount_dec_and_test(uint32_t *p) 13 { 14 return atomic_dec_and_test(p); 15 } 16 17 static inline bool 18 refcount_inc_not_zero(uint32_t *p) 19 { 20 return atomic_inc_not_zero(p); 21 } 22 23 static inline void 24 refcount_set(uint32_t *p, int v) 25 { 26 atomic_set(p, v); 27 } 28 29 static inline bool 30 refcount_dec_and_lock_irqsave(volatile int *v, struct mutex *lock, 31 unsigned long *flags) 32 { 33 if (atomic_add_unless(v, -1, 1)) 34 return false; 35 36 mtx_enter(lock); 37 if (atomic_dec_return(v) == 0) 38 return true; 39 mtx_leave(lock); 40 return false; 41 } 42 43 #endif 44