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