Skip to content

Commit 32411c2

Browse files
committed
Merge pull request #20 from dgrove-oss/sem_timedwait_fix_v2
posix sem_timedwait requires epoch-based timeout
2 parents ceafaf6 + 84494ad commit 32411c2

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ void _dispatch_vtable_init(void);
528528
char *_dispatch_get_build(void);
529529

530530
uint64_t _dispatch_timeout(dispatch_time_t when);
531+
uint64_t _dispatch_time_to_nanoseconds(dispatch_time_t when);
531532

532533
extern bool _dispatch_safe_fork, _dispatch_child_of_unsafe_fork;
533534

src/semaphore.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ _dispatch_semaphore_wait_slow(dispatch_semaphore_t dsema,
342342
}
343343
#elif USE_POSIX_SEM
344344
do {
345-
uint64_t nsec = _dispatch_timeout(timeout);
345+
uint64_t nsec = _dispatch_time_to_nanoseconds(timeout);
346346
_timeout.tv_sec = (typeof(_timeout.tv_sec))(nsec / NSEC_PER_SEC);
347347
_timeout.tv_nsec = (typeof(_timeout.tv_nsec))(nsec % NSEC_PER_SEC);
348348
ret = slowpath(sem_timedwait(&dsema->dsema_sem, &_timeout));
@@ -582,7 +582,7 @@ _dispatch_group_wait_slow(dispatch_semaphore_t dsema, dispatch_time_t timeout)
582582
}
583583
#elif USE_POSIX_SEM
584584
do {
585-
uint64_t nsec = _dispatch_timeout(timeout);
585+
uint64_t nsec = _dispatch_time_to_nanoseconds(timeout);
586586
_timeout.tv_sec = (typeof(_timeout.tv_sec))(nsec / NSEC_PER_SEC);
587587
_timeout.tv_nsec = (typeof(_timeout.tv_nsec))(nsec % NSEC_PER_SEC);
588588
ret = slowpath(sem_timedwait(&dsema->dsema_sem, &_timeout));

src/time.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,16 @@ _dispatch_timeout(dispatch_time_t when)
145145
now = _dispatch_absolute_time();
146146
return now >= when ? 0 : _dispatch_time_mach2nano(when - now);
147147
}
148+
149+
uint64_t
150+
_dispatch_time_to_nanoseconds(dispatch_time_t when)
151+
{
152+
if (when == DISPATCH_TIME_FOREVER) {
153+
return DISPATCH_TIME_FOREVER;
154+
}
155+
if ((int64_t)when < 0) {
156+
// time in nanoseconds since the POSIX epoch already
157+
return -(int64_t)when;
158+
}
159+
return _dispatch_get_nanoseconds() + _dispatch_timeout(when);
160+
}

0 commit comments

Comments
 (0)