thread_win32.c

Go to the documentation of this file.
00001 /* -*-c-*- */
00002 /**********************************************************************
00003 
00004   thread_win32.c -
00005 
00006   $Author: nagachika $
00007 
00008   Copyright (C) 2004-2007 Koichi Sasada
00009 
00010 **********************************************************************/
00011 
00012 #ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
00013 
00014 #include <process.h>
00015 
00016 #define TIME_QUANTUM_USEC (10 * 1000)
00017 #define RB_CONDATTR_CLOCK_MONOTONIC 1 /* no effect */
00018 
00019 #undef Sleep
00020 
00021 #define native_thread_yield() Sleep(0)
00022 #define remove_signal_thread_list(th)
00023 
00024 static volatile DWORD ruby_native_thread_key = TLS_OUT_OF_INDEXES;
00025 
00026 static int w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th);
00027 static int native_mutex_lock(rb_nativethread_lock_t *lock);
00028 static int native_mutex_unlock(rb_nativethread_lock_t *lock);
00029 
00030 static void
00031 w32_error(const char *func)
00032 {
00033     LPVOID lpMsgBuf;
00034     DWORD err = GetLastError();
00035     if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00036                       FORMAT_MESSAGE_FROM_SYSTEM |
00037                       FORMAT_MESSAGE_IGNORE_INSERTS,
00038                       NULL,
00039                       err,
00040                       MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
00041                       (LPTSTR) & lpMsgBuf, 0, NULL) == 0)
00042         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00043                       FORMAT_MESSAGE_FROM_SYSTEM |
00044                       FORMAT_MESSAGE_IGNORE_INSERTS,
00045                       NULL,
00046                       err,
00047                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00048                       (LPTSTR) & lpMsgBuf, 0, NULL);
00049     rb_bug("%s: %s", func, (char*)lpMsgBuf);
00050 }
00051 
00052 static int
00053 w32_mutex_lock(HANDLE lock)
00054 {
00055     DWORD result;
00056     while (1) {
00057         thread_debug("native_mutex_lock: %p\n", lock);
00058         result = w32_wait_events(&lock, 1, INFINITE, 0);
00059         switch (result) {
00060           case WAIT_OBJECT_0:
00061             /* get mutex object */
00062             thread_debug("acquire mutex: %p\n", lock);
00063             return 0;
00064           case WAIT_OBJECT_0 + 1:
00065             /* interrupt */
00066             errno = EINTR;
00067             thread_debug("acquire mutex interrupted: %p\n", lock);
00068             return 0;
00069           case WAIT_TIMEOUT:
00070             thread_debug("timeout mutex: %p\n", lock);
00071             break;
00072           case WAIT_ABANDONED:
00073             rb_bug("win32_mutex_lock: WAIT_ABANDONED");
00074             break;
00075           default:
00076             rb_bug("win32_mutex_lock: unknown result (%ld)", result);
00077             break;
00078         }
00079     }
00080     return 0;
00081 }
00082 
00083 static HANDLE
00084 w32_mutex_create(void)
00085 {
00086     HANDLE lock = CreateMutex(NULL, FALSE, NULL);
00087     if (lock == NULL) {
00088         w32_error("native_mutex_initialize");
00089     }
00090     return lock;
00091 }
00092 
00093 #define GVL_DEBUG 0
00094 
00095 static void
00096 gvl_acquire(rb_vm_t *vm, rb_thread_t *th)
00097 {
00098     w32_mutex_lock(vm->gvl.lock);
00099     if (GVL_DEBUG) fprintf(stderr, "gvl acquire (%p): acquire\n", th);
00100 }
00101 
00102 static void
00103 gvl_release(rb_vm_t *vm)
00104 {
00105     ReleaseMutex(vm->gvl.lock);
00106 }
00107 
00108 static void
00109 gvl_yield(rb_vm_t *vm, rb_thread_t *th)
00110 {
00111   gvl_release(th->vm);
00112   native_thread_yield();
00113   gvl_acquire(vm, th);
00114 }
00115 
00116 
00117 static void
00118 gvl_atfork(rb_vm_t *vm)
00119 {
00120     rb_bug("gvl_atfork() is called on win32");
00121 }
00122 
00123 static void
00124 gvl_init(rb_vm_t *vm)
00125 {
00126     if (GVL_DEBUG) fprintf(stderr, "gvl init\n");
00127     vm->gvl.lock = w32_mutex_create();
00128 }
00129 
00130 static void
00131 gvl_destroy(rb_vm_t *vm)
00132 {
00133     if (GVL_DEBUG) fprintf(stderr, "gvl destroy\n");
00134     CloseHandle(vm->gvl.lock);
00135 }
00136 
00137 static rb_thread_t *
00138 ruby_thread_from_native(void)
00139 {
00140     return TlsGetValue(ruby_native_thread_key);
00141 }
00142 
00143 static int
00144 ruby_thread_set_native(rb_thread_t *th)
00145 {
00146     return TlsSetValue(ruby_native_thread_key, th);
00147 }
00148 
00149 void
00150 Init_native_thread(void)
00151 {
00152     rb_thread_t *th = GET_THREAD();
00153 
00154     ruby_native_thread_key = TlsAlloc();
00155     ruby_thread_set_native(th);
00156     DuplicateHandle(GetCurrentProcess(),
00157                     GetCurrentThread(),
00158                     GetCurrentProcess(),
00159                     &th->thread_id, 0, FALSE, DUPLICATE_SAME_ACCESS);
00160 
00161     th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
00162 
00163     thread_debug("initial thread (th: %p, thid: %p, event: %p)\n",
00164                  th, GET_THREAD()->thread_id,
00165                  th->native_thread_data.interrupt_event);
00166 }
00167 
00168 static void
00169 w32_set_event(HANDLE handle)
00170 {
00171     if (SetEvent(handle) == 0) {
00172         w32_error("w32_set_event");
00173     }
00174 }
00175 
00176 static void
00177 w32_reset_event(HANDLE handle)
00178 {
00179     if (ResetEvent(handle) == 0) {
00180         w32_error("w32_reset_event");
00181     }
00182 }
00183 
00184 static int
00185 w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th)
00186 {
00187     HANDLE *targets = events;
00188     HANDLE intr;
00189     DWORD ret;
00190 
00191     thread_debug("  w32_wait_events events:%p, count:%d, timeout:%ld, th:%p\n",
00192                  events, count, timeout, th);
00193     if (th && (intr = th->native_thread_data.interrupt_event)) {
00194         gvl_acquire(th->vm, th);
00195         if (intr == th->native_thread_data.interrupt_event) {
00196             w32_reset_event(intr);
00197             if (RUBY_VM_INTERRUPTED(th)) {
00198                 w32_set_event(intr);
00199             }
00200 
00201             targets = ALLOCA_N(HANDLE, count + 1);
00202             memcpy(targets, events, sizeof(HANDLE) * count);
00203 
00204             targets[count++] = intr;
00205             thread_debug("  * handle: %p (count: %d, intr)\n", intr, count);
00206         }
00207         gvl_release(th->vm);
00208     }
00209 
00210     thread_debug("  WaitForMultipleObjects start (count: %d)\n", count);
00211     ret = WaitForMultipleObjects(count, targets, FALSE, timeout);
00212     thread_debug("  WaitForMultipleObjects end (ret: %lu)\n", ret);
00213 
00214     if (ret == (DWORD)(WAIT_OBJECT_0 + count - 1) && th) {
00215         errno = EINTR;
00216     }
00217     if (ret == WAIT_FAILED && THREAD_DEBUG) {
00218         int i;
00219         DWORD dmy;
00220         for (i = 0; i < count; i++) {
00221             thread_debug("  * error handle %d - %s\n", i,
00222                          GetHandleInformation(targets[i], &dmy) ? "OK" : "NG");
00223         }
00224     }
00225     return ret;
00226 }
00227 
00228 static void ubf_handle(void *ptr);
00229 #define ubf_select ubf_handle
00230 
00231 int
00232 rb_w32_wait_events_blocking(HANDLE *events, int num, DWORD timeout)
00233 {
00234     return w32_wait_events(events, num, timeout, ruby_thread_from_native());
00235 }
00236 
00237 int
00238 rb_w32_wait_events(HANDLE *events, int num, DWORD timeout)
00239 {
00240     int ret;
00241 
00242     BLOCKING_REGION(ret = rb_w32_wait_events_blocking(events, num, timeout),
00243                     ubf_handle, ruby_thread_from_native(), FALSE);
00244     return ret;
00245 }
00246 
00247 static void
00248 w32_close_handle(HANDLE handle)
00249 {
00250     if (CloseHandle(handle) == 0) {
00251         w32_error("w32_close_handle");
00252     }
00253 }
00254 
00255 static void
00256 w32_resume_thread(HANDLE handle)
00257 {
00258     if (ResumeThread(handle) == (DWORD)-1) {
00259         w32_error("w32_resume_thread");
00260     }
00261 }
00262 
00263 #ifdef _MSC_VER
00264 #define HAVE__BEGINTHREADEX 1
00265 #else
00266 #undef HAVE__BEGINTHREADEX
00267 #endif
00268 
00269 #ifdef HAVE__BEGINTHREADEX
00270 #define start_thread (HANDLE)_beginthreadex
00271 #define thread_errno errno
00272 typedef unsigned long (__stdcall *w32_thread_start_func)(void*);
00273 #else
00274 #define start_thread CreateThread
00275 #define thread_errno rb_w32_map_errno(GetLastError())
00276 typedef LPTHREAD_START_ROUTINE w32_thread_start_func;
00277 #endif
00278 
00279 static HANDLE
00280 w32_create_thread(DWORD stack_size, w32_thread_start_func func, void *val)
00281 {
00282     return start_thread(0, stack_size, func, val, CREATE_SUSPENDED, 0);
00283 }
00284 
00285 int
00286 rb_w32_sleep(unsigned long msec)
00287 {
00288     return w32_wait_events(0, 0, msec, ruby_thread_from_native());
00289 }
00290 
00291 int WINAPI
00292 rb_w32_Sleep(unsigned long msec)
00293 {
00294     int ret;
00295 
00296     BLOCKING_REGION(ret = rb_w32_sleep(msec),
00297                     ubf_handle, ruby_thread_from_native(), FALSE);
00298     return ret;
00299 }
00300 
00301 static void
00302 native_sleep(rb_thread_t *th, struct timeval *tv)
00303 {
00304     const volatile DWORD msec = (tv) ?
00305         (DWORD)(tv->tv_sec * 1000 + tv->tv_usec / 1000) : INFINITE;
00306 
00307     GVL_UNLOCK_BEGIN();
00308     {
00309         DWORD ret;
00310 
00311         native_mutex_lock(&th->interrupt_lock);
00312         th->unblock.func = ubf_handle;
00313         th->unblock.arg = th;
00314         native_mutex_unlock(&th->interrupt_lock);
00315 
00316         if (RUBY_VM_INTERRUPTED(th)) {
00317             /* interrupted.  return immediate */
00318         }
00319         else {
00320             thread_debug("native_sleep start (%lu)\n", msec);
00321             ret = w32_wait_events(0, 0, msec, th);
00322             thread_debug("native_sleep done (%lu)\n", ret);
00323         }
00324 
00325         native_mutex_lock(&th->interrupt_lock);
00326         th->unblock.func = 0;
00327         th->unblock.arg = 0;
00328         native_mutex_unlock(&th->interrupt_lock);
00329     }
00330     GVL_UNLOCK_END();
00331 }
00332 
00333 static int
00334 native_mutex_lock(rb_nativethread_lock_t *lock)
00335 {
00336 #if USE_WIN32_MUTEX
00337     w32_mutex_lock(lock->mutex);
00338 #else
00339     EnterCriticalSection(&lock->crit);
00340 #endif
00341     return 0;
00342 }
00343 
00344 static int
00345 native_mutex_unlock(rb_nativethread_lock_t *lock)
00346 {
00347 #if USE_WIN32_MUTEX
00348     thread_debug("release mutex: %p\n", lock->mutex);
00349     return ReleaseMutex(lock->mutex);
00350 #else
00351     LeaveCriticalSection(&lock->crit);
00352     return 0;
00353 #endif
00354 }
00355 
00356 static int
00357 native_mutex_trylock(rb_nativethread_lock_t *lock)
00358 {
00359 #if USE_WIN32_MUTEX
00360     int result;
00361     thread_debug("native_mutex_trylock: %p\n", lock->mutex);
00362     result = w32_wait_events(&lock->mutex, 1, 1, 0);
00363     thread_debug("native_mutex_trylock result: %d\n", result);
00364     switch (result) {
00365       case WAIT_OBJECT_0:
00366         return 0;
00367       case WAIT_TIMEOUT:
00368         return EBUSY;
00369     }
00370     return EINVAL;
00371 #else
00372     return TryEnterCriticalSection(&lock->crit) == 0;
00373 #endif
00374 }
00375 
00376 static void
00377 native_mutex_initialize(rb_nativethread_lock_t *lock)
00378 {
00379 #if USE_WIN32_MUTEX
00380     lock->mutex = w32_mutex_create();
00381     /* thread_debug("initialize mutex: %p\n", lock->mutex); */
00382 #else
00383     InitializeCriticalSection(&lock->crit);
00384 #endif
00385 }
00386 
00387 static void
00388 native_mutex_destroy(rb_nativethread_lock_t *lock)
00389 {
00390 #if USE_WIN32_MUTEX
00391     w32_close_handle(lock->mutex);
00392 #else
00393     DeleteCriticalSection(&lock->crit);
00394 #endif
00395 }
00396 
00397 struct cond_event_entry {
00398     struct cond_event_entry* next;
00399     struct cond_event_entry* prev;
00400     HANDLE event;
00401 };
00402 
00403 static void
00404 native_cond_signal(rb_nativethread_cond_t *cond)
00405 {
00406     /* cond is guarded by mutex */
00407     struct cond_event_entry *e = cond->next;
00408     struct cond_event_entry *head = (struct cond_event_entry*)cond;
00409 
00410     if (e != head) {
00411         struct cond_event_entry *next = e->next;
00412         struct cond_event_entry *prev = e->prev;
00413 
00414         prev->next = next;
00415         next->prev = prev;
00416         e->next = e->prev = e;
00417 
00418         SetEvent(e->event);
00419     }
00420 }
00421 
00422 static void
00423 native_cond_broadcast(rb_nativethread_cond_t *cond)
00424 {
00425     /* cond is guarded by mutex */
00426     struct cond_event_entry *e = cond->next;
00427     struct cond_event_entry *head = (struct cond_event_entry*)cond;
00428 
00429     while (e != head) {
00430         struct cond_event_entry *next = e->next;
00431         struct cond_event_entry *prev = e->prev;
00432 
00433         SetEvent(e->event);
00434 
00435         prev->next = next;
00436         next->prev = prev;
00437         e->next = e->prev = e;
00438 
00439         e = next;
00440     }
00441 }
00442 
00443 
00444 static int
00445 native_cond_timedwait_ms(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex, unsigned long msec)
00446 {
00447     DWORD r;
00448     struct cond_event_entry entry;
00449     struct cond_event_entry *head = (struct cond_event_entry*)cond;
00450 
00451     entry.event = CreateEvent(0, FALSE, FALSE, 0);
00452 
00453     /* cond is guarded by mutex */
00454     entry.next = head;
00455     entry.prev = head->prev;
00456     head->prev->next = &entry;
00457     head->prev = &entry;
00458 
00459     native_mutex_unlock(mutex);
00460     {
00461         r = WaitForSingleObject(entry.event, msec);
00462         if ((r != WAIT_OBJECT_0) && (r != WAIT_TIMEOUT)) {
00463             rb_bug("native_cond_wait: WaitForSingleObject returns %lu", r);
00464         }
00465     }
00466     native_mutex_lock(mutex);
00467 
00468     entry.prev->next = entry.next;
00469     entry.next->prev = entry.prev;
00470 
00471     w32_close_handle(entry.event);
00472     return (r == WAIT_OBJECT_0) ? 0 : ETIMEDOUT;
00473 }
00474 
00475 static int
00476 native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex)
00477 {
00478     return native_cond_timedwait_ms(cond, mutex, INFINITE);
00479 }
00480 
00481 static unsigned long
00482 abs_timespec_to_timeout_ms(struct timespec *ts)
00483 {
00484     struct timeval tv;
00485     struct timeval now;
00486 
00487     gettimeofday(&now, NULL);
00488     tv.tv_sec = ts->tv_sec;
00489     tv.tv_usec = ts->tv_nsec / 1000;
00490 
00491     if (!rb_w32_time_subtract(&tv, &now))
00492         return 0;
00493 
00494     return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
00495 }
00496 
00497 static int
00498 native_cond_timedwait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex, struct timespec *ts)
00499 {
00500     unsigned long timeout_ms;
00501 
00502     timeout_ms = abs_timespec_to_timeout_ms(ts);
00503     if (!timeout_ms)
00504         return ETIMEDOUT;
00505 
00506     return native_cond_timedwait_ms(cond, mutex, timeout_ms);
00507 }
00508 
00509 static struct timespec
00510 native_cond_timeout(rb_nativethread_cond_t *cond, struct timespec timeout_rel)
00511 {
00512     int ret;
00513     struct timeval tv;
00514     struct timespec timeout;
00515     struct timespec now;
00516 
00517     ret = gettimeofday(&tv, 0);
00518     if (ret != 0)
00519         rb_sys_fail(0);
00520     now.tv_sec = tv.tv_sec;
00521     now.tv_nsec = tv.tv_usec * 1000;
00522 
00523     timeout.tv_sec = now.tv_sec;
00524     timeout.tv_nsec = now.tv_nsec;
00525     timeout.tv_sec += timeout_rel.tv_sec;
00526     timeout.tv_nsec += timeout_rel.tv_nsec;
00527 
00528     if (timeout.tv_nsec >= 1000*1000*1000) {
00529         timeout.tv_sec++;
00530         timeout.tv_nsec -= 1000*1000*1000;
00531     }
00532 
00533     if (timeout.tv_sec < now.tv_sec)
00534         timeout.tv_sec = TIMET_MAX;
00535 
00536     return timeout;
00537 }
00538 
00539 static void
00540 native_cond_initialize(rb_nativethread_cond_t *cond, int flags)
00541 {
00542     cond->next = (struct cond_event_entry *)cond;
00543     cond->prev = (struct cond_event_entry *)cond;
00544 }
00545 
00546 static void
00547 native_cond_destroy(rb_nativethread_cond_t *cond)
00548 {
00549     /* */
00550 }
00551 
00552 void
00553 ruby_init_stack(volatile VALUE *addr)
00554 {
00555 }
00556 
00557 #define CHECK_ERR(expr) \
00558     {if (!(expr)) {rb_bug("err: %lu - %s", GetLastError(), #expr);}}
00559 
00560 static void
00561 native_thread_init_stack(rb_thread_t *th)
00562 {
00563     MEMORY_BASIC_INFORMATION mi;
00564     char *base, *end;
00565     DWORD size, space;
00566 
00567     CHECK_ERR(VirtualQuery(&mi, &mi, sizeof(mi)));
00568     base = mi.AllocationBase;
00569     end = mi.BaseAddress;
00570     end += mi.RegionSize;
00571     size = end - base;
00572     space = size / 5;
00573     if (space > 1024*1024) space = 1024*1024;
00574     th->machine.stack_start = (VALUE *)end - 1;
00575     th->machine.stack_maxsize = size - space;
00576 }
00577 
00578 #ifndef InterlockedExchangePointer
00579 #define InterlockedExchangePointer(t, v) \
00580     (void *)InterlockedExchange((long *)(t), (long)(v))
00581 #endif
00582 static void
00583 native_thread_destroy(rb_thread_t *th)
00584 {
00585     HANDLE intr = InterlockedExchangePointer(&th->native_thread_data.interrupt_event, 0);
00586     thread_debug("close handle - intr: %p, thid: %p\n", intr, th->thread_id);
00587     w32_close_handle(intr);
00588 }
00589 
00590 static unsigned long __stdcall
00591 thread_start_func_1(void *th_ptr)
00592 {
00593     rb_thread_t *th = th_ptr;
00594     volatile HANDLE thread_id = th->thread_id;
00595 
00596     native_thread_init_stack(th);
00597     th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
00598 
00599     /* run */
00600     thread_debug("thread created (th: %p, thid: %p, event: %p)\n", th,
00601                  th->thread_id, th->native_thread_data.interrupt_event);
00602 
00603     thread_start_func_2(th, th->machine.stack_start, rb_ia64_bsp());
00604 
00605     w32_close_handle(thread_id);
00606     thread_debug("thread deleted (th: %p)\n", th);
00607     return 0;
00608 }
00609 
00610 static int
00611 native_thread_create(rb_thread_t *th)
00612 {
00613     size_t stack_size = 4 * 1024; /* 4KB is the minimum commit size */
00614     th->thread_id = w32_create_thread(stack_size, thread_start_func_1, th);
00615 
00616     if ((th->thread_id) == 0) {
00617         return thread_errno;
00618     }
00619 
00620     w32_resume_thread(th->thread_id);
00621 
00622     if (THREAD_DEBUG) {
00623         Sleep(0);
00624         thread_debug("create: (th: %p, thid: %p, intr: %p), stack size: %"PRIdSIZE"\n",
00625                      th, th->thread_id,
00626                      th->native_thread_data.interrupt_event, stack_size);
00627     }
00628     return 0;
00629 }
00630 
00631 static void
00632 native_thread_join(HANDLE th)
00633 {
00634     w32_wait_events(&th, 1, INFINITE, 0);
00635 }
00636 
00637 #if USE_NATIVE_THREAD_PRIORITY
00638 
00639 static void
00640 native_thread_apply_priority(rb_thread_t *th)
00641 {
00642     int priority = th->priority;
00643     if (th->priority > 0) {
00644         priority = THREAD_PRIORITY_ABOVE_NORMAL;
00645     }
00646     else if (th->priority < 0) {
00647         priority = THREAD_PRIORITY_BELOW_NORMAL;
00648     }
00649     else {
00650         priority = THREAD_PRIORITY_NORMAL;
00651     }
00652 
00653     SetThreadPriority(th->thread_id, priority);
00654 }
00655 
00656 #endif /* USE_NATIVE_THREAD_PRIORITY */
00657 
00658 int rb_w32_select_with_thread(int, fd_set *, fd_set *, fd_set *, struct timeval *, void *);     /* @internal */
00659 
00660 static int
00661 native_fd_select(int n, rb_fdset_t *readfds, rb_fdset_t *writefds, rb_fdset_t *exceptfds, struct timeval *timeout, rb_thread_t *th)
00662 {
00663     fd_set *r = NULL, *w = NULL, *e = NULL;
00664     if (readfds) {
00665         rb_fd_resize(n - 1, readfds);
00666         r = rb_fd_ptr(readfds);
00667     }
00668     if (writefds) {
00669         rb_fd_resize(n - 1, writefds);
00670         w = rb_fd_ptr(writefds);
00671     }
00672     if (exceptfds) {
00673         rb_fd_resize(n - 1, exceptfds);
00674         e = rb_fd_ptr(exceptfds);
00675     }
00676     return rb_w32_select_with_thread(n, r, w, e, timeout, th);
00677 }
00678 
00679 /* @internal */
00680 int
00681 rb_w32_check_interrupt(rb_thread_t *th)
00682 {
00683     return w32_wait_events(0, 0, 0, th);
00684 }
00685 
00686 static void
00687 ubf_handle(void *ptr)
00688 {
00689     rb_thread_t *th = (rb_thread_t *)ptr;
00690     thread_debug("ubf_handle: %p\n", th);
00691 
00692     w32_set_event(th->native_thread_data.interrupt_event);
00693 }
00694 
00695 static HANDLE timer_thread_id = 0;
00696 static HANDLE timer_thread_lock;
00697 
00698 static unsigned long __stdcall
00699 timer_thread_func(void *dummy)
00700 {
00701     thread_debug("timer_thread\n");
00702     while (WaitForSingleObject(timer_thread_lock, TIME_QUANTUM_USEC/1000) ==
00703            WAIT_TIMEOUT) {
00704         timer_thread_function(dummy);
00705     }
00706     thread_debug("timer killed\n");
00707     return 0;
00708 }
00709 
00710 void
00711 rb_thread_wakeup_timer_thread(void)
00712 {
00713     /* do nothing */
00714 }
00715 
00716 static void
00717 rb_thread_create_timer_thread(void)
00718 {
00719     if (timer_thread_id == 0) {
00720         if (!timer_thread_lock) {
00721             timer_thread_lock = CreateEvent(0, TRUE, FALSE, 0);
00722         }
00723         timer_thread_id = w32_create_thread(1024 + (THREAD_DEBUG ? BUFSIZ : 0),
00724                                             timer_thread_func, 0);
00725         w32_resume_thread(timer_thread_id);
00726     }
00727 }
00728 
00729 static int
00730 native_stop_timer_thread(int close_anyway)
00731 {
00732     int stopped = --system_working <= 0;
00733     if (stopped) {
00734         SetEvent(timer_thread_lock);
00735         native_thread_join(timer_thread_id);
00736         CloseHandle(timer_thread_lock);
00737         timer_thread_lock = 0;
00738     }
00739     return stopped;
00740 }
00741 
00742 static void
00743 native_reset_timer_thread(void)
00744 {
00745     if (timer_thread_id) {
00746         CloseHandle(timer_thread_id);
00747         timer_thread_id = 0;
00748     }
00749 }
00750 
00751 int
00752 ruby_stack_overflowed_p(const rb_thread_t *th, const void *addr)
00753 {
00754     return rb_thread_raised_p(th, RAISED_STACKOVERFLOW);
00755 }
00756 
00757 #if defined(__MINGW32__)
00758 LONG WINAPI
00759 rb_w32_stack_overflow_handler(struct _EXCEPTION_POINTERS *exception)
00760 {
00761     if (exception->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
00762         rb_thread_raised_set(GET_THREAD(), RAISED_STACKOVERFLOW);
00763         raise(SIGSEGV);
00764     }
00765     return EXCEPTION_CONTINUE_SEARCH;
00766 }
00767 #endif
00768 
00769 #ifdef RUBY_ALLOCA_CHKSTK
00770 void
00771 ruby_alloca_chkstk(size_t len, void *sp)
00772 {
00773     if (ruby_stack_length(NULL) * sizeof(VALUE) >= len) {
00774         rb_thread_t *th = GET_THREAD();
00775         if (!rb_thread_raised_p(th, RAISED_STACKOVERFLOW)) {
00776             rb_thread_raised_set(th, RAISED_STACKOVERFLOW);
00777             rb_exc_raise(sysstack_error);
00778         }
00779     }
00780 }
00781 #endif
00782 int
00783 rb_reserved_fd_p(int fd)
00784 {
00785     return 0;
00786 }
00787 
00788 rb_nativethread_id_t
00789 rb_nativethread_self(void)
00790 {
00791     return GetCurrentThread();
00792 }
00793 
00794 #endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
00795 

Generated on 19 Jul 2016 for Ruby by  doxygen 1.4.7