FreeBSD lock 内核源码解析

前言

此文主要介绍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上要求重新排队; 
         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值