https://blog.csdn.net/Auris/article/details/107404962
一. 在Linux内核驱动中使用信号量(semaphore)常规操作步骤:
[0]. 定义信号量结构体变量;
struct semaphore sem;
[1]. 初始化信号量变量
- void sema_init(struct semaphore *sem, int n);
- eg. sema_init(&sem, 1);
[2]. 获取信号量:
- void down(struct semaphore *sem); // 获取信号量, 资源不足则睡眠等待
- int down_trylock(struct semaphore* sem); // 试图获取信号量, 如果没有则直接返回 不睡眠
- int down_interruptible(struct semaphore* sem); // 获取信号量, 可以被信号打断;
[3]. 释放信号量:
void up(struct semaphore* sem); // 释放信号量, 唤醒信号等待队列中的第一个等待进程
二. 重要的数据结构体:
- struct semaphore {
- raw_spinlock_t lock;
- unsigned int count;
- struct list_head wait_list;
- };
- struct semaphore_waiter {
- struct task_struct *task;
- bool up;
- struct list_head list;
- };
三. down操作源码分析, 以down函数为例:
- void down(struct semaphore *sem)
- {
- unsigned long flags;
- raw_spin_lock_irqsave(&sem->lock, flags);
- if (likely(sem->count > 0))
- sem->count--;
- else
- __down(sem);
- raw_spin_unlock_irqrestore(&sem->lock, flags);
- }
- static noinline void __sched __down(struct semaphore *sem)
- {
- __down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
- }
- static inline int __sched __down_common(struct semaphore *sem, long state, long timeout)
- {
- struct task_struct *task = current;
- struct semaphore_waiter waiter;
- list_add_tail(&waiter.list, &sem->wait_list);
- waiter.task = task;
- waiter.up = false;
- for (;;) {
- if (signal_pending_state(state, task))
- goto interrupted;
- if (unlikely(timeout <= 0))
- goto timed_out;
- __set_task_state(task, state);
- raw_spin_unlock_irq(&sem->lock);
- timeout = schedule_timeout(timeout);
- raw_spin_lock_irq(&sem->lock);
- if (waiter.up)
- return 0;
- }
- timed_out:
- list_del(&waiter.list);
- return -ETIME;
- interrupted:
- list_del(&waiter.list);
- return -EINTR;
- }
四. up操作源码分析, 以up函数为例:
- void up(struct semaphore *sem)
- {
- unsigned long flags;
- raw_spin_lock_irqsave(&sem->lock, flags);
- if (likely(list_empty(&sem->wait_list)))
- sem->count++;
- else
- __up(sem);
- raw_spin_unlock_irqrestore(&sem->lock, flags);
- }
- static noinline void __sched __up(struct semaphore *sem)
- {
- struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
- struct semaphore_waiter, list);
- list_del(&waiter->list);
- waiter->up = true;
- wake_up_process(waiter->task);
- }
五. 流程分析与注意点:
[0]. sem->count的数值>=0, 如果count>0, 说明当前信号量没有被占用, 可以获取;
如果count<=0(==0), 需要根据sem->list来添加等待任务或者唤醒任务;