semaphore的释放操作代码如下
static noinline void __sched __up(struct semaphore *sem)
{
//拿到sem等待链表中的第一个节点,对应的semaphore_waiter,线程描述符存放在那个里面
struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
struct semaphore_waiter, list);
//从sem等待链表中摘除
list_del(&waiter->list);
//置up标志,使waier->task的__down_common for(;;){}循环退出,开始继续执行
waiter->up = true;
wake_up_process(waiter->task);
}
其中semaphore,semaphore_waiter,list_head结构如下
struct semaphore {
raw_spinlock_t lock;
unsigned int count;
struct list_head wait_list;
};
struct semaphore_waiter {
struct list_head list;
struct task_struct *task;
bool up;
};
struct list_head {
struct list_head *next, *prev;
};
这里面list_head就是一个双向链表的作用,就像串起珠子的线。这个线可以串任何需要串的东西,比如珠子,铜钱,贝壳……
list_head内部是用指针来串的,而在semaphore和semophore_waiter里面不是指针,这样的设计实现了刚开始那个__up()中反向查找的功能。就是根据waiter的list_head成员得到相应waiter的过程。方法就是,根据waiter中list的地址,以及list在waiter中的偏移,算出waiter的起始地址。我感觉这样的设计很巧妙。
container_of宏的具体实现分析这里很详细——https://blog.youkuaiyun.com/npy_lp/article/details/7010752
本文详细介绍了Linux内核中的Semaphore机制,特别是__sched__up()函数的释放操作。该函数用于唤醒等待在semaphore上的线程,通过将waiter结构体的up标志设置为true并调用wake_up_process()来完成。文章还探讨了list_head结构作为双向链表的作用,以及如何通过container_of宏进行反向查找,这种设计巧妙地实现了线程的唤醒和同步。
448

被折叠的 条评论
为什么被折叠?



