-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathathr_thread.c
62 lines (54 loc) · 1.37 KB
/
athr_thread.c
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
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "athr_thread.h"
#if defined(ATHR_OS_WIN32)
#define WRAPPER_RETURN DWORD WINAPI
#define WRAPPER_ARG_T LPVOID
#elif defined(ATHR_OS_UNIX)
#define WRAPPER_RETURN void *
#define WRAPPER_ARG_T void *
#endif
static WRAPPER_RETURN __thr_wrapper(WRAPPER_ARG_T arg)
{
struct athr_thread *x = (struct athr_thread *)arg;
x->func(x->arg);
#if defined(ATHR_OS_WIN32)
ExitThread(0);
#elif defined(ATHR_OS_UNIX)
pthread_exit(0);
#endif
return 0;
}
int athr_thread_create(struct athr_thread *x, athr_thread_start *func, void *arg)
{
x->has_been_created = 0;
x->func = func;
x->arg = arg;
int rc = 0;
#if defined(ATHR_OS_WIN32)
x->handle = CreateThread(NULL, 0, __thr_wrapper, (LPVOID)x, 0, NULL);
rc = !x->handle;
#elif defined(ATHR_OS_UNIX)
rc = pthread_create(&x->handle, 0, __thr_wrapper, (void *)x);
#endif
x->has_been_created = !rc;
return rc;
}
void athr_thread_detach(struct athr_thread *x)
{
if (!x->has_been_created) return;
#if defined(ATHR_OS_WIN32)
CloseHandle(x->handle);
#elif defined(ATHR_OS_UNIX)
pthread_detach(x->handle);
#endif
}
int athr_thread_join(struct athr_thread *x)
{
#if defined(ATHR_OS_WIN32)
if (WaitForSingleObject(x, INFINITE) == WAIT_FAILED) return 1;
CloseHandle(x->handle);
return 0;
#elif defined(ATHR_OS_UNIX)
void *pres = NULL;
return pthread_join(x->handle, &pres);
#endif
}