1 #include "private/gc_priv.h"
3 #if defined(GC_WIN32_THREADS)
10 /* Cygwin-specific forward decls */
11 # undef pthread_create
12 # undef pthread_sigmask
14 # undef pthread_detach
17 # define DEBUG_CYGWIN_THREADS 0
19 void * GC_start_routine(void * arg);
20 void GC_thread_exit_proc(void *arg);
24 /* The type of the first argument to InterlockedExchange. */
25 /* Documented to be LONG volatile *, but at least gcc likes */
30 # define MAX_THREADS 256
32 /* Things may get quite slow for large numbers of threads, */
33 /* since we look them up with sequential search. */
36 GC_bool GC_thr_initialized = FALSE;
38 DWORD GC_main_thread = 0;
40 struct GC_thread_Rep {
41 LONG in_use; /* Updated without lock. */
42 /* We assert that unused */
43 /* entries have invalid ids of */
44 /* zero and zero stack fields. */
47 ptr_t stack_base; /* The cold end of the stack. */
48 /* 0 ==> entry not valid. */
49 /* !in_use ==> stack_base == 0 */
53 void *status; /* hold exit value until join in case it's a pointer */
55 short flags; /* Protected by GC lock. */
56 # define FINISHED 1 /* Thread has exited. */
57 # define DETACHED 2 /* Thread is intended to be detached. */
61 typedef volatile struct GC_thread_Rep * GC_thread;
64 * We generally assume that volatile ==> memory ordering, at least among
68 volatile GC_bool GC_please_stop = FALSE;
70 volatile struct GC_thread_Rep thread_table[MAX_THREADS];
72 volatile LONG GC_max_thread_index = 0; /* Largest index in thread_table */
73 /* that was ever used. */
75 extern LONG WINAPI GC_write_fault_handler(struct _EXCEPTION_POINTERS *exc_info);
77 int GC_thread_is_registered (void)
79 #if defined(GC_DLL) || defined(GC_INSIDE_DLL)
80 /* Registered by DllMain */
89 * This may be called from DllMain, and hence operates under unusual
92 static GC_thread GC_new_thread(void) {
94 /* It appears to be unsafe to acquire a lock here, since this */
95 /* code is apparently not preeemptible on some systems. */
96 /* (This is based on complaints, not on Microsoft's official */
97 /* documentation, which says this should perform "only simple */
98 /* initialization tasks".) */
99 /* Hence we make do with nonblocking synchronization. */
101 /* The following should be a noop according to the win32 */
102 /* documentation. There is empirical evidence that it */
104 # if defined(MPROTECT_VDB)
105 if (GC_incremental) SetUnhandledExceptionFilter(GC_write_fault_handler);
107 /* cast away volatile qualifier */
108 for (i = 0; InterlockedExchange((IE_t)&thread_table[i].in_use,1) != 0; i++) {
109 /* Compare-and-swap would make this cleaner, but that's not */
110 /* supported before Windows 98 and NT 4.0. In Windows 2000, */
111 /* InterlockedExchange is supposed to be replaced by */
112 /* InterlockedExchangePointer, but that's not really what I */
114 if (i == MAX_THREADS - 1)
115 ABORT("too many threads");
117 /* Update GC_max_thread_index if necessary. The following is safe, */
118 /* and unlike CompareExchange-based solutions seems to work on all */
119 /* Windows95 and later platforms. */
120 /* Unfortunately, GC_max_thread_index may be temporarily out of */
121 /* bounds, so readers have to compensate. */
122 while (i > GC_max_thread_index) {
123 InterlockedIncrement((IE_t)&GC_max_thread_index);
125 if (GC_max_thread_index >= MAX_THREADS) {
126 /* We overshot due to simultaneous increments. */
127 /* Setting it to MAX_THREADS-1 is always safe. */
128 GC_max_thread_index = MAX_THREADS - 1;
132 thread_table[i].pthread_id = pthread_self();
134 if (!DuplicateHandle(GetCurrentProcess(),
137 (HANDLE*)&thread_table[i].handle,
140 DUPLICATE_SAME_ACCESS)) {
141 DWORD last_error = GetLastError();
142 GC_printf1("Last error code: %lx\n", last_error);
143 ABORT("DuplicateHandle failed");
145 thread_table[i].stack_base = GC_get_stack_base();
146 /* Up until this point, GC_push_all_stacks considers this thread */
148 if (thread_table[i].stack_base == NULL)
149 ABORT("Failed to find stack base in GC_new_thread");
150 /* Up until this point, this entry is viewed as reserved but invalid */
151 /* by GC_delete_thread. */
152 thread_table[i].id = GetCurrentThreadId();
153 /* If this thread is being created while we are trying to stop */
154 /* the world, wait here. Hopefully this can't happen on any */
155 /* systems that don't allow us to block here. */
156 while (GC_please_stop) Sleep(20);
157 return thread_table + i;
161 * GC_max_thread_index may temporarily be larger than MAX_THREADS.
162 * To avoid subscript errors, we check on access.
167 LONG GC_get_max_thread_index()
169 LONG my_max = GC_max_thread_index;
171 if (my_max >= MAX_THREADS) return MAX_THREADS-1;
175 /* This is intended to be lock-free, though that */
176 /* assumes that the CloseHandle becomes visible before the */
177 /* in_use assignment. */
178 static void GC_delete_gc_thread(GC_thread thr)
180 CloseHandle(thr->handle);
181 /* cast away volatile qualifier */
186 # endif /* CYGWIN32 */
190 static void GC_delete_thread(DWORD thread_id) {
192 LONG my_max = GC_get_max_thread_index();
196 (!thread_table[i].in_use || thread_table[i].id != thread_id);
197 /* Must still be in_use, since nobody else can store our thread_id. */
200 WARN("Removing nonexistent thread %ld\n", (GC_word)thread_id);
202 GC_delete_gc_thread(thread_table+i);
209 /* Return a GC_thread corresponding to a given pthread_t. */
210 /* Returns 0 if it's not there. */
211 /* We assume that this is only called for pthread ids that */
212 /* have not yet terminated or are still joinable. */
213 static GC_thread GC_lookup_thread(pthread_t id)
216 LONG my_max = GC_get_max_thread_index();
220 (!thread_table[i].in_use || thread_table[i].pthread_id != id
221 || !thread_table[i].in_use);
222 /* Must still be in_use, since nobody else can store our thread_id. */
224 if (i > my_max) return 0;
225 return thread_table + i;
228 #endif /* CYGWIN32 */
230 void GC_push_thread_structures GC_PROTO((void))
232 /* Unlike the other threads implementations, the thread table here */
233 /* contains no pointers to the collectable heap. Thus we have */
234 /* no private structures we need to preserve. */
236 { int i; /* pthreads may keep a pointer in the thread exit value */
237 LONG my_max = GC_get_max_thread_index();
239 for (i = 0; i <= my_max; i++)
240 if (thread_table[i].in_use)
241 GC_push_all((ptr_t)&(thread_table[i].status),
242 (ptr_t)(&(thread_table[i].status)+1));
247 /* Defined in misc.c */
248 extern CRITICAL_SECTION GC_write_cs;
252 DWORD thread_id = GetCurrentThreadId();
255 if (!GC_thr_initialized) ABORT("GC_stop_world() called before GC_thr_init()");
257 GC_please_stop = TRUE;
259 EnterCriticalSection(&GC_write_cs);
260 # endif /* !CYGWIN32 */
261 for (i = 0; i <= GC_get_max_thread_index(); i++)
262 if (thread_table[i].stack_base != 0
263 && thread_table[i].id != thread_id) {
265 /* SuspendThread will fail if thread is running kernel code */
266 while (SuspendThread(thread_table[i].handle) == (DWORD)-1)
269 /* Apparently the Windows 95 GetOpenFileName call creates */
270 /* a thread that does not properly get cleaned up, and */
271 /* SuspendThread on its descriptor may provoke a crash. */
272 /* This reduces the probability of that event, though it still */
273 /* appears there's a race here. */
275 if (GetExitCodeThread(thread_table[i].handle,&exitCode) &&
276 exitCode != STILL_ACTIVE) {
277 thread_table[i].stack_base = 0; /* prevent stack from being pushed */
279 /* this breaks pthread_join on Cygwin, which is guaranteed to */
280 /* only see user pthreads */
281 thread_table[i].in_use = FALSE;
282 CloseHandle(thread_table[i].handle);
286 if (SuspendThread(thread_table[i].handle) == (DWORD)-1) {
287 thread_table[i].stack_base = 0; /* prevent stack from being pushed */
289 /* this breaks pthread_join on Cygwin, which is guaranteed to */
290 /* only see user pthreads */
291 thread_table[i].in_use = FALSE;
292 CloseHandle(thread_table[i].handle);
296 thread_table[i].suspended = TRUE;
299 LeaveCriticalSection(&GC_write_cs);
300 # endif /* !CYGWIN32 */
303 void GC_start_world()
305 DWORD thread_id = GetCurrentThreadId();
307 LONG my_max = GC_get_max_thread_index();
309 for (i = 0; i <= my_max; i++)
310 if (thread_table[i].stack_base != 0 && thread_table[i].suspended
311 && thread_table[i].id != thread_id) {
312 if (ResumeThread(thread_table[i].handle) == (DWORD)-1)
313 ABORT("ResumeThread failed");
314 thread_table[i].suspended = FALSE;
316 GC_please_stop = FALSE;
320 # pragma warning(disable:4715)
322 ptr_t GC_current_stackbottom()
324 DWORD thread_id = GetCurrentThreadId();
326 LONG my_max = GC_get_max_thread_index();
328 for (i = 0; i <= my_max; i++)
329 if (thread_table[i].stack_base && thread_table[i].id == thread_id)
330 return thread_table[i].stack_base;
331 ABORT("no thread table entry for current thread");
334 # pragma warning(default:4715)
338 /* The VirtualQuery calls below won't work properly on WinCE, but */
339 /* since each stack is restricted to an aligned 64K region of */
340 /* virtual memory we can just take the next lowest multiple of 64K. */
341 # define GC_get_stack_min(s) \
342 ((ptr_t)(((DWORD)(s) - 1) & 0xFFFF0000))
344 static ptr_t GC_get_stack_min(ptr_t s)
347 MEMORY_BASIC_INFORMATION info;
348 VirtualQuery(s, &info, sizeof(info));
350 bottom = info.BaseAddress;
351 VirtualQuery(bottom - 1, &info, sizeof(info));
352 } while ((info.Protect & PAGE_READWRITE)
353 && !(info.Protect & PAGE_GUARD));
358 void GC_push_all_stacks()
360 DWORD thread_id = GetCurrentThreadId();
361 GC_bool found_me = FALSE;
366 LONG my_max = GC_get_max_thread_index();
368 for (i = 0; i <= my_max; i++) {
369 thread = thread_table + i;
370 if (thread -> in_use && thread -> stack_base) {
371 if (thread -> id == thread_id) {
376 context.ContextFlags = CONTEXT_INTEGER|CONTEXT_CONTROL;
377 if (!GetThreadContext(thread_table[i].handle, &context))
378 ABORT("GetThreadContext failed");
380 /* Push all registers that might point into the heap. Frame */
381 /* pointer registers are included in case client code was */
382 /* compiled with the 'omit frame pointer' optimisation. */
383 # define PUSH1(reg) GC_push_one((word)context.reg)
384 # define PUSH2(r1,r2) PUSH1(r1), PUSH1(r2)
385 # define PUSH4(r1,r2,r3,r4) PUSH2(r1,r2), PUSH2(r3,r4)
387 PUSH4(Edi,Esi,Ebx,Edx), PUSH2(Ecx,Eax), PUSH1(Ebp);
388 sp = (ptr_t)context.Esp;
389 # elif defined(ARM32)
390 PUSH4(R0,R1,R2,R3),PUSH4(R4,R5,R6,R7),PUSH4(R8,R9,R10,R11),PUSH1(R12);
391 sp = (ptr_t)context.Sp;
393 PUSH4(R0,R1,R2,R3), PUSH4(R4,R5,R6,R7), PUSH4(R8,R9,R10,R11);
394 PUSH2(R12,R13), PUSH1(R14);
395 sp = (ptr_t)context.R15;
397 PUSH4(IntAt,IntV0,IntV1,IntA0), PUSH4(IntA1,IntA2,IntA3,IntT0);
398 PUSH4(IntT1,IntT2,IntT3,IntT4), PUSH4(IntT5,IntT6,IntT7,IntS0);
399 PUSH4(IntS1,IntS2,IntS3,IntS4), PUSH4(IntS5,IntS6,IntS7,IntT8);
400 PUSH4(IntT9,IntK0,IntK1,IntS8);
401 sp = (ptr_t)context.IntSp;
403 PUSH4(Gpr0, Gpr3, Gpr4, Gpr5), PUSH4(Gpr6, Gpr7, Gpr8, Gpr9);
404 PUSH4(Gpr10,Gpr11,Gpr12,Gpr14), PUSH4(Gpr15,Gpr16,Gpr17,Gpr18);
405 PUSH4(Gpr19,Gpr20,Gpr21,Gpr22), PUSH4(Gpr23,Gpr24,Gpr25,Gpr26);
406 PUSH4(Gpr27,Gpr28,Gpr29,Gpr30), PUSH1(Gpr31);
407 sp = (ptr_t)context.Gpr1;
408 # elif defined(ALPHA)
409 PUSH4(IntV0,IntT0,IntT1,IntT2), PUSH4(IntT3,IntT4,IntT5,IntT6);
410 PUSH4(IntT7,IntS0,IntS1,IntS2), PUSH4(IntS3,IntS4,IntS5,IntFp);
411 PUSH4(IntA0,IntA1,IntA2,IntA3), PUSH4(IntA4,IntA5,IntT8,IntT9);
412 PUSH4(IntT10,IntT11,IntT12,IntAt);
413 sp = (ptr_t)context.IntSp;
415 # error "architecture is not supported"
419 stack_min = GC_get_stack_min(thread->stack_base);
421 if (sp >= stack_min && sp < thread->stack_base)
422 GC_push_all_stack(sp, thread->stack_base);
424 WARN("Thread stack pointer 0x%lx out of range, pushing everything\n",
426 GC_push_all_stack(stack_min, thread->stack_base);
430 if (!found_me) ABORT("Collecting from unknown thread.");
433 void GC_get_next_stack(char *start, char **lo, char **hi)
436 # define ADDR_LIMIT (char *)(-1L)
437 char * current_min = ADDR_LIMIT;
438 LONG my_max = GC_get_max_thread_index();
440 for (i = 0; i <= my_max; i++) {
441 char * s = (char *)thread_table[i].stack_base;
443 if (0 != s && s > start && s < current_min) {
448 if (current_min == ADDR_LIMIT) {
452 *lo = GC_get_stack_min(current_min);
453 if (*lo < start) *lo = start;
456 #if !defined(CYGWIN32)
458 #if !defined(MSWINCE) && defined(GC_DLL)
460 /* We register threads from DllMain */
462 GC_API HANDLE WINAPI GC_CreateThread(
463 LPSECURITY_ATTRIBUTES lpThreadAttributes,
464 DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress,
465 LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId )
467 return CreateThread(lpThreadAttributes, dwStackSize, lpStartAddress,
468 lpParameter, dwCreationFlags, lpThreadId);
471 #else /* defined(MSWINCE) || !defined(GC_DLL)) */
473 /* We have no DllMain to take care of new threads. Thus we */
474 /* must properly intercept thread creation. */
477 LPTHREAD_START_ROUTINE start;
481 static DWORD WINAPI thread_start(LPVOID arg);
483 GC_API HANDLE WINAPI GC_CreateThread(
484 LPSECURITY_ATTRIBUTES lpThreadAttributes,
485 DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress,
486 LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId )
488 HANDLE thread_h = NULL;
492 if (!GC_is_initialized) GC_init();
493 /* make sure GC is initialized (i.e. main thread is attached) */
495 args = GC_malloc_uncollectable(sizeof(thread_args));
496 /* Handed off to and deallocated by child thread. */
498 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
502 /* set up thread arguments */
503 args -> start = lpStartAddress;
504 args -> param = lpParameter;
506 thread_h = CreateThread(lpThreadAttributes,
507 dwStackSize, thread_start,
508 args, dwCreationFlags,
514 static DWORD WINAPI thread_start(LPVOID arg)
517 thread_args *args = (thread_args *)arg;
521 /* Clear the thread entry even if we exit with an exception. */
522 /* This is probably pointless, since an uncaught exception is */
523 /* supposed to result in the process being killed. */
526 #endif /* __GNUC__ */
527 ret = args->start (args->param);
530 #endif /* __GNUC__ */
532 GC_delete_thread(GetCurrentThreadId());
535 #endif /* __GNUC__ */
539 #endif /* !defined(MSWINCE) && !(defined(__MINGW32__) && !defined(_DLL)) */
541 #endif /* !CYGWIN32 */
547 HINSTANCE hPrevInstance;
552 DWORD WINAPI main_thread_start(LPVOID arg);
554 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
555 LPWSTR lpCmdLine, int nShowCmd)
559 main_thread_args args = {
560 hInstance, hPrevInstance, lpCmdLine, nShowCmd
565 /* initialize everything */
568 /* start the main thread */
569 thread_h = GC_CreateThread(
570 NULL, 0, main_thread_start, &args, 0, &thread_id);
572 if (thread_h != NULL)
574 WaitForSingleObject (thread_h, INFINITE);
575 GetExitCodeThread (thread_h, &exit_code);
576 CloseHandle (thread_h);
580 DeleteCriticalSection(&GC_allocate_ml);
582 return (int) exit_code;
585 DWORD WINAPI main_thread_start(LPVOID arg)
587 main_thread_args * args = (main_thread_args *) arg;
589 return (DWORD) GC_WinMain (args->hInstance, args->hPrevInstance,
590 args->lpCmdLine, args->nShowCmd);
593 # else /* !MSWINCE */
595 /* Called by GC_init() - we hold the allocation lock. */
597 if (GC_thr_initialized) return;
598 GC_main_thread = GetCurrentThreadId();
599 GC_thr_initialized = TRUE;
601 /* Add the initial thread, so we can stop it. */
608 void *(*start_routine)(void *);
613 int GC_pthread_join(pthread_t pthread_id, void **retval) {
618 # if DEBUG_CYGWIN_THREADS
619 GC_printf3("thread 0x%x(0x%x) is joining thread 0x%x.\n",
620 (int)pthread_self(), GetCurrentThreadId(), (int)pthread_id);
623 /* Thread being joined might not have registered itself yet. */
624 /* After the join,thread id may have been recycled. */
625 /* FIXME: It would be better if this worked more like */
626 /* pthread_support.c. */
628 while ((me = GC_lookup_thread(pthread_id)) == 0) Sleep(10);
630 result = pthread_join(pthread_id, retval);
632 GC_delete_gc_thread(me);
634 # if DEBUG_CYGWIN_THREADS
635 GC_printf3("thread 0x%x(0x%x) completed join with thread 0x%x.\n",
636 (int)pthread_self(), GetCurrentThreadId(), (int)pthread_id);
642 /* Cygwin-pthreads calls CreateThread internally, but it's not
643 * easily interceptible by us..
644 * so intercept pthread_create instead
647 GC_pthread_create(pthread_t *new_thread,
648 const pthread_attr_t *attr,
649 void *(*start_routine)(void *), void *arg) {
651 struct start_info * si;
653 if (!GC_is_initialized) GC_init();
654 /* make sure GC is initialized (i.e. main thread is attached) */
656 /* This is otherwise saved only in an area mmapped by the thread */
657 /* library, which isn't visible to the collector. */
658 si = GC_malloc_uncollectable(sizeof(struct start_info));
659 if (0 == si) return(EAGAIN);
661 si -> start_routine = start_routine;
664 pthread_attr_getdetachstate(attr, &si->detached)
665 == PTHREAD_CREATE_DETACHED) {
669 # if DEBUG_CYGWIN_THREADS
670 GC_printf2("About to create a thread from 0x%x(0x%x)\n",
671 (int)pthread_self(), GetCurrentThreadId);
673 result = pthread_create(new_thread, attr, GC_start_routine, si);
675 if (result) { /* failure */
682 void * GC_start_routine(void * arg)
684 struct start_info * si = arg;
686 void *(*start)(void *);
688 pthread_t pthread_id;
693 # if DEBUG_CYGWIN_THREADS
694 GC_printf2("thread 0x%x(0x%x) starting...\n",(int)pthread_self(),
695 GetCurrentThreadId());
698 /* If a GC occurs before the thread is registered, that GC will */
699 /* ignore this thread. That's fine, since it will block trying to */
700 /* acquire the allocation lock, and won't yet hold interesting */
703 /* We register the thread here instead of in the parent, so that */
704 /* we don't need to hold the allocation lock during pthread_create. */
705 me = GC_new_thread();
708 start = si -> start_routine;
709 start_arg = si -> arg;
710 if (si-> detached) me -> flags |= DETACHED;
711 me -> pthread_id = pthread_id = pthread_self();
713 GC_free(si); /* was allocated uncollectable */
715 pthread_cleanup_push(GC_thread_exit_proc, (void *)me);
716 result = (*start)(start_arg);
717 me -> status = result;
718 pthread_cleanup_pop(0);
720 # if DEBUG_CYGWIN_THREADS
721 GC_printf2("thread 0x%x(0x%x) returned from start routine.\n",
722 (int)pthread_self(),GetCurrentThreadId());
728 void GC_thread_exit_proc(void *arg)
730 GC_thread me = (GC_thread)arg;
733 # if DEBUG_CYGWIN_THREADS
734 GC_printf2("thread 0x%x(0x%x) called pthread_exit().\n",
735 (int)pthread_self(),GetCurrentThreadId());
739 if (me -> flags & DETACHED) {
740 GC_delete_thread(GetCurrentThreadId());
742 /* deallocate it as part of join */
743 me -> flags |= FINISHED;
748 /* nothing required here... */
749 int GC_pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) {
750 return pthread_sigmask(how, set, oset);
753 int GC_pthread_detach(pthread_t thread)
756 GC_thread thread_gc_id;
759 thread_gc_id = GC_lookup_thread(thread);
761 result = pthread_detach(thread);
764 thread_gc_id -> flags |= DETACHED;
765 /* Here the pthread thread id may have been recycled. */
766 if (thread_gc_id -> flags & FINISHED) {
767 GC_delete_gc_thread(thread_gc_id);
774 #else /* !CYGWIN32 */
777 * We avoid acquiring locks here, since this doesn't seem to be preemptable.
778 * Pontus Rydin suggests wrapping the thread start routine instead.
780 #if defined(GC_DLL) || defined(GC_INSIDE_DLL)
781 BOOL WINAPI DllMain(HINSTANCE inst, ULONG reason, LPVOID reserved)
784 case DLL_PROCESS_ATTACH:
785 GC_init(); /* Force initialization before thread attach. */
787 case DLL_THREAD_ATTACH:
788 GC_ASSERT(GC_thr_initialized);
789 if (GC_main_thread != GetCurrentThreadId()) {
791 } /* o.w. we already did it during GC_thr_init(), called by GC_init() */
794 case DLL_THREAD_DETACH:
795 GC_delete_thread(GetCurrentThreadId());
798 case DLL_PROCESS_DETACH:
803 for (i = 0; i <= GC_get_max_thread_index(); ++i)
805 if (thread_table[i].in_use)
806 GC_delete_gc_thread(thread_table + i);
811 DeleteCriticalSection(&GC_allocate_ml);
819 #endif /* !CYGWIN32 */
821 # endif /* !MSWINCE */
823 #endif /* GC_WIN32_THREADS */