* call.c (z_candidate::template_decl): Rename from template.
[official-gcc.git] / boehm-gc / win32_threads.c
blob7a1dbe27cd294ceff6f1512c23d108d9d4a4a721
1 #if defined(GC_WIN32_THREADS)
3 #include "private/gc_priv.h"
4 #include <windows.h>
6 #ifdef CYGWIN32
7 # include <errno.h>
9 /* Cygwin-specific forward decls */
10 # undef pthread_create
11 # undef pthread_sigmask
12 # undef pthread_join
13 # undef dlopen
15 # define DEBUG_CYGWIN_THREADS 0
17 void * GC_start_routine(void * arg);
18 void GC_thread_exit_proc(void *arg);
20 #endif
22 /* The type of the first argument to InterlockedExchange. */
23 /* Documented to be LONG volatile *, but at least gcc likes */
24 /* this better. */
25 typedef LONG * IE_t;
27 #ifndef MAX_THREADS
28 # define MAX_THREADS 256
29 /* FIXME: */
30 /* Things may get quite slow for large numbers of threads, */
31 /* since we look them up with sequential search. */
32 #endif
34 GC_bool GC_thr_initialized = FALSE;
36 DWORD GC_main_thread = 0;
38 struct GC_thread_Rep {
39 LONG in_use; /* Updated without lock. */
40 /* We assert that unused */
41 /* entries have invalid ids of */
42 /* zero and zero stack fields. */
43 DWORD id;
44 HANDLE handle;
45 ptr_t stack_base; /* The cold end of the stack. */
46 /* 0 ==> entry not valid. */
47 /* !in_use ==> stack_base == 0 */
48 GC_bool suspended;
50 # ifdef CYGWIN32
51 void *status; /* hold exit value until join in case it's a pointer */
52 pthread_t pthread_id;
53 short flags; /* Protected by GC lock. */
54 # define FINISHED 1 /* Thread has exited. */
55 # define DETACHED 2 /* Thread is intended to be detached. */
56 # endif
59 typedef volatile struct GC_thread_Rep * GC_thread;
62 * We generally assume that volatile ==> memory ordering, at least among
63 * volatiles.
66 volatile GC_bool GC_please_stop = FALSE;
68 volatile struct GC_thread_Rep thread_table[MAX_THREADS];
70 volatile LONG GC_max_thread_index = 0; /* Largest index in thread_table */
71 /* that was ever used. */
73 extern LONG WINAPI GC_write_fault_handler(struct _EXCEPTION_POINTERS *exc_info);
76 * This may be called from DllMain, and hence operates under unusual
77 * constraints.
79 static GC_thread GC_new_thread(void) {
80 int i;
81 /* It appears to be unsafe to acquire a lock here, since this */
82 /* code is apparently not preeemptible on some systems. */
83 /* (This is based on complaints, not on Microsoft's official */
84 /* documentation, which says this should perform "only simple */
85 /* initialization tasks".) */
86 /* Hence we make do with nonblocking synchronization. */
88 /* The following should be a noop according to the win32 */
89 /* documentation. There is empirical evidence that it */
90 /* isn't. - HB */
91 # if defined(MPROTECT_VDB)
92 if (GC_incremental) SetUnhandledExceptionFilter(GC_write_fault_handler);
93 # endif
94 /* cast away volatile qualifier */
95 for (i = 0; InterlockedExchange((IE_t)&thread_table[i].in_use,1) != 0; i++) {
96 /* Compare-and-swap would make this cleaner, but that's not */
97 /* supported before Windows 98 and NT 4.0. In Windows 2000, */
98 /* InterlockedExchange is supposed to be replaced by */
99 /* InterlockedExchangePointer, but that's not really what I */
100 /* want here. */
101 if (i == MAX_THREADS - 1)
102 ABORT("too many threads");
104 /* Update GC_max_thread_index if necessary. The following is safe, */
105 /* and unlike CompareExchange-based solutions seems to work on all */
106 /* Windows95 and later platforms. */
107 /* Unfortunately, GC_max_thread_index may be temporarily out of */
108 /* bounds, so readers have to compensate. */
109 while (i > GC_max_thread_index) {
110 InterlockedIncrement((IE_t)&GC_max_thread_index);
112 if (GC_max_thread_index >= MAX_THREADS) {
113 /* We overshot due to simultaneous increments. */
114 /* Setting it to MAX_THREADS-1 is always safe. */
115 GC_max_thread_index = MAX_THREADS - 1;
118 # ifdef CYGWIN32
119 thread_table[i].pthread_id = pthread_self();
120 # endif
121 if (!DuplicateHandle(GetCurrentProcess(),
122 GetCurrentThread(),
123 GetCurrentProcess(),
124 (HANDLE*)&thread_table[i].handle,
127 DUPLICATE_SAME_ACCESS)) {
128 DWORD last_error = GetLastError();
129 GC_printf1("Last error code: %lx\n", last_error);
130 ABORT("DuplicateHandle failed");
132 thread_table[i].stack_base = GC_get_stack_base();
133 /* Up until this point, GC_psuh_all_stacks considers this thread */
134 /* invalid. */
135 if (thread_table[i].stack_base == NULL)
136 ABORT("Failed to find stack base in GC_new_thread");
137 /* Up until this point, this entry is viewed as reserved but invalid */
138 /* by GC_delete_thread. */
139 thread_table[i].id = GetCurrentThreadId();
140 /* If this thread is being created while we are trying to stop */
141 /* the world, wait here. Hopefully this can't happen on any */
142 /* systems that don't allow us to block here. */
143 while (GC_please_stop) Sleep(20);
144 return thread_table + i;
148 * GC_max_thread_index may temporarily be larger than MAX_THREADS.
149 * To avoid subscript errors, we check on access.
151 #ifdef __GNUC__
152 __inline__
153 #endif
154 LONG GC_get_max_thread_index()
156 LONG my_max = GC_max_thread_index;
158 if (my_max >= MAX_THREADS) return MAX_THREADS-1;
159 return my_max;
162 /* This is intended to be lock-free, though that */
163 /* assumes that the CloseHandle becomes visible before the */
164 /* in_use assignment. */
165 static void GC_delete_gc_thread(GC_thread thr)
167 CloseHandle(thr->handle);
168 /* cast away volatile qualifier */
169 thr->stack_base = 0;
170 thr->id = 0;
171 # ifdef CYGWIN32
172 thr->pthread_id = 0;
173 # endif /* CYGWIN32 */
174 thr->in_use = FALSE;
177 static void GC_delete_thread(DWORD thread_id) {
178 int i;
179 LONG my_max = GC_get_max_thread_index();
181 for (i = 0;
182 i <= my_max &&
183 (!thread_table[i].in_use || thread_table[i].id != thread_id);
184 /* Must still be in_use, since nobody else can store our thread_id. */
185 i++) {}
186 if (i > my_max) {
187 WARN("Removing nonexisiting thread %ld\n", (GC_word)thread_id);
188 } else {
189 GC_delete_gc_thread(thread_table+i);
194 #ifdef CYGWIN32
196 /* Return a GC_thread corresponding to a given pthread_t. */
197 /* Returns 0 if it's not there. */
198 /* We assume that this is only called for pthread ids that */
199 /* have not yet terminated or are still joinable. */
200 static GC_thread GC_lookup_thread(pthread_t id)
202 int i;
203 LONG my_max = GC_get_max_thread_index();
205 for (i = 0;
206 i <= my_max &&
207 (!thread_table[i].in_use || thread_table[i].pthread_id != id
208 || !thread_table[i].in_use);
209 /* Must still be in_use, since nobody else can store our thread_id. */
210 i++);
211 if (i > my_max) return 0;
212 return thread_table + i;
215 #endif /* CYGWIN32 */
217 void GC_push_thread_structures GC_PROTO((void))
219 /* Unlike the other threads implementations, the thread table here */
220 /* contains no pointers to the collectable heap. Thus we have */
221 /* no private structures we need to preserve. */
222 # ifdef CYGWIN32
223 { int i; /* pthreads may keep a pointer in the thread exit value */
224 LONG my_max = GC_get_max_thread_index();
226 for (i = 0; i <= my_max; i++)
227 if (thread_table[i].in_use)
228 GC_push_all((ptr_t)&(thread_table[i].status),
229 (ptr_t)(&(thread_table[i].status)+1));
231 # endif
234 void GC_stop_world()
236 DWORD thread_id = GetCurrentThreadId();
237 int i;
239 if (!GC_thr_initialized) ABORT("GC_stop_world() called before GC_thr_init()");
241 GC_please_stop = TRUE;
242 for (i = 0; i <= GC_get_max_thread_index(); i++)
243 if (thread_table[i].stack_base != 0
244 && thread_table[i].id != thread_id) {
245 # ifdef MSWINCE
246 /* SuspendThread will fail if thread is running kernel code */
247 while (SuspendThread(thread_table[i].handle) == (DWORD)-1)
248 Sleep(10);
249 # else
250 /* Apparently the Windows 95 GetOpenFileName call creates */
251 /* a thread that does not properly get cleaned up, and */
252 /* SuspendThread on its descriptor may provoke a crash. */
253 /* This reduces the probability of that event, though it still */
254 /* appears there's a race here. */
255 DWORD exitCode;
256 if (GetExitCodeThread(thread_table[i].handle,&exitCode) &&
257 exitCode != STILL_ACTIVE) {
258 thread_table[i].stack_base = 0; /* prevent stack from being pushed */
259 # ifndef CYGWIN32
260 /* this breaks pthread_join on Cygwin, which is guaranteed to */
261 /* only see user pthreads */
262 thread_table[i].in_use = FALSE;
263 CloseHandle(thread_table[i].handle);
264 # endif
265 continue;
267 if (SuspendThread(thread_table[i].handle) == (DWORD)-1)
268 ABORT("SuspendThread failed");
269 # endif
270 thread_table[i].suspended = TRUE;
274 void GC_start_world()
276 DWORD thread_id = GetCurrentThreadId();
277 int i;
278 LONG my_max = GC_get_max_thread_index();
280 for (i = 0; i <= my_max; i++)
281 if (thread_table[i].stack_base != 0 && thread_table[i].suspended
282 && thread_table[i].id != thread_id) {
283 if (ResumeThread(thread_table[i].handle) == (DWORD)-1)
284 ABORT("ResumeThread failed");
285 thread_table[i].suspended = FALSE;
287 GC_please_stop = FALSE;
290 # ifdef _MSC_VER
291 # pragma warning(disable:4715)
292 # endif
293 ptr_t GC_current_stackbottom()
295 DWORD thread_id = GetCurrentThreadId();
296 int i;
297 LONG my_max = GC_get_max_thread_index();
299 for (i = 0; i <= my_max; i++)
300 if (thread_table[i].stack_base && thread_table[i].id == thread_id)
301 return thread_table[i].stack_base;
302 ABORT("no thread table entry for current thread");
304 # ifdef _MSC_VER
305 # pragma warning(default:4715)
306 # endif
308 # ifdef MSWINCE
309 /* The VirtualQuery calls below won't work properly on WinCE, but */
310 /* since each stack is restricted to an aligned 64K region of */
311 /* virtual memory we can just take the next lowest multiple of 64K. */
312 # define GC_get_stack_min(s) \
313 ((ptr_t)(((DWORD)(s) - 1) & 0xFFFF0000))
314 # else
315 static ptr_t GC_get_stack_min(ptr_t s)
317 ptr_t bottom;
318 MEMORY_BASIC_INFORMATION info;
319 VirtualQuery(s, &info, sizeof(info));
320 do {
321 bottom = info.BaseAddress;
322 VirtualQuery(bottom - 1, &info, sizeof(info));
323 } while ((info.Protect & PAGE_READWRITE)
324 && !(info.Protect & PAGE_GUARD));
325 return(bottom);
327 # endif
329 void GC_push_all_stacks()
331 DWORD thread_id = GetCurrentThreadId();
332 GC_bool found_me = FALSE;
333 int i;
334 int dummy;
335 ptr_t sp, stack_min;
336 GC_thread thread;
337 LONG my_max = GC_get_max_thread_index();
339 for (i = 0; i <= my_max; i++) {
340 thread = thread_table + i;
341 if (thread -> in_use && thread -> stack_base) {
342 if (thread -> id == thread_id) {
343 sp = (ptr_t) &dummy;
344 found_me = TRUE;
345 } else {
346 CONTEXT context;
347 context.ContextFlags = CONTEXT_INTEGER|CONTEXT_CONTROL;
348 if (!GetThreadContext(thread_table[i].handle, &context))
349 ABORT("GetThreadContext failed");
351 /* Push all registers that might point into the heap. Frame */
352 /* pointer registers are included in case client code was */
353 /* compiled with the 'omit frame pointer' optimisation. */
354 # define PUSH1(reg) GC_push_one((word)context.reg)
355 # define PUSH2(r1,r2) PUSH1(r1), PUSH1(r2)
356 # define PUSH4(r1,r2,r3,r4) PUSH2(r1,r2), PUSH2(r3,r4)
357 # if defined(I386)
358 PUSH4(Edi,Esi,Ebx,Edx), PUSH2(Ecx,Eax), PUSH1(Ebp);
359 sp = (ptr_t)context.Esp;
360 # elif defined(ARM32)
361 PUSH4(R0,R1,R2,R3),PUSH4(R4,R5,R6,R7),PUSH4(R8,R9,R10,R11),PUSH1(R12);
362 sp = (ptr_t)context.Sp;
363 # elif defined(SHx)
364 PUSH4(R0,R1,R2,R3), PUSH4(R4,R5,R6,R7), PUSH4(R8,R9,R10,R11);
365 PUSH2(R12,R13), PUSH1(R14);
366 sp = (ptr_t)context.R15;
367 # elif defined(MIPS)
368 PUSH4(IntAt,IntV0,IntV1,IntA0), PUSH4(IntA1,IntA2,IntA3,IntT0);
369 PUSH4(IntT1,IntT2,IntT3,IntT4), PUSH4(IntT5,IntT6,IntT7,IntS0);
370 PUSH4(IntS1,IntS2,IntS3,IntS4), PUSH4(IntS5,IntS6,IntS7,IntT8);
371 PUSH4(IntT9,IntK0,IntK1,IntS8);
372 sp = (ptr_t)context.IntSp;
373 # elif defined(PPC)
374 PUSH4(Gpr0, Gpr3, Gpr4, Gpr5), PUSH4(Gpr6, Gpr7, Gpr8, Gpr9);
375 PUSH4(Gpr10,Gpr11,Gpr12,Gpr14), PUSH4(Gpr15,Gpr16,Gpr17,Gpr18);
376 PUSH4(Gpr19,Gpr20,Gpr21,Gpr22), PUSH4(Gpr23,Gpr24,Gpr25,Gpr26);
377 PUSH4(Gpr27,Gpr28,Gpr29,Gpr30), PUSH1(Gpr31);
378 sp = (ptr_t)context.Gpr1;
379 # elif defined(ALPHA)
380 PUSH4(IntV0,IntT0,IntT1,IntT2), PUSH4(IntT3,IntT4,IntT5,IntT6);
381 PUSH4(IntT7,IntS0,IntS1,IntS2), PUSH4(IntS3,IntS4,IntS5,IntFp);
382 PUSH4(IntA0,IntA1,IntA2,IntA3), PUSH4(IntA4,IntA5,IntT8,IntT9);
383 PUSH4(IntT10,IntT11,IntT12,IntAt);
384 sp = (ptr_t)context.IntSp;
385 # else
386 # error "architecture is not supported"
387 # endif
390 stack_min = GC_get_stack_min(thread->stack_base);
392 if (sp >= stack_min && sp < thread->stack_base)
393 GC_push_all_stack(sp, thread->stack_base);
394 else {
395 WARN("Thread stack pointer 0x%lx out of range, pushing everything\n",
396 (unsigned long)sp);
397 GC_push_all_stack(stack_min, thread->stack_base);
401 if (!found_me) ABORT("Collecting from unknown thread.");
404 void GC_get_next_stack(char *start, char **lo, char **hi)
406 int i;
407 # define ADDR_LIMIT (char *)(-1L)
408 char * current_min = ADDR_LIMIT;
409 LONG my_max = GC_get_max_thread_index();
411 for (i = 0; i <= my_max; i++) {
412 char * s = (char *)thread_table[i].stack_base;
414 if (0 != s && s > start && s < current_min) {
415 current_min = s;
418 *hi = current_min;
419 if (current_min == ADDR_LIMIT) {
420 *lo = ADDR_LIMIT;
421 return;
423 *lo = GC_get_stack_min(current_min);
424 if (*lo < start) *lo = start;
427 #if !defined(CYGWIN32)
429 #if !defined(MSWINCE) && defined(GC_DLL)
431 /* We register threads from DllMain */
433 GC_API HANDLE WINAPI GC_CreateThread(
434 LPSECURITY_ATTRIBUTES lpThreadAttributes,
435 DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress,
436 LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId )
438 return CreateThread(lpThreadAttributes, dwStackSize, lpStartAddress,
439 lpParameter, dwCreationFlags, lpThreadId);
442 #else /* defined(MSWINCE) || !defined(GC_DLL)) */
444 /* We have no DllMain to take care of new threads. Thus we */
445 /* must properly intercept thread creation. */
447 typedef struct {
448 LPTHREAD_START_ROUTINE start;
449 LPVOID param;
450 } thread_args;
452 static DWORD WINAPI thread_start(LPVOID arg);
454 GC_API HANDLE WINAPI GC_CreateThread(
455 LPSECURITY_ATTRIBUTES lpThreadAttributes,
456 DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress,
457 LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId )
459 HANDLE thread_h = NULL;
461 thread_args *args;
463 if (!GC_is_initialized) GC_init();
464 /* make sure GC is initialized (i.e. main thread is attached) */
466 args = GC_malloc_uncollectable(sizeof(thread_args));
467 /* Handed off to and deallocated by child thread. */
468 if (0 == args) {
469 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
470 return NULL;
473 /* set up thread arguments */
474 args -> start = lpStartAddress;
475 args -> param = lpParameter;
477 thread_h = CreateThread(lpThreadAttributes,
478 dwStackSize, thread_start,
479 args, dwCreationFlags,
480 lpThreadId);
482 return thread_h;
485 static DWORD WINAPI thread_start(LPVOID arg)
487 DWORD ret = 0;
488 thread_args *args = (thread_args *)arg;
490 GC_new_thread();
492 /* Clear the thread entry even if we exit with an exception. */
493 /* This is probably pointless, since an uncaught exception is */
494 /* supposed to result in the process being killed. */
495 #ifndef __GNUC__
496 __try {
497 #endif /* __GNUC__ */
498 ret = args->start (args->param);
499 #ifndef __GNUC__
500 } __finally {
501 #endif /* __GNUC__ */
502 GC_free(args);
503 GC_delete_thread(GetCurrentThreadId());
504 #ifndef __GNUC__
506 #endif /* __GNUC__ */
508 return ret;
510 #endif /* !defined(MSWINCE) && !(defined(__MINGW32__) && !defined(_DLL)) */
512 #endif /* !CYGWIN32 */
514 #ifdef MSWINCE
516 typedef struct {
517 HINSTANCE hInstance;
518 HINSTANCE hPrevInstance;
519 LPWSTR lpCmdLine;
520 int nShowCmd;
521 } main_thread_args;
523 DWORD WINAPI main_thread_start(LPVOID arg);
525 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
526 LPWSTR lpCmdLine, int nShowCmd)
528 DWORD exit_code = 1;
530 main_thread_args args = {
531 hInstance, hPrevInstance, lpCmdLine, nShowCmd
533 HANDLE thread_h;
534 DWORD thread_id;
536 /* initialize everything */
537 GC_init();
539 /* start the main thread */
540 thread_h = GC_CreateThread(
541 NULL, 0, main_thread_start, &args, 0, &thread_id);
543 if (thread_h != NULL)
545 WaitForSingleObject (thread_h, INFINITE);
546 GetExitCodeThread (thread_h, &exit_code);
547 CloseHandle (thread_h);
550 GC_deinit();
551 DeleteCriticalSection(&GC_allocate_ml);
553 return (int) exit_code;
556 DWORD WINAPI main_thread_start(LPVOID arg)
558 main_thread_args * args = (main_thread_args *) arg;
560 return (DWORD) GC_WinMain (args->hInstance, args->hPrevInstance,
561 args->lpCmdLine, args->nShowCmd);
564 # else /* !MSWINCE */
566 /* Called by GC_init() - we hold the allocation lock. */
567 void GC_thr_init() {
568 if (GC_thr_initialized) return;
569 GC_main_thread = GetCurrentThreadId();
570 GC_thr_initialized = TRUE;
572 /* Add the initial thread, so we can stop it. */
573 GC_new_thread();
576 #ifdef CYGWIN32
578 struct start_info {
579 void *(*start_routine)(void *);
580 void *arg;
581 GC_bool detached;
584 int GC_pthread_join(pthread_t pthread_id, void **retval) {
585 int result;
586 int i;
587 GC_thread me;
589 # if DEBUG_CYGWIN_THREADS
590 GC_printf3("thread 0x%x(0x%x) is joining thread 0x%x.\n",
591 (int)pthread_self(), GetCurrentThreadId(), (int)pthread_id);
592 # endif
594 /* Thread being joined might not have registered itself yet. */
595 /* After the join,thread id may have been recycled. */
596 /* FIXME: It would be better if this worked more like */
597 /* pthread_support.c. */
599 while ((me = GC_lookup_thread(pthread_id)) == 0) Sleep(10);
601 result = pthread_join(pthread_id, retval);
603 GC_delete_gc_thread(me);
605 # if DEBUG_CYGWIN_THREADS
606 GC_printf3("thread 0x%x(0x%x) completed join with thread 0x%x.\n",
607 (int)pthread_self(), GetCurrentThreadId(), (int)pthread_id);
608 # endif
610 return result;
613 /* Cygwin-pthreads calls CreateThread internally, but it's not
614 * easily interceptible by us..
615 * so intercept pthread_create instead
618 GC_pthread_create(pthread_t *new_thread,
619 const pthread_attr_t *attr,
620 void *(*start_routine)(void *), void *arg) {
621 int result;
622 struct start_info * si;
624 if (!GC_is_initialized) GC_init();
625 /* make sure GC is initialized (i.e. main thread is attached) */
627 /* This is otherwise saved only in an area mmapped by the thread */
628 /* library, which isn't visible to the collector. */
629 si = GC_malloc_uncollectable(sizeof(struct start_info));
630 if (0 == si) return(EAGAIN);
632 si -> start_routine = start_routine;
633 si -> arg = arg;
634 if (attr != 0 &&
635 pthread_attr_getdetachstate(attr, &si->detached)
636 == PTHREAD_CREATE_DETACHED) {
637 si->detached = TRUE;
640 # if DEBUG_CYGWIN_THREADS
641 GC_printf2("About to create a thread from 0x%x(0x%x)\n",
642 (int)pthread_self(), GetCurrentThreadId);
643 # endif
644 result = pthread_create(new_thread, attr, GC_start_routine, si);
646 if (result) { /* failure */
647 GC_free(si);
650 return(result);
653 void * GC_start_routine(void * arg)
655 struct start_info * si = arg;
656 void * result;
657 void *(*start)(void *);
658 void *start_arg;
659 pthread_t pthread_id;
660 GC_thread me;
661 GC_bool detached;
662 int i;
664 # if DEBUG_CYGWIN_THREADS
665 GC_printf2("thread 0x%x(0x%x) starting...\n",(int)pthread_self(),
666 GetCurrentThreadId());
667 # endif
669 /* If a GC occurs before the thread is registered, that GC will */
670 /* ignore this thread. That's fine, since it will block trying to */
671 /* acquire the allocation lock, and won't yet hold interesting */
672 /* pointers. */
673 LOCK();
674 /* We register the thread here instead of in the parent, so that */
675 /* we don't need to hold the allocation lock during pthread_create. */
676 me = GC_new_thread();
677 UNLOCK();
679 start = si -> start_routine;
680 start_arg = si -> arg;
681 if (si-> detached) me -> flags |= DETACHED;
682 me -> pthread_id = pthread_id = pthread_self();
684 GC_free(si); /* was allocated uncollectable */
686 pthread_cleanup_push(GC_thread_exit_proc, (void *)me);
687 result = (*start)(start_arg);
688 me -> status = result;
689 pthread_cleanup_pop(0);
691 # if DEBUG_CYGWIN_THREADS
692 GC_printf2("thread 0x%x(0x%x) returned from start routine.\n",
693 (int)pthread_self(),GetCurrentThreadId());
694 # endif
696 return(result);
699 void GC_thread_exit_proc(void *arg)
701 GC_thread me = (GC_thread)arg;
702 int i;
704 # if DEBUG_CYGWIN_THREADS
705 GC_printf2("thread 0x%x(0x%x) called pthread_exit().\n",
706 (int)pthread_self(),GetCurrentThreadId());
707 # endif
709 LOCK();
710 if (me -> flags & DETACHED) {
711 GC_delete_thread(GetCurrentThreadId());
712 } else {
713 /* deallocate it as part of join */
714 me -> flags |= FINISHED;
716 UNLOCK();
719 /* nothing required here... */
720 int GC_pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) {
721 return pthread_sigmask(how, set, oset);
724 int GC_pthread_detach(pthread_t thread)
726 int result;
727 GC_thread thread_gc_id;
729 LOCK();
730 thread_gc_id = GC_lookup_thread(thread);
731 UNLOCK();
732 result = pthread_detach(thread);
733 if (result == 0) {
734 LOCK();
735 thread_gc_id -> flags |= DETACHED;
736 /* Here the pthread thread id may have been recycled. */
737 if (thread_gc_id -> flags & FINISHED) {
738 GC_delete_gc_thread(thread_gc_id);
740 UNLOCK();
742 return result;
745 #else /* !CYGWIN32 */
748 * We avoid acquiring locks here, since this doesn't seem to be preemptable.
749 * Pontus Rydin suggests wrapping the thread start routine instead.
751 #ifdef GC_DLL
752 BOOL WINAPI DllMain(HINSTANCE inst, ULONG reason, LPVOID reserved)
754 switch (reason) {
755 case DLL_PROCESS_ATTACH:
756 GC_init(); /* Force initialization before thread attach. */
757 /* fall through */
758 case DLL_THREAD_ATTACH:
759 GC_ASSERT(GC_thr_initialized);
760 if (GC_main_thread != GetCurrentThreadId()) {
761 GC_new_thread();
762 } /* o.w. we already did it during GC_thr_init(), called by GC_init() */
763 break;
765 case DLL_THREAD_DETACH:
766 GC_delete_thread(GetCurrentThreadId());
767 break;
769 case DLL_PROCESS_DETACH:
771 int i;
773 LOCK();
774 for (i = 0; i <= GC_get_max_thread_index(); ++i)
776 if (thread_table[i].in_use)
777 GC_delete_gc_thread(thread_table + i);
779 UNLOCK();
781 GC_deinit();
782 DeleteCriticalSection(&GC_allocate_ml);
784 break;
787 return TRUE;
789 #endif /* GC_DLL */
790 #endif /* !CYGWIN32 */
792 # endif /* !MSWINCE */
794 #endif /* GC_WIN32_THREADS */