Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sched/sem_rw: convert read-lock to write-lock when self already holds write-lock #13797

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions boards/arm/sam34/sam3u-ek/configs/knsh/defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ CONFIG_ARM_MPU=y
CONFIG_ARM_TOOLCHAIN_BUILDROOT_OABI=y
CONFIG_BOARD_LOOPSPERMSEC=8720
CONFIG_BUILD_PROTECTED=y
CONFIG_DISABLE_PTHREAD=y
CONFIG_FS_FAT=y
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_MMCSD=y
Expand Down
4 changes: 2 additions & 2 deletions fs/inode/fs_inodefind.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int inode_find(FAR struct inode_search_s *desc)
* references on the node.
*/

inode_lock();
inode_rlock();
ret = inode_search(desc);
if (ret >= 0)
{
Expand All @@ -69,6 +69,6 @@ int inode_find(FAR struct inode_search_s *desc)
atomic_fetch_add(&node->i_crefs, 1);
}

inode_unlock();
inode_runlock();
return ret;
}
40 changes: 40 additions & 0 deletions sched/semaphore/sem_rw.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ int down_read_trylock(FAR rw_semaphore_t *rwsem)
{
irqstate_t flags = spin_lock_irqsave(&rwsem->protected);

/* if the write lock is already held by oneself and since the write lock
* can be recursively held, so, this operation can be converted to a write
* lock to avoid deadlock.
*/

if (rwsem->holder == _SCHED_GETTID())
{
rwsem->writer++;
goto out;
}

if (rwsem->writer > 0)
{
spin_unlock_irqrestore(&rwsem->protected, flags);
Expand All @@ -80,6 +91,7 @@ int down_read_trylock(FAR rw_semaphore_t *rwsem)

rwsem->reader++;

out:
spin_unlock_irqrestore(&rwsem->protected, flags);

return 1;
Expand All @@ -104,6 +116,17 @@ void down_read(FAR rw_semaphore_t *rwsem)

irqstate_t flags = spin_lock_irqsave(&rwsem->protected);

/* if the write lock is already held by oneself and since the write lock
* can be recursively held, so, this operation can be converted to a write
* lock to avoid deadlock.
*/

if (rwsem->holder == _SCHED_GETTID())
{
rwsem->writer++;
goto out;
}

while (rwsem->writer > 0)
{
rwsem->waiter++;
Expand All @@ -119,6 +142,7 @@ void down_read(FAR rw_semaphore_t *rwsem)

rwsem->reader++;

out:
spin_unlock_irqrestore(&rwsem->protected, flags);
}

Expand All @@ -137,6 +161,21 @@ void up_read(FAR rw_semaphore_t *rwsem)
{
irqstate_t flags = spin_lock_irqsave(&rwsem->protected);

/* when releasing a read lock and holder is oneself, the read lock is a
* write lock that has been converted, so it should be released according
* to the procedures for releasing a write lock.
*/

if (rwsem->holder == _SCHED_GETTID())
{
if (--rwsem->writer <= 0)
{
rwsem->holder = RWSEM_NO_HOLDER;
}

goto out;
}

DEBUGASSERT(rwsem->reader > 0);

rwsem->reader--;
Expand All @@ -146,6 +185,7 @@ void up_read(FAR rw_semaphore_t *rwsem)
up_wait(rwsem);
}

out:
spin_unlock_irqrestore(&rwsem->protected, flags);
}

Expand Down
Loading