zoukankan      html  css  js  c++  java
  • Linux内核深度解析之内核互斥技术——读写信号量

    读写信号量

    读写信号量是对互斥信号量的改进,允许多个读者同时进入临界区,读者和写者互斥,写者和写者互斥,适合在以读为主的情况下使用。

    读写信号量的定义:

    include/linux/rwsem.h
    struct rw_semaphore {
        atomic_long_t count;
        struct list_head wait_list;
        raw_spinlock_t wait_lock;
    #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
        struct optimistic_spin_queue osq; /* spinner MCS lock */
        /*
         * Write owner. Used as a speculative check to see
         * if the owner is running on the cpu.
         */
        struct task_struct *owner;
    #endif
    #ifdef CONFIG_DEBUG_LOCK_ALLOC
        struct lockdep_map    dep_map;
    #endif
    };

    初始化静态读写信号量的方法:

    DECLARE_RWSEM(name)

    在运行时动态初始化读写信号量的方法:

    init_rwsem(sem)

    申请读锁的函数:

    (1)申请读锁,如果写者占有写锁或者正在等待写锁,那么进程深度睡眠

    void down_read(struct rw_semaphore *sem);

    (2)申请读锁,如果写者占有写锁或者正在等待写锁,那么进程中度睡眠

    int down_read_killable(struct rw_semaphore *sem);

    (3)尝试申请读锁,不会等待。如果申请成功,返回1;否则返回0。

    int down_read_trylock(struct rw_semaphore *sem);

    释放读锁的函数:

    void up_read(struct rw_semaphore *sem);

    申请写锁的函数:

    (1)申请写锁,如果写者占有写锁或者读者占有读锁,那么进程深度睡眠

    void down_write(struct rw_semaphore *sem);

    (2)申请写锁,如果写者占有写锁或者读者占有读锁,那么进程中度睡眠

    int down_write_killable(struct rw_semaphore *sem);

    (3)尝试申请写锁,不会等待。如果申请成功,返回1;否则返回0

    int down_write_trylock(struct rw_semaphore *sem);

    占有写锁以后,把写锁降级为读锁的函数:

    void downgrade_write(struct rw_semaphore *sem);

    释放写锁的函数:

    void up_write(struct rw_semaphore *sem);

    from:
    https://www.codeleading.com/article/87593997601/

  • 相关阅读:
    全面整理的C++面试题
    在chrome下安装Proxy SwitchySharp插件
    VC中获取窗体句柄的各种方法
    炙手可热的12款站点模板推荐
    _blank开新窗体不符合标准?
    欢迎大家来我的小站
    白话经典算法系列之中的一个 冒泡排序的三种实现
    CRF++使用小结(转)
    智能点餐系统开发纪实2-----系统总体结构和业务流程
    Windows Mobile 6.0 SDK和中文模拟器下载
  • 原文地址:https://www.cnblogs.com/aspirs/p/15153381.html
Copyright © 2011-2022 走看看