From 15cce56411409d9b8b2cc4228ebd7b13043ce199 Mon Sep 17 00:00:00 2001 From: Timur Sultanov Date: Wed, 16 Feb 2022 21:23:24 +0300 Subject: [PATCH] LibPthread: Set errno in sem_trywait() --- Userland/Libraries/LibPthread/semaphore.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibPthread/semaphore.cpp b/Userland/Libraries/LibPthread/semaphore.cpp index 81564b1422..d60d43fc1e 100644 --- a/Userland/Libraries/LibPthread/semaphore.cpp +++ b/Userland/Libraries/LibPthread/semaphore.cpp @@ -93,15 +93,19 @@ int sem_trywait(sem_t* sem) { u32 value = AK::atomic_load(&sem->value, AK::memory_order_relaxed); u32 count = value & ~POST_WAKES; - if (count == 0) - return EAGAIN; + if (count == 0) { + errno = EAGAIN; + return -1; + } // Decrement the count without touching the flag. u32 desired = (count - 1) | (value & POST_WAKES); bool exchanged = AK::atomic_compare_exchange_strong(&sem->value, value, desired, AK::memory_order_acquire); - if (exchanged) [[likely]] + if (exchanged) [[likely]] { return 0; - else - return EAGAIN; + } else { + errno = EAGAIN; + return -1; + } } // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_wait.html