-
Notifications
You must be signed in to change notification settings - Fork 39
/
win32-port.h
49 lines (43 loc) · 1.12 KB
/
win32-port.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#if !defined WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h>
#include <process.h>
#include <assert.h>
typedef struct {
void *(*pthread_routine)(void *);
void *pthread_arg;
HANDLE handle;
} pthread_t;
static unsigned __stdcall win32_start_routine(void *arg) {
pthread_t *p = (pthread_t *)arg;
p->pthread_routine(p->pthread_arg);
return 0;
}
static int pthread_create(pthread_t *id, void *attr,
void *(*start_routine)(void *), void *arg) {
assert(attr == 0);
id->pthread_routine = start_routine;
id->pthread_arg = arg;
id->handle =
(HANDLE)_beginthreadex(0, 0, win32_start_routine, (void *)id, 0, 0);
if (id->handle != 0) return 0;
return -1;
}
static int pthread_join(pthread_t thread, void **retval) {
WaitForSingleObject(thread.handle, INFINITE);
if (retval) {
*retval = 0;
}
return 0;
}
static void pthread_exit(void *p) { _endthreadex(0); }
static int posix_memalign(void **memptr, size_t alignment, size_t size) {
assert(memptr);
*memptr = _aligned_malloc(size, alignment);
if (*memptr) {
return 0;
} else {
return -1;
}
}