注解读写锁

    百图画鸿蒙 + 百文说内核 + 百万注源码  => 挖透鸿蒙内核源码
    鸿蒙研究站 | http://weharmonyos.com (国内)
              | https://weharmony.github.io (国外)
    oschina | https://my.oschina.net/weharmony
    博客园 | https://www.cnblogs.com/weharmony/
    知乎 | https://www.zhihu.com/people/weharmonyos
    csdn | https://blog.csdn.net/kuangyufei
    51cto | https://harmonyos.51cto.com/column/34
    掘金 | https://juejin.cn/user/756888642000808
    公众号 | 鸿蒙研究站 (weharmonyos)
上级 d4dd7ee4
/*!
* @file los_rwlock.c
* @brief
* @link rwlock https://weharmony.github.io/openharmony/zh-cn/device-dev/kernel/kernel-small-basic-trans-rwlock.html @endlink
@verbatim
基本概念
读写锁与互斥锁类似,可用来同步同一进程中的各个任务,但与互斥锁不同的是,其允许多个读操作并发重入,而写操作互斥。
相对于互斥锁的开锁或闭锁状态,读写锁有三种状态:读模式下的锁,写模式下的锁,无锁。
读写锁的使用规则:
保护区无写模式下的锁,任何任务均可以为其增加读模式下的锁。
保护区处于无锁状态下,才可增加写模式下的锁。
多任务环境下往往存在多个任务访问同一共享资源的应用场景,读模式下的锁以共享状态对保护区访问,
而写模式下的锁可被用于对共享资源的保护从而实现独占式访问。
这种共享-独占的方式非常适合多任务中读数据频率远大于写数据频率的应用中,提高应用多任务并发度。
运行机制
相较于互斥锁,读写锁如何实现读模式下的锁及写模式下的锁来控制多任务的读写访问呢?
若A任务首次获取了写模式下的锁,有其他任务来获取或尝试获取读模式下的锁,均无法再上锁。
若A任务获取了读模式下的锁,当有任务来获取或尝试获取读模式下的锁时,读写锁计数均加一。
@endverbatim
@image html
* @attention
* @version
* @author weharmonyos.com | 鸿蒙研究站 | 每天死磕一点点
* @date 2022-02-18
*/
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
......
......@@ -49,13 +49,14 @@ extern "C" {
* @ingroup los_rwlock
* Rwlock object.
*/
typedef struct OsRwlock {
INT32 magic:24; /**< Magic number */
typedef struct OsRwlock {//读写锁
INT32 magic:24; /**< Magic number | 魔法数字 为什么要用魔法数字 ? */
INT32 rwCount:8; /**< Times of locking the rwlock, rwCount > 0 when rwkick is read mode, rwCount < 0
when the rwlock is write mode, rwCount = 0 when the lock is free. */
VOID *writeOwner; /**< The current write thread that is locking the rwlock */
LOS_DL_LIST readList; /**< Read waiting list */
LOS_DL_LIST writeList; /**< Write waiting list */
when the rwlock is write mode, rwCount = 0 when the lock is free.
大于0时为读模式 , 小于0时为写模式 等于0为自由模式*/
VOID *writeOwner; /**< The current write thread that is locking the rwlock | 拥有写权限的任务*/
LOS_DL_LIST readList; /**< Read waiting list | 等待读锁的任务链表*/
LOS_DL_LIST writeList; /**< Write waiting list | 等待写*/
} LosRwlock;
extern BOOL LOS_RwlockIsValid(const LosRwlock *rwlock);
......
git add -A
git commit -m ' 同步官方代码,除了几个测试用例并无更新
git commit -m ' 注解读写锁
百图画鸿蒙 + 百文说内核 + 百万注源码 => 挖透鸿蒙内核源码
鸿蒙研究站 | http://weharmonyos.com (国内)
| https://weharmony.github.io (国外)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册