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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/* -*-c-*- */
/**********************************************************************
thread_win32.c -
$Author$
$Date$
Copyright (C) 2004-2007 Koichi Sasada
**********************************************************************/
#ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
#include <process.h>
#define WIN32_WAIT_TIMEOUT 10 /* 10 ms */
#undef Sleep
#define native_thread_yield() Sleep(0)
#define remove_signal_thread_list(th)
static volatile DWORD ruby_native_thread_key = TLS_OUT_OF_INDEXES;
static rb_thread_t *
ruby_thread_from_native(void)
{
return TlsGetValue(ruby_native_thread_key);
}
static int
ruby_thread_set_native(rb_thread_t *th)
{
return TlsSetValue(ruby_native_thread_key, th);
}
static void
Init_native_thread(void)
{
rb_thread_t *th = GET_THREAD();
ruby_native_thread_key = TlsAlloc();
DuplicateHandle(GetCurrentProcess(),
GetCurrentThread(),
GetCurrentProcess(),
&th->thread_id, 0, FALSE, DUPLICATE_SAME_ACCESS);
th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
thread_debug("initial thread (th: %p, thid: %p, event: %p)\n",
th, GET_THREAD()->thread_id,
th->native_thread_data.interrupt_event);
}
static void
w32_error(void)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) & lpMsgBuf, 0, NULL);
rb_bug("%s", lpMsgBuf);
}
static void
w32_set_event(HANDLE handle)
{
if (SetEvent(handle) == 0) {
w32_error();
}
}
static void
w32_reset_event(HANDLE handle)
{
if (ResetEvent(handle) == 0) {
w32_error();
}
}
static int
w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th)
{
HANDLE *targets = events;
HANDLE intr;
DWORD ret;
thread_debug(" w32_wait_events events:%p, count:%d, timeout:%ld, th:%p\n",
events, count, timeout, th);
if (th && (intr = th->native_thread_data.interrupt_event)) {
w32_reset_event(intr);
if (th->interrupt_flag) {
w32_set_event(intr);
}
targets = ALLOCA_N(HANDLE, count + 1);
memcpy(targets, events, sizeof(HANDLE) * count);
targets[count++] = intr;
thread_debug(" * handle: %p (count: %d, intr)\n", intr, count);
}
thread_debug(" WaitForMultipleObjects start (count: %d)\n", count);
ret = WaitForMultipleObjects(count, targets, FALSE, timeout);
thread_debug(" WaitForMultipleObjects end (ret: %d)\n", ret);
if (ret == WAIT_OBJECT_0 + count - 1 && th) {
errno = EINTR;
}
if (ret == -1 && THREAD_DEBUG) {
int i;
DWORD dmy;
for (i = 0; i < count; i++) {
thread_debug(" * error handle %d - %s\n", i,
GetHandleInformation(targets[i], &dmy) ? "OK" : "NG");
}
}
return ret;
}
static void ubf_handle(void *ptr);
#define ubf_select ubf_handle
int
rb_w32_wait_events_blocking(HANDLE *events, int num, DWORD timeout)
{
return w32_wait_events(events, num, timeout, GET_THREAD());
}
int
rb_w32_wait_events(HANDLE *events, int num, DWORD timeout)
{
int ret;
BLOCKING_REGION(ret = rb_w32_wait_events_blocking(events, num, timeout),
ubf_handle, GET_THREAD());
return ret;
}
static void
w32_close_handle(HANDLE handle)
{
if (CloseHandle(handle) == 0) {
w32_error();
}
}
static void
w32_resume_thread(HANDLE handle)
{
if (ResumeThread(handle) == -1) {
w32_error();
}
}
#ifdef _MSC_VER
#define HAVE__BEGINTHREADEX 1
#else
#undef HAVE__BEGINTHREADEX
#endif
#ifdef HAVE__BEGINTHREADEX
#define start_thread (HANDLE)_beginthreadex
typedef unsigned long (_stdcall *w32_thread_start_func)(void*);
#else
#define start_thread CreateThread
typedef LPTHREAD_START_ROUTINE w32_thread_start_func;
#endif
static HANDLE
w32_create_thread(DWORD stack_size, w32_thread_start_func func, void *val)
{
return start_thread(0, stack_size, func, val, CREATE_SUSPENDED, 0);
}
int
rb_w32_sleep(unsigned long msec)
{
return w32_wait_events(0, 0, msec, GET_THREAD());
}
int WINAPI
rb_w32_Sleep(unsigned long msec)
{
int ret;
BLOCKING_REGION(ret = rb_w32_sleep(msec),
ubf_handle, GET_THREAD());
return ret;
}
static void
native_sleep(rb_thread_t *th, struct timeval *tv)
{
DWORD msec;
if (tv) {
msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
}
else {
msec = INFINITE;
}
GVL_UNLOCK_BEGIN();
{
DWORD ret;
int status = th->status;
th->status = THREAD_STOPPED;
th->unblock_function = ubf_handle;
th->unblock_function_arg = th;
thread_debug("native_sleep start (%d)\n", (int)msec);
ret = w32_wait_events(0, 0, msec, th);
thread_debug("native_sleep done (%d)\n", ret);
th->unblock_function = 0;
th->unblock_function_arg = 0;
th->status = status;
}
GVL_UNLOCK_END();
}
int
native_mutex_lock(rb_thread_lock_t *lock)
{
#if USE_WIN32_MUTEX
DWORD result;
while (1) {
thread_debug("native_mutex_lock: %p\n", *lock);
result = w32_wait_events(&*lock, 1, INFINITE, 0);
switch (result) {
case WAIT_OBJECT_0:
/* get mutex object */
thread_debug("acquire mutex: %p\n", *lock);
return 0;
case WAIT_OBJECT_0 + 1:
/* interrupt */
errno = EINTR;
thread_debug("acquire mutex interrupted: %p\n", *lock);
return 0;
case WAIT_TIMEOUT:
thread_debug("timeout mutex: %p\n", *lock);
break;
case WAIT_ABANDONED:
rb_bug("win32_mutex_lock: WAIT_ABANDONED");
break;
default:
rb_bug("win32_mutex_lock: unknown result (%d)", result);
break;
}
}
return 0;
#else
EnterCriticalSection(lock);
return 0;
#endif
}
int
native_mutex_unlock(rb_thread_lock_t *lock)
{
#if USE_WIN32_MUTEX
thread_debug("release mutex: %p\n", *lock);
return ReleaseMutex(*lock);
#else
LeaveCriticalSection(lock);
return 0;
#endif
}
int
native_mutex_trylock(rb_thread_lock_t *lock)
{
#if USE_WIN32_MUTEX
int result;
thread_debug("native_mutex_trylock: %p\n", *lock);
result = w32_wait_events(&*lock, 1, 1, 0);
thread_debug("native_mutex_trylock result: %d\n", result);
switch (result) {
case WAIT_OBJECT_0:
return 0;
case WAIT_TIMEOUT:
return EBUSY;
}
return EINVAL;
#else
return TryEnterCriticalSection(lock) == 0;
#endif
}
void
native_mutex_initialize(rb_thread_lock_t *lock)
{
#if USE_WIN32_MUTEX
*lock = CreateMutex(NULL, FALSE, NULL);
if (*lock == NULL) {
w32_error();
}
/* thread_debug("initialize mutex: %p\n", *lock); */
#else
InitializeCriticalSection(lock);
#endif
}
void
native_mutex_destroy(rb_thread_lock_t *lock)
{
#if USE_WIN32_MUTEX
w32_close_handle(lock);
#else
DeleteCriticalSection(lock);
#endif
}
struct cond_event_entry {
struct cond_event_entry* next;
HANDLE event;
};
struct rb_thread_cond_struct {
struct cond_event_entry *next;
struct cond_event_entry *last;
};
void
native_cond_signal(rb_thread_cond_t *cond)
{
/* cond is guarded by mutex */
struct cond_event_entry *e = cond->next;
if (e) {
cond->next = e->next;
SetEvent(e->event);
}
else {
rb_bug("native_cond_signal: no pending threads");
}
}
void
native_cond_broadcast(rb_thread_cond_t *cond)
{
/* cond is guarded by mutex */
struct cond_event_entry *e = cond->next;
cond->next = 0;
while (e) {
SetEvent(e->event);
e = e->next;
}
}
void
native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex)
{
DWORD r;
struct cond_event_entry entry;
entry.next = 0;
entry.event = CreateEvent(0, FALSE, FALSE, 0);
/* cond is guarded by mutex */
if (cond->next) {
cond->last->next = &entry;
cond->last = &entry;
}
else {
cond->next = &entry;
cond->last = &entry;
}
native_mutex_unlock(mutex);
{
r = WaitForSingleObject(entry.event, INFINITE);
if (r != WAIT_OBJECT_0) {
rb_bug("native_cond_wait: WaitForSingleObject returns %d", r);
}
}
native_mutex_lock(mutex);
w32_close_handle(entry.event);
}
void
native_cond_initialize(rb_thread_cond_t *cond)
{
cond->next = 0;
cond->last = 0;
}
void
native_cond_destroy(rb_thread_cond_t *cond)
{
/* */
}
static void
native_thread_destroy(rb_thread_t *th)
{
HANDLE intr = th->native_thread_data.interrupt_event;
thread_debug("close handle - intr: %p, thid: %p\n", intr, th->thread_id);
th->native_thread_data.interrupt_event = 0;
w32_close_handle(intr);
}
static unsigned long _stdcall
thread_start_func_1(void *th_ptr)
{
rb_thread_t *th = th_ptr;
VALUE stack_start;
volatile HANDLE thread_id = th->thread_id;
th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
/* run */
thread_debug("thread created (th: %p, thid: %p, event: %p)\n", th,
th->thread_id, th->native_thread_data.interrupt_event);
thread_start_func_2(th, &stack_start, 0);
w32_close_handle(thread_id);
thread_debug("thread deleted (th: %p)\n", th);
return 0;
}
extern size_t rb_gc_stack_maxsize;
static int
native_thread_create(rb_thread_t *th)
{
size_t stack_size = 4 * 1024; /* 4KB */
th->thread_id = w32_create_thread(stack_size, thread_start_func_1, th);
th->machine_stack_maxsize = rb_gc_stack_maxsize; /* not tested. */
if ((th->thread_id) == 0) {
st_delete_wrap(th->vm->living_threads, th->self);
rb_raise(rb_eThreadError, "can't create Thread (%d)", errno);
}
w32_resume_thread(th->thread_id);
if (THREAD_DEBUG) {
Sleep(0);
thread_debug("create: (th: %p, thid: %p, intr: %p), stack size: %d\n",
th, th->thread_id,
th->native_thread_data.interrupt_event, stack_size);
}
return 0;
}
static void
native_thread_join(HANDLE th)
{
w32_wait_events(&th, 1, 0, 0);
}
static void
native_thread_apply_priority(rb_thread_t *th)
{
int priority = th->priority;
if (th->priority > 0) {
priority = THREAD_PRIORITY_ABOVE_NORMAL;
}
else if (th->priority < 0) {
priority = THREAD_PRIORITY_BELOW_NORMAL;
}
else {
priority = THREAD_PRIORITY_NORMAL;
}
SetThreadPriority(th->thread_id, priority);
}
static void
ubf_handle(void *ptr)
{
rb_thread_t *th = (rb_thread_t *)ptr;
thread_debug("ubf_handle: %p\n", th);
w32_set_event(th->native_thread_data.interrupt_event);
}
static void timer_thread_function(void);
static HANDLE timer_thread_id = 0;
static unsigned long _stdcall
timer_thread_func(void *dummy)
{
thread_debug("timer_thread\n");
while (system_working) {
Sleep(WIN32_WAIT_TIMEOUT);
timer_thread_function();
}
thread_debug("timer killed\n");
return 0;
}
void
rb_thread_create_timer_thread(void)
{
if (timer_thread_id == 0) {
timer_thread_id = w32_create_thread(1024, timer_thread_func, 0);
w32_resume_thread(timer_thread_id);
}
}
#endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
|