| | |
| | | #include "psem.h" |
| | | #include <semaphore.h> |
| | | #include "time_util.h" |
| | | |
| | | #define NANO 1000000000 |
| | | |
| | | |
| | | static struct timespec psem_calc_abs_timeout(const struct timespec *ts) { |
| | | |
| | | struct timespec res; |
| | | struct timespec timeout; |
| | | if (clock_gettime(CLOCK_REALTIME, &timeout) == -1) |
| | | err_exit(errno, "clock_gettime"); |
| | | |
| | | res.tv_sec = timeout.tv_sec + ts->tv_sec; |
| | | res.tv_nsec = timeout.tv_nsec + ts->tv_nsec; |
| | | res.tv_sec = res.tv_sec + floor(res.tv_nsec / NANO); |
| | | res.tv_nsec = res.tv_nsec % NANO; |
| | | return res; |
| | | } |
| | | |
| | | |
| | | int psem_timedwait(sem_t *sem, const struct timespec *ts) { |
| | | struct timespec abs_timeout = psem_calc_abs_timeout(ts); |
| | | struct timespec abs_timeout = TimeUtil::calc_abs_time(ts); |
| | | |
| | | int rv ; |
| | | while ( (rv = sem_timedwait(sem, &abs_timeout)) == -1) { |