Skip to content
Newer
Older
100644 729 lines (615 sloc) 15.7 KB
a3e1b1c @ko1 * Merge YARV
ko1 authored
1 /* -*-c-*- */
2 /**********************************************************************
3
c334a09 @ko1 * common.mk, *.ci: renamed to *.c.
ko1 authored
4 thread_win32.c -
a3e1b1c @ko1 * Merge YARV
ko1 authored
5
6 $Author$
7
d907cbc @ko1 * blockinlining.c, compile.c, compile.h, debug.c, debug.h,
ko1 authored
8 Copyright (C) 2004-2007 Koichi Sasada
a3e1b1c @ko1 * Merge YARV
ko1 authored
9
10 **********************************************************************/
11
12 #ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
13
14 #include <process.h>
15
16 #define WIN32_WAIT_TIMEOUT 10 /* 10 ms */
604a1e2 @kosaki fix win32 compile error.
kosaki authored
17 #define RB_CONDATTR_CLOCK_MONOTONIC 1 /* no effect */
18
a3e1b1c @ko1 * Merge YARV
ko1 authored
19 #undef Sleep
20
21 #define native_thread_yield() Sleep(0)
ae317b5 @ko1 * yarvcore.h, thread.c: fix to use pthread on cygwin.
ko1 authored
22 #define remove_signal_thread_list(th)
a3e1b1c @ko1 * Merge YARV
ko1 authored
23
6fc746d @nobu * thread.c (is_ruby_native_thread): check properly. [ruby-dev:31166]
nobu authored
24 static volatile DWORD ruby_native_thread_key = TLS_OUT_OF_INDEXES;
25
5f131b6 @nobu * thread_win32.c, include/ruby/win32.h: add prototypes.
nobu authored
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
450463d @ko1 * thread.c, vm_core.h: make gvl_acquire/release/init/destruct
ko1 authored
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:
f0445d1 @nobu * thread_win32.c (gvl_release, gvl_init): suppress warnings.
nobu authored
76 rb_bug("win32_mutex_lock: unknown result (%ld)", result);
450463d @ko1 * thread.c, vm_core.h: make gvl_acquire/release/init/destruct
ko1 authored
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_atfork(rb_vm_t *vm)
110 {
111 rb_bug("gvl_atfork() is called on win32");
112 }
113
114 static void
115 gvl_init(rb_vm_t *vm)
116 {
117 if (GVL_DEBUG) fprintf(stderr, "gvl init\n");
118 vm->gvl.lock = w32_mutex_create();
119 }
120
121 static void
122 gvl_destroy(rb_vm_t *vm)
123 {
124 if (GVL_DEBUG) fprintf(stderr, "gvl destroy\n");
125 CloseHandle(vm->gvl.lock);
126 }
127
6fc746d @nobu * thread.c (is_ruby_native_thread): check properly. [ruby-dev:31166]
nobu authored
128 static rb_thread_t *
129 ruby_thread_from_native(void)
130 {
131 return TlsGetValue(ruby_native_thread_key);
132 }
133
134 static int
135 ruby_thread_set_native(rb_thread_t *th)
136 {
137 return TlsSetValue(ruby_native_thread_key, th);
138 }
139
f40d2c9 @akr * vm.c (Init_BareVM): call Init_native_thread here.
akr authored
140 void
6fc746d @nobu * thread.c (is_ruby_native_thread): check properly. [ruby-dev:31166]
nobu authored
141 Init_native_thread(void)
a3e1b1c @ko1 * Merge YARV
ko1 authored
142 {
9c57438 @ko1 * blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
ko1 authored
143 rb_thread_t *th = GET_THREAD();
6fc746d @nobu * thread.c (is_ruby_native_thread): check properly. [ruby-dev:31166]
nobu authored
144
145 ruby_native_thread_key = TlsAlloc();
b451955 @unak * thread_win32.c (Init_native_thread): need to call
unak authored
146 ruby_thread_set_native(th);
a3e1b1c @ko1 * Merge YARV
ko1 authored
147 DuplicateHandle(GetCurrentProcess(),
148 GetCurrentThread(),
149 GetCurrentProcess(),
150 &th->thread_id, 0, FALSE, DUPLICATE_SAME_ACCESS);
151
152 th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
153
154 thread_debug("initial thread (th: %p, thid: %p, event: %p)\n",
155 th, GET_THREAD()->thread_id,
156 th->native_thread_data.interrupt_event);
157 }
158
159 static void
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
160 w32_set_event(HANDLE handle)
161 {
162 if (SetEvent(handle) == 0) {
5bc85d6 @unak * thread_win32.c (w32_error): should report the function.
unak authored
163 w32_error("w32_set_event");
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
164 }
165 }
166
167 static void
168 w32_reset_event(HANDLE handle)
169 {
170 if (ResetEvent(handle) == 0) {
5bc85d6 @unak * thread_win32.c (w32_error): should report the function.
unak authored
171 w32_error("w32_reset_event");
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
172 }
a3e1b1c @ko1 * Merge YARV
ko1 authored
173 }
174
175 static int
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
176 w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th)
a3e1b1c @ko1 * Merge YARV
ko1 authored
177 {
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
178 HANDLE *targets = events;
00f60d0 @unak * thread_win32.ci (w32_wait_events): check whether interrupt_event is
unak authored
179 HANDLE intr;
a3e1b1c @ko1 * Merge YARV
ko1 authored
180 DWORD ret;
181
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
182 thread_debug(" w32_wait_events events:%p, count:%d, timeout:%ld, th:%p\n",
183 events, count, timeout, th);
00f60d0 @unak * thread_win32.ci (w32_wait_events): check whether interrupt_event is
unak authored
184 if (th && (intr = th->native_thread_data.interrupt_event)) {
450463d @ko1 * thread.c, vm_core.h: make gvl_acquire/release/init/destruct
ko1 authored
185 gvl_acquire(th->vm, th);
bd21118 @wanabe * thread_win32.c (w32_wait_events): get GVL before handle interrupt
wanabe authored
186 if (intr == th->native_thread_data.interrupt_event) {
187 w32_reset_event(intr);
188 if (RUBY_VM_INTERRUPTED(th)) {
189 w32_set_event(intr);
190 }
191
192 targets = ALLOCA_N(HANDLE, count + 1);
193 memcpy(targets, events, sizeof(HANDLE) * count);
194
195 targets[count++] = intr;
196 thread_debug(" * handle: %p (count: %d, intr)\n", intr, count);
a3e1b1c @ko1 * Merge YARV
ko1 authored
197 }
450463d @ko1 * thread.c, vm_core.h: make gvl_acquire/release/init/destruct
ko1 authored
198 gvl_release(th->vm);
a3e1b1c @ko1 * Merge YARV
ko1 authored
199 }
200
201 thread_debug(" WaitForMultipleObjects start (count: %d)\n", count);
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
202 ret = WaitForMultipleObjects(count, targets, FALSE, timeout);
87e4ad0 @nobu * io.c, signal.c, thread.c, thread_win32.c, include/ruby/intern.h:
nobu authored
203 thread_debug(" WaitForMultipleObjects end (ret: %lu)\n", ret);
a3e1b1c @ko1 * Merge YARV
ko1 authored
204
060f18c @wanabe * thread_win32.c (w32_wait_events, w32_close_handle): suppress warnings.
wanabe authored
205 if (ret == (DWORD)(WAIT_OBJECT_0 + count - 1) && th) {
a3e1b1c @ko1 * Merge YARV
ko1 authored
206 errno = EINTR;
207 }
060f18c @wanabe * thread_win32.c (w32_wait_events, w32_close_handle): suppress warnings.
wanabe authored
208 if (ret == WAIT_FAILED && THREAD_DEBUG) {
a3e1b1c @ko1 * Merge YARV
ko1 authored
209 int i;
210 DWORD dmy;
211 for (i = 0; i < count; i++) {
212 thread_debug(" * error handle %d - %s\n", i,
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
213 GetHandleInformation(targets[i], &dmy) ? "OK" : "NG");
a3e1b1c @ko1 * Merge YARV
ko1 authored
214 }
215 }
216 return ret;
217 }
218
3453b2b @ko1 * gc.h, vm_core.h: decl of rb_gc_save_machine_context()
ko1 authored
219 static void ubf_handle(void *ptr);
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
220 #define ubf_select ubf_handle
221
222 int
c034fce @nobu * process.c (rb_waitpid_blocking, rb_waitpid): use UBF feature.
nobu authored
223 rb_w32_wait_events_blocking(HANDLE *events, int num, DWORD timeout)
224 {
225 return w32_wait_events(events, num, timeout, GET_THREAD());
226 }
227
228 int
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
229 rb_w32_wait_events(HANDLE *events, int num, DWORD timeout)
230 {
231 int ret;
232
3453b2b @ko1 * gc.h, vm_core.h: decl of rb_gc_save_machine_context()
ko1 authored
233 BLOCKING_REGION(ret = rb_w32_wait_events_blocking(events, num, timeout),
4b645dc @mame * thread.c, thread_win32.c, vm_core.h: try to remove false positive of
mame authored
234 ubf_handle, GET_THREAD());
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
235 return ret;
236 }
237
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
238 static void
239 w32_close_handle(HANDLE handle)
240 {
241 if (CloseHandle(handle) == 0) {
5bc85d6 @unak * thread_win32.c (w32_error): should report the function.
unak authored
242 w32_error("w32_close_handle");
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
243 }
244 }
245
246 static void
247 w32_resume_thread(HANDLE handle)
248 {
060f18c @wanabe * thread_win32.c (w32_wait_events, w32_close_handle): suppress warnings.
wanabe authored
249 if (ResumeThread(handle) == (DWORD)-1) {
5bc85d6 @unak * thread_win32.c (w32_error): should report the function.
unak authored
250 w32_error("w32_resume_thread");
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
251 }
252 }
253
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
254 #ifdef _MSC_VER
255 #define HAVE__BEGINTHREADEX 1
256 #else
257 #undef HAVE__BEGINTHREADEX
258 #endif
259
260 #ifdef HAVE__BEGINTHREADEX
261 #define start_thread (HANDLE)_beginthreadex
9e0f3e0 @nobu * thread_win32.c (thread_errno): CreateThread does not set errno.
nobu authored
262 #define thread_errno errno
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
263 typedef unsigned long (_stdcall *w32_thread_start_func)(void*);
264 #else
265 #define start_thread CreateThread
9e0f3e0 @nobu * thread_win32.c (thread_errno): CreateThread does not set errno.
nobu authored
266 #define thread_errno rb_w32_map_errno(GetLastError())
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
267 typedef LPTHREAD_START_ROUTINE w32_thread_start_func;
268 #endif
269
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
270 static HANDLE
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
271 w32_create_thread(DWORD stack_size, w32_thread_start_func func, void *val)
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
272 {
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
273 return start_thread(0, stack_size, func, val, CREATE_SUSPENDED, 0);
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
274 }
275
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
276 int
277 rb_w32_sleep(unsigned long msec)
278 {
279 return w32_wait_events(0, 0, msec, GET_THREAD());
280 }
281
282 int WINAPI
283 rb_w32_Sleep(unsigned long msec)
284 {
285 int ret;
286
3453b2b @ko1 * gc.h, vm_core.h: decl of rb_gc_save_machine_context()
ko1 authored
287 BLOCKING_REGION(ret = rb_w32_sleep(msec),
4b645dc @mame * thread.c, thread_win32.c, vm_core.h: try to remove false positive of
mame authored
288 ubf_handle, GET_THREAD());
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
289 return ret;
290 }
ae317b5 @ko1 * yarvcore.h, thread.c: fix to use pthread on cygwin.
ko1 authored
291
a3e1b1c @ko1 * Merge YARV
ko1 authored
292 static void
47e3f4e @nobu * thread.c (thread_start_func_2): wake up joining threads.
nobu authored
293 native_sleep(rb_thread_t *th, struct timeval *tv)
a3e1b1c @ko1 * Merge YARV
ko1 authored
294 {
a146829 @nobu * thread.c (sleep_forever): wait until timed out. [ruby-core:17270]
nobu authored
295 DWORD msec;
4b02286 @unak * thread_win32.c (native_sleep): fixed previous commit.
unak authored
296
a3e1b1c @ko1 * Merge YARV
ko1 authored
297 if (tv) {
298 msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
299 }
300 else {
301 msec = INFINITE;
302 }
303
304 GVL_UNLOCK_BEGIN();
305 {
a146829 @nobu * thread.c (sleep_forever): wait until timed out. [ruby-core:17270]
nobu authored
306 DWORD ret;
307
8724448 @unak * thread_win32.c (native_sleep): must block reentrance when accessing
unak authored
308 native_mutex_lock(&th->interrupt_lock);
1b63d7b @nobu * vm_core.h (struct rb_unblock_callback), thread.c
nobu authored
309 th->unblock.func = ubf_handle;
310 th->unblock.arg = th;
8724448 @unak * thread_win32.c (native_sleep): must block reentrance when accessing
unak authored
311 native_mutex_unlock(&th->interrupt_lock);
5f0b8af @ko1 * vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
ko1 authored
312
313 if (RUBY_VM_INTERRUPTED(th)) {
314 /* interrupted. return immediate */
315 }
316 else {
87e4ad0 @nobu * io.c, signal.c, thread.c, thread_win32.c, include/ruby/intern.h:
nobu authored
317 thread_debug("native_sleep start (%lu)\n", msec);
5f0b8af @ko1 * vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
ko1 authored
318 ret = w32_wait_events(0, 0, msec, th);
87e4ad0 @nobu * io.c, signal.c, thread.c, thread_win32.c, include/ruby/intern.h:
nobu authored
319 thread_debug("native_sleep done (%lu)\n", ret);
5f0b8af @ko1 * vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
ko1 authored
320 }
321
8724448 @unak * thread_win32.c (native_sleep): must block reentrance when accessing
unak authored
322 native_mutex_lock(&th->interrupt_lock);
1b63d7b @nobu * vm_core.h (struct rb_unblock_callback), thread.c
nobu authored
323 th->unblock.func = 0;
324 th->unblock.arg = 0;
8724448 @unak * thread_win32.c (native_sleep): must block reentrance when accessing
unak authored
325 native_mutex_unlock(&th->interrupt_lock);
a3e1b1c @ko1 * Merge YARV
ko1 authored
326 }
327 GVL_UNLOCK_END();
328 }
329
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
330 static int
8ee7d07 @ko1 * blockinlining.c, compile.c, compile.h, error.c, eval.c,
ko1 authored
331 native_mutex_lock(rb_thread_lock_t *lock)
a3e1b1c @ko1 * Merge YARV
ko1 authored
332 {
333 #if USE_WIN32_MUTEX
450463d @ko1 * thread.c, vm_core.h: make gvl_acquire/release/init/destruct
ko1 authored
334 w32_mutex_lock(*lock);
a3e1b1c @ko1 * Merge YARV
ko1 authored
335 #else
336 EnterCriticalSection(lock);
337 return 0;
338 #endif
339 }
340
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
341 static int
8ee7d07 @ko1 * blockinlining.c, compile.c, compile.h, error.c, eval.c,
ko1 authored
342 native_mutex_unlock(rb_thread_lock_t *lock)
a3e1b1c @ko1 * Merge YARV
ko1 authored
343 {
344 #if USE_WIN32_MUTEX
345 thread_debug("release mutex: %p\n", *lock);
346 return ReleaseMutex(*lock);
347 #else
348 LeaveCriticalSection(lock);
349 return 0;
350 #endif
351 }
352
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
353 static int
8ee7d07 @ko1 * blockinlining.c, compile.c, compile.h, error.c, eval.c,
ko1 authored
354 native_mutex_trylock(rb_thread_lock_t *lock)
a3e1b1c @ko1 * Merge YARV
ko1 authored
355 {
35eb542 @nobu * thread_win32.ci: fixed typo.
nobu authored
356 #if USE_WIN32_MUTEX
a3e1b1c @ko1 * Merge YARV
ko1 authored
357 int result;
358 thread_debug("native_mutex_trylock: %p\n", *lock);
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
359 result = w32_wait_events(&*lock, 1, 1, 0);
a3e1b1c @ko1 * Merge YARV
ko1 authored
360 thread_debug("native_mutex_trylock result: %d\n", result);
361 switch (result) {
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
362 case WAIT_OBJECT_0:
a3e1b1c @ko1 * Merge YARV
ko1 authored
363 return 0;
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
364 case WAIT_TIMEOUT:
a3e1b1c @ko1 * Merge YARV
ko1 authored
365 return EBUSY;
366 }
367 return EINVAL;
368 #else
369 return TryEnterCriticalSection(lock) == 0;
370 #endif
371 }
372
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
373 static void
8ee7d07 @ko1 * blockinlining.c, compile.c, compile.h, error.c, eval.c,
ko1 authored
374 native_mutex_initialize(rb_thread_lock_t *lock)
a3e1b1c @ko1 * Merge YARV
ko1 authored
375 {
35eb542 @nobu * thread_win32.ci: fixed typo.
nobu authored
376 #if USE_WIN32_MUTEX
450463d @ko1 * thread.c, vm_core.h: make gvl_acquire/release/init/destruct
ko1 authored
377 *lock = w32_mutex_create();
205f310 @nobu * call_cfunc.ci, compile.c, compile.h, debug.h, eval.c,
nobu authored
378 /* thread_debug("initialize mutex: %p\n", *lock); */
a3e1b1c @ko1 * Merge YARV
ko1 authored
379 #else
380 InitializeCriticalSection(lock);
381 #endif
382 }
383
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
384 static void
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
385 native_mutex_destroy(rb_thread_lock_t *lock)
386 {
35eb542 @nobu * thread_win32.ci: fixed typo.
nobu authored
387 #if USE_WIN32_MUTEX
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
388 w32_close_handle(lock);
389 #else
390 DeleteCriticalSection(lock);
391 #endif
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
392 }
393
a73ba1d @unak * thread_win32.[ch] (cond_every_entry, rb_thread_cond_struct): reverted
unak authored
394 struct cond_event_entry {
395 struct cond_event_entry* next;
396 HANDLE event;
397 };
398
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
399 static void
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
400 native_cond_signal(rb_thread_cond_t *cond)
401 {
402 /* cond is guarded by mutex */
403 struct cond_event_entry *e = cond->next;
404
405 if (e) {
406 cond->next = e->next;
407 SetEvent(e->event);
408 }
409 else {
410 rb_bug("native_cond_signal: no pending threads");
411 }
412 }
413
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
414 static void
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
415 native_cond_broadcast(rb_thread_cond_t *cond)
416 {
417 /* cond is guarded by mutex */
418 struct cond_event_entry *e = cond->next;
419 cond->next = 0;
420
421 while (e) {
422 SetEvent(e->event);
423 e = e->next;
424 }
425 }
426
b4c5fad @kosaki * thread_win32.c (native_cond_timedwait): New. r31373 caused
kosaki authored
427
428 static int
429 __cond_timedwait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex, unsigned long msec)
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
430 {
431 DWORD r;
432 struct cond_event_entry entry;
433
434 entry.next = 0;
435 entry.event = CreateEvent(0, FALSE, FALSE, 0);
436
437 /* cond is guarded by mutex */
438 if (cond->next) {
439 cond->last->next = &entry;
440 cond->last = &entry;
441 }
442 else {
443 cond->next = &entry;
444 cond->last = &entry;
445 }
446
447 native_mutex_unlock(mutex);
448 {
b4c5fad @kosaki * thread_win32.c (native_cond_timedwait): New. r31373 caused
kosaki authored
449 r = WaitForSingleObject(entry.event, msec);
450 if ((r != WAIT_OBJECT_0) && (r != WAIT_TIMEOUT)) {
87e4ad0 @nobu * io.c, signal.c, thread.c, thread_win32.c, include/ruby/intern.h:
nobu authored
451 rb_bug("native_cond_wait: WaitForSingleObject returns %lu", r);
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
452 }
453 }
454 native_mutex_lock(mutex);
455
456 w32_close_handle(entry.event);
b4c5fad @kosaki * thread_win32.c (native_cond_timedwait): New. r31373 caused
kosaki authored
457 return (r == WAIT_OBJECT_0) ? 0 : ETIMEDOUT;
458 }
459
460 static int
461 native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex)
462 {
463 return __cond_timedwait(cond, mutex, INFINITE);
464 }
465
466 static unsigned long
467 abs_timespec_to_timeout_ms(struct timespec *ts)
468 {
469 struct timeval tv;
470 struct timeval now;
471
472 gettimeofday(&now, NULL);
473 tv.tv_sec = ts->tv_sec;
474 tv.tv_usec = ts->tv_nsec;
475
476 if (!rb_w32_time_subtract(&tv, &now))
477 return 0;
478
479 return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
480 }
481
482 static int
483 native_cond_timedwait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex, struct timespec *ts)
484 {
485 unsigned long timeout_ms;
486
487 timeout_ms = abs_timespec_to_timeout_ms(ts);
488 if (!timeout_ms)
489 return ETIMEDOUT;
490
491 return __cond_timedwait(cond, mutex, timeout_ms);
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
492 }
493
7ac6b70 @kosaki sleep_cond use monotonic time if possible.
kosaki authored
494 #if SIZEOF_TIME_T == SIZEOF_LONG
495 typedef unsigned long unsigned_time_t;
496 #elif SIZEOF_TIME_T == SIZEOF_INT
497 typedef unsigned int unsigned_time_t;
498 #elif SIZEOF_TIME_T == SIZEOF_LONG_LONG
499 typedef unsigned LONG_LONG unsigned_time_t;
500 #else
501 # error cannot find integer type which size is same as time_t.
502 #endif
503
504 #define TIMET_MAX (~(time_t)0 <= 0 ? (time_t)((~(unsigned_time_t)0) >> 1) : (time_t)(~(unsigned_time_t)0))
505
b2ea836 @kosaki mutex: deadlock check timeout use monotonic time.
kosaki authored
506 static struct timespec
507 native_cond_timeout(rb_thread_cond_t *cond, struct timespec timeout_rel)
508 {
509 int ret;
510 struct timeval tv;
511 struct timespec timeout;
7ac6b70 @kosaki sleep_cond use monotonic time if possible.
kosaki authored
512 struct timespec now;
b2ea836 @kosaki mutex: deadlock check timeout use monotonic time.
kosaki authored
513
514 ret = gettimeofday(&tv, 0);
515 if (ret != 0)
516 rb_sys_fail(0);
7ac6b70 @kosaki sleep_cond use monotonic time if possible.
kosaki authored
517 now.tv_sec = tv.tv_sec;
518 now.tv_nsec = tv.tv_usec * 1000;
b2ea836 @kosaki mutex: deadlock check timeout use monotonic time.
kosaki authored
519
7ac6b70 @kosaki sleep_cond use monotonic time if possible.
kosaki authored
520 out:
521 timeout.tv_sec = now.tv_sec;
522 timeout.tv_nsec = now.tv_nsec;
b2ea836 @kosaki mutex: deadlock check timeout use monotonic time.
kosaki authored
523 timeout.tv_sec += timeout_rel.tv_sec;
524 timeout.tv_nsec += timeout_rel.tv_nsec;
7ac6b70 @kosaki sleep_cond use monotonic time if possible.
kosaki authored
525
b2ea836 @kosaki mutex: deadlock check timeout use monotonic time.
kosaki authored
526 if (timeout.tv_nsec >= 1000*1000*1000) {
527 timeout.tv_sec++;
528 timeout.tv_nsec -= 1000*1000*1000;
529 }
7ac6b70 @kosaki sleep_cond use monotonic time if possible.
kosaki authored
530
531 if (timeout.tv_sec < now.tv_sec)
532 timeout.tv_sec = TIMET_MAX;
533
b2ea836 @kosaki mutex: deadlock check timeout use monotonic time.
kosaki authored
534 return timeout;
535 }
536
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
537 static void
a1cfaf4 @kosaki * thread_pthread.h (rb_thread_cond_t): add clockid field. it's
kosaki authored
538 native_cond_initialize(rb_thread_cond_t *cond, int flags)
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
539 {
540 cond->next = 0;
541 cond->last = 0;
542 }
543
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
544 static void
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
545 native_cond_destroy(rb_thread_cond_t *cond)
546 {
547 /* */
548 }
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
549
fc3c60f @nobu * gc.h (STACK_UPPER): moved from gc.c
nobu authored
550 void
c7853b4 @nobu * eval.c (ruby_cleanup): the order of local variables on stack is
nobu authored
551 ruby_init_stack(volatile VALUE *addr)
fc3c60f @nobu * gc.h (STACK_UPPER): moved from gc.c
nobu authored
552 {
553 }
554
555 #define CHECK_ERR(expr) \
556 {if (!(expr)) {rb_bug("err: %lu - %s", GetLastError(), #expr);}}
557
558 static void
559 native_thread_init_stack(rb_thread_t *th)
560 {
561 MEMORY_BASIC_INFORMATION mi;
562 char *base, *end;
563 DWORD size, space;
564
565 CHECK_ERR(VirtualQuery(&mi, &mi, sizeof(mi)));
566 base = mi.AllocationBase;
567 end = mi.BaseAddress;
568 end += mi.RegionSize;
569 size = end - base;
570 space = size / 5;
571 if (space > 1024*1024) space = 1024*1024;
572 th->machine_stack_start = (VALUE *)end - 1;
573 th->machine_stack_maxsize = size - space;
574 }
575
dfc07e8 @unak * thread_win32.c (InterlockedExchangePointer): old SDK support.
unak authored
576 #ifndef InterlockedExchangePointer
577 #define InterlockedExchangePointer(t, v) \
578 (void *)InterlockedExchange((long *)(t), (long)(v))
579 #endif
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
580 static void
581 native_thread_destroy(rb_thread_t *th)
582 {
9f90682 @unak * thread_win32.c (native_thread_destroy): decreased the probability of
unak authored
583 HANDLE intr = InterlockedExchangePointer(&th->native_thread_data.interrupt_event, 0);
00f60d0 @unak * thread_win32.ci (w32_wait_events): check whether interrupt_event is
unak authored
584 thread_debug("close handle - intr: %p, thid: %p\n", intr, th->thread_id);
585 w32_close_handle(intr);
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
586 }
a3e1b1c @ko1 * Merge YARV
ko1 authored
587
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
588 static unsigned long _stdcall
a3e1b1c @ko1 * Merge YARV
ko1 authored
589 thread_start_func_1(void *th_ptr)
590 {
9c57438 @ko1 * blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
ko1 authored
591 rb_thread_t *th = th_ptr;
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
592 volatile HANDLE thread_id = th->thread_id;
593
fc3c60f @nobu * gc.h (STACK_UPPER): moved from gc.c
nobu authored
594 native_thread_init_stack(th);
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
595 th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
a3e1b1c @ko1 * Merge YARV
ko1 authored
596
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
597 /* run */
a3e1b1c @ko1 * Merge YARV
ko1 authored
598 thread_debug("thread created (th: %p, thid: %p, event: %p)\n", th,
599 th->thread_id, th->native_thread_data.interrupt_event);
5732566 @nobu * thread.c (rb_thread_stop_timer_thread): terminates timer thread
nobu authored
600
df6ea23 @nobu * thread_win32.c (thread_start_func_1): use already gotten stack info.
nobu authored
601 thread_start_func_2(th, th->machine_stack_start, rb_ia64_bsp());
a3e1b1c @ko1 * Merge YARV
ko1 authored
602
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
603 w32_close_handle(thread_id);
a3e1b1c @ko1 * Merge YARV
ko1 authored
604 thread_debug("thread deleted (th: %p)\n", th);
605 return 0;
606 }
607
608 static int
9c57438 @ko1 * blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
ko1 authored
609 native_thread_create(rb_thread_t *th)
a3e1b1c @ko1 * Merge YARV
ko1 authored
610 {
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
611 size_t stack_size = 4 * 1024; /* 4KB */
612 th->thread_id = w32_create_thread(stack_size, thread_start_func_1, th);
a3e1b1c @ko1 * Merge YARV
ko1 authored
613
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
614 if ((th->thread_id) == 0) {
9e0f3e0 @nobu * thread_win32.c (thread_errno): CreateThread does not set errno.
nobu authored
615 return thread_errno;
a3e1b1c @ko1 * Merge YARV
ko1 authored
616 }
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
617
618 w32_resume_thread(th->thread_id);
619
a3e1b1c @ko1 * Merge YARV
ko1 authored
620 if (THREAD_DEBUG) {
621 Sleep(0);
cea3919 @nobu * configure.in (RUBY_CHECK_PRINTF_PREFIX): check for printf format
nobu authored
622 thread_debug("create: (th: %p, thid: %p, intr: %p), stack size: %"PRIdSIZE"\n",
a3e1b1c @ko1 * Merge YARV
ko1 authored
623 th, th->thread_id,
624 th->native_thread_data.interrupt_event, stack_size);
625 }
626 return 0;
627 }
628
629 static void
84f8da1 @ko1 * thread.c (rb_thread_stop_timer_thread(), rb_thread_reset_timer_thre…
ko1 authored
630 native_thread_join(HANDLE th)
631 {
6198f53 @unak * thread_win32.c (native_thread_join): need to wait thread, of course.
unak authored
632 w32_wait_events(&th, 1, INFINITE, 0);
84f8da1 @ko1 * thread.c (rb_thread_stop_timer_thread(), rb_thread_reset_timer_thre…
ko1 authored
633 }
634
16612b3 @ko1 * thread.c, vm_core.h: add manual priority support
ko1 authored
635 #if USE_NATIVE_THREAD_PRIORITY
636
84f8da1 @ko1 * thread.c (rb_thread_stop_timer_thread(), rb_thread_reset_timer_thre…
ko1 authored
637 static void
9c57438 @ko1 * blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
ko1 authored
638 native_thread_apply_priority(rb_thread_t *th)
a3e1b1c @ko1 * Merge YARV
ko1 authored
639 {
640 int priority = th->priority;
641 if (th->priority > 0) {
642 priority = THREAD_PRIORITY_ABOVE_NORMAL;
643 }
644 else if (th->priority < 0) {
645 priority = THREAD_PRIORITY_BELOW_NORMAL;
646 }
647 else {
648 priority = THREAD_PRIORITY_NORMAL;
649 }
650
651 SetThreadPriority(th->thread_id, priority);
652 }
653
16612b3 @ko1 * thread.c, vm_core.h: add manual priority support
ko1 authored
654 #endif /* USE_NATIVE_THREAD_PRIORITY */
655
a3e1b1c @ko1 * Merge YARV
ko1 authored
656 static void
3453b2b @ko1 * gc.h, vm_core.h: decl of rb_gc_save_machine_context()
ko1 authored
657 ubf_handle(void *ptr)
a3e1b1c @ko1 * Merge YARV
ko1 authored
658 {
3453b2b @ko1 * gc.h, vm_core.h: decl of rb_gc_save_machine_context()
ko1 authored
659 rb_thread_t *th = (rb_thread_t *)ptr;
ae317b5 @ko1 * yarvcore.h, thread.c: fix to use pthread on cygwin.
ko1 authored
660 thread_debug("ubf_handle: %p\n", th);
25498d2 @unak * thread_win32.c (ubf_handle): cancel blocking IO if it can (only
unak authored
661
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
662 w32_set_event(th->native_thread_data.interrupt_event);
a3e1b1c @ko1 * Merge YARV
ko1 authored
663 }
664
84f8da1 @ko1 * thread.c (rb_thread_stop_timer_thread(), rb_thread_reset_timer_thre…
ko1 authored
665 static HANDLE timer_thread_id = 0;
5732566 @nobu * thread.c (rb_thread_stop_timer_thread): terminates timer thread
nobu authored
666 static HANDLE timer_thread_lock;
a3e1b1c @ko1 * Merge YARV
ko1 authored
667
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
668 static unsigned long _stdcall
a3e1b1c @ko1 * Merge YARV
ko1 authored
669 timer_thread_func(void *dummy)
670 {
671 thread_debug("timer_thread\n");
5732566 @nobu * thread.c (rb_thread_stop_timer_thread): terminates timer thread
nobu authored
672 while (WaitForSingleObject(timer_thread_lock, WIN32_WAIT_TIMEOUT) ==
673 WAIT_TIMEOUT) {
6bbbf98 @nobu * thread.c (thread_initialize): NUM2INT() returns int.
nobu authored
674 timer_thread_function(dummy);
a3e1b1c @ko1 * Merge YARV
ko1 authored
675 }
676 thread_debug("timer killed\n");
677 return 0;
678 }
679
5732566 @nobu * thread.c (rb_thread_stop_timer_thread): terminates timer thread
nobu authored
680 static void
a3e1b1c @ko1 * Merge YARV
ko1 authored
681 rb_thread_create_timer_thread(void)
682 {
84f8da1 @ko1 * thread.c (rb_thread_stop_timer_thread(), rb_thread_reset_timer_thre…
ko1 authored
683 if (timer_thread_id == 0) {
5732566 @nobu * thread.c (rb_thread_stop_timer_thread): terminates timer thread
nobu authored
684 if (!timer_thread_lock) {
685 timer_thread_lock = CreateEvent(0, TRUE, FALSE, 0);
686 }
a534e39 @nobu * thread_{pthread,win32}.c (rb_thread_create_timer_thread): needs more
nobu authored
687 timer_thread_id = w32_create_thread(1024 + (THREAD_DEBUG ? BUFSIZ : 0),
5732566 @nobu * thread.c (rb_thread_stop_timer_thread): terminates timer thread
nobu authored
688 timer_thread_func, 0);
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
689 w32_resume_thread(timer_thread_id);
a3e1b1c @ko1 * Merge YARV
ko1 authored
690 }
691 }
692
641f43d @nobu * thread_pthread.c (thread_timer): checks working flags again.
nobu authored
693 static int
694 native_stop_timer_thread(void)
695 {
696 int stopped = --system_working <= 0;
697 if (stopped) {
9ee5e61 @unak * thread_{pthread,win32}.c (native_stop_timer_thread): join the thread
unak authored
698 SetEvent(timer_thread_lock);
699 native_thread_join(timer_thread_id);
641f43d @nobu * thread_pthread.c (thread_timer): checks working flags again.
nobu authored
700 CloseHandle(timer_thread_lock);
701 timer_thread_lock = 0;
702 }
703 return stopped;
704 }
5732566 @nobu * thread.c (rb_thread_stop_timer_thread): terminates timer thread
nobu authored
705
9ee5e61 @unak * thread_{pthread,win32}.c (native_stop_timer_thread): join the thread
unak authored
706 static void
707 native_reset_timer_thread(void)
708 {
709 if (timer_thread_id) {
710 CloseHandle(timer_thread_id);
711 timer_thread_id = 0;
712 }
713 }
714
767d708 @nobu * Makefile.in (ASFLAGS): needs INCFLAGS.
nobu authored
715 #ifdef RUBY_ALLOCA_CHKSTK
716 void
717 ruby_alloca_chkstk(size_t len, void *sp)
718 {
719 if (ruby_stack_length(NULL) * sizeof(VALUE) >= len) {
720 rb_thread_t *th = GET_THREAD();
721 if (!rb_thread_raised_p(th, RAISED_STACKOVERFLOW)) {
722 rb_thread_raised_set(th, RAISED_STACKOVERFLOW);
723 rb_exc_raise(sysstack_error);
724 }
725 }
726 }
727 #endif
a3e1b1c @ko1 * Merge YARV
ko1 authored
728 #endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
Something went wrong with that request. Please try again.