#include "private/bionic_constants.h"
#include "private/bionic_futex.h"
+#include "private/bionic_sdk_version.h"
#include "private/bionic_time_conversions.h"
// In this implementation, a semaphore contains a
return 0;
}
- __futex_wait_ex(sem_count_ptr, shared, shared | SEMCOUNT_MINUS_ONE, NULL);
+ int result = __futex_wait_ex(sem_count_ptr, shared, shared | SEMCOUNT_MINUS_ONE, false, nullptr);
+ if (bionic_get_application_target_sdk_version() >= __ANDROID_API_N__) {
+ if (result ==-EINTR) {
+ errno = EINTR;
+ return -1;
+ }
+ }
}
}
}
// Check it as per POSIX.
- if (abs_timeout == NULL || abs_timeout->tv_sec < 0 || abs_timeout->tv_nsec < 0 || abs_timeout->tv_nsec >= NS_PER_S) {
- errno = EINVAL;
+ int result = check_timespec(abs_timeout, false);
+ if (result != 0) {
+ errno = result;
return -1;
}
unsigned int shared = SEM_GET_SHARED(sem_count_ptr);
while (true) {
- // POSIX mandates CLOCK_REALTIME here.
- timespec ts;
- if (!timespec_from_absolute_timespec(ts, *abs_timeout, CLOCK_REALTIME)) {
- errno = ETIMEDOUT;
- return -1;
- }
-
// Try to grab the semaphore. If the value was 0, this will also change it to -1.
if (__sem_dec(sem_count_ptr) > 0) {
- break;
+ return 0;
}
// Contention detected. Wait for a wakeup event.
- int ret = __futex_wait_ex(sem_count_ptr, shared, shared | SEMCOUNT_MINUS_ONE, &ts);
+ int result = __futex_wait_ex(sem_count_ptr, shared, shared | SEMCOUNT_MINUS_ONE, true, abs_timeout);
// Return in case of timeout or interrupt.
- if (ret == -ETIMEDOUT || ret == -EINTR) {
- errno = -ret;
+ if (result == -ETIMEDOUT || result == -EINTR) {
+ errno = -result;
return -1;
}
}
- return 0;
}
int sem_post(sem_t* sem) {