Skip to content

Commit

Permalink
Fix THREAD_CPUTIME clock_time_get on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer committed Jan 15, 2023
1 parent 02a7e48 commit d16ebe6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/clocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
#include "uv_mapping.h"


#define UVWASI__WIN_TIME_AND_RETURN(handle, time) \
#define UVWASI__WIN_TIME_AND_RETURN(handle, get_times, time) \
do { \
FILETIME create; \
FILETIME exit; \
FILETIME system; \
FILETIME user; \
SYSTEMTIME sys_system; \
SYSTEMTIME sys_user; \
if (0 == GetProcessTimes((handle), &create, &exit, &system, &user)) { \
if (0 == get_times((handle), &create, &exit, &system, &user)) { \
return uvwasi__translate_uv_error( \
uv_translate_sys_error(GetLastError()) \
); \
Expand Down Expand Up @@ -137,7 +137,7 @@ uvwasi_errno_t uvwasi__clock_gettime_realtime(uvwasi_timestamp_t* time) {

uvwasi_errno_t uvwasi__clock_gettime_process_cputime(uvwasi_timestamp_t* time) {
#if defined(_WIN32)
UVWASI__WIN_TIME_AND_RETURN(GetCurrentProcess(), *time);
UVWASI__WIN_TIME_AND_RETURN(GetCurrentProcess(), GetProcessTimes, *time);
#elif defined(CLOCK_PROCESS_CPUTIME_ID) && \
!defined(__APPLE__) && \
!defined(__sun)
Expand All @@ -150,7 +150,7 @@ uvwasi_errno_t uvwasi__clock_gettime_process_cputime(uvwasi_timestamp_t* time) {

uvwasi_errno_t uvwasi__clock_gettime_thread_cputime(uvwasi_timestamp_t* time) {
#if defined(_WIN32)
UVWASI__WIN_TIME_AND_RETURN(GetCurrentThread(), *time);
UVWASI__WIN_TIME_AND_RETURN(GetCurrentThread(), GetThreadTimes, *time);
#elif defined(__APPLE__)
UVWASI__OSX_THREADTIME_AND_RETURN(*time);
#elif defined(CLOCK_THREAD_CPUTIME_ID) && !defined(__sun) && !defined(__PASE__)
Expand Down
37 changes: 37 additions & 0 deletions test/test-clock-time-get.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "uvwasi.h"

int main(void) {
uvwasi_t uvwasi;
uvwasi_options_t init_options;
uvwasi_errno_t err;
uvwasi_timestamp_t time;
uvwasi_timestamp_t precision = 1000;

uvwasi_options_init(&init_options);

err = uvwasi_init(&uvwasi, &init_options);
assert(err == 0);

err = uvwasi_clock_time_get(&uvwasi, UVWASI_CLOCK_REALTIME, precision, &time);
assert(err == 0);
assert(time > 0);

err = uvwasi_clock_time_get(&uvwasi, UVWASI_CLOCK_MONOTONIC, precision, &time);
assert(err == 0);
assert(time > 0);

err = uvwasi_clock_time_get(&uvwasi, UVWASI_CLOCK_PROCESS_CPUTIME_ID, precision, &time);
assert(err == 0);
assert(time > 0);

err = uvwasi_clock_time_get(&uvwasi, UVWASI_CLOCK_THREAD_CPUTIME_ID, precision, &time);
assert(err == 0);
assert(time > 0);

uvwasi_destroy(&uvwasi);

return 0;
}

0 comments on commit d16ebe6

Please sign in to comment.