Skip to content

Conversation

AlbertSanoe
Copy link
Contributor

No description provided.

#[inline]
/// @brief 获取实时的读者数并尝试加1,如果增加值成功则返回增加1后的读者数,否则panic
fn current_reader(&self) -> Result<u32, u32> {
const MAX_READERS: u32 = core::u32::MAX / READER / 2; //右移3位
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

歧义,加括号。
然后,所有用乘除法实现的位运算功能,直接改为位运算即可。

/// @brief 获取读者+UPGRADER的数量, 不能保证能否获得同步值
pub fn reader_count(&self) -> u32 {
let state = self.lock.load(Ordering::Relaxed);
state / READER + (state & UPGRADED) / UPGRADED
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/// @brief 尝试获得UPGRADER守卫
pub fn try_upgradeable_read(&self) -> Option<RwLockUpgradableGuard<T>> {
//获得UPGRADER守卫不需要查看读者位
//如果获得读者锁失败,不撤回fetch_or的原子操作
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么

#[inline]
pub fn downgrade(self) -> RwLockReadGuard<'rwlock, T> {
while {
let res = self.inner.current_reader();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不需要写成闭包,一句话写好即可

@fslongjin fslongjin merged commit f1284c3 into DragonOS-Community:master Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants