lib: posix: pthread_rwlock: Fix a racy issue

Multiple reader threads unlocking the read lock simultaneously might
cause the program hang because it's possible that no thread is
identified as the last one to active the writer thread.

To fix the issue, swap the k_sem_give sequence.

Signed-off-by: Jaxson Han <jaxson.han@arm.com>
This commit is contained in:
Jaxson Han 2023-07-10 22:04:05 +08:00 committed by Carles Cufí
parent 8fd1ce7579
commit 0df7bd26ed

View File

@ -200,13 +200,13 @@ int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
k_sem_give(&rwlock->wr_sem);
} else {
/* Read unlock */
k_sem_give(&rwlock->rd_sem);
if (k_sem_count_get(&rwlock->rd_sem) ==
(CONCURRENT_READER_LIMIT - 1)) {
CONCURRENT_READER_LIMIT) {
/* Last read lock, unlock writer */
k_sem_give(&rwlock->reader_active);
}
k_sem_give(&rwlock->rd_sem);
}
return 0;
}