Unified · Split
| @@ -8,6 +8,52 @@ · 7 unmodified lines | ||
| 8 | 8 | #include <errno.h> |
| 9 | 9 | #include <setjmp.h> |
| 10 | 10 | |
| 11 | +#if defined(__APPLE__) | |
| 12 | +/* | |
| 13 | + * clock_nanosleep is missing on all macOS version, add emulation. | |
| 14 | + */ | |
| 15 | + | |
| 16 | +/* scale factors */ | |
| 17 | +#define TIMING_GIGA 1000000000 | |
| 18 | +#define TIMER_ABSTIME 0 /* not used */ | |
| 19 | + | |
| 20 | +/* timespec difference (monotonic) right - left */ | |
| 21 | +static inline void | |
| 22 | +timespec_monodiff_rml(struct timespec *ts_out, const struct timespec *ts_in) { | |
| 23 | + /* | |
| 24 | + * out = in - out, | |
| 25 | + * where in > out | |
| 26 | + */ | |
| 27 | + ts_out->tv_sec = ts_in->tv_sec - ts_out->tv_sec; | |
| 28 | + ts_out->tv_nsec = ts_in->tv_nsec - ts_out->tv_nsec; | |
| 29 | + if (ts_out->tv_sec < 0) { | |
| 30 | + ts_out->tv_sec = 0; | |
| 31 | + ts_out->tv_nsec = 0; | |
| 32 | + } else if (ts_out->tv_nsec < 0) { | |
| 33 | + if (ts_out->tv_sec == 0) { | |
| 34 | + ts_out->tv_sec = 0; | |
| 35 | + ts_out->tv_nsec = 0; | |
| 36 | + } else { | |
| 37 | + ts_out->tv_sec = ts_out->tv_sec - 1; | |
| 38 | + ts_out->tv_nsec = ts_out->tv_nsec + TIMING_GIGA; | |
| 39 | + } | |
| 40 | + } | |
| 41 | +} | |
| 42 | + | |
| 43 | +static inline int | |
| 44 | +clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *req, struct timespec *remain) { | |
| 45 | + struct timespec ts_delta; | |
| 46 | + int retval = clock_gettime(clock_id, &ts_delta); | |
| 47 | + (void)remain; | |
| 48 | + (void)flags; | |
| 49 | + if (retval == 0) { | |
| 50 | + timespec_monodiff_rml(&ts_delta, req); | |
| 51 | + retval = nanosleep(&ts_delta, NULL); | |
| 52 | + } | |
| 53 | + return retval; | |
| 54 | +} | |
| 55 | +#endif | |
| 56 | + | |
| 11 | 57 | static volatile int run = 1; |
| 12 | 58 | static int dummy[10]; |
| 13 | 59 | static sem_t sem; |