前言
此文主要介绍FreeBSD lock的内核实现,只是几年前的随笔记录,希望能提供帮助。lock的实现和进程调度相关,有兴趣的需要联合进程调度一起分析,当把这些串联起来就发现操作系统就是个哲学系统,不是什么不可逾越的天堑,国内没有成熟的操作系统只是这方面没有从0-N的积累,没有培养国产操作系统的土壤。
正文
propagate_priority 是在turnstile_wait 中被调用(能调用到propagate_priority的地方基本上是拿着锁的线程也是被blocked),其内部会查这个锁(blocking lock: mutex,rw,rm)被谁占用,一旦查出是由sleep lock(sx,lockmgr,sleep())占用,则直接panic 说一个睡的线程占用一个不可睡的锁(这里的锁就是上面的block锁)
propagate_priority(td) 函数实现 :
ts = td->td_blocked; 当前线程锁在的turnstile
pri = td->td_priority;
for (;;){
td = ts->ts_owner; 当前拿着这个turnstile的thread
/*
* If this thread already has higher priority than the
* thread that is being blocked, we are finished.
*/
if (td->td_priority <= pri) {
thread_unlock(td);
return;
}
/*
* Bump this thread's priority.
*/
sched_lend_prio(td, pri); 升级拿着此lock的线程优先级
/*
* If lock holder is actually running or on the run queue
* then we are done.
*/
if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) { ------------>线程正在运行, 或是在可运行队列 则退出函数;
MPASS(td->td_blocked == NULL);
thread_unlock(td);
return;
}
/*
* Pick up the lock that td is blocked on.
*/
ts = td->td_blocked; 当前拿着这个turnstile的thread 锁在的turnstile (这里相当于链表的next,这样一直把调用propagate_priority的线程的优先级尽可能沿着thread的 td_blocked(td_blocked指向的是turnstile)向上传递;如果调用该函数的thread优先级很高则会提升所有挡在它前面的线程优先级)
/* Resort td on the list if needed. */
if (!turnstile_adjust_thread(ts, td)) { -------->这个线程的优先级已经被上面的函数sched_lend_prio 提升过优先级了,而且这个线程也是被block, 所以自然需要在blocked的turnstile上要求重新排队;