Skip to content

tests: support the timer source tests on Windows #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ add_unit_test(dispatch_c99 NO_BSD_OVERLAY SOURCES dispatch_c99.c)
add_unit_test(dispatch_plusplus SOURCES dispatch_plusplus.cpp)

# test-specific link options
target_link_libraries(dispatch_group PRIVATE m)
target_link_libraries(dispatch_timer_short PRIVATE m)
if(NOT WIN32)
target_link_libraries(dispatch_group PRIVATE m)
target_link_libraries(dispatch_timer_short PRIVATE m)
endif()

# test-specific compile options
set_target_properties(dispatch_c99 PROPERTIES C_STANDARD 99)
7 changes: 6 additions & 1 deletion tests/dispatch_drift.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include <mach/mach_time.h>
#endif
#include <dispatch/dispatch.h>
#include <sys/time.h>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/time.h>
#include <unistd.h>
#endif
#include <stdio.h>
Expand All @@ -46,8 +46,13 @@ main(int argc __attribute__((unused)), char* argv[] __attribute__((unused)))
__block uint32_t count = 0;
__block double last_jitter = 0;
__block double drift_sum = 0;
#if defined(_WIN32)
// 25 times a second (Windows timer resolution is poor)
uint64_t interval = 1000000000 / 25;
#else
// 100 times a second
uint64_t interval = 1000000000 / 100;
#endif
double interval_d = interval / 1000000000.0;
// for 25 seconds
unsigned int target = (unsigned int)(25.0 / interval_d);
Expand Down
2 changes: 2 additions & 0 deletions tests/dispatch_timer_bit31.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/time.h>
#endif

#include <dispatch/dispatch.h>

Expand Down
2 changes: 2 additions & 0 deletions tests/dispatch_timer_bit63.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/time.h>
#endif

#include <dispatch/dispatch.h>

Expand Down
3 changes: 2 additions & 1 deletion tests/dispatch_timer_set_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
* @APPLE_APACHE_LICENSE_HEADER_END@
*/

#include <sys/time.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/time.h>
#endif

#include <dispatch/dispatch.h>

Expand Down
2 changes: 2 additions & 0 deletions tests/dispatch_timer_timeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/time.h>
#endif

#include <dispatch/dispatch.h>

Expand Down
52 changes: 40 additions & 12 deletions tests/generic_win_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,50 @@ gettimeofday(struct timeval *tp, void *tzp)
return 0;
}

typedef void (WINAPI *QueryUnbiasedInterruptTimePreciseT)(PULONGLONG);
static QueryUnbiasedInterruptTimePreciseT QueryUnbiasedInterruptTimePrecisePtr;

static BOOL
mach_absolute_time_init(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *lpContext)
{
// QueryUnbiasedInterruptTimePrecise() is declared in the Windows headers
// but it isn't available in any import libraries. We must manually load it
// from KernelBase.dll.
HMODULE kernelbase = LoadLibraryW(L"KernelBase.dll");
if (!kernelbase) {
print_winapi_error("LoadLibraryW", GetLastError());
abort();
}
QueryUnbiasedInterruptTimePrecisePtr =
(QueryUnbiasedInterruptTimePreciseT)GetProcAddress(kernelbase,
"QueryUnbiasedInterruptTimePrecise");
if (!QueryUnbiasedInterruptTimePrecisePtr) {
fprintf(stderr, "QueryUnbiasedInterruptTimePrecise is not available\n");
abort();
}
return TRUE;
}

uint64_t
mach_absolute_time(void)
{
static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
if (!InitOnceExecuteOnce(&init_once, mach_absolute_time_init, NULL, NULL)) {
print_winapi_error("InitOnceExecuteOnce", GetLastError());
abort();
}
ULONGLONG result = 0;
QueryUnbiasedInterruptTimePrecisePtr(&result);
return result * 100; // Convert from 100ns units
}

void
print_winapi_error(const char *function_name, DWORD error)
{
char *message = NULL;
DWORD len = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error,
0,
(LPSTR)&message,
0,
NULL);
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
error, 0, (LPSTR)&message, 0, NULL);
if (len > 0) {
// Note: FormatMessage includes a newline at the end of the message
fprintf(stderr, "%s: %s", function_name, message);
Expand All @@ -214,10 +246,6 @@ sleep(unsigned int seconds)
int
usleep(unsigned int usec)
{
DWORD ms = usec / 1000;
if (ms == 0 && usec != 0) {
ms = 1;
}
Sleep(ms);
Sleep((usec + 999) / 1000);
return 0;
}
20 changes: 20 additions & 0 deletions tests/generic_win_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ typedef long long ssize_t;
typedef long ssize_t;
#endif

struct mach_timebase_info {
uint32_t numer;
uint32_t denom;
};

typedef struct mach_timebase_info *mach_timebase_info_t;
typedef struct mach_timebase_info mach_timebase_info_data_t;

static inline int32_t
OSAtomicIncrement32(volatile int32_t *var)
{
Expand Down Expand Up @@ -45,6 +53,18 @@ getpid(void);
int
gettimeofday(struct timeval *tp, void *tzp);

uint64_t
mach_absolute_time(void);

static inline
int
mach_timebase_info(mach_timebase_info_t tbi)
{
tbi->numer = 1;
tbi->denom = 1;
return 0;
}

void
print_winapi_error(const char *function_name, DWORD error);

Expand Down