2 /**********************************************************************
8 Copyright (C) 2004-2007 Koichi Sasada
10 **********************************************************************/
12 #ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
16 #define WIN32_WAIT_TIMEOUT 10 /* 10 ms */
19 #define native_thread_yield() Sleep(0)
20 #define remove_signal_thread_list(th)
22 static volatile DWORD ruby_native_thread_key = TLS_OUT_OF_INDEXES;
24 static int native_mutex_lock(rb_thread_lock_t *);
25 static int native_mutex_unlock(rb_thread_lock_t *);
26 static int native_mutex_trylock(rb_thread_lock_t *);
27 static void native_mutex_initialize(rb_thread_lock_t *);
29 static void native_cond_signal(rb_thread_cond_t *cond);
30 static void native_cond_broadcast(rb_thread_cond_t *cond);
31 static void native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex);
32 static void native_cond_initialize(rb_thread_cond_t *cond);
33 static void native_cond_destroy(rb_thread_cond_t *cond);
36 ruby_thread_from_native(void)
38 return TlsGetValue(ruby_native_thread_key);
42 ruby_thread_set_native(rb_thread_t *th)
44 return TlsSetValue(ruby_native_thread_key, th);
48 Init_native_thread(void)
50 rb_thread_t *th = GET_THREAD();
52 ruby_native_thread_key = TlsAlloc();
53 ruby_thread_set_native(th);
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", (char*)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 (RUBY_VM_INTERRUPTED(th)) {
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: %lu)\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)
213 msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
223 native_mutex_lock(&th->interrupt_lock);
224 th->unblock.func = ubf_handle;
225 th->unblock.arg = th;
226 native_mutex_unlock(&th->interrupt_lock);
228 if (RUBY_VM_INTERRUPTED(th)) {
229 /* interrupted. return immediate */
232 thread_debug("native_sleep start (%lu)\n", msec);
233 ret = w32_wait_events(0, 0, msec, th);
234 thread_debug("native_sleep done (%lu)\n", ret);
237 native_mutex_lock(&th->interrupt_lock);
238 th->unblock.func = 0;
240 native_mutex_unlock(&th->interrupt_lock);
246 native_mutex_lock(rb_thread_lock_t *lock)
251 thread_debug("native_mutex_lock: %p\n", *lock);
252 result = w32_wait_events(&*lock, 1, INFINITE, 0);
255 /* get mutex object */
256 thread_debug("acquire mutex: %p\n", *lock);
258 case WAIT_OBJECT_0 + 1:
261 thread_debug("acquire mutex interrupted: %p\n", *lock);
264 thread_debug("timeout mutex: %p\n", *lock);
267 rb_bug("win32_mutex_lock: WAIT_ABANDONED");
270 rb_bug("win32_mutex_lock: unknown result (%d)", result);
276 EnterCriticalSection(lock);
282 native_mutex_unlock(rb_thread_lock_t *lock)
285 thread_debug("release mutex: %p\n", *lock);
286 return ReleaseMutex(*lock);
288 LeaveCriticalSection(lock);
294 native_mutex_trylock(rb_thread_lock_t *lock)
298 thread_debug("native_mutex_trylock: %p\n", *lock);
299 result = w32_wait_events(&*lock, 1, 1, 0);
300 thread_debug("native_mutex_trylock result: %d\n", result);
309 return TryEnterCriticalSection(lock) == 0;
314 native_mutex_initialize(rb_thread_lock_t *lock)
317 *lock = CreateMutex(NULL, FALSE, NULL);
321 /* thread_debug("initialize mutex: %p\n", *lock); */
323 InitializeCriticalSection(lock);
328 native_mutex_destroy(rb_thread_lock_t *lock)
331 w32_close_handle(lock);
333 DeleteCriticalSection(lock);
337 struct cond_event_entry {
338 struct cond_event_entry* next;
342 struct rb_thread_cond_struct {
343 struct cond_event_entry *next;
344 struct cond_event_entry *last;
348 native_cond_signal(rb_thread_cond_t *cond)
350 /* cond is guarded by mutex */
351 struct cond_event_entry *e = cond->next;
354 cond->next = e->next;
358 rb_bug("native_cond_signal: no pending threads");
363 native_cond_broadcast(rb_thread_cond_t *cond)
365 /* cond is guarded by mutex */
366 struct cond_event_entry *e = cond->next;
376 native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex)
379 struct cond_event_entry entry;
382 entry.event = CreateEvent(0, FALSE, FALSE, 0);
384 /* cond is guarded by mutex */
386 cond->last->next = &entry;
394 native_mutex_unlock(mutex);
396 r = WaitForSingleObject(entry.event, INFINITE);
397 if (r != WAIT_OBJECT_0) {
398 rb_bug("native_cond_wait: WaitForSingleObject returns %lu", r);
401 native_mutex_lock(mutex);
403 w32_close_handle(entry.event);
407 native_cond_initialize(rb_thread_cond_t *cond)
414 native_cond_destroy(rb_thread_cond_t *cond)
420 ruby_init_stack(VALUE *addr)
424 #define CHECK_ERR(expr) \
425 {if (!(expr)) {rb_bug("err: %lu - %s", GetLastError(), #expr);}}
428 native_thread_init_stack(rb_thread_t *th)
430 MEMORY_BASIC_INFORMATION mi;
434 CHECK_ERR(VirtualQuery(&mi, &mi, sizeof(mi)));
435 base = mi.AllocationBase;
436 end = mi.BaseAddress;
437 end += mi.RegionSize;
440 if (space > 1024*1024) space = 1024*1024;
441 th->machine_stack_start = (VALUE *)end - 1;
442 th->machine_stack_maxsize = size - space;
446 native_thread_destroy(rb_thread_t *th)
448 HANDLE intr = th->native_thread_data.interrupt_event;
449 native_mutex_destroy(&th->interrupt_lock);
450 thread_debug("close handle - intr: %p, thid: %p\n", intr, th->thread_id);
451 th->native_thread_data.interrupt_event = 0;
452 w32_close_handle(intr);
455 static unsigned long _stdcall
456 thread_start_func_1(void *th_ptr)
458 rb_thread_t *th = th_ptr;
459 volatile HANDLE thread_id = th->thread_id;
461 native_thread_init_stack(th);
462 th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
465 thread_debug("thread created (th: %p, thid: %p, event: %p)\n", th,
466 th->thread_id, th->native_thread_data.interrupt_event);
468 thread_start_func_2(th, th->machine_stack_start, rb_ia64_bsp());
470 w32_close_handle(thread_id);
471 thread_debug("thread deleted (th: %p)\n", th);
476 native_thread_create(rb_thread_t *th)
478 size_t stack_size = 4 * 1024; /* 4KB */
479 th->thread_id = w32_create_thread(stack_size, thread_start_func_1, th);
481 if ((th->thread_id) == 0) {
482 st_delete_wrap(th->vm->living_threads, th->self);
483 rb_raise(rb_eThreadError, "can't create Thread (%d)", errno);
486 w32_resume_thread(th->thread_id);
490 thread_debug("create: (th: %p, thid: %p, intr: %p), stack size: %d\n",
492 th->native_thread_data.interrupt_event, stack_size);
498 native_thread_join(HANDLE th)
500 w32_wait_events(&th, 1, 0, 0);
503 #if USE_NATIVE_THREAD_PRIORITY
506 native_thread_apply_priority(rb_thread_t *th)
508 int priority = th->priority;
509 if (th->priority > 0) {
510 priority = THREAD_PRIORITY_ABOVE_NORMAL;
512 else if (th->priority < 0) {
513 priority = THREAD_PRIORITY_BELOW_NORMAL;
516 priority = THREAD_PRIORITY_NORMAL;
519 SetThreadPriority(th->thread_id, priority);
522 #endif /* USE_NATIVE_THREAD_PRIORITY */
525 ubf_handle(void *ptr)
527 typedef BOOL (WINAPI *cancel_io_func_t)(HANDLE);
528 rb_thread_t *th = (rb_thread_t *)ptr;
529 thread_debug("ubf_handle: %p\n", th);
531 w32_set_event(th->native_thread_data.interrupt_event);
534 static HANDLE timer_thread_id = 0;
535 static HANDLE timer_thread_lock;
537 static unsigned long _stdcall
538 timer_thread_func(void *dummy)
540 thread_debug("timer_thread\n");
541 while (WaitForSingleObject(timer_thread_lock, WIN32_WAIT_TIMEOUT) ==
543 timer_thread_function(dummy);
545 thread_debug("timer killed\n");
550 rb_thread_create_timer_thread(void)
552 if (timer_thread_id == 0) {
553 if (!timer_thread_lock) {
554 timer_thread_lock = CreateEvent(0, TRUE, FALSE, 0);
556 timer_thread_id = w32_create_thread(1024 + (THREAD_DEBUG ? BUFSIZ : 0),
557 timer_thread_func, 0);
558 w32_resume_thread(timer_thread_id);
563 native_stop_timer_thread(void)
565 int stopped = --system_working <= 0;
567 CloseHandle(timer_thread_lock);
568 timer_thread_lock = 0;
573 #endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */