Ruby  2.0.0p645(2015-04-13revision50299)
thread_win32.c
Go to the documentation of this file.
1 /* -*-c-*- */
2 /**********************************************************************
3 
4  thread_win32.c -
5 
6  $Author: usa $
7 
8  Copyright (C) 2004-2007 Koichi Sasada
9 
10 **********************************************************************/
11 
12 #ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
13 
14 #include <process.h>
15 
16 #define TIME_QUANTUM_USEC (10 * 1000)
17 #define RB_CONDATTR_CLOCK_MONOTONIC 1 /* no effect */
18 
19 #undef Sleep
20 
21 #define native_thread_yield() Sleep(0)
22 #define remove_signal_thread_list(th)
23 
24 static volatile DWORD ruby_native_thread_key = TLS_OUT_OF_INDEXES;
25 
26 static int w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th);
27 static int native_mutex_lock(rb_thread_lock_t *lock);
28 static int native_mutex_unlock(rb_thread_lock_t *lock);
29 
30 static void
31 w32_error(const char *func)
32 {
33  LPVOID lpMsgBuf;
34  DWORD err = GetLastError();
35  if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
36  FORMAT_MESSAGE_FROM_SYSTEM |
37  FORMAT_MESSAGE_IGNORE_INSERTS,
38  NULL,
39  err,
40  MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
41  (LPTSTR) & lpMsgBuf, 0, NULL) == 0)
42  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
43  FORMAT_MESSAGE_FROM_SYSTEM |
44  FORMAT_MESSAGE_IGNORE_INSERTS,
45  NULL,
46  err,
47  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
48  (LPTSTR) & lpMsgBuf, 0, NULL);
49  rb_bug("%s: %s", func, (char*)lpMsgBuf);
50 }
51 
52 static int
53 w32_mutex_lock(HANDLE lock)
54 {
55  DWORD result;
56  while (1) {
57  thread_debug("native_mutex_lock: %p\n", lock);
58  result = w32_wait_events(&lock, 1, INFINITE, 0);
59  switch (result) {
60  case WAIT_OBJECT_0:
61  /* get mutex object */
62  thread_debug("acquire mutex: %p\n", lock);
63  return 0;
64  case WAIT_OBJECT_0 + 1:
65  /* interrupt */
66  errno = EINTR;
67  thread_debug("acquire mutex interrupted: %p\n", lock);
68  return 0;
69  case WAIT_TIMEOUT:
70  thread_debug("timeout mutex: %p\n", lock);
71  break;
72  case WAIT_ABANDONED:
73  rb_bug("win32_mutex_lock: WAIT_ABANDONED");
74  break;
75  default:
76  rb_bug("win32_mutex_lock: unknown result (%ld)", result);
77  break;
78  }
79  }
80  return 0;
81 }
82 
83 static HANDLE
84 w32_mutex_create(void)
85 {
86  HANDLE lock = CreateMutex(NULL, FALSE, NULL);
87  if (lock == NULL) {
88  w32_error("native_mutex_initialize");
89  }
90  return lock;
91 }
92 
93 #define GVL_DEBUG 0
94 
95 static void
96 gvl_acquire(rb_vm_t *vm, rb_thread_t *th)
97 {
98  w32_mutex_lock(vm->gvl.lock);
99  if (GVL_DEBUG) fprintf(stderr, "gvl acquire (%p): acquire\n", th);
100 }
101 
102 static void
103 gvl_release(rb_vm_t *vm)
104 {
105  ReleaseMutex(vm->gvl.lock);
106 }
107 
108 static void
109 gvl_yield(rb_vm_t *vm, rb_thread_t *th)
110 {
111  gvl_release(th->vm);
112  native_thread_yield();
113  gvl_acquire(vm, th);
114 }
115 
116 
117 static void
118 gvl_atfork(rb_vm_t *vm)
119 {
120  rb_bug("gvl_atfork() is called on win32");
121 }
122 
123 static void
124 gvl_init(rb_vm_t *vm)
125 {
126  if (GVL_DEBUG) fprintf(stderr, "gvl init\n");
127  vm->gvl.lock = w32_mutex_create();
128 }
129 
130 static void
131 gvl_destroy(rb_vm_t *vm)
132 {
133  if (GVL_DEBUG) fprintf(stderr, "gvl destroy\n");
134  CloseHandle(vm->gvl.lock);
135 }
136 
137 static rb_thread_t *
138 ruby_thread_from_native(void)
139 {
140  return TlsGetValue(ruby_native_thread_key);
141 }
142 
143 static int
144 ruby_thread_set_native(rb_thread_t *th)
145 {
146  return TlsSetValue(ruby_native_thread_key, th);
147 }
148 
149 void
150 Init_native_thread(void)
151 {
152  rb_thread_t *th = GET_THREAD();
153 
154  ruby_native_thread_key = TlsAlloc();
155  ruby_thread_set_native(th);
156  DuplicateHandle(GetCurrentProcess(),
157  GetCurrentThread(),
158  GetCurrentProcess(),
159  &th->thread_id, 0, FALSE, DUPLICATE_SAME_ACCESS);
160 
161  th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
162 
163  thread_debug("initial thread (th: %p, thid: %p, event: %p)\n",
164  th, GET_THREAD()->thread_id,
166 }
167 
168 static void
169 w32_set_event(HANDLE handle)
170 {
171  if (SetEvent(handle) == 0) {
172  w32_error("w32_set_event");
173  }
174 }
175 
176 static void
177 w32_reset_event(HANDLE handle)
178 {
179  if (ResetEvent(handle) == 0) {
180  w32_error("w32_reset_event");
181  }
182 }
183 
184 static int
185 w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th)
186 {
187  HANDLE *targets = events;
188  HANDLE intr;
189  DWORD ret;
190 
191  thread_debug(" w32_wait_events events:%p, count:%d, timeout:%ld, th:%p\n",
192  events, count, timeout, th);
193  if (th && (intr = th->native_thread_data.interrupt_event)) {
194  gvl_acquire(th->vm, th);
195  if (intr == th->native_thread_data.interrupt_event) {
196  w32_reset_event(intr);
197  if (RUBY_VM_INTERRUPTED(th)) {
198  w32_set_event(intr);
199  }
200 
201  targets = ALLOCA_N(HANDLE, count + 1);
202  memcpy(targets, events, sizeof(HANDLE) * count);
203 
204  targets[count++] = intr;
205  thread_debug(" * handle: %p (count: %d, intr)\n", intr, count);
206  }
207  gvl_release(th->vm);
208  }
209 
210  thread_debug(" WaitForMultipleObjects start (count: %d)\n", count);
211  ret = WaitForMultipleObjects(count, targets, FALSE, timeout);
212  thread_debug(" WaitForMultipleObjects end (ret: %lu)\n", ret);
213 
214  if (ret == (DWORD)(WAIT_OBJECT_0 + count - 1) && th) {
215  errno = EINTR;
216  }
217  if (ret == WAIT_FAILED && THREAD_DEBUG) {
218  int i;
219  DWORD dmy;
220  for (i = 0; i < count; i++) {
221  thread_debug(" * error handle %d - %s\n", i,
222  GetHandleInformation(targets[i], &dmy) ? "OK" : "NG");
223  }
224  }
225  return ret;
226 }
227 
228 static void ubf_handle(void *ptr);
229 #define ubf_select ubf_handle
230 
231 int
232 rb_w32_wait_events_blocking(HANDLE *events, int num, DWORD timeout)
233 {
234  return w32_wait_events(events, num, timeout, ruby_thread_from_native());
235 }
236 
237 int
238 rb_w32_wait_events(HANDLE *events, int num, DWORD timeout)
239 {
240  int ret;
241 
242  BLOCKING_REGION(ret = rb_w32_wait_events_blocking(events, num, timeout),
243  ubf_handle, ruby_thread_from_native(), FALSE);
244  return ret;
245 }
246 
247 static void
248 w32_close_handle(HANDLE handle)
249 {
250  if (CloseHandle(handle) == 0) {
251  w32_error("w32_close_handle");
252  }
253 }
254 
255 static void
256 w32_resume_thread(HANDLE handle)
257 {
258  if (ResumeThread(handle) == (DWORD)-1) {
259  w32_error("w32_resume_thread");
260  }
261 }
262 
263 #ifdef _MSC_VER
264 #define HAVE__BEGINTHREADEX 1
265 #else
266 #undef HAVE__BEGINTHREADEX
267 #endif
268 
269 #ifdef HAVE__BEGINTHREADEX
270 #define start_thread (HANDLE)_beginthreadex
271 #define thread_errno errno
272 typedef unsigned long (_stdcall *w32_thread_start_func)(void*);
273 #else
274 #define start_thread CreateThread
275 #define thread_errno rb_w32_map_errno(GetLastError())
276 typedef LPTHREAD_START_ROUTINE w32_thread_start_func;
277 #endif
278 
279 static HANDLE
280 w32_create_thread(DWORD stack_size, w32_thread_start_func func, void *val)
281 {
282  return start_thread(0, stack_size, func, val, CREATE_SUSPENDED, 0);
283 }
284 
285 int
286 rb_w32_sleep(unsigned long msec)
287 {
288  return w32_wait_events(0, 0, msec, ruby_thread_from_native());
289 }
290 
291 int WINAPI
292 rb_w32_Sleep(unsigned long msec)
293 {
294  int ret;
295 
296  BLOCKING_REGION(ret = rb_w32_sleep(msec),
297  ubf_handle, ruby_thread_from_native(), FALSE);
298  return ret;
299 }
300 
301 static void
302 native_sleep(rb_thread_t *th, struct timeval *tv)
303 {
304  DWORD msec;
305 
306  if (tv) {
307  msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
308  }
309  else {
310  msec = INFINITE;
311  }
312 
314  {
315  DWORD ret;
316 
317  native_mutex_lock(&th->interrupt_lock);
318  th->unblock.func = ubf_handle;
319  th->unblock.arg = th;
320  native_mutex_unlock(&th->interrupt_lock);
321 
322  if (RUBY_VM_INTERRUPTED(th)) {
323  /* interrupted. return immediate */
324  }
325  else {
326  thread_debug("native_sleep start (%lu)\n", msec);
327  ret = w32_wait_events(0, 0, msec, th);
328  thread_debug("native_sleep done (%lu)\n", ret);
329  }
330 
331  native_mutex_lock(&th->interrupt_lock);
332  th->unblock.func = 0;
333  th->unblock.arg = 0;
334  native_mutex_unlock(&th->interrupt_lock);
335  }
336  GVL_UNLOCK_END();
337 }
338 
339 static int
340 native_mutex_lock(rb_thread_lock_t *lock)
341 {
342 #if USE_WIN32_MUTEX
343  w32_mutex_lock(lock->mutex);
344 #else
345  EnterCriticalSection(&lock->crit);
346 #endif
347  return 0;
348 }
349 
350 static int
351 native_mutex_unlock(rb_thread_lock_t *lock)
352 {
353 #if USE_WIN32_MUTEX
354  thread_debug("release mutex: %p\n", lock->mutex);
355  return ReleaseMutex(lock->mutex);
356 #else
357  LeaveCriticalSection(&lock->crit);
358  return 0;
359 #endif
360 }
361 
362 static int
363 native_mutex_trylock(rb_thread_lock_t *lock)
364 {
365 #if USE_WIN32_MUTEX
366  int result;
367  thread_debug("native_mutex_trylock: %p\n", lock->mutex);
368  result = w32_wait_events(&lock->mutex, 1, 1, 0);
369  thread_debug("native_mutex_trylock result: %d\n", result);
370  switch (result) {
371  case WAIT_OBJECT_0:
372  return 0;
373  case WAIT_TIMEOUT:
374  return EBUSY;
375  }
376  return EINVAL;
377 #else
378  return TryEnterCriticalSection(&lock->crit) == 0;
379 #endif
380 }
381 
382 static void
383 native_mutex_initialize(rb_thread_lock_t *lock)
384 {
385 #if USE_WIN32_MUTEX
386  lock->mutex = w32_mutex_create();
387  /* thread_debug("initialize mutex: %p\n", lock->mutex); */
388 #else
389  InitializeCriticalSection(&lock->crit);
390 #endif
391 }
392 
393 static void
394 native_mutex_destroy(rb_thread_lock_t *lock)
395 {
396 #if USE_WIN32_MUTEX
397  w32_close_handle(lock->mutex);
398 #else
399  DeleteCriticalSection(&lock->crit);
400 #endif
401 }
402 
403 struct cond_event_entry {
404  struct cond_event_entry* next;
405  struct cond_event_entry* prev;
406  HANDLE event;
407 };
408 
409 static void
410 native_cond_signal(rb_thread_cond_t *cond)
411 {
412  /* cond is guarded by mutex */
413  struct cond_event_entry *e = cond->next;
414  struct cond_event_entry *head = (struct cond_event_entry*)cond;
415 
416  if (e != head) {
417  struct cond_event_entry *next = e->next;
418  struct cond_event_entry *prev = e->prev;
419 
420  prev->next = next;
421  next->prev = prev;
422  e->next = e->prev = e;
423 
424  SetEvent(e->event);
425  }
426 }
427 
428 static void
429 native_cond_broadcast(rb_thread_cond_t *cond)
430 {
431  /* cond is guarded by mutex */
432  struct cond_event_entry *e = cond->next;
433  struct cond_event_entry *head = (struct cond_event_entry*)cond;
434 
435  while (e != head) {
436  struct cond_event_entry *next = e->next;
437  struct cond_event_entry *prev = e->prev;
438 
439  SetEvent(e->event);
440 
441  prev->next = next;
442  next->prev = prev;
443  e->next = e->prev = e;
444 
445  e = next;
446  }
447 }
448 
449 
450 static int
451 native_cond_timedwait_ms(rb_thread_cond_t *cond, rb_thread_lock_t *mutex, unsigned long msec)
452 {
453  DWORD r;
454  struct cond_event_entry entry;
455  struct cond_event_entry *head = (struct cond_event_entry*)cond;
456 
457  entry.event = CreateEvent(0, FALSE, FALSE, 0);
458 
459  /* cond is guarded by mutex */
460  entry.next = head;
461  entry.prev = head->prev;
462  head->prev->next = &entry;
463  head->prev = &entry;
464 
465  native_mutex_unlock(mutex);
466  {
467  r = WaitForSingleObject(entry.event, msec);
468  if ((r != WAIT_OBJECT_0) && (r != WAIT_TIMEOUT)) {
469  rb_bug("native_cond_wait: WaitForSingleObject returns %lu", r);
470  }
471  }
472  native_mutex_lock(mutex);
473 
474  entry.prev->next = entry.next;
475  entry.next->prev = entry.prev;
476 
477  w32_close_handle(entry.event);
478  return (r == WAIT_OBJECT_0) ? 0 : ETIMEDOUT;
479 }
480 
481 static int
482 native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex)
483 {
484  return native_cond_timedwait_ms(cond, mutex, INFINITE);
485 }
486 
487 static unsigned long
488 abs_timespec_to_timeout_ms(struct timespec *ts)
489 {
490  struct timeval tv;
491  struct timeval now;
492 
493  gettimeofday(&now, NULL);
494  tv.tv_sec = ts->tv_sec;
495  tv.tv_usec = ts->tv_nsec / 1000;
496 
497  if (!rb_w32_time_subtract(&tv, &now))
498  return 0;
499 
500  return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
501 }
502 
503 static int
504 native_cond_timedwait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex, struct timespec *ts)
505 {
506  unsigned long timeout_ms;
507 
508  timeout_ms = abs_timespec_to_timeout_ms(ts);
509  if (!timeout_ms)
510  return ETIMEDOUT;
511 
512  return native_cond_timedwait_ms(cond, mutex, timeout_ms);
513 }
514 
515 #if SIZEOF_TIME_T == SIZEOF_LONG
516 typedef unsigned long unsigned_time_t;
517 #elif SIZEOF_TIME_T == SIZEOF_INT
518 typedef unsigned int unsigned_time_t;
519 #elif SIZEOF_TIME_T == SIZEOF_LONG_LONG
520 typedef unsigned LONG_LONG unsigned_time_t;
521 #else
522 # error cannot find integer type which size is same as time_t.
523 #endif
524 
525 #define TIMET_MAX (~(time_t)0 <= 0 ? (time_t)((~(unsigned_time_t)0) >> 1) : (time_t)(~(unsigned_time_t)0))
526 
527 static struct timespec
528 native_cond_timeout(rb_thread_cond_t *cond, struct timespec timeout_rel)
529 {
530  int ret;
531  struct timeval tv;
532  struct timespec timeout;
533  struct timespec now;
534 
535  ret = gettimeofday(&tv, 0);
536  if (ret != 0)
537  rb_sys_fail(0);
538  now.tv_sec = tv.tv_sec;
539  now.tv_nsec = tv.tv_usec * 1000;
540 
541  timeout.tv_sec = now.tv_sec;
542  timeout.tv_nsec = now.tv_nsec;
543  timeout.tv_sec += timeout_rel.tv_sec;
544  timeout.tv_nsec += timeout_rel.tv_nsec;
545 
546  if (timeout.tv_nsec >= 1000*1000*1000) {
547  timeout.tv_sec++;
548  timeout.tv_nsec -= 1000*1000*1000;
549  }
550 
551  if (timeout.tv_sec < now.tv_sec)
552  timeout.tv_sec = TIMET_MAX;
553 
554  return timeout;
555 }
556 
557 static void
558 native_cond_initialize(rb_thread_cond_t *cond, int flags)
559 {
560  cond->next = (struct cond_event_entry *)cond;
561  cond->prev = (struct cond_event_entry *)cond;
562 }
563 
564 static void
565 native_cond_destroy(rb_thread_cond_t *cond)
566 {
567  /* */
568 }
569 
570 void
571 ruby_init_stack(volatile VALUE *addr)
572 {
573 }
574 
575 #define CHECK_ERR(expr) \
576  {if (!(expr)) {rb_bug("err: %lu - %s", GetLastError(), #expr);}}
577 
578 static void
579 native_thread_init_stack(rb_thread_t *th)
580 {
581  MEMORY_BASIC_INFORMATION mi;
582  char *base, *end;
583  DWORD size, space;
584 
585  CHECK_ERR(VirtualQuery(&mi, &mi, sizeof(mi)));
586  base = mi.AllocationBase;
587  end = mi.BaseAddress;
588  end += mi.RegionSize;
589  size = end - base;
590  space = size / 5;
591  if (space > 1024*1024) space = 1024*1024;
592  th->machine_stack_start = (VALUE *)end - 1;
593  th->machine_stack_maxsize = size - space;
594 }
595 
596 #ifndef InterlockedExchangePointer
597 #define InterlockedExchangePointer(t, v) \
598  (void *)InterlockedExchange((long *)(t), (long)(v))
599 #endif
600 static void
601 native_thread_destroy(rb_thread_t *th)
602 {
603  HANDLE intr = InterlockedExchangePointer(&th->native_thread_data.interrupt_event, 0);
604  thread_debug("close handle - intr: %p, thid: %p\n", intr, th->thread_id);
605  w32_close_handle(intr);
606 }
607 
608 static unsigned long _stdcall
609 thread_start_func_1(void *th_ptr)
610 {
611  rb_thread_t *th = th_ptr;
612  volatile HANDLE thread_id = th->thread_id;
613 
614  native_thread_init_stack(th);
615  th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
616 
617  /* run */
618  thread_debug("thread created (th: %p, thid: %p, event: %p)\n", th,
620 
621  thread_start_func_2(th, th->machine_stack_start, rb_ia64_bsp());
622 
623  w32_close_handle(thread_id);
624  thread_debug("thread deleted (th: %p)\n", th);
625  return 0;
626 }
627 
628 static int
629 native_thread_create(rb_thread_t *th)
630 {
631  size_t stack_size = 4 * 1024; /* 4KB is the minimum commit size */
632  th->thread_id = w32_create_thread(stack_size, thread_start_func_1, th);
633 
634  if ((th->thread_id) == 0) {
635  return thread_errno;
636  }
637 
638  w32_resume_thread(th->thread_id);
639 
640  if (THREAD_DEBUG) {
641  Sleep(0);
642  thread_debug("create: (th: %p, thid: %p, intr: %p), stack size: %"PRIdSIZE"\n",
643  th, th->thread_id,
644  th->native_thread_data.interrupt_event, stack_size);
645  }
646  return 0;
647 }
648 
649 static void
650 native_thread_join(HANDLE th)
651 {
652  w32_wait_events(&th, 1, INFINITE, 0);
653 }
654 
655 #if USE_NATIVE_THREAD_PRIORITY
656 
657 static void
658 native_thread_apply_priority(rb_thread_t *th)
659 {
660  int priority = th->priority;
661  if (th->priority > 0) {
662  priority = THREAD_PRIORITY_ABOVE_NORMAL;
663  }
664  else if (th->priority < 0) {
665  priority = THREAD_PRIORITY_BELOW_NORMAL;
666  }
667  else {
668  priority = THREAD_PRIORITY_NORMAL;
669  }
670 
671  SetThreadPriority(th->thread_id, priority);
672 }
673 
674 #endif /* USE_NATIVE_THREAD_PRIORITY */
675 
676 int rb_w32_select_with_thread(int, fd_set *, fd_set *, fd_set *, struct timeval *, void *); /* @internal */
677 
678 static int
679 native_fd_select(int n, rb_fdset_t *readfds, rb_fdset_t *writefds, rb_fdset_t *exceptfds, struct timeval *timeout, rb_thread_t *th)
680 {
681  fd_set *r = NULL, *w = NULL, *e = NULL;
682  if (readfds) {
683  rb_fd_resize(n - 1, readfds);
684  r = rb_fd_ptr(readfds);
685  }
686  if (writefds) {
687  rb_fd_resize(n - 1, writefds);
688  w = rb_fd_ptr(writefds);
689  }
690  if (exceptfds) {
691  rb_fd_resize(n - 1, exceptfds);
692  e = rb_fd_ptr(exceptfds);
693  }
694  return rb_w32_select_with_thread(n, r, w, e, timeout, th);
695 }
696 
697 /* @internal */
698 int
700 {
701  return w32_wait_events(0, 0, 0, th);
702 }
703 
704 static void
705 ubf_handle(void *ptr)
706 {
707  rb_thread_t *th = (rb_thread_t *)ptr;
708  thread_debug("ubf_handle: %p\n", th);
709 
710  w32_set_event(th->native_thread_data.interrupt_event);
711 }
712 
713 static HANDLE timer_thread_id = 0;
714 static HANDLE timer_thread_lock;
715 
716 static unsigned long _stdcall
717 timer_thread_func(void *dummy)
718 {
719  thread_debug("timer_thread\n");
720  while (WaitForSingleObject(timer_thread_lock, TIME_QUANTUM_USEC/1000) ==
721  WAIT_TIMEOUT) {
722  timer_thread_function(dummy);
723  }
724  thread_debug("timer killed\n");
725  return 0;
726 }
727 
728 void
730 {
731  /* do nothing */
732 }
733 
734 static void
735 rb_thread_create_timer_thread(void)
736 {
737  if (timer_thread_id == 0) {
738  if (!timer_thread_lock) {
739  timer_thread_lock = CreateEvent(0, TRUE, FALSE, 0);
740  }
741  timer_thread_id = w32_create_thread(1024 + (THREAD_DEBUG ? BUFSIZ : 0),
742  timer_thread_func, 0);
743  w32_resume_thread(timer_thread_id);
744  }
745 }
746 
747 static int
748 native_stop_timer_thread(int close_anyway)
749 {
750  int stopped = --system_working <= 0;
751  if (stopped) {
752  SetEvent(timer_thread_lock);
753  native_thread_join(timer_thread_id);
754  CloseHandle(timer_thread_lock);
755  timer_thread_lock = 0;
756  }
757  return stopped;
758 }
759 
760 static void
761 native_reset_timer_thread(void)
762 {
763  if (timer_thread_id) {
764  CloseHandle(timer_thread_id);
765  timer_thread_id = 0;
766  }
767 }
768 
769 int
770 ruby_stack_overflowed_p(const rb_thread_t *th, const void *addr)
771 {
773 }
774 
775 #if defined(__MINGW32__)
776 LONG WINAPI
777 rb_w32_stack_overflow_handler(struct _EXCEPTION_POINTERS *exception)
778 {
779  if (exception->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
781  raise(SIGSEGV);
782  }
783  return EXCEPTION_CONTINUE_SEARCH;
784 }
785 #endif
786 
787 #ifdef RUBY_ALLOCA_CHKSTK
788 void
789 ruby_alloca_chkstk(size_t len, void *sp)
790 {
791  if (ruby_stack_length(NULL) * sizeof(VALUE) >= len) {
792  rb_thread_t *th = GET_THREAD();
796  }
797  }
798 }
799 #endif
800 int
801 rb_reserved_fd_p(int fd)
802 {
803  return 0;
804 }
805 #endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
#define cond(node)
Definition: ripper.c:424
rb_vm_t * vm
Definition: vm_core.h:495
void rb_bug(const char *fmt,...)
Definition: error.c:295
int gettimeofday(struct timeval *, struct timezone *)
Definition: win32.c:4023
#define FALSE
Definition: nkf.h:174
int rb_w32_wait_events_blocking(HANDLE *events, int num, DWORD timeout)
int i
Definition: win32ole.c:784
int rb_w32_wait_events(HANDLE *events, int num, DWORD timeout)
int count
Definition: encoding.c:51
rb_thread_lock_t interrupt_lock
Definition: vm_core.h:556
size_t ruby_stack_length(VALUE **p)
Definition: gc.c:2280
pthread_mutex_t rb_thread_lock_t
rb_unblock_function_t * func
Definition: vm_core.h:480
#define THREAD_DEBUG
Definition: thread.c:73
long tv_sec
Definition: ossl_asn1.c:17
#define sysstack_error
Definition: vm_core.h:868
static volatile int system_working
Definition: thread.c:96
unsigned long unsigned_time_t
Definition: time.c:732
#define head
Definition: st.c:107
time_t tv_sec
Definition: missing.h:46
WINBASEAPI BOOL WINAPI TryEnterCriticalSection(IN OUT LPCRITICAL_SECTION lpCriticalSection)
void rb_exc_raise(VALUE mesg)
Definition: eval.c:527
fd_set rb_fdset_t
Definition: intern.h:326
void rb_thread_wakeup_timer_thread(void)
#define val
long tv_usec
Definition: ossl_asn1.c:18
IUnknown DWORD
Definition: win32ole.c:149
#define rb_fd_ptr(f)
Definition: intern.h:334
int WINAPI rb_w32_Sleep(unsigned long msec)
long tv_nsec
Definition: missing.h:47
#define thread_debug
Definition: thread.c:211
int rb_w32_sleep(unsigned long msec)
struct cond_event_entry * next
Definition: thread_win32.h:32
#define ALLOCA_N(type, n)
Definition: ruby.h:1227
int rb_w32_select_with_thread(int nfds, fd_set *rd, fd_set *wr, fd_set *ex, struct timeval *timeout, void *th)
Definition: win32.c:2749
int err
Definition: win32.c:87
VALUE * machine_stack_start
Definition: vm_core.h:588
#define GVL_UNLOCK_BEGIN()
Definition: thread.c:137
#define TIMET_MAX
Definition: thread.c:76
int rb_w32_time_subtract(struct timeval *rest, const struct timeval *wait)
Definition: win32.c:2710
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:108
int errno
#define TRUE
Definition: nkf.h:175
struct cond_event_entry * prev
Definition: thread_win32.h:33
#define rb_thread_raised_set(th, f)
Definition: eval_intern.h:198
#define GVL_UNLOCK_END()
Definition: thread.c:142
unsigned long VALUE
Definition: ruby.h:104
static VALUE result
Definition: nkf.c:40
void Init_native_thread(void)
void ruby_init_stack(volatile VALUE *)
static void timer_thread_function(void *)
Definition: thread.c:3745
void rb_sys_fail(const char *mesg)
Definition: error.c:1907
int rb_reserved_fd_p(int fd)
#define thread_start_func_2(th, st, rst)
Definition: thread.c:215
int size
Definition: encoding.c:52
struct rb_unblock_callback unblock
Definition: vm_core.h:557
#define ETIMEDOUT
Definition: win32.h:549
native_thread_data_t native_thread_data
Definition: vm_core.h:535
#define BLOCKING_REGION(func, arg)
Definition: rubysocket.h:205
#define rb_thread_raised_p(th, f)
Definition: eval_intern.h:200
#define rb_fd_resize(n, f)
Definition: intern.h:333
#define PRIdSIZE
Definition: ruby.h:186
rb_global_vm_lock_t gvl
Definition: vm_core.h:340
#define RUBY_VM_INTERRUPTED(th)
Definition: vm_core.h:926
size_t machine_stack_maxsize
Definition: vm_core.h:590
#define NULL
Definition: _sdbm.c:102
static rb_thread_t * GET_THREAD(void)
Definition: vm_core.h:890
int rb_w32_check_interrupt(void *)
rb_thread_id_t thread_id
Definition: vm_core.h:530