Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 562 lines (476 sloc) 12.326 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),
4b02286 @unak * thread_win32.c (native_sleep): fixed previous commit.
unak authored
150 ubf_handle, GET_THREAD(), 1);
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),
4b02286 @unak * thread_win32.c (native_sleep): fixed previous commit.
unak authored
202 ubf_handle, GET_THREAD(), 1);
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
1b63d7b @nobu * vm_core.h (struct rb_unblock_callback), thread.c
nobu authored
232 th->unblock.func = ubf_handle;
233 th->unblock.arg = th;
5f0b8af @ko1 * vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
ko1 authored
234
235 if (RUBY_VM_INTERRUPTED(th)) {
236 /* interrupted. return immediate */
237 }
238 else {
87e4ad0 @nobu * io.c, signal.c, thread.c, thread_win32.c, include/ruby/intern.h:
nobu authored
239 thread_debug("native_sleep start (%lu)\n", msec);
5f0b8af @ko1 * vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
ko1 authored
240 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
241 thread_debug("native_sleep done (%lu)\n", ret);
5f0b8af @ko1 * vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
ko1 authored
242 }
243
1b63d7b @nobu * vm_core.h (struct rb_unblock_callback), thread.c
nobu authored
244 th->unblock.func = 0;
245 th->unblock.arg = 0;
a3e1b1c @ko1 * Merge YARV
ko1 authored
246 }
247 GVL_UNLOCK_END();
4b02286 @unak * thread_win32.c (native_sleep): fixed previous commit.
unak authored
248 th->status = prev_status;
6f5aaff @mame * thread.c, vm_core.h, vm.c, thread_pthread.c, thread_win32.c: add
mame authored
249 if (!tv && deadlockable) th->vm->sleeper++;
5f0b8af @ko1 * vm_core.h, thread.c, cont.c: add RUBY_VM_SET_INTERRUPT(),
ko1 authored
250 RUBY_VM_CHECK_INTS();
a3e1b1c @ko1 * Merge YARV
ko1 authored
251 }
252
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
253 static int
8ee7d07 @ko1 * blockinlining.c, compile.c, compile.h, error.c, eval.c,
ko1 authored
254 native_mutex_lock(rb_thread_lock_t *lock)
a3e1b1c @ko1 * Merge YARV
ko1 authored
255 {
256 #if USE_WIN32_MUTEX
257 DWORD result;
258 while (1) {
259 thread_debug("native_mutex_lock: %p\n", *lock);
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
260 result = w32_wait_events(&*lock, 1, INFINITE, 0);
a3e1b1c @ko1 * Merge YARV
ko1 authored
261 switch (result) {
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
262 case WAIT_OBJECT_0:
a3e1b1c @ko1 * Merge YARV
ko1 authored
263 /* get mutex object */
264 thread_debug("acquire mutex: %p\n", *lock);
265 return 0;
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
266 case WAIT_OBJECT_0 + 1:
a3e1b1c @ko1 * Merge YARV
ko1 authored
267 /* interrupt */
268 errno = EINTR;
269 thread_debug("acquire mutex interrupted: %p\n", *lock);
270 return 0;
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
271 case WAIT_TIMEOUT:
a3e1b1c @ko1 * Merge YARV
ko1 authored
272 thread_debug("timeout mutex: %p\n", *lock);
273 break;
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
274 case WAIT_ABANDONED:
a3e1b1c @ko1 * Merge YARV
ko1 authored
275 rb_bug("win32_mutex_lock: WAIT_ABANDONED");
276 break;
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
277 default:
a3e1b1c @ko1 * Merge YARV
ko1 authored
278 rb_bug("win32_mutex_lock: unknown result (%d)", result);
279 break;
280 }
281 }
282 return 0;
283 #else
284 EnterCriticalSection(lock);
285 return 0;
286 #endif
287 }
288
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
289 static int
8ee7d07 @ko1 * blockinlining.c, compile.c, compile.h, error.c, eval.c,
ko1 authored
290 native_mutex_unlock(rb_thread_lock_t *lock)
a3e1b1c @ko1 * Merge YARV
ko1 authored
291 {
292 #if USE_WIN32_MUTEX
293 thread_debug("release mutex: %p\n", *lock);
294 return ReleaseMutex(*lock);
295 #else
296 LeaveCriticalSection(lock);
297 return 0;
298 #endif
299 }
300
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
301 static int
8ee7d07 @ko1 * blockinlining.c, compile.c, compile.h, error.c, eval.c,
ko1 authored
302 native_mutex_trylock(rb_thread_lock_t *lock)
a3e1b1c @ko1 * Merge YARV
ko1 authored
303 {
35eb542 @nobu * thread_win32.ci: fixed typo.
nobu authored
304 #if USE_WIN32_MUTEX
a3e1b1c @ko1 * Merge YARV
ko1 authored
305 int result;
306 thread_debug("native_mutex_trylock: %p\n", *lock);
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
307 result = w32_wait_events(&*lock, 1, 1, 0);
a3e1b1c @ko1 * Merge YARV
ko1 authored
308 thread_debug("native_mutex_trylock result: %d\n", result);
309 switch (result) {
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
310 case WAIT_OBJECT_0:
a3e1b1c @ko1 * Merge YARV
ko1 authored
311 return 0;
99d65b1 @nobu * compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
nobu authored
312 case WAIT_TIMEOUT:
a3e1b1c @ko1 * Merge YARV
ko1 authored
313 return EBUSY;
314 }
315 return EINVAL;
316 #else
317 return TryEnterCriticalSection(lock) == 0;
318 #endif
319 }
320
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
321 static void
8ee7d07 @ko1 * blockinlining.c, compile.c, compile.h, error.c, eval.c,
ko1 authored
322 native_mutex_initialize(rb_thread_lock_t *lock)
a3e1b1c @ko1 * Merge YARV
ko1 authored
323 {
35eb542 @nobu * thread_win32.ci: fixed typo.
nobu authored
324 #if USE_WIN32_MUTEX
a3e1b1c @ko1 * Merge YARV
ko1 authored
325 *lock = CreateMutex(NULL, FALSE, NULL);
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
326 if (*lock == NULL) {
327 w32_error();
328 }
205f310 @nobu * call_cfunc.ci, compile.c, compile.h, debug.h, eval.c,
nobu authored
329 /* thread_debug("initialize mutex: %p\n", *lock); */
a3e1b1c @ko1 * Merge YARV
ko1 authored
330 #else
331 InitializeCriticalSection(lock);
332 #endif
333 }
334
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
335 static void
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
336 native_mutex_destroy(rb_thread_lock_t *lock)
337 {
35eb542 @nobu * thread_win32.ci: fixed typo.
nobu authored
338 #if USE_WIN32_MUTEX
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
339 w32_close_handle(lock);
340 #else
341 DeleteCriticalSection(lock);
342 #endif
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
343 }
344
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
345 struct cond_event_entry {
346 struct cond_event_entry* next;
347 HANDLE event;
348 };
349
350 struct rb_thread_cond_struct {
351 struct cond_event_entry *next;
352 struct cond_event_entry *last;
353 };
354
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
355 static void
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
356 native_cond_signal(rb_thread_cond_t *cond)
357 {
358 /* cond is guarded by mutex */
359 struct cond_event_entry *e = cond->next;
360
361 if (e) {
362 cond->next = e->next;
363 SetEvent(e->event);
364 }
365 else {
366 rb_bug("native_cond_signal: no pending threads");
367 }
368 }
369
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
370 static void
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
371 native_cond_broadcast(rb_thread_cond_t *cond)
372 {
373 /* cond is guarded by mutex */
374 struct cond_event_entry *e = cond->next;
375 cond->next = 0;
376
377 while (e) {
378 SetEvent(e->event);
379 e = e->next;
380 }
381 }
382
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
383 static void
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
384 native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex)
385 {
386 DWORD r;
387 struct cond_event_entry entry;
388
389 entry.next = 0;
390 entry.event = CreateEvent(0, FALSE, FALSE, 0);
391
392 /* cond is guarded by mutex */
393 if (cond->next) {
394 cond->last->next = &entry;
395 cond->last = &entry;
396 }
397 else {
398 cond->next = &entry;
399 cond->last = &entry;
400 }
401
402 native_mutex_unlock(mutex);
403 {
404 r = WaitForSingleObject(entry.event, INFINITE);
405 if (r != WAIT_OBJECT_0) {
87e4ad0 @nobu * io.c, signal.c, thread.c, thread_win32.c, include/ruby/intern.h:
nobu authored
406 rb_bug("native_cond_wait: WaitForSingleObject returns %lu", r);
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
407 }
408 }
409 native_mutex_lock(mutex);
410
411 w32_close_handle(entry.event);
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_initialize(rb_thread_cond_t *cond)
416 {
417 cond->next = 0;
418 cond->last = 0;
419 }
420
42f0b52 @ko1 * thread_pthread.c, thread_pthread.h, thread_win32.c,
ko1 authored
421 static void
6244e50 @ko1 * thread.c: fix Mutex to be interruptable lock.
ko1 authored
422 native_cond_destroy(rb_thread_cond_t *cond)
423 {
424 /* */
425 }
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
426
fc3c60f @nobu * gc.h (STACK_UPPER): moved from gc.c
nobu authored
427 void
428 ruby_init_stack(VALUE *addr)
429 {
430 }
431
432 #define CHECK_ERR(expr) \
433 {if (!(expr)) {rb_bug("err: %lu - %s", GetLastError(), #expr);}}
434
435 static void
436 native_thread_init_stack(rb_thread_t *th)
437 {
438 MEMORY_BASIC_INFORMATION mi;
439 char *base, *end;
440 DWORD size, space;
441
442 CHECK_ERR(VirtualQuery(&mi, &mi, sizeof(mi)));
443 base = mi.AllocationBase;
444 end = mi.BaseAddress;
445 end += mi.RegionSize;
446 size = end - base;
447 space = size / 5;
448 if (space > 1024*1024) space = 1024*1024;
449 th->machine_stack_start = (VALUE *)end - 1;
450 th->machine_stack_maxsize = size - space;
451 }
452
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
453 static void
454 native_thread_destroy(rb_thread_t *th)
455 {
00f60d0 @unak * thread_win32.ci (w32_wait_events): check whether interrupt_event is
unak authored
456 HANDLE intr = th->native_thread_data.interrupt_event;
3a4bfba @nobu * thread.c (thread_cleanup_func): ignore errors from destroying mutex
nobu authored
457 native_mutex_destroy(&th->interrupt_lock);
00f60d0 @unak * thread_win32.ci (w32_wait_events): check whether interrupt_event is
unak authored
458 thread_debug("close handle - intr: %p, thid: %p\n", intr, th->thread_id);
459 th->native_thread_data.interrupt_event = 0;
460 w32_close_handle(intr);
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
461 }
a3e1b1c @ko1 * Merge YARV
ko1 authored
462
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
463 static unsigned long _stdcall
a3e1b1c @ko1 * Merge YARV
ko1 authored
464 thread_start_func_1(void *th_ptr)
465 {
9c57438 @ko1 * blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
ko1 authored
466 rb_thread_t *th = th_ptr;
a3e1b1c @ko1 * Merge YARV
ko1 authored
467 VALUE stack_start;
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
468 volatile HANDLE thread_id = th->thread_id;
469
fc3c60f @nobu * gc.h (STACK_UPPER): moved from gc.c
nobu authored
470 native_thread_init_stack(th);
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
471 th->native_thread_data.interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
a3e1b1c @ko1 * Merge YARV
ko1 authored
472
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
473 /* run */
a3e1b1c @ko1 * Merge YARV
ko1 authored
474 thread_debug("thread created (th: %p, thid: %p, event: %p)\n", th,
475 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
476 thread_start_func_2(th, &stack_start, 0);
a3e1b1c @ko1 * Merge YARV
ko1 authored
477
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
478 w32_close_handle(thread_id);
a3e1b1c @ko1 * Merge YARV
ko1 authored
479 thread_debug("thread deleted (th: %p)\n", th);
480 return 0;
481 }
482
483 static int
9c57438 @ko1 * blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
ko1 authored
484 native_thread_create(rb_thread_t *th)
a3e1b1c @ko1 * Merge YARV
ko1 authored
485 {
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
486 size_t stack_size = 4 * 1024; /* 4KB */
487 th->thread_id = w32_create_thread(stack_size, thread_start_func_1, th);
a3e1b1c @ko1 * Merge YARV
ko1 authored
488
a902624 @ko1 * thread.c, thread_pthread.ci, thread_win32.ci (thread_start_func_1):
ko1 authored
489 if ((th->thread_id) == 0) {
490 st_delete_wrap(th->vm->living_threads, th->self);
a3e1b1c @ko1 * Merge YARV
ko1 authored
491 rb_raise(rb_eThreadError, "can't create Thread (%d)", errno);
492 }
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
493
494 w32_resume_thread(th->thread_id);
495
a3e1b1c @ko1 * Merge YARV
ko1 authored
496 if (THREAD_DEBUG) {
497 Sleep(0);
498 thread_debug("create: (th: %p, thid: %p, intr: %p), stack size: %d\n",
499 th, th->thread_id,
500 th->native_thread_data.interrupt_event, stack_size);
501 }
502 return 0;
503 }
504
505 static void
84f8da1 @ko1 * thread.c (rb_thread_stop_timer_thread(), rb_thread_reset_timer_thre…
ko1 authored
506 native_thread_join(HANDLE th)
507 {
bb022be @unak * thread.c (rb_thread_polling): check interrupts here.
unak authored
508 w32_wait_events(&th, 1, 0, 0);
84f8da1 @ko1 * thread.c (rb_thread_stop_timer_thread(), rb_thread_reset_timer_thre…
ko1 authored
509 }
510
511 static void
9c57438 @ko1 * blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
ko1 authored
512 native_thread_apply_priority(rb_thread_t *th)
a3e1b1c @ko1 * Merge YARV
ko1 authored
513 {
514 int priority = th->priority;
515 if (th->priority > 0) {
516 priority = THREAD_PRIORITY_ABOVE_NORMAL;
517 }
518 else if (th->priority < 0) {
519 priority = THREAD_PRIORITY_BELOW_NORMAL;
520 }
521 else {
522 priority = THREAD_PRIORITY_NORMAL;
523 }
524
525 SetThreadPriority(th->thread_id, priority);
526 }
527
528 static void
3453b2b @ko1 * gc.h, vm_core.h: decl of rb_gc_save_machine_context()
ko1 authored
529 ubf_handle(void *ptr)
a3e1b1c @ko1 * Merge YARV
ko1 authored
530 {
3453b2b @ko1 * gc.h, vm_core.h: decl of rb_gc_save_machine_context()
ko1 authored
531 rb_thread_t *th = (rb_thread_t *)ptr;
ae317b5 @ko1 * yarvcore.h, thread.c: fix to use pthread on cygwin.
ko1 authored
532 thread_debug("ubf_handle: %p\n", th);
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
533 w32_set_event(th->native_thread_data.interrupt_event);
a3e1b1c @ko1 * Merge YARV
ko1 authored
534 }
535
536 static void timer_thread_function(void);
537
84f8da1 @ko1 * thread.c (rb_thread_stop_timer_thread(), rb_thread_reset_timer_thre…
ko1 authored
538 static HANDLE timer_thread_id = 0;
a3e1b1c @ko1 * Merge YARV
ko1 authored
539
2840fa6 @nobu * common.mk: inverted rules order.
nobu authored
540 static unsigned long _stdcall
a3e1b1c @ko1 * Merge YARV
ko1 authored
541 timer_thread_func(void *dummy)
542 {
543 thread_debug("timer_thread\n");
544 while (system_working) {
545 Sleep(WIN32_WAIT_TIMEOUT);
546 timer_thread_function();
547 }
548 thread_debug("timer killed\n");
549 return 0;
550 }
551
552 void
553 rb_thread_create_timer_thread(void)
554 {
84f8da1 @ko1 * thread.c (rb_thread_stop_timer_thread(), rb_thread_reset_timer_thre…
ko1 authored
555 if (timer_thread_id == 0) {
556 timer_thread_id = w32_create_thread(1024, timer_thread_func, 0);
a5abb1c @ko1 * thread_win32.ci (w32_show_error_message): renamed to w32_error.
ko1 authored
557 w32_resume_thread(timer_thread_id);
a3e1b1c @ko1 * Merge YARV
ko1 authored
558 }
559 }
560
561 #endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
Something went wrong with that request. Please try again.