Ticket #5628: 0001-sem_timedwait-fix-errno-return-value-and-NULL-behavi.patch

File 0001-sem_timedwait-fix-errno-return-value-and-NULL-behavi.patch, 1.4 KB (added by korli, 7 years ago)

gci 2012

  • src/system/libroot/posix/semaphore.cpp

    From 3ca4b42d6d75602b051a0fd331f6d61af81eb73f Mon Sep 17 00:00:00 2001
    From: Sam Toyer <sam@qxcv.net>
    Date: Fri, 30 Nov 2012 08:57:10 +1000
    Subject: [PATCH] sem_timedwait: fix errno, return value and NULL behaviour
    
    ---
     src/system/libroot/posix/semaphore.cpp |   20 ++++++++++++++++----
     1 file changed, 16 insertions(+), 4 deletions(-)
    
    diff --git a/src/system/libroot/posix/semaphore.cpp b/src/system/libroot/posix/semaphore.cpp
    index b96b835..e581d7a 100644
    a b sem_post(sem_t* semaphore)  
    112112int
    113113sem_timedwait(sem_t* semaphore, const struct timespec* timeout)
    114114{
    115     bigtime_t timeoutMicros = ((bigtime_t)timeout->tv_sec) * 1000000
    116         + timeout->tv_nsec / 1000;
     115    status_t err = _kern_realtime_sem_wait(semaphore->id, 0);
     116
     117    if (err != B_WOULD_BLOCK) {
     118        // do nothing, return err as it is.
     119    } else if (timeout == NULL) {
     120        err = _kern_realtime_sem_wait(semaphore->id, B_INFINITE_TIMEOUT);
     121    } else if (timeout->tv_nsec < 0 ||
     122               timeout->tv_nsec >= 1000000000) {
     123        err = EINVAL;
     124    } else {
     125        bigtime_t timeoutMicros = ((bigtime_t)timeout->tv_sec) * 1000000
     126            + timeout->tv_nsec / 1000;
    117127
    118     RETURN_AND_SET_ERRNO_TEST_CANCEL(
    119         _kern_realtime_sem_wait(semaphore->id, timeoutMicros));
     128        err = _kern_realtime_sem_wait(semaphore->id, timeoutMicros);
     129    }
     130
     131    RETURN_AND_SET_ERRNO_TEST_CANCEL(err);
    120132}
    121133
    122134