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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
/* */
/* Copyright 2009 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
/* Win32 implementation of the "st" interface */
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <winerror.h>
#include <stdio.h>
#include <signal.h>
#include <caml/osdeps.h>
#define INLINE __inline
#if 1
#define TRACE(x)
#define TRACE1(x,y)
#else
#include <stdio.h>
#define TRACE(x) printf("%d: %s\n", GetCurrentThreadId(), x); fflush(stdout)
#define TRACE1(x,y) printf("%d: %s %p\n", GetCurrentThreadId(), x, (void *)y); \
fflush(stdout)
#endif
typedef DWORD st_retcode;
#define SIGPREEMPTION SIGTERM
/* Thread-local storage associating a Win32 event to every thread. */
static DWORD st_thread_sem_key;
/* OS-specific initialization */
static DWORD st_initialize(void)
{
st_thread_sem_key = TlsAlloc();
if (st_thread_sem_key == TLS_OUT_OF_INDEXES)
return GetLastError();
else
return 0;
}
/* Thread creation. Created in detached mode if [res] is NULL. */
typedef HANDLE st_thread_id;
static DWORD st_thread_create(st_thread_id * res,
LPTHREAD_START_ROUTINE fn, void * arg)
{
HANDLE h = CreateThread(NULL, 0, fn, arg, 0, NULL);
TRACE1("st_thread_create", h);
if (h == NULL) return GetLastError();
if (res == NULL)
CloseHandle(h);
else
*res = h;
return 0;
}
#define ST_THREAD_FUNCTION DWORD WINAPI
/* Cleanup at thread exit */
static void st_thread_cleanup(void)
{
HANDLE ev = (HANDLE) TlsGetValue(st_thread_sem_key);
if (ev != NULL) CloseHandle(ev);
}
/* Thread termination */
CAMLnoreturn_start
static void st_thread_exit(void)
CAMLnoreturn_end;
static void st_thread_exit(void)
{
TRACE("st_thread_exit");
ExitThread(0);
}
static void st_thread_join(st_thread_id thr)
{
TRACE1("st_thread_join", h);
WaitForSingleObject(thr, INFINITE);
}
/* Thread-specific state */
typedef DWORD st_tlskey;
static DWORD st_tls_newkey(st_tlskey * res)
{
*res = TlsAlloc();
if (*res == TLS_OUT_OF_INDEXES)
return GetLastError();
else
return 0;
}
static INLINE void * st_tls_get(st_tlskey k)
{
return TlsGetValue(k);
}
static INLINE void st_tls_set(st_tlskey k, void * v)
{
TlsSetValue(k, v);
}
/* The master lock. */
typedef CRITICAL_SECTION st_masterlock;
static void st_masterlock_init(st_masterlock * m)
{
TRACE("st_masterlock_init");
InitializeCriticalSection(m);
EnterCriticalSection(m);
}
static INLINE void st_masterlock_acquire(st_masterlock * m)
{
TRACE("st_masterlock_acquire");
EnterCriticalSection(m);
TRACE("st_masterlock_acquire (done)");
}
static INLINE void st_masterlock_release(st_masterlock * m)
{
LeaveCriticalSection(m);
TRACE("st_masterlock_released");
}
static INLINE int st_masterlock_waiters(st_masterlock * m)
{
return 1; /* info not maintained */
}
/* Scheduling hints */
static INLINE void st_thread_yield(st_masterlock * m)
{
LeaveCriticalSection(m);
Sleep(0);
EnterCriticalSection(m);
}
/* Mutexes */
typedef CRITICAL_SECTION * st_mutex;
static DWORD st_mutex_create(st_mutex * res)
{
st_mutex m = caml_stat_alloc_noexc(sizeof(CRITICAL_SECTION));
if (m == NULL) return ERROR_NOT_ENOUGH_MEMORY;
InitializeCriticalSection(m);
*res = m;
return 0;
}
static DWORD st_mutex_destroy(st_mutex m)
{
DeleteCriticalSection(m);
caml_stat_free(m);
return 0;
}
static INLINE DWORD st_mutex_lock(st_mutex m)
{
TRACE1("st_mutex_lock", m);
EnterCriticalSection(m);
TRACE1("st_mutex_lock (done)", m);
return 0;
}
/* Error codes with the 29th bit set are reserved for the application */
#define PREVIOUSLY_UNLOCKED 0
#define ALREADY_LOCKED (1<<29)
static INLINE DWORD st_mutex_trylock(st_mutex m)
{
TRACE1("st_mutex_trylock", m);
if (TryEnterCriticalSection(m)) {
TRACE1("st_mutex_trylock (success)", m);
return PREVIOUSLY_UNLOCKED;
} else {
TRACE1("st_mutex_trylock (failure)", m);
return ALREADY_LOCKED;
}
}
static INLINE DWORD st_mutex_unlock(st_mutex m)
{
TRACE1("st_mutex_unlock", m);
LeaveCriticalSection(m);
return 0;
}
/* Condition variables */
/* A condition variable is just a list of threads currently
waiting on this c.v. Each thread is represented by its
associated event. */
struct st_wait_list {
HANDLE event; /* event of the first waiting thread */
struct st_wait_list * next;
};
typedef struct st_condvar_struct {
CRITICAL_SECTION lock; /* protect the data structure */
struct st_wait_list * waiters; /* list of threads waiting */
} * st_condvar;
static DWORD st_condvar_create(st_condvar * res)
{
st_condvar c = caml_stat_alloc_noexc(sizeof(struct st_condvar_struct));
if (c == NULL) return ERROR_NOT_ENOUGH_MEMORY;
InitializeCriticalSection(&c->lock);
c->waiters = NULL;
*res = c;
return 0;
}
static DWORD st_condvar_destroy(st_condvar c)
{
TRACE1("st_condvar_destroy", c);
DeleteCriticalSection(&c->lock);
caml_stat_free(c);
return 0;
}
static DWORD st_condvar_signal(st_condvar c)
{
DWORD rc = 0;
struct st_wait_list * curr, * next;
TRACE1("st_condvar_signal", c);
EnterCriticalSection(&c->lock);
curr = c->waiters;
if (curr != NULL) {
next = curr->next;
/* Wake up the first waiting thread */
TRACE1("st_condvar_signal: waking up", curr->event);
if (! SetEvent(curr->event)) rc = GetLastError();
/* Remove it from the waiting list */
c->waiters = next;
}
LeaveCriticalSection(&c->lock);
return rc;
}
static DWORD st_condvar_broadcast(st_condvar c)
{
DWORD rc = 0;
struct st_wait_list * curr, * next;
TRACE1("st_condvar_broadcast", c);
EnterCriticalSection(&c->lock);
/* Wake up all waiting threads */
curr = c->waiters;
while (curr != NULL) {
next = curr->next;
TRACE1("st_condvar_signal: waking up", curr->event);
if (! SetEvent(curr->event)) rc = GetLastError();
curr = next;
}
/* Remove them all from the waiting list */
c->waiters = NULL;
LeaveCriticalSection(&c->lock);
return rc;
}
static DWORD st_condvar_wait(st_condvar c, st_mutex m)
{
HANDLE ev;
struct st_wait_list wait;
TRACE1("st_condvar_wait", c);
/* Recover (or create) the event associated with the calling thread */
ev = (HANDLE) TlsGetValue(st_thread_sem_key);
if (ev == 0) {
ev = CreateEvent(NULL,
FALSE /*auto reset*/,
FALSE /*initially unset*/,
NULL);
if (ev == NULL) return GetLastError();
TlsSetValue(st_thread_sem_key, (void *) ev);
}
EnterCriticalSection(&c->lock);
/* Insert the current thread in the waiting list (atomically) */
wait.event = ev;
wait.next = c->waiters;
c->waiters = &wait;
LeaveCriticalSection(&c->lock);
/* Release the mutex m */
LeaveCriticalSection(m);
/* Wait for our event to be signaled. There is no risk of lost
wakeup, since we inserted ourselves on the waiting list of c
before releasing m */
TRACE1("st_condvar_wait: blocking on event", ev);
if (WaitForSingleObject(ev, INFINITE) == WAIT_FAILED)
return GetLastError();
/* Reacquire the mutex m */
TRACE1("st_condvar_wait: restarted, acquiring mutex", m);
EnterCriticalSection(m);
TRACE1("st_condvar_wait: acquired mutex", m);
return 0;
}
/* Triggered events */
typedef HANDLE st_event;
static DWORD st_event_create(st_event * res)
{
st_event m =
CreateEvent(NULL, TRUE/*manual reset*/, FALSE/*initially unset*/, NULL);
TRACE1("st_event_create", m);
if (m == NULL) return GetLastError();
*res = m;
return 0;
}
static DWORD st_event_destroy(st_event e)
{
TRACE1("st_event_destroy", e);
if (CloseHandle(e))
return 0;
else
return GetLastError();
}
static DWORD st_event_trigger(st_event e)
{
TRACE1("st_event_trigger", e);
if (SetEvent(e))
return 0;
else
return GetLastError();
}
static DWORD st_event_wait(st_event e)
{
TRACE1("st_event_wait", e);
if (WaitForSingleObject(e, INFINITE) == WAIT_FAILED)
return GetLastError();
else
return 0;
}
/* Reporting errors */
static void st_check_error(DWORD retcode, char * msg)
{
wchar_t err[1024];
int errlen, msglen, ret;
value str;
if (retcode == 0) return;
if (retcode == ERROR_NOT_ENOUGH_MEMORY) caml_raise_out_of_memory();
ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
retcode,
0,
err,
sizeof(err)/sizeof(wchar_t),
NULL);
if (! ret) {
ret =
swprintf(err, sizeof(err)/sizeof(wchar_t), L"error code %lx", retcode);
}
msglen = strlen(msg);
errlen = win_wide_char_to_multi_byte(err, ret, NULL, 0);
str = caml_alloc_string(msglen + 2 + errlen);
memmove (&Byte(str, 0), msg, msglen);
memmove (&Byte(str, msglen), ": ", 2);
win_wide_char_to_multi_byte(err, ret, &Byte(str, msglen + 2), errlen);
caml_raise_sys_error(str);
}
/* Variable used to stop the "tick" thread */
static volatile int caml_tick_thread_stop = 0;
/* The tick thread: posts a SIGPREEMPTION signal periodically */
static DWORD WINAPI caml_thread_tick(void * arg)
{
while(! caml_tick_thread_stop) {
Sleep(Thread_timeout);
/* The preemption signal should never cause a callback, so don't
go through caml_handle_signal(), just record signal delivery via
caml_record_signal(). */
caml_record_signal(SIGPREEMPTION);
}
return 0;
}
/* "At fork" processing -- none under Win32 */
static DWORD st_atfork(void (*fn)(void))
{
return 0;
}
/* Signal handling -- none under Win32 */
value caml_thread_sigmask(value cmd, value sigs) /* ML */
{
caml_invalid_argument("Thread.sigmask not implemented");
return Val_int(0); /* not reached */
}
value caml_wait_signal(value sigs) /* ML */
{
caml_invalid_argument("Thread.wait_signal not implemented");
return Val_int(0); /* not reached */
}
|