syzkaller/executor/executor_windows.h
Dmitry Vyukov fd3e9f2b97 executor: introduce uint64/32/16/8 types
The "define uint64_t unsigned long long" were too good to work.
With a different toolchain I am getting:

cstdint:69:11: error: expected unqualified-id
  using ::uint64_t;
          ^
executor/common.h:34:18: note: expanded from macro 'uint64_t'

Do it the proper way: introduce uint64/32/16/8 types and use them.

pkg/csource then does s/uint64/uint64_t/ to not clutter code with
additional typedefs.
2017-12-27 11:15:04 +01:00

75 lines
1.5 KiB
C

// Copyright 2017 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
#include <windows.h>
typedef HANDLE osthread_t;
void thread_start(osthread_t* t, void* (*fn)(void*), void* arg)
{
*t = CreateThread(NULL, 128 << 10, (LPTHREAD_START_ROUTINE)fn, arg, 0, NULL);
if (*t == NULL)
exitf("CreateThread failed");
}
struct event_t {
CRITICAL_SECTION cs;
CONDITION_VARIABLE cv;
int state;
};
void event_init(event_t* ev)
{
InitializeCriticalSection(&ev->cs);
InitializeConditionVariable(&ev->cv);
ev->state = 0;
}
void event_reset(event_t* ev)
{
ev->state = 0;
}
void event_set(event_t* ev)
{
EnterCriticalSection(&ev->cs);
if (ev->state)
fail("event already set");
ev->state = true;
LeaveCriticalSection(&ev->cs);
WakeAllConditionVariable(&ev->cv);
}
void event_wait(event_t* ev)
{
EnterCriticalSection(&ev->cs);
while (!ev->state)
SleepConditionVariableCS(&ev->cv, &ev->cs, INFINITE);
LeaveCriticalSection(&ev->cs);
}
bool event_isset(event_t* ev)
{
EnterCriticalSection(&ev->cs);
bool res = ev->state;
LeaveCriticalSection(&ev->cs);
return res;
}
bool event_timedwait(event_t* ev, uint64 timeout_ms)
{
EnterCriticalSection(&ev->cs);
uint64 start = current_time_ms();
for (;;) {
if (ev->state)
break;
uint64 now = current_time_ms();
if (now - start > timeout_ms)
break;
SleepConditionVariableCS(&ev->cv, &ev->cs, timeout_ms - (now - start));
}
bool res = ev->state;
LeaveCriticalSection(&ev->cs);
return res;
}