2
/**********************************************************************
7
$Date: 2007-12-25 13:35:17 +0900 (Tue, 25 Dec 2007) $
9
Copyright (C) 2004-2007 Koichi Sasada
11
**********************************************************************/
13
#ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
17
#define WIN32_WAIT_TIMEOUT 10 /* 10 ms */
20
#define native_thread_yield() Sleep(0)
21
#define remove_signal_thread_list(th)
23
static volatile DWORD ruby_native_thread_key = TLS_OUT_OF_INDEXES;
25
static int native_mutex_lock(rb_thread_lock_t *);
26
static int native_mutex_unlock(rb_thread_lock_t *);
27
static int native_mutex_trylock(rb_thread_lock_t *);
28
static void native_mutex_initialize(rb_thread_lock_t *);
30
static void native_cond_signal(rb_thread_cond_t *cond);
31
static void native_cond_broadcast(rb_thread_cond_t *cond);
32
static void native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex);
33
static void native_cond_initialize(rb_thread_cond_t *cond);
34
static void native_cond_destroy(rb_thread_cond_t *cond);
37
ruby_thread_from_native(void)
39
return TlsGetValue(ruby_native_thread_key);
43
ruby_thread_set_native(rb_thread_t *th)
45
return TlsSetValue(ruby_native_thread_key, th);
49
Init_native_thread(void)
51
rb_thread_t *th = GET_THREAD();
53
ruby_native_thread_key = TlsAlloc();
54
DuplicateHandle(GetCurrentProcess(),
57
&th->thread_id, 0, FALSE, DUPLICATE_SAME_ACCESS);
59
th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
61
thread_debug("initial thread (th: %p, thid: %p, event: %p)\n",
62
th, GET_THREAD()->thread_id,
63
th->native_thread_data.interrupt_event);
70
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
71
FORMAT_MESSAGE_FROM_SYSTEM |
72
FORMAT_MESSAGE_IGNORE_INSERTS,
75
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
76
(LPTSTR) & lpMsgBuf, 0, NULL);
77
rb_bug("%s", lpMsgBuf);
81
w32_set_event(HANDLE handle)
83
if (SetEvent(handle) == 0) {
89
w32_reset_event(HANDLE handle)
91
if (ResetEvent(handle) == 0) {
97
w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th)
99
HANDLE *targets = events;
103
thread_debug(" w32_wait_events events:%p, count:%d, timeout:%ld, th:%p\n",
104
events, count, timeout, th);
105
if (th && (intr = th->native_thread_data.interrupt_event)) {
106
w32_reset_event(intr);
107
if (th->interrupt_flag) {
111
targets = ALLOCA_N(HANDLE, count + 1);
112
memcpy(targets, events, sizeof(HANDLE) * count);
114
targets[count++] = intr;
115
thread_debug(" * handle: %p (count: %d, intr)\n", intr, count);
118
thread_debug(" WaitForMultipleObjects start (count: %d)\n", count);
119
ret = WaitForMultipleObjects(count, targets, FALSE, timeout);
120
thread_debug(" WaitForMultipleObjects end (ret: %d)\n", ret);
122
if (ret == WAIT_OBJECT_0 + count - 1 && th) {
125
if (ret == -1 && THREAD_DEBUG) {
128
for (i = 0; i < count; i++) {
129
thread_debug(" * error handle %d - %s\n", i,
130
GetHandleInformation(targets[i], &dmy) ? "OK" : "NG");
136
static void ubf_handle(void *ptr);
137
#define ubf_select ubf_handle
140
rb_w32_wait_events_blocking(HANDLE *events, int num, DWORD timeout)
142
return w32_wait_events(events, num, timeout, GET_THREAD());
146
rb_w32_wait_events(HANDLE *events, int num, DWORD timeout)
150
BLOCKING_REGION(ret = rb_w32_wait_events_blocking(events, num, timeout),
151
ubf_handle, GET_THREAD());
156
w32_close_handle(HANDLE handle)
158
if (CloseHandle(handle) == 0) {
164
w32_resume_thread(HANDLE handle)
166
if (ResumeThread(handle) == -1) {
172
#define HAVE__BEGINTHREADEX 1
174
#undef HAVE__BEGINTHREADEX
177
#ifdef HAVE__BEGINTHREADEX
178
#define start_thread (HANDLE)_beginthreadex
179
typedef unsigned long (_stdcall *w32_thread_start_func)(void*);
181
#define start_thread CreateThread
182
typedef LPTHREAD_START_ROUTINE w32_thread_start_func;
186
w32_create_thread(DWORD stack_size, w32_thread_start_func func, void *val)
188
return start_thread(0, stack_size, func, val, CREATE_SUSPENDED, 0);
192
rb_w32_sleep(unsigned long msec)
194
return w32_wait_events(0, 0, msec, GET_THREAD());
198
rb_w32_Sleep(unsigned long msec)
202
BLOCKING_REGION(ret = rb_w32_sleep(msec),
203
ubf_handle, GET_THREAD());
208
native_sleep(rb_thread_t *th, struct timeval *tv)
212
msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
221
int status = th->status;
223
th->status = THREAD_STOPPED;
224
th->unblock_function = ubf_handle;
225
th->unblock_function_arg = th;
227
if (RUBY_VM_INTERRUPTED(th)) {
228
/* interrupted. return immediate */
231
thread_debug("native_sleep start (%d)\n", (int)msec);
232
ret = w32_wait_events(0, 0, msec, th);
233
thread_debug("native_sleep done (%d)\n", ret);
236
th->unblock_function = 0;
237
th->unblock_function_arg = 0;
241
RUBY_VM_CHECK_INTS();
245
native_mutex_lock(rb_thread_lock_t *lock)
250
thread_debug("native_mutex_lock: %p\n", *lock);
251
result = w32_wait_events(&*lock, 1, INFINITE, 0);
254
/* get mutex object */
255
thread_debug("acquire mutex: %p\n", *lock);
257
case WAIT_OBJECT_0 + 1:
260
thread_debug("acquire mutex interrupted: %p\n", *lock);
263
thread_debug("timeout mutex: %p\n", *lock);
266
rb_bug("win32_mutex_lock: WAIT_ABANDONED");
269
rb_bug("win32_mutex_lock: unknown result (%d)", result);
275
EnterCriticalSection(lock);
281
native_mutex_unlock(rb_thread_lock_t *lock)
284
thread_debug("release mutex: %p\n", *lock);
285
return ReleaseMutex(*lock);
287
LeaveCriticalSection(lock);
293
native_mutex_trylock(rb_thread_lock_t *lock)
297
thread_debug("native_mutex_trylock: %p\n", *lock);
298
result = w32_wait_events(&*lock, 1, 1, 0);
299
thread_debug("native_mutex_trylock result: %d\n", result);
308
return TryEnterCriticalSection(lock) == 0;
313
native_mutex_initialize(rb_thread_lock_t *lock)
316
*lock = CreateMutex(NULL, FALSE, NULL);
320
/* thread_debug("initialize mutex: %p\n", *lock); */
322
InitializeCriticalSection(lock);
327
native_mutex_destroy(rb_thread_lock_t *lock)
330
w32_close_handle(lock);
332
DeleteCriticalSection(lock);
336
struct cond_event_entry {
337
struct cond_event_entry* next;
341
struct rb_thread_cond_struct {
342
struct cond_event_entry *next;
343
struct cond_event_entry *last;
347
native_cond_signal(rb_thread_cond_t *cond)
349
/* cond is guarded by mutex */
350
struct cond_event_entry *e = cond->next;
353
cond->next = e->next;
357
rb_bug("native_cond_signal: no pending threads");
362
native_cond_broadcast(rb_thread_cond_t *cond)
364
/* cond is guarded by mutex */
365
struct cond_event_entry *e = cond->next;
375
native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex)
378
struct cond_event_entry entry;
381
entry.event = CreateEvent(0, FALSE, FALSE, 0);
383
/* cond is guarded by mutex */
385
cond->last->next = &entry;
393
native_mutex_unlock(mutex);
395
r = WaitForSingleObject(entry.event, INFINITE);
396
if (r != WAIT_OBJECT_0) {
397
rb_bug("native_cond_wait: WaitForSingleObject returns %d", r);
400
native_mutex_lock(mutex);
402
w32_close_handle(entry.event);
406
native_cond_initialize(rb_thread_cond_t *cond)
413
native_cond_destroy(rb_thread_cond_t *cond)
419
native_thread_destroy(rb_thread_t *th)
421
HANDLE intr = th->native_thread_data.interrupt_event;
422
thread_debug("close handle - intr: %p, thid: %p\n", intr, th->thread_id);
423
th->native_thread_data.interrupt_event = 0;
424
w32_close_handle(intr);
427
static unsigned long _stdcall
428
thread_start_func_1(void *th_ptr)
430
rb_thread_t *th = th_ptr;
432
volatile HANDLE thread_id = th->thread_id;
434
th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
437
thread_debug("thread created (th: %p, thid: %p, event: %p)\n", th,
438
th->thread_id, th->native_thread_data.interrupt_event);
439
thread_start_func_2(th, &stack_start, 0);
441
w32_close_handle(thread_id);
442
thread_debug("thread deleted (th: %p)\n", th);
446
extern size_t rb_gc_stack_maxsize;
449
native_thread_create(rb_thread_t *th)
451
size_t stack_size = 4 * 1024; /* 4KB */
452
th->thread_id = w32_create_thread(stack_size, thread_start_func_1, th);
454
th->machine_stack_maxsize = rb_gc_stack_maxsize; /* not tested. */
456
if ((th->thread_id) == 0) {
457
st_delete_wrap(th->vm->living_threads, th->self);
458
rb_raise(rb_eThreadError, "can't create Thread (%d)", errno);
461
w32_resume_thread(th->thread_id);
465
thread_debug("create: (th: %p, thid: %p, intr: %p), stack size: %d\n",
467
th->native_thread_data.interrupt_event, stack_size);
473
native_thread_join(HANDLE th)
475
w32_wait_events(&th, 1, 0, 0);
479
native_thread_apply_priority(rb_thread_t *th)
481
int priority = th->priority;
482
if (th->priority > 0) {
483
priority = THREAD_PRIORITY_ABOVE_NORMAL;
485
else if (th->priority < 0) {
486
priority = THREAD_PRIORITY_BELOW_NORMAL;
489
priority = THREAD_PRIORITY_NORMAL;
492
SetThreadPriority(th->thread_id, priority);
496
ubf_handle(void *ptr)
498
rb_thread_t *th = (rb_thread_t *)ptr;
499
thread_debug("ubf_handle: %p\n", th);
500
w32_set_event(th->native_thread_data.interrupt_event);
503
static void timer_thread_function(void);
505
static HANDLE timer_thread_id = 0;
507
static unsigned long _stdcall
508
timer_thread_func(void *dummy)
510
thread_debug("timer_thread\n");
511
while (system_working) {
512
Sleep(WIN32_WAIT_TIMEOUT);
513
timer_thread_function();
515
thread_debug("timer killed\n");
520
rb_thread_create_timer_thread(void)
522
if (timer_thread_id == 0) {
523
timer_thread_id = w32_create_thread(1024, timer_thread_func, 0);
524
w32_resume_thread(timer_thread_id);
528
#endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */