• 自旋锁spin_lock、spin_lock_irq 和 spin_lock_irqsave


    自旋锁和互斥锁的区别是,自旋锁不会引起睡眠,所以可用于不能休眠的代码中(如IRQ)
    自旋锁保持期间抢占失效,而信号量保持期间可以被抢占

    定义

    spinlock_t lock;

    init

    #define spin_lock_init(_lock)               
    do {                            
        spinlock_check(_lock);              
        raw_spin_lock_init(&(_lock)->rlock);        
    } while (0)

    获得

    static inline void spin_lock(spinlock_t *lock)
    static inline int spin_trylock(spinlock_t *lock)

    spin_lock:获得锁以后,立即返回,否则,它将自旋在那里,直到该自旋锁的保持者释放
    spin_trylock:获得锁失败,立即返回假

    释放

    static inline void spin_unlock(spinlock_t *lock)

    举例

    spinlock_t lock;
    spin_lock_init(&lock)
    ...
    critical section
    ...
    spin_unlock(&lock)

    其他
    尽管自旋锁可以保证临界区不受别的CPU和本CPU抢占进程打扰,但是得到锁的代码在执行临界区的时候,还可能受到中断和底半部的影响

    static inline void spin_lock_irq(spinlock_t *lock)
    #define spin_lock_irqsave(lock, flags)  
    static inline void spin_lock_bh(spinlock_t *lock)
    static inline void spin_unlock_irq(spinlock_t *lock)
    static inline void spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags)
    static inline void spin_unlock_bh(spinlock_t *lock)

    spin_lock_irq() = spin_lock() + local_irq_disable()
    spin_lock_irqsave() = spin_lock() + local_irq_save()
    spin_lock_bh() = spin_lock() + local_bh_disable()
    spin_unlock_irq() = spin_unlock() + local_irq_enable()
    spin_unlock_irqrestore() = spin_unlock() + local_irq_restore()
    spin_unlock_bh = spin_unlock() + local_bh_enable()

    local_irq_disable:关中断
    local_irq_save:关中断并保存状态字
    local_bh_disable:关底半部
    local_irq_enable:开中断
    local_irq_restore:开中断并恢复状态字
    local_bh_enable:开底半部

    在任何情况下使用spin_lock_irq都是安全的。因为它既禁止本地中断,又禁止内核抢占
    spin_lock比spin_lock_irq速度快,但是它并不是任何情况下都是安全的

    spin_lock_irqsave

    1. 保存本地中断状态
    2. 关闭本地中断
    3. 获取自旋锁

    spin_unlock_irqrestore

    1. 释放锁
    2. 恢复本地中断

    :读写自旋锁待补充

  • 相关阅读:
    python全栈开发从入门到放弃之内置函数
    python全栈开发从入门到放弃之递归函数的调用
    python全栈开发从入门到放弃之字典的应用
    python全栈开发从入门到放弃之元组的内置应用
    python全栈开发从入门到放弃之装饰器函数
    [LeetCode-JAVA] Remove Duplicates from Sorted Array II
    [LeetCode-JAVA] Simplify Path
    [LeetCode-JAVA] Permutations
    tensorboard在windows系统浏览器显示空白的解决writer =tf.summary.FileWriter("logs/", sess.graph)
    Windows64位安装CPU版TensorFlow
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11709823.html
Copyright © 2020-2023  润新知