Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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