Skip to content

Commit 833eacc

Browse files
ClaCodeskartben
authored andcommitted
posix: rwlock: Refactor locking using k_timepoint_t
Make use of k_timepoint_t ind the calculation of the remaining time for the rwlock. Signed-off-by: Cla Mattia Galliard <clamattia@gmail.com>
1 parent 8e2c053 commit 833eacc

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

lib/posix/options/rwlock.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -359,24 +359,14 @@ static uint32_t read_lock_acquire(struct posix_rwlock *rwl, uint32_t timeout)
359359
static uint32_t write_lock_acquire(struct posix_rwlock *rwl, uint32_t timeout)
360360
{
361361
uint32_t ret = 0U;
362-
int64_t elapsed_time, st_time = k_uptime_get();
363-
k_timeout_t k_timeout;
362+
k_timepoint_t deadline;
364363

365-
k_timeout = SYS_TIMEOUT_MS(timeout);
364+
deadline = sys_timepoint_calc(SYS_TIMEOUT_MS(timeout));
366365

367366
/* waiting for release of write lock */
368-
if (sys_sem_take(&rwl->wr_sem, k_timeout) == 0) {
369-
/* update remaining timeout time for 2nd sem */
370-
if (timeout != SYS_FOREVER_MS) {
371-
elapsed_time = k_uptime_get() - st_time;
372-
timeout = timeout <= elapsed_time ? 0 :
373-
timeout - elapsed_time;
374-
}
375-
376-
k_timeout = SYS_TIMEOUT_MS(timeout);
377-
367+
if (sys_sem_take(&rwl->wr_sem, sys_timepoint_timeout(deadline)) == 0) {
378368
/* waiting for reader to complete operation */
379-
if (sys_sem_take(&rwl->reader_active, k_timeout) == 0) {
369+
if (sys_sem_take(&rwl->reader_active, sys_timepoint_timeout(deadline)) == 0) {
380370
rwl->wr_owner = k_current_get();
381371
} else {
382372
(void)sys_sem_give(&rwl->wr_sem);

0 commit comments

Comments
 (0)