Skip to content

HTTPS clone URL

Subversion checkout URL

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