semaphore.h (include\linux) 1392 2013-2-28
/* Please don't access any members of this structure directly */
struct semaphore {
raw_spinlock_t lock;
unsigned int count;
struct list_head wait_list;
};
其中,lock是个自旋锁变量,用于实现对信号量的另一个成员count的原子操作
无符号整形变量count用于表示通过该信号量允许进入临界区的执行路径的个数
wait_list用于管理所有在该信号量上睡眠的进程,无法获得该信号量的进程将进入睡眠状态
如果驱动程序中定义了一个struct semaphore型的信号变量,需要注意的是不要直接对该变量的成员进行赋值,而应该使用sema_init函数来初始化该信号量,sema_init函数
的定义如下:
static inline void sema_init(struct semaphore *sem, int val)
{
static struct lock_class_key __key;
*sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0);
}
初始化主要通过宏__SEMAPHORE_INITIALIZER完成:
#define __SEMAPHORE_INITIALIZER(name, n) \
{ \
.lock = __RAW_SPIN_LOCK_UNLOCKED((name).lock), \
.count = n, \
.wait_list = LIST_HEAD_INIT((name).wait_list), \
}
#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
(raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
#define __RAW_SPIN_LOCK_INITIALIZER(lockname)
{
.raw_lock = __ARCH_SPIN_LOCK_UNLOCKED
}
spinlock_types.h (arch\arm\include\asm)
#define __ARCH_SPIN_LOCK_UNLOCKED { 0 }
所以sema_init(struct semaphore *sem, int val)调用会把信号量sem的lock值设定为解锁状态,count值设定为函数的调用参数val,同时初始化wait_list链表头
#define DEFINE_SEMAPHORE(name) \
struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)