Skip to content

Commit 12b5206

Browse files
committed
Use monotonic clock for semaphore timeout where supported (fixes cameron314#289)
1 parent 68aacbc commit 12b5206

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

Diff for: lightweightsemaphore.h

+8
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ class Semaphore
209209
struct timespec ts;
210210
const int usecs_in_1_sec = 1000000;
211211
const int nsecs_in_1_sec = 1000000000;
212+
#if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2,30) && defined(_GNU_SOURCE)
213+
clock_gettime(CLOCK_MONOTONIC, &ts);
214+
#else
212215
clock_gettime(CLOCK_REALTIME, &ts);
216+
#endif
213217
ts.tv_sec += (time_t)(usecs / usecs_in_1_sec);
214218
ts.tv_nsec += (long)(usecs % usecs_in_1_sec) * 1000;
215219
// sem_timedwait bombs if you have more than 1e9 in tv_nsec
@@ -221,7 +225,11 @@ class Semaphore
221225

222226
int rc;
223227
do {
228+
#if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2,30) && defined(_GNU_SOURCE)
229+
rc = sem_clockwait(&m_sema, CLOCK_MONOTONIC, &ts);
230+
#else
224231
rc = sem_timedwait(&m_sema, &ts);
232+
#endif
225233
} while (rc == -1 && errno == EINTR);
226234
return rc == 0;
227235
}

0 commit comments

Comments
 (0)