libroot: Use timespec_to_bigtime for time conversions in the POSIX semaphore implementation.

This commit is contained in:
Augustin Cavalier
2024-10-12 13:48:14 -04:00
parent 0324754ddb
commit c63558614e
+8 -10
View File
@@ -19,6 +19,7 @@
#include <posix/realtime_sem_defs.h> #include <posix/realtime_sem_defs.h>
#include <syscall_utils.h> #include <syscall_utils.h>
#include <syscalls.h> #include <syscalls.h>
#include <time_private.h>
#include <user_mutex_defs.h> #include <user_mutex_defs.h>
@@ -170,10 +171,9 @@ unnamed_sem_timedwait(sem_t* semaphore, clockid_t clock_id,
if (semaphore->type == SEM_TYPE_UNNAMED_SHARED) if (semaphore->type == SEM_TYPE_UNNAMED_SHARED)
flags |= B_USER_MUTEX_SHARED; flags |= B_USER_MUTEX_SHARED;
if (timeout != NULL) { if (timeout != NULL) {
timeoutMicros = ((bigtime_t)timeout->tv_sec) * 1000000 if (!timespec_to_bigtime(*timeout, timeoutMicros))
+ timeout->tv_nsec / 1000;
if (timeout->tv_nsec < 0 || timeout->tv_nsec >= 1000000000)
timeoutMicros = -1; timeoutMicros = -1;
switch (clock_id) { switch (clock_id) {
case CLOCK_REALTIME: case CLOCK_REALTIME:
flags |= B_ABSOLUTE_REAL_TIME_TIMEOUT; flags |= B_ABSOLUTE_REAL_TIME_TIMEOUT;
@@ -213,8 +213,10 @@ static int
named_sem_timedwait(sem_t* semaphore, clockid_t clock_id, named_sem_timedwait(sem_t* semaphore, clockid_t clock_id,
const struct timespec* timeout) const struct timespec* timeout)
{ {
if (timeout != NULL bigtime_t timeoutMicros = B_INFINITE_TIMEOUT;
&& (timeout->tv_nsec < 0 || timeout->tv_nsec >= 1000000000)) { uint32 flags = 0;
if (timeout != NULL) {
if (!timespec_to_bigtime(*timeout, timeoutMicros)) {
status_t err = _kern_realtime_sem_wait(semaphore->u.named_sem_id, status_t err = _kern_realtime_sem_wait(semaphore->u.named_sem_id,
B_RELATIVE_TIMEOUT, 0); B_RELATIVE_TIMEOUT, 0);
if (err == B_WOULD_BLOCK) if (err == B_WOULD_BLOCK)
@@ -223,11 +225,6 @@ named_sem_timedwait(sem_t* semaphore, clockid_t clock_id,
return err; return err;
} }
bigtime_t timeoutMicros = B_INFINITE_TIMEOUT;
uint32 flags = 0;
if (timeout != NULL) {
timeoutMicros = ((bigtime_t)timeout->tv_sec) * 1000000
+ timeout->tv_nsec / 1000;
switch (clock_id) { switch (clock_id) {
case CLOCK_REALTIME: case CLOCK_REALTIME:
flags = B_ABSOLUTE_REAL_TIME_TIMEOUT; flags = B_ABSOLUTE_REAL_TIME_TIMEOUT;
@@ -239,6 +236,7 @@ named_sem_timedwait(sem_t* semaphore, clockid_t clock_id,
return EINVAL; return EINVAL;
} }
} }
status_t err = _kern_realtime_sem_wait(semaphore->u.named_sem_id, flags, status_t err = _kern_realtime_sem_wait(semaphore->u.named_sem_id, flags,
timeoutMicros); timeoutMicros);
if (err == B_WOULD_BLOCK) if (err == B_WOULD_BLOCK)