@@ -37,52 +37,48 @@ static long long get_ticks_per_second() {
3737ErrorOr<int > clock_gettime (clockid_t clockid, timespec *ts) {
3838 using namespace time_units ;
3939 ErrorOr<int > ret = 0 ;
40- __try {
41- switch (clockid) {
42- default :
43- ret = cpp::unexpected (EINVAL);
44- break ;
40+ switch (clockid) {
41+ default :
42+ ret = cpp::unexpected (EINVAL);
43+ break ;
4544
46- case CLOCK_MONOTONIC: {
47- // see
48- // https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps
49- // Is the performance counter monotonic (non-decreasing)?
50- // Yes. QPC does not go backward.
51- UNINITIALIZED LARGE_INTEGER buffer;
52- // On systems that run Windows XP or later, the function will always
53- // succeed and will thus never return zero.
54- ::QueryPerformanceCounter (&buffer);
55- long long freq = get_ticks_per_second ();
56- long long ticks = buffer.QuadPart ;
57- long long tv_sec = ticks / freq;
58- long long tv_nsec = (ticks % freq) * 1_s_ns / freq;
59- ts->tv_sec = static_cast <decltype (ts->tv_sec )>(tv_sec);
60- ts->tv_nsec = static_cast <decltype (ts->tv_nsec )>(tv_nsec);
61- break ;
62- }
63- case CLOCK_REALTIME: {
64- // https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime
65- // GetSystemTimePreciseAsFileTime
66- // This function is best suited for high-resolution time-of-day
67- // measurements, or time stamps that are synchronized to UTC
68- UNINITIALIZED FILETIME file_time;
69- UNINITIALIZED ULARGE_INTEGER time;
70- ::GetSystemTimePreciseAsFileTime (&file_time);
71- time.LowPart = file_time.dwLowDateTime ;
72- time.HighPart = file_time.dwHighDateTime ;
45+ case CLOCK_MONOTONIC: {
46+ // see
47+ // https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps
48+ // Is the performance counter monotonic (non-decreasing)?
49+ // Yes. QPC does not go backward.
50+ UNINITIALIZED LARGE_INTEGER buffer;
51+ // On systems that run Windows XP or later, the function will always
52+ // succeed and will thus never return zero.
53+ ::QueryPerformanceCounter (&buffer);
54+ long long freq = get_ticks_per_second ();
55+ long long ticks = buffer.QuadPart ;
56+ long long tv_sec = ticks / freq;
57+ long long tv_nsec = (ticks % freq) * 1_s_ns / freq;
58+ ts->tv_sec = static_cast <decltype (ts->tv_sec )>(tv_sec);
59+ ts->tv_nsec = static_cast <decltype (ts->tv_nsec )>(tv_nsec);
60+ break ;
61+ }
62+ case CLOCK_REALTIME: {
63+ // https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime
64+ // GetSystemTimePreciseAsFileTime
65+ // This function is best suited for high-resolution time-of-day
66+ // measurements, or time stamps that are synchronized to UTC
67+ UNINITIALIZED FILETIME file_time;
68+ UNINITIALIZED ULARGE_INTEGER time;
69+ ::GetSystemTimePreciseAsFileTime (&file_time);
70+ time.LowPart = file_time.dwLowDateTime ;
71+ time.HighPart = file_time.dwHighDateTime ;
7372
74- // adjust to POSIX epoch (from Jan 1, 1601 to Jan 1, 1970)
75- constexpr unsigned long long HNS_PER_SEC = 1_s_ns / 100ULL ;
76- time.QuadPart -= (11644473600ULL * HNS_PER_SEC);
77- unsigned long long tv_sec = time.QuadPart / HNS_PER_SEC;
78- unsigned long long tv_nsec = (time.QuadPart % HNS_PER_SEC) * 100ULL ;
79- ts->tv_sec = static_cast <decltype (ts->tv_sec )>(tv_sec);
80- ts->tv_nsec = static_cast <decltype (ts->tv_nsec )>(tv_nsec);
81- break ;
82- }
83- }
84- } __except (EXCEPTION_EXECUTE_HANDLER) {
85- ret = cpp::unexpected (EFAULT);
73+ // adjust to POSIX epoch (from Jan 1, 1601 to Jan 1, 1970)
74+ constexpr unsigned long long HNS_PER_SEC = 1_s_ns / 100ULL ;
75+ time.QuadPart -= (11644473600ULL * HNS_PER_SEC);
76+ unsigned long long tv_sec = time.QuadPart / HNS_PER_SEC;
77+ unsigned long long tv_nsec = (time.QuadPart % HNS_PER_SEC) * 100ULL ;
78+ ts->tv_sec = static_cast <decltype (ts->tv_sec )>(tv_sec);
79+ ts->tv_nsec = static_cast <decltype (ts->tv_nsec )>(tv_nsec);
80+ break ;
81+ }
8682 }
8783 return ret;
8884}
0 commit comments