-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy paththread.c
138 lines (109 loc) · 2.64 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "TeensyControls.h"
//http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
#if defined(MACOSX) || defined(LINUX)
typedef struct {
void (*function)(void*);
void *arg;
} threadin_t;
static void * launcher(void *in)
{
void (*function)(void*);
void *arg;
function = ((threadin_t *)in)->function;
arg = ((threadin_t *)in)->arg;
free(in);
function(arg);
pthread_exit(NULL);
return NULL;
}
int thread_start(void (*function)(void*), void *arg)
{
pthread_t pth;
int r, retry;
threadin_t *in;
in = (threadin_t *)malloc(sizeof(threadin_t));
if (!in) return 0;
in->function = function;
in->arg = arg;
for (retry=0; retry<5; retry++) {
r = pthread_create(&pth, NULL, launcher, in);
if (r == 0) {
pthread_detach(pth);
return 1;
}
usleep(1000);
}
return 0;
}
#elif defined(WINDOWS) || defined(WINDOWS64)
typedef struct {
void (*function)(void*);
void *arg;
} threadin_t;
WINAPI DWORD launcher(void *in)
{
void (*function)(void*);
void *arg;
function = ((threadin_t *)in)->function;
arg = ((threadin_t *)in)->arg;
free(in);
function(arg);
//pthread_exit(NULL); // TODO: windows thread exit function?
return 0;
}
int thread_start(void (*function)(void*), void *arg)
{
HANDLE th;
threadin_t *in;
in = (threadin_t *)malloc(sizeof(threadin_t));
if (!in) return 0;
in->function = function;
in->arg = arg;
// TODO: should we use _beginthreadex instead?
th = CreateThread(NULL, 0, launcher, in, 0, NULL);
if (th == NULL) return 0;
return 1;
}
// From http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
// pthread broadcast feature removed - only signal is possible
// "_timeout" not quite POSIX...
int pthread_cond_wait_timeout(pthread_cond_t *cv, pthread_mutex_t *external_mutex, DWORD msec)
{
DWORD result;
EnterCriticalSection (&cv->lock);
cv->count++;
LeaveCriticalSection (&cv->lock);
LeaveCriticalSection (external_mutex);
result = WaitForSingleObject(cv->event, msec);
EnterCriticalSection(&cv->lock);
cv->count--;
LeaveCriticalSection(&cv->lock);
EnterCriticalSection(external_mutex);
return 0;
}
int pthread_cond_signal(pthread_cond_t *cv)
{
int have_waiters=0;
EnterCriticalSection (&cv->lock);
if (cv->count > 0) have_waiters = 1;
LeaveCriticalSection (&cv->lock);
if (have_waiters) SetEvent(cv->event);
return 0;
}
int pthread_cond_init(pthread_cond_t *cv, const void *attr)
{
cv->count = 0;
InitializeCriticalSection(&cv->lock);
cv->event = CreateEvent (NULL, // no security
FALSE, // auto-reset event
FALSE, // non-signaled initially
NULL); // unnamed
return 0;
}
int pthread_cond_destroy(pthread_cond_t *cond)
{
DeleteCriticalSection(&cond->lock);
CloseHandle(cond->event);
return 0;
}
#endif