-
Notifications
You must be signed in to change notification settings - Fork 1
/
win32.c
294 lines (240 loc) · 5.39 KB
/
win32.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright (c) 2001-2005 OmniTI, Inc. All rights reserved
*
* Licensed to the Backhand project to be distributed under the term of
* the Wackamole license.
*/
#include "config.h"
#undef read
#undef write
#undef close
#ifndef DONT_USE_THREADS
/* cancellation is not 100% implemented; it's dangerous in any case,
* and there is no need to run threaded in wackamole unless you're
* embedding perl... which we don't support on win32 right now. */
struct pthread_thread_t {
struct pthread_thread_t *next, *prev;
int refcount;
int joinable;
void *retval;
pthread_t thread_id;
HANDLE kill_me;
HANDLE my_handle;
};
static CRITICAL_SECTION thread_mutex;
static struct pthread_thread_t *threads = NULL;
/* pthread_cancel is evil */
int pthread_setcanceltype(int type, int *oldtype)
{
if (type != PTHREAD_CANCEL_DEFERRED) {
return -1;
}
if (oldtype) *oldtype = PTHREAD_CANCEL_DEFERRED;
return 0;
}
struct __win32_pthread_create_param {
void *(*start_routine)(void *);
void *arg;
struct pthread_thread_t *t;
};
static struct pthread_thread_t *find_thread(pthread_t id)
{
struct pthread_thread_t *thr;
EnterCriticalSection(&thread_mutex);
for (thr = threads; thr; thr = thr->next) {
if (thr->thread_id == id) {
InterlockedIncrement(&thr->refcount);
LeaveCriticalSection(&thread_mutex);
return thr;
}
}
LeaveCriticalSection(&thread_mutex);
return NULL;
}
static void delref_thread(struct pthread_thread_t *t)
{
EnterCriticalSection(&thread_mutex);
if (InterlockedDecrement(&t->refcount)) {
LeaveCriticalSection(&thread_mutex);
return;
}
if (t->next)
t->next->prev = t->prev;
if (t->prev)
t->prev->next = t->next;
else
threads = t->next;
LeaveCriticalSection(&thread_mutex);
CloseHandle(t->kill_me);
CloseHandle(t->my_handle);
free(t);
}
void pthread_exit(void *retval)
{
struct pthread_thread_t *t;
t = find_thread(GetCurrentThreadId());
t->retval = retval;
delref_thread(t);
ExitThread((DWORD)retval);
}
int pthread_cancel(pthread_t threadid)
{
struct pthread_thread_t *t;
t = find_thread(threadid);
if (t) {
SetEvent(t->kill_me);
delref_thread(t);
return 0;
}
return -1;
}
int pthread_attr_destroy(pthread_attr_t *attr)
{
return 0;
}
static DWORD WINAPI __win32_pthread_create_bridge(LPVOID param)
{
struct __win32_pthread_create_param p = *(struct __win32_pthread_create_param*)param;
free(param);
pthread_exit(p.start_routine(p.arg));
/* not reached */
return 0;
}
int pthread_create(pthread_t *thread, pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg)
{
struct __win32_pthread_create_param *p = malloc(sizeof(*p));
HANDLE thr;
struct pthread_thread_t *the_thread = calloc(1, sizeof(*p));
the_thread->refcount = attr->detached ? 1 : 2;
the_thread->kill_me = CreateEvent(NULL, TRUE, FALSE, NULL);
if (threads == NULL) {
InitializeCriticalSection(&thread_mutex);
threads = the_thread;
} else {
EnterCriticalSection(&thread_mutex);
the_thread->next = threads;
if (threads)
threads->prev = the_thread;
LeaveCriticalSection(&thread_mutex);
}
p->start_routine = start_routine;
p->arg = arg;
the_thread->my_handle = CreateThread(NULL, 0, __win32_pthread_create_bridge, p, 0, &the_thread->thread_id);
if (the_thread->my_handle) {
*thread = the_thread->thread_id;
return 0;
}
the_thread->refcount = 1;
delref_thread(the_thread);
free(p);
return -1;
}
int pthread_attr_init(pthread_attr_t *attr)
{
memset(attr, 0, sizeof(*attr));
return 0;
}
int pthread_attr_setdetachstate(pthread_attr_t *attr, int state)
{
attr->detached = state;
return 0;
}
int pthread_mutex_init(pthread_mutex_t *mutex, void *attr)
{
InitializeCriticalSection(mutex);
return 0;
}
int pthread_mutex_lock(pthread_mutex_t *mutex)
{
EnterCriticalSection(mutex);
return 0;
}
int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
LeaveCriticalSection(mutex);
return 0;
}
int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
DeleteCriticalSection(mutex);
return 0;
}
#endif
/* usleep. This is a pthread cancellation point */
int usleep(unsigned long usec)
{
int msec = usec / 1000;
#ifndef DONT_USE_THREADS
struct pthread_thread_t *t;
t = find_thread(GetCurrentThreadId());
if (t) {
if (WAIT_OBJECT_0 == WaitForSingleObjectEx(t->kill_me, msec, 0)) {
pthread_exit(NULL);
/* not reached */
return -1;
}
return 0;
}
#endif
Sleep(msec);
return 0;
}
static void update_errno_from_win(DWORD code)
{
switch (code) {
case WSAEWOULDBLOCK: errno = EAGAIN; break;
default:
errno = code;
}
}
int wackamole_win32_write(int fd, const void *buf, int len)
{
if (_get_osfhandle(fd) == 0xffffffff) {
int r;
r = send(fd, buf, len, 0);
if (r == SOCKET_ERROR) {
update_errno_from_win(WSAGetLastError());
return -1;
}
return r;
}
return write(fd, buf, len);
}
int wackamole_win32_read(int fd, void *buf, int len)
{
if (_get_osfhandle(fd) == 0xffffffff) {
int r;
r = recv(fd, buf, len, 0);
if (r == SOCKET_ERROR) {
update_errno_from_win(WSAGetLastError());
return -1;
}
return r;
}
return read(fd, buf, len);
}
int wackamole_win32_close(int fd)
{
if (_get_osfhandle(fd) == 0xffffffff) {
return closesocket(fd);
} else {
return close(fd);
}
}
void *dlopen(const char *name, int flags)
{
return LoadLibrary(name);
}
void *dlsym(void *handle, const char *symbol)
{
return GetProcAddress(handle, symbol);
}
void dlclose(void *handle)
{
FreeLibrary(handle);
}
const char *dlerror(void)
{
return "Unknown Error";
}