Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 564 lines (479 sloc) 12.458 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 */
17 #undef Sleep
18
19 #define native_thread_yield() Sleep(0)
ae317b5 @ko1 * yarvcore.h, thread.c: fix to use pthread on cygwin.
ko1 authored
20 #define remove_signal_thread_list(th)
a3e1b1c @ko1 * Merge YARV
ko1 authored
21
6fc746d @nobu * thread.c (is_ruby_native_thread): check properly. [ruby-dev:31166]
nobu authored
22 static volatile DWORD ruby_native_thread_key = TLS_OUT_OF_INDEXES;
23
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
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 *);
28
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);
34
6fc746d @nobu * thread.c (is_ruby_native_thread): check properly. [ruby-dev:31166]
nobu authored
35 static rb_thread_t *
36 ruby_thread_from_native(void)
37 {
38 return TlsGetValue(ruby_native_thread_key);
39 }
40
41 static int
42 ruby_thread_set_native(rb_thread_t *th)
43 {
44 return TlsSetValue(ruby_native_thread_key, th);
45 }
46
a3e1b1c @ko1 * Merge YARV
ko1 authored
47 static void
6fc746d @nobu * thread.c (is_ruby_native_thread): check properly. [ruby-dev:31166]
nobu authored
48 Init_native_thread(void)
a3e1b1c @ko1 * Merge YARV
ko1 authored
49 {
9c57438 @ko1 * blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
ko1 authored
50 rb_thread_t *th = GET_THREAD();
6fc746d @nobu * thread.c (is_ruby_native_thread): check properly. [ruby-dev:31166]
nobu authored
51
52 ruby_native_thread_key = TlsAlloc();
a3e1b1c @ko1 * Merge YARV
ko1 authored
53 DuplicateHandle(GetCurrentProcess(),
54 GetCurrentThread(),
55 GetCurrentProcess(),
56 &th->thread_id, 0, FALSE, DUPLICATE_SAME_ACCESS);
57
58 th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
59
60 thread_debug("initial thread (th: %p, thid: %p, event: %p)\n",
61 th, GET_THREAD()->thread_id,
62 th->native_thread_data.interrupt_event);
63 }
64
65 static void
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
66 w32_error(void)
a3e1b1c @ko1 * Merge YARV
ko1 authored
67 {
68 LPVOID lpMsgBuf;
69 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
70 FORMAT_MESSAGE_FROM_SYSTEM |
71 FORMAT_MESSAGE_IGNORE_INSERTS,
72 NULL,
73 GetLastError(),
74 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
75 (LPTSTR) & lpMsgBuf, 0, NULL);
87e4ad0 @nobu * io.c, signal.c, thread.c, thread_win32.c, include/ruby/intern.h:
nobu authored
76 rb_bug("%s", (char*)lpMsgBuf);
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
77 }
78
79 static void
80 w32_set_event(HANDLE handle)
81 {
82 if (SetEvent(handle) == 0) {
83 w32_error();
84 }
85 }
86
87 static void
88 w32_reset_event(HANDLE handle)
89 {
90 if (ResetEvent(handle) == 0) {
91 w32_error();
92 }
a3e1b1c @ko1 * Merge YARV
ko1 authored
93 }
94
95 static int
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
96 w32_wait_events(HANDLE *events, int count, DWORD timeout, rb_thread_t *th)
a3e1b1c @ko1 * Merge YARV
ko1 authored
97 {
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
98 HANDLE *targets = events;
00f60d0 @unak * thread_win32.ci (w32_wait_events): check whether interrupt_event is
unak authored
99 HANDLE intr;
a3e1b1c @ko1 * Merge YARV
ko1 authored
100 DWORD ret;
101
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
102 thread_debug(" w32_wait_events events:%p, count:%d, timeout:%ld, th:%p\n",
103 events, count, timeout, th);
00f60d0 @unak * thread_win32.ci (w32_wait_events): check whether interrupt_event is
unak authored
104 if (th && (intr = th->native_thread_data.interrupt_event)) {
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
105 w32_reset_event(intr);
caa539f @unak * thread_win32.c (w32_wait_events): shouldn't invoke interrupt handle
unak authored
106 if (RUBY_VM_INTERRUPTED(th)) {
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
107 w32_set_event(intr);
a3e1b1c @ko1 * Merge YARV
ko1 authored
108 }
109
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
110 targets = ALLOCA_N(HANDLE, count + 1);
111 memcpy(targets, events, sizeof(HANDLE) * count);
112
113 targets[count++] = intr;
a3e1b1c @ko1 * Merge YARV
ko1 authored
114 thread_debug(" * handle: %p (count: %d, intr)\n", intr, count);
115 }
116
117 thread_debug(" WaitForMultipleObjects start (count: %d)\n", count);
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
118 ret = WaitForMultipleObjects(count, targets, FALSE, timeout);
87e4ad0 @nobu * io.c, signal.c, thread.c, thread_win32.c, include/ruby/intern.h:
nobu authored
119 thread_debug(" WaitForMultipleObjects end (ret: %lu)\n", ret);
a3e1b1c @ko1 * Merge YARV
ko1 authored
120
121 if (ret == WAIT_OBJECT_0 + count - 1 && th) {
122 errno = EINTR;
123 }
124 if (ret == -1 && THREAD_DEBUG) {
125 int i;
126 DWORD dmy;
127 for (i = 0; i < count; i++) {
128 thread_debug(" * error handle %d - %s\n", i,
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
129 GetHandleInformation(targets[i], &dmy) ? "OK" : "NG");
a3e1b1c @ko1 * Merge YARV
ko1 authored
130 }
131 }
132 return ret;
133 }
134
3453b2b @ko1 * gc.h, vm_core.h: decl of rb_gc_save_machine_context()
ko1 authored
135 static void ubf_handle(void *ptr);
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
136 #define ubf_select ubf_handle
137
138 int
c034fce @nobu * process.c (rb_waitpid_blocking, rb_waitpid): use UBF feature.
nobu authored
139 rb_w32_wait_events_blocking(HANDLE *events, int num, DWORD timeout)
140 {
141 return w32_wait_events(events, num, timeout, GET_THREAD());
142 }
143
144 int
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
145 rb_w32_wait_events(HANDLE *events, int num, DWORD timeout)
146 {
147 int ret;
148
3453b2b @ko1 * gc.h, vm_core.h: decl of rb_gc_save_machine_context()
ko1 authored
149 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
150 ubf_handle, GET_THREAD());
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
151 return ret;
152 }
153
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
154 static void
155 w32_close_handle(HANDLE handle)
156 {
157 if (CloseHandle(handle) == 0) {
158 w32_error();
159 }
160 }
161
162 static void
163 w32_resume_thread(HANDLE handle)
164 {
165 if (ResumeThread(handle) == -1) {
166 w32_error();
167 }
168 }
169
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
170 #ifdef _MSC_VER
171 #define HAVE__BEGINTHREADEX 1
172 #else
173 #undef HAVE__BEGINTHREADEX
174 #endif
175
176 #ifdef HAVE__BEGINTHREADEX
177 #define start_thread (HANDLE)_beginthreadex
178 typedef unsigned long (_stdcall *w32_thread_start_func)(void*);
179 #else
180 #define start_thread CreateThread
181 typedef LPTHREAD_START_ROUTINE w32_thread_start_func;
182 #endif
183
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
184 static HANDLE
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
185 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
186 {
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
187 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
188 }
189
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
190 int
191 rb_w32_sleep(unsigned long msec)
192 {
193 return w32_wait_events(0, 0, msec, GET_THREAD());
194 }
195
196 int WINAPI
197 rb_w32_Sleep(unsigned long msec)
198 {
199 int ret;
200
3453b2b @ko1 * gc.h, vm_core.h: decl of rb_gc_save_machine_context()
ko1 authored
201 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
202 ubf_handle, GET_THREAD());
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
203 return ret;
204 }
ae317b5 @ko1 * yarvcore.h, thread.c: fix to use pthread on cygwin.
ko1 authored
205
a3e1b1c @ko1 * Merge YARV
ko1 authored
206 static void
6f5aaff @mame * thread.c, vm_core.h, vm.c, thread_pthread.c, thread_win32.c: add
mame authored
207 native_sleep(rb_thread_t *th, struct timeval *tv, int deadlockable)
a3e1b1c @ko1 * Merge YARV
ko1 authored
208 {
4b02286 @unak * thread_win32.c (native_sleep): fixed previous commit.
unak authored
209 int prev_status = th->status;
a3e1b1c @ko1 * Merge YARV
ko1 authored
210 DWORD msec;
4b02286 @unak * thread_win32.c (native_sleep): fixed previous commit.
unak authored
211
a3e1b1c @ko1 * Merge YARV
ko1 authored
212 if (tv) {
213 msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
214 }
215 else {
216 msec = INFINITE;
217 }
218
6f5aaff @mame * thread.c, vm_core.h, vm.c, thread_pthread.c, thread_win32.c: add
mame authored
219 if (!tv && deadlockable) {
220 th->status = THREAD_STOPPED_FOREVER;
221 th->vm->sleeper++;
222 rb_check_deadlock(th->vm);
223 }
224 else {
225 th->status = THREAD_STOPPED;
226 }
4b02286 @unak * thread_win32.c (native_sleep): fixed previous commit.
unak authored
227
a3e1b1c @ko1 * Merge YARV
ko1 authored
228 GVL_UNLOCK_BEGIN();
229 {
230 DWORD ret;
5f0b8af @ko1 * vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
ko1 authored
231
8724448 @unak * thread_win32.c (native_sleep): must block reentrance when accessing
unak authored
232 native_mutex_lock(&th->interrupt_lock);
1b63d7b @nobu * vm_core.h (struct rb_unblock_callback), thread.c
nobu authored
233 th->unblock.func = ubf_handle;
234 th->unblock.arg = th;
8724448 @unak * thread_win32.c (native_sleep): must block reentrance when accessing
unak authored
235 native_mutex_unlock(&th->interrupt_lock);
5f0b8af @ko1 * vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
ko1 authored
236
237 if (RUBY_VM_INTERRUPTED(th)) {
238 /* interrupted. return immediate */
239 }
240 else {
87e4ad0 @nobu * io.c, signal.c, thread.c, thread_win32.c, include/ruby/intern.h:
nobu authored
241 thread_debug("native_sleep start (%lu)\n", msec);
5f0b8af @ko1 * vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
ko1 authored
242 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
243 thread_debug("native_sleep done (%lu)\n", ret);
5f0b8af @ko1 * vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
ko1 authored
244 }
245
8724448 @unak * thread_win32.c (native_sleep): must block reentrance when accessing
unak authored
246 native_mutex_lock(&th->interrupt_lock);
1b63d7b @nobu * vm_core.h (struct rb_unblock_callback), thread.c
nobu authored
247 th->unblock.func = 0;
248 th->unblock.arg = 0;
8724448 @unak * thread_win32.c (native_sleep): must block reentrance when accessing
unak authored
249 native_mutex_unlock(&th->interrupt_lock);
a3e1b1c @ko1 * Merge YARV
ko1 authored
250 }
251 GVL_UNLOCK_END();
4b02286 @unak * thread_win32.c (native_sleep): fixed previous commit.
unak authored
252 th->status = prev_status;
af94c81 @ko1 * thread_win32.c (native_sleep): fix to decrement sleeper count.
ko1 authored
253 if (!tv && deadlockable) th->vm->sleeper--;
5f0b8af @ko1 * vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
ko1 authored
254 RUBY_VM_CHECK_INTS();
a3e1b1c @ko1 * Merge YARV
ko1 authored
255 }
256
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
257 static int
8ee7d07 @ko1 * blockinlining.c, compile.c, compile.h, error.c, eval.c,
ko1 authored
258 native_mutex_lock(rb_thread_lock_t *lock)
a3e1b1c @ko1 * Merge YARV
ko1 authored
259 {
260 #if USE_WIN32_MUTEX
261 DWORD result;
262 while (1) {
263 thread_debug("native_mutex_lock: %p\n", *lock);
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
264 result = w32_wait_events(&*lock, 1, INFINITE, 0);
a3e1b1c @ko1 * Merge YARV
ko1 authored
265 switch (result) {
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
266 case WAIT_OBJECT_0:
a3e1b1c @ko1 * Merge YARV
ko1 authored
267 /* get mutex object */
268 thread_debug("acquire mutex: %p\n", *lock);
269 return 0;
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
270 case WAIT_OBJECT_0 + 1:
a3e1b1c @ko1 * Merge YARV
ko1 authored
271 /* interrupt */
272 errno = EINTR;
273 thread_debug("acquire mutex interrupted: %p\n", *lock);
274 return 0;
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
275 case WAIT_TIMEOUT:
a3e1b1c @ko1 * Merge YARV
ko1 authored
276 thread_debug("timeout mutex: %p\n", *lock);
277 break;
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
278 case WAIT_ABANDONED:
a3e1b1c @ko1 * Merge YARV
ko1 authored
279 rb_bug("win32_mutex_lock: WAIT_ABANDONED");
280 break;
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
281 default:
a3e1b1c @ko1 * Merge YARV
ko1 authored
282 rb_bug("win32_mutex_lock: unknown result (%d)", result);
283 break;
284 }
285 }
286 return 0;
287 #else
288 EnterCriticalSection(lock);
289 return 0;
290 #endif
291 }
292
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
293 static int
8ee7d07 @ko1 * blockinlining.c, compile.c, compile.h, error.c, eval.c,
ko1 authored
294 native_mutex_unlock(rb_thread_lock_t *lock)
a3e1b1c @ko1 * Merge YARV
ko1 authored
295 {
296 #if USE_WIN32_MUTEX
297 thread_debug("release mutex: %p\n", *lock);
298 return ReleaseMutex(*lock);
299 #else
300 LeaveCriticalSection(lock);
301 return 0;
302 #endif
303 }
304
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
305 static int
8ee7d07 @ko1 * blockinlining.c, compile.c, compile.h, error.c, eval.c,
ko1 authored
306 native_mutex_trylock(rb_thread_lock_t *lock)
a3e1b1c @ko1 * Merge YARV
ko1 authored
307 {
35eb542 @nobu * thread_win32.ci: fixed typo.
nobu authored
308 #if USE_WIN32_MUTEX
a3e1b1c @ko1 * Merge YARV
ko1 authored
309 int result;
310 thread_debug("native_mutex_trylock: %p\n", *lock);
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
311 result = w32_wait_events(&*lock, 1, 1, 0);
a3e1b1c @ko1 * Merge YARV
ko1 authored
312 thread_debug("native_mutex_trylock result: %d\n", result);
313 switch (result) {
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
314 case WAIT_OBJECT_0:
a3e1b1c @ko1 * Merge YARV
ko1 authored
315 return 0;
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
316 case WAIT_TIMEOUT:
a3e1b1c @ko1 * Merge YARV
ko1 authored
317 return EBUSY;
318 }
319 return EINVAL;
320 #else
321 return TryEnterCriticalSection(lock) == 0;
322 #endif
323 }
324
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
325 static void
8ee7d07 @ko1 * blockinlining.c, compile.c, compile.h, error.c, eval.c,
ko1 authored
326 native_mutex_initialize(rb_thread_lock_t *lock)
a3e1b1c @ko1 * Merge YARV
ko1 authored
327 {
35eb542 @nobu * thread_win32.ci: fixed typo.
nobu authored
328 #if USE_WIN32_MUTEX
a3e1b1c @ko1 * Merge YARV
ko1 authored
329 *lock = CreateMutex(NULL, FALSE, NULL);
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
330 if (*lock == NULL) {
331 w32_error();
332 }
205f310 @nobu * call_cfunc.ci, compile.c, compile.h, debug.h, eval.c,
nobu authored
333 /* thread_debug("initialize mutex: %p\n", *lock); */
a3e1b1c @ko1 * Merge YARV
ko1 authored
334 #else
335 InitializeCriticalSection(lock);
336 #endif
337 }
338
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
339 static void
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
340 native_mutex_destroy(rb_thread_lock_t *lock)
341 {
35eb542 @nobu * thread_win32.ci: fixed typo.
nobu authored
342 #if USE_WIN32_MUTEX
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
343 w32_close_handle(lock);
344 #else
345 DeleteCriticalSection(lock);
346 #endif
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
347 }
348
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
349 struct cond_event_entry {
350 struct cond_event_entry* next;
351 HANDLE event;
352 };
353
354 struct rb_thread_cond_struct {
355 struct cond_event_entry *next;
356 struct cond_event_entry *last;
357 };
358
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
359 static void
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
360 native_cond_signal(rb_thread_cond_t *cond)
361 {
362 /* cond is guarded by mutex */
363 struct cond_event_entry *e = cond->next;
364
365 if (e) {
366 cond->next = e->next;
367 SetEvent(e->event);
368 }
369 else {
370 rb_bug("native_cond_signal: no pending threads");
371 }
372 }
373
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
374 static void
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
375 native_cond_broadcast(rb_thread_cond_t *cond)
376 {
377 /* cond is guarded by mutex */
378 struct cond_event_entry *e = cond->next;
379 cond->next = 0;
380
381 while (e) {
382 SetEvent(e->event);
383 e = e->next;
384 }
385 }
386
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
387 static void
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
388 native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex)
389 {
390 DWORD r;
391 struct cond_event_entry entry;
392
393 entry.next = 0;
394 entry.event = CreateEvent(0, FALSE, FALSE, 0);
395
396 /* cond is guarded by mutex */
397 if (cond->next) {
398 cond->last->next = &entry;
399 cond->last = &entry;
400 }
401 else {
402 cond->next = &entry;
403 cond->last = &entry;
404 }
405
406 native_mutex_unlock(mutex);
407 {
408 r = WaitForSingleObject(entry.event, INFINITE);
409 if (r != WAIT_OBJECT_0) {
87e4ad0 @nobu * io.c, signal.c, thread.c, thread_win32.c, include/ruby/intern.h:
nobu authored
410 rb_bug("native_cond_wait: WaitForSingleObject returns %lu", r);
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
411 }
412 }
413 native_mutex_lock(mutex);
414
415 w32_close_handle(entry.event);
416 }
417
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
418 static void
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
419 native_cond_initialize(rb_thread_cond_t *cond)
420 {
421 cond->next = 0;
422 cond->last = 0;
423 }
424
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
425 static void
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
426 native_cond_destroy(rb_thread_cond_t *cond)
427 {
428 /* */
429 }
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
430
fc3c60f @nobu * gc.h (STACK_UPPER): moved from gc.c
nobu authored
431 void
432 ruby_init_stack(VALUE *addr)
433 {
434 }
435
436 #define CHECK_ERR(expr) \
437 {if (!(expr)) {rb_bug("err: %lu - %s", GetLastError(), #expr);}}
438
439 static void
440 native_thread_init_stack(rb_thread_t *th)
441 {
442 MEMORY_BASIC_INFORMATION mi;
443 char *base, *end;
444 DWORD size, space;
445
446 CHECK_ERR(VirtualQuery(&mi, &mi, sizeof(mi)));
447 base = mi.AllocationBase;
448 end = mi.BaseAddress;
449 end += mi.RegionSize;
450 size = end - base;
451 space = size / 5;
452 if (space > 1024*1024) space = 1024*1024;
453 th->machine_stack_start = (VALUE *)end - 1;
454 th->machine_stack_maxsize = size - space;
455 }
456
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
457 static void
458 native_thread_destroy(rb_thread_t *th)
459 {
00f60d0 @unak * thread_win32.ci (w32_wait_events): check whether interrupt_event is
unak authored
460 HANDLE intr = th->native_thread_data.interrupt_event;
3a4bfba @nobu * thread.c (thread_cleanup_func): ignore errors from destroying mutex
nobu authored
461 native_mutex_destroy(&th->interrupt_lock);
00f60d0 @unak * thread_win32.ci (w32_wait_events): check whether interrupt_event is
unak authored
462 thread_debug("close handle - intr: %p, thid: %p\n", intr, th->thread_id);
463 th->native_thread_data.interrupt_event = 0;
464 w32_close_handle(intr);
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
465 }
a3e1b1c @ko1 * Merge YARV
ko1 authored
466
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
467 static unsigned long _stdcall
a3e1b1c @ko1 * Merge YARV
ko1 authored
468 thread_start_func_1(void *th_ptr)
469 {
9c57438 @ko1 * blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
ko1 authored
470 rb_thread_t *th = th_ptr;
a3e1b1c @ko1 * Merge YARV
ko1 authored
471 VALUE stack_start;
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
472 volatile HANDLE thread_id = th->thread_id;
473
fc3c60f @nobu * gc.h (STACK_UPPER): moved from gc.c
nobu authored
474 native_thread_init_stack(th);
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
475 th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
a3e1b1c @ko1 * Merge YARV
ko1 authored
476
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
477 /* run */
a3e1b1c @ko1 * Merge YARV
ko1 authored
478 thread_debug("thread created (th: %p, thid: %p, event: %p)\n", th,
479 th->thread_id, th->native_thread_data.interrupt_event);
0fcf8e1 @nobu * thread_pthread.ci (thread_start_func_2): not use a directive inside
nobu authored
480 thread_start_func_2(th, &stack_start, 0);
a3e1b1c @ko1 * Merge YARV
ko1 authored
481
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
482 w32_close_handle(thread_id);
a3e1b1c @ko1 * Merge YARV
ko1 authored
483 thread_debug("thread deleted (th: %p)\n", th);
484 return 0;
485 }
486
487 static int
9c57438 @ko1 * blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
ko1 authored
488 native_thread_create(rb_thread_t *th)
a3e1b1c @ko1 * Merge YARV
ko1 authored
489 {
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
490 size_t stack_size = 4 * 1024; /* 4KB */
491 th->thread_id = w32_create_thread(stack_size, thread_start_func_1, th);
a3e1b1c @ko1 * Merge YARV
ko1 authored
492
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
493 if ((th->thread_id) == 0) {
494 st_delete_wrap(th->vm->living_threads, th->self);
a3e1b1c @ko1 * Merge YARV
ko1 authored
495 rb_raise(rb_eThreadError, "can't create Thread (%d)", errno);
496 }
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
497
498 w32_resume_thread(th->thread_id);
499
a3e1b1c @ko1 * Merge YARV
ko1 authored
500 if (THREAD_DEBUG) {
501 Sleep(0);
502 thread_debug("create: (th: %p, thid: %p, intr: %p), stack size: %d\n",
503 th, th->thread_id,
504 th->native_thread_data.interrupt_event, stack_size);
505 }
506 return 0;
507 }
508
509 static void
84f8da1 @ko1 * thread.c (rb_thread_stop_timer_thread(), rb_thread_reset_timer_thre…
ko1 authored
510 native_thread_join(HANDLE th)
511 {
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
512 w32_wait_events(&th, 1, 0, 0);
84f8da1 @ko1 * thread.c (rb_thread_stop_timer_thread(), rb_thread_reset_timer_thre…
ko1 authored
513 }
514
515 static void
9c57438 @ko1 * blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
ko1 authored
516 native_thread_apply_priority(rb_thread_t *th)
a3e1b1c @ko1 * Merge YARV
ko1 authored
517 {
518 int priority = th->priority;
519 if (th->priority > 0) {
520 priority = THREAD_PRIORITY_ABOVE_NORMAL;
521 }
522 else if (th->priority < 0) {
523 priority = THREAD_PRIORITY_BELOW_NORMAL;
524 }
525 else {
526 priority = THREAD_PRIORITY_NORMAL;
527 }
528
529 SetThreadPriority(th->thread_id, priority);
530 }
531
532 static void
3453b2b @ko1 * gc.h, vm_core.h: decl of rb_gc_save_machine_context()
ko1 authored
533 ubf_handle(void *ptr)
a3e1b1c @ko1 * Merge YARV
ko1 authored
534 {
3453b2b @ko1 * gc.h, vm_core.h: decl of rb_gc_save_machine_context()
ko1 authored
535 rb_thread_t *th = (rb_thread_t *)ptr;
ae317b5 @ko1 * yarvcore.h, thread.c: fix to use pthread on cygwin.
ko1 authored
536 thread_debug("ubf_handle: %p\n", th);
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
537 w32_set_event(th->native_thread_data.interrupt_event);
a3e1b1c @ko1 * Merge YARV
ko1 authored
538 }
539
84f8da1 @ko1 * thread.c (rb_thread_stop_timer_thread(), rb_thread_reset_timer_thre…
ko1 authored
540 static HANDLE timer_thread_id = 0;
a3e1b1c @ko1 * Merge YARV
ko1 authored
541
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
542 static unsigned long _stdcall
a3e1b1c @ko1 * Merge YARV
ko1 authored
543 timer_thread_func(void *dummy)
544 {
545 thread_debug("timer_thread\n");
546 while (system_working) {
547 Sleep(WIN32_WAIT_TIMEOUT);
6bbbf98 @nobu * thread.c (thread_initialize): NUM2INT() returns int.
nobu authored
548 timer_thread_function(dummy);
a3e1b1c @ko1 * Merge YARV
ko1 authored
549 }
550 thread_debug("timer killed\n");
551 return 0;
552 }
553
554 void
555 rb_thread_create_timer_thread(void)
556 {
84f8da1 @ko1 * thread.c (rb_thread_stop_timer_thread(), rb_thread_reset_timer_thre…
ko1 authored
557 if (timer_thread_id == 0) {
6bbbf98 @nobu * thread.c (thread_initialize): NUM2INT() returns int.
nobu authored
558 timer_thread_id = w32_create_thread(1024, timer_thread_func, GET_VM());
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
559 w32_resume_thread(timer_thread_id);
a3e1b1c @ko1 * Merge YARV
ko1 authored
560 }
561 }
562
563 #endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
Something went wrong with that request. Please try again.