📄 rwsem.h
字号:
#ifndef _LINUX_RWSEM_H#define _LINUX_RWSEM_H#ifdef __KERNEL__#include <linux/compiler.h>#include <linux/kernel.h>struct rw_semaphore{ spinlock_t lock; long count;#define RWSEM_READ_BIAS 1#define RWSEM_WRITE_BIAS (~(~0UL >> (BITS_PER_LONG>>1))) struct list_head wait;#if RWSEM_DEBUG long __magic;#endif};struct rw_sem_recursor{ int counter;};#if RWSEM_DEBUG#define __SEM_DEBUG_INIT(name) \ , (long)&(name).__magic#define RWSEM_MAGIC(x) \ do { \ if ((x) != (long)&(x)) { \ printk("rwsem bad magic %lx (should be %lx), ", \ (long)x, (long)&(x)); \ BUG(); \ } \ } while (0)#else#define __SEM_DEBUG_INIT(name)#define CHECK_MAGIC(x)#endif#define __RWSEM_INITIALIZER(name, count) \{ \ SPIN_LOCK_UNLOCKED, \ (count), \ LIST_HEAD_INIT((name).wait) \ __SEM_DEBUG_INIT(name) \}#define RWSEM_INITIALIZER(name) __RWSEM_INITIALIZER(name, 0)#define RWSEM_RECURSOR_INITIALIZER ((struct rw_sem_recursor) { 0, })#define __DECLARE_RWSEM(name, count) \ struct rw_semaphore name = __RWSEM_INITIALIZER(name, count)#define DECLARE_RWSEM(name) __DECLARE_RWSEM(name, 0)#define DECLARE_RWSEM_READ_LOCKED(name) __DECLARE_RWSEM(name, RWSEM_READ_BIAS)#define DECLARE_RWSEM_WRITE_LOCKED(name) __DECLARE_RWSEM(name, RWSEM_WRITE_BIAS)#define RWSEM_READ_BLOCKING_BIAS (RWSEM_WRITE_BIAS-RWSEM_READ_BIAS)#define RWSEM_WRITE_BLOCKING_BIAS (0)#define RWSEM_READ_MASK (~RWSEM_WRITE_BIAS)#define RWSEM_WRITE_MASK (RWSEM_WRITE_BIAS)extern void FASTCALL(rwsem_down_failed(struct rw_semaphore *, long));extern void FASTCALL(rwsem_wake(struct rw_semaphore *));static inline void init_rwsem(struct rw_semaphore *sem){ spin_lock_init(&sem->lock); sem->count = 0; INIT_LIST_HEAD(&sem->wait);#if RWSEM_DEBUG sem->__magic = (long)&sem->__magic;#endif}static inline void down_read(struct rw_semaphore *sem){ long count; CHECK_MAGIC(sem->__magic); spin_lock(&sem->lock); count = sem->count; sem->count += RWSEM_READ_BIAS; if (unlikely(count < 0)) rwsem_down_failed(sem, RWSEM_READ_BLOCKING_BIAS); spin_unlock(&sem->lock);}static inline void down_write(struct rw_semaphore *sem){ long count; CHECK_MAGIC(sem->__magic); spin_lock(&sem->lock); count = sem->count; sem->count += RWSEM_WRITE_BIAS; if (unlikely(count)) rwsem_down_failed(sem, RWSEM_WRITE_BLOCKING_BIAS); spin_unlock(&sem->lock);}static inline void up_read(struct rw_semaphore *sem){ CHECK_MAGIC(sem->__magic); spin_lock(&sem->lock); sem->count -= RWSEM_READ_BIAS; if (unlikely(sem->count < 0 && !(sem->count & RWSEM_READ_MASK))) rwsem_wake(sem); spin_unlock(&sem->lock);}static inline void up_write(struct rw_semaphore *sem){ CHECK_MAGIC(sem->__magic); spin_lock(&sem->lock); sem->count -= RWSEM_WRITE_BIAS; if (unlikely(sem->count)) rwsem_wake(sem); spin_unlock(&sem->lock);}static inline void down_read_recursive(struct rw_semaphore *sem, struct rw_sem_recursor * recursor){ long count; int counter; CHECK_MAGIC(sem->__magic); spin_lock(&sem->lock); count = sem->count; sem->count += RWSEM_READ_BIAS; counter = recursor->counter++; if (unlikely(count < 0 && !counter && !(count & RWSEM_READ_MASK))) rwsem_down_failed(sem, RWSEM_READ_BLOCKING_BIAS); spin_unlock(&sem->lock);}static inline void up_read_recursive(struct rw_semaphore *sem, struct rw_sem_recursor * recursor){ CHECK_MAGIC(sem->__magic); spin_lock(&sem->lock); sem->count -= RWSEM_READ_BIAS; recursor->counter--; if (unlikely(sem->count < 0 && !(sem->count & RWSEM_READ_MASK))) rwsem_wake(sem); spin_unlock(&sem->lock);}#endif /* __KERNEL__ */#endif /* _LINUX_RWSEM_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -