Linux内核基于位的自旋锁

自旋锁相对而言比较简单,一个自旋锁是一个互斥设备,它只能有两个值:“锁定”和“解锁”。内核中为了节省空间,很多情况下使用单个位标示某一信号,基于此,内核提供拉基于位的自旋锁

  /* Don't use this unless you really need to: spin_lock() and spin_unlock()
  * are significantly faster.
  */
  //内核明确写出, 除非不得以,否则尽量不要使用该自旋锁
 static inline void bit_spin_lock(int bitnum, unsigned long *addr)
 {
         /*
          * Assuming the lock is uncontended, this never enters
          * the body of the outer loop. If it is contended, then
          * within the inner loop a non-atomic test is used to
          * busywait with less bus contention for a good time to
          * attempt to acquire the lock bit.
          */
         preempt_disable();
 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
         while (unlikely(test_and_set_bit_lock(bitnum, addr))) {
                 preempt_enable();
                 do {
                        cpu_relax();
                 } while (test_bit(bitnum, addr));
                 preempt_disable();
         }
 #endif
         __acquire(bitlock);
 }

对应的解锁函数

 /*
  *  bit-based spin_unlock()
  */
 static inline void bit_spin_unlock(int bitnum, unsigned long *addr)
 {
 #ifdef CONFIG_DEBUG_SPINLOCK
         BUG_ON(!test_bit(bitnum, addr));
 #endif
 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
         clear_bit_unlock(bitnum, addr);
 #endif
         preempt_enable();
         __release(bitlock);
 }
 //如何使用就不用废话了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值