| 64 | /// @tparam Period A fl::ratio representing the tick period |
| 65 | template<typename Rep, typename Period> |
| 66 | inline void native_sleep(const fl::chrono::duration<Rep, Period>& sleep_duration) FL_NOEXCEPT { |
| 67 | // Convert to nanoseconds |
| 68 | auto ns = fl::chrono::duration_cast<fl::chrono::nanoseconds>(sleep_duration).count(); |
| 69 | |
| 70 | if (ns <= 0) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | #ifdef FL_IS_WIN |
| 75 | // Windows: Sleep takes milliseconds |
| 76 | // Convert nanoseconds to milliseconds (round up) |
| 77 | unsigned long ms = static_cast<unsigned long>((ns + 999999) / 1000000); |
| 78 | Sleep(ms) FL_NOEXCEPT; |
| 79 | #else |
| 80 | // POSIX: Use nanosleep |
| 81 | struct timespec ts; |
| 82 | ts.tv_sec = ns / 1000000000LL; |
| 83 | ts.tv_nsec = ns % 1000000000LL; |
| 84 | |
| 85 | // Handle EINTR (interrupted by signal) by retrying |
| 86 | while (::nanosleep(&ts, &ts) == -1 && errno == EINTR) { |
| 87 | // Continue with remaining time in ts |
| 88 | } |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | } // namespace detail |
| 93 | |