Skip to content

Commit

Permalink
Fix for maxOS and OpenBSD
Browse files Browse the repository at this point in the history
Fallback to nanosleep if clock_nanosleep doesn't exist.

Closes #33
  • Loading branch information
rigtorp committed Sep 25, 2023
1 parent 52bd71d commit 0498ff0
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/udpreplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,25 @@ int main(int argc, char *argv[]) {

if (deadline.tv_sec > now.tv_sec ||
(deadline.tv_sec == now.tv_sec && deadline.tv_nsec > now.tv_nsec)) {
#if _POSIX_C_SOURCE >= 200112L
if (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &deadline,
nullptr) == -1) {
std::cerr << "clock_nanosleep: " << strerror(errno) << std::endl;
return 1;
}
#else
timespec duration;
duration.tv_sec = deadline.tv_sec - now.tv_sec;
duration.tv_nsec = deadline.tv_nsec - now.tv_nsec;
if (duration.tv_nsec < 0) {
--duration.tv_sec;
duration.tv_nsec += NANOSECONDS_PER_SECOND;
}
if (nanosleep(&duration, nullptr) == -1) {
std::cerr << "nanosleep: " << strerror(errno) << std::endl;
return 1;
}
#endif
}

#ifdef __GLIBC__
Expand Down

0 comments on commit 0498ff0

Please sign in to comment.