2 * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
3 * Copyright (c) 1996 by Silicon Graphics. All rights reserved.
4 * Copyright (c) 1998 by Fergus Henderson. All rights reserved.
5 * Copyright (c) 2000-2008 by Hewlett-Packard Development Company.
8 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
11 * Permission is hereby granted to use or copy this program
12 * for any purpose, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
18 #include "private/gc_priv.h"
20 #if defined(GC_WIN32_THREADS)
22 #ifndef WIN32_LEAN_AND_MEAN
23 # define WIN32_LEAN_AND_MEAN 1
28 #ifdef THREAD_LOCAL_ALLOC
29 # include "private/thread_local_alloc.h"
30 #endif /* THREAD_LOCAL_ALLOC */
32 /* Allocation lock declarations. */
33 #if !defined(USE_PTHREAD_LOCKS)
34 GC_INNER CRITICAL_SECTION GC_allocate_ml
;
35 GC_INNER DWORD GC_lock_holder
= NO_THREAD
;
36 /* Thread id for current holder of allocation lock */
38 GC_INNER pthread_mutex_t GC_allocate_ml
= PTHREAD_MUTEX_INITIALIZER
;
39 GC_INNER
unsigned long GC_lock_holder
= NO_THREAD
;
48 # include <errno.h> /* for EAGAIN */
50 /* Cygwin-specific forward decls */
51 # undef pthread_create
53 # undef pthread_detach
55 # ifndef GC_NO_PTHREAD_SIGMASK
56 # undef pthread_sigmask
59 STATIC
void * GC_pthread_start(void * arg
);
60 STATIC
void GC_thread_exit_proc(void *arg
);
63 # ifdef CAN_CALL_ATFORK
70 /* Force DONT_USE_SIGNALANDWAIT implementation of PARALLEL_MARK */
71 /* for WinCE (since Win32 SignalObjectAndWait() is missing). */
72 # ifndef DONT_USE_SIGNALANDWAIT
73 # define DONT_USE_SIGNALANDWAIT
76 # include <process.h> /* For _beginthreadex, _endthreadex */
77 # include <errno.h> /* for errno, EAGAIN */
82 /* DllMain-based thread registration is currently incompatible */
83 /* with thread-local allocation, pthreads and WinCE. */
84 #if (defined(GC_DLL) || defined(GC_INSIDE_DLL)) \
85 && !defined(GC_NO_THREADS_DISCOVERY) && !defined(MSWINCE) \
86 && !defined(THREAD_LOCAL_ALLOC) && !defined(GC_PTHREADS)
87 # include "atomic_ops.h"
89 /* This code operates in two distinct modes, depending on */
90 /* the setting of GC_win32_dll_threads. */
91 /* If GC_win32_dll_threads is set, all threads in the process */
92 /* are implicitly registered with the GC by DllMain. */
93 /* No explicit registration is required, and attempts at */
94 /* explicit registration are ignored. This mode is */
95 /* very different from the Posix operation of the collector. */
96 /* In this mode access to the thread table is lock-free. */
97 /* Hence there is a static limit on the number of threads. */
99 # ifdef GC_DISCOVER_TASK_THREADS
100 /* GC_DISCOVER_TASK_THREADS should be used if DllMain-based */
101 /* thread registration is required but it is impossible to */
102 /* call GC_use_threads_discovery before other GC routines. */
103 # define GC_win32_dll_threads TRUE
105 STATIC GC_bool GC_win32_dll_threads
= FALSE
;
106 /* GC_win32_dll_threads must be set (if needed) at the */
107 /* application initialization time, i.e. before any */
108 /* collector or thread calls. We make it a "dynamic" */
109 /* option only to avoid multiple library versions. */
113 /* If GC_win32_dll_threads is FALSE (or the collector is */
114 /* built without GC_DLL defined), things operate in a way */
115 /* that is very similar to Posix platforms, and new threads */
116 /* must be registered with the collector, e.g. by using */
117 /* preprocessor-based interception of the thread primitives. */
118 /* In this case, we use a real data structure for the thread */
119 /* table. Note that there is no equivalent of linker-based */
120 /* call interception, since we don't have ELF-like */
121 /* facilities. The Windows analog appears to be "API */
122 /* hooking", which really seems to be a standard way to */
123 /* do minor binary rewriting (?). I'd prefer not to have */
124 /* the basic collector rely on such facilities, but an */
125 /* optional package that intercepts thread calls this way */
126 /* would probably be nice. */
127 # ifndef GC_NO_THREADS_DISCOVERY
128 # define GC_NO_THREADS_DISCOVERY
130 # define GC_win32_dll_threads FALSE
132 # define MAX_THREADS 1 /* dll_thread_table[] is always empty. */
133 #endif /* GC_NO_THREADS_DISCOVERY */
135 /* We have two versions of the thread table. Which one */
136 /* we us depends on whether or not GC_win32_dll_threads */
137 /* is set. Note that before initialization, we don't */
138 /* add any entries to either table, even if DllMain is */
139 /* called. The main thread will be added on */
140 /* initialization. */
142 /* The type of the first argument to InterlockedExchange. */
143 /* Documented to be LONG volatile *, but at least gcc likes */
147 STATIC GC_bool GC_thr_initialized
= FALSE
;
149 GC_INNER GC_bool GC_need_to_lock
= FALSE
;
151 static GC_bool parallel_initialized
= FALSE
;
153 /* GC_use_threads_discovery() is currently incompatible with pthreads */
154 /* and WinCE. It might be possible to get DllMain-based thread */
155 /* registration to work with Cygwin, but if you try it then you are on */
157 GC_API
void GC_CALL
GC_use_threads_discovery(void)
159 # ifdef GC_NO_THREADS_DISCOVERY
160 ABORT("GC DllMain-based thread registration unsupported");
162 /* Turn on GC_win32_dll_threads. */
163 GC_ASSERT(!parallel_initialized
);
164 # ifndef GC_DISCOVER_TASK_THREADS
165 GC_win32_dll_threads
= TRUE
;
171 STATIC DWORD GC_main_thread
= 0;
173 #define ADDR_LIMIT ((ptr_t)(word)-1)
175 struct GC_Thread_Rep
{
177 # ifndef GC_NO_THREADS_DISCOVERY
178 volatile AO_t in_use
;
179 /* Updated without lock. */
180 /* We assert that unused */
181 /* entries have invalid ids of */
182 /* zero and zero stack fields. */
183 /* Used only with GC_win32_dll_threads. */
185 struct GC_Thread_Rep
* next
;
186 /* Hash table link without */
187 /* GC_win32_dll_threads. */
188 /* More recently allocated threads */
189 /* with a given pthread id come */
190 /* first. (All but the first are */
191 /* guaranteed to be dead, but we may */
192 /* not yet have registered the join.) */
193 } tm
; /* table_management */
197 /* According to MSDN specs for WinCE targets: */
198 /* - DuplicateHandle() is not applicable to thread handles; and */
199 /* - the value returned by GetCurrentThreadId() could be used as */
200 /* a "real" thread handle (for SuspendThread(), ResumeThread() and */
201 /* GetThreadContext()). */
202 # define THREAD_HANDLE(t) (HANDLE)(word)(t)->id
205 # define THREAD_HANDLE(t) (t)->handle
208 ptr_t stack_base
; /* The cold end of the stack. */
209 /* 0 ==> entry not valid. */
210 /* !in_use ==> stack_base == 0 */
211 ptr_t last_stack_min
; /* Last known minimum (hottest) address */
212 /* in stack or ADDR_LIMIT if unset */
214 ptr_t backing_store_end
;
215 ptr_t backing_store_ptr
;
218 ptr_t thread_blocked_sp
; /* Protected by GC lock. */
219 /* NULL value means thread unblocked. */
220 /* If set to non-NULL, thread will */
221 /* acquire GC lock before doing any */
222 /* pointer manipulations. Thus it does */
223 /* not need to stop this thread. */
225 struct GC_traced_stack_sect_s
*traced_stack_sect
;
226 /* Points to the "stack section" data */
227 /* held in stack by the innermost */
228 /* GC_call_with_gc_active() of this */
229 /* thread. May be NULL. */
231 unsigned short finalizer_skipped
;
232 unsigned char finalizer_nested
;
233 /* Used by GC_check_finalizer_nested() */
234 /* to minimize the level of recursion */
235 /* when a client finalizer allocates */
236 /* memory (initially both are 0). */
238 unsigned char suspended
; /* really of GC_bool type */
241 unsigned char flags
; /* Protected by GC lock. */
242 # define FINISHED 1 /* Thread has exited. */
243 # define DETACHED 2 /* Thread is intended to be detached. */
244 # define KNOWN_FINISHED(t) (((t) -> flags) & FINISHED)
245 pthread_t pthread_id
;
246 void *status
; /* hold exit value until join in case it's a pointer */
248 # define KNOWN_FINISHED(t) 0
251 # ifdef THREAD_LOCAL_ALLOC
252 struct thread_local_freelists tlfs
;
256 typedef struct GC_Thread_Rep
* GC_thread
;
257 typedef volatile struct GC_Thread_Rep
* GC_vthread
;
259 #ifndef GC_NO_THREADS_DISCOVERY
260 /* We assumed that volatile ==> memory ordering, at least among */
261 /* volatiles. This code should consistently use atomic_ops. */
262 STATIC
volatile GC_bool GC_please_stop
= FALSE
;
263 #elif defined(GC_ASSERTIONS)
264 STATIC GC_bool GC_please_stop
= FALSE
;
268 * We track thread attachments while the world is supposed to be stopped.
269 * Unfortunately, we can't stop them from starting, since blocking in
270 * DllMain seems to cause the world to deadlock. Thus we have to recover
271 * If we notice this in the middle of marking.
274 #ifndef GC_NO_THREADS_DISCOVERY
275 STATIC
volatile AO_t GC_attached_thread
= FALSE
;
278 #if !defined(__GNUC__)
279 /* Return TRUE if an thread was attached since we last asked or */
280 /* since GC_attached_thread was explicitly reset. */
281 GC_bool
GC_started_thread_while_stopped(void)
283 # ifndef GC_NO_THREADS_DISCOVERY
284 if (GC_win32_dll_threads
) {
285 # ifdef AO_HAVE_compare_and_swap_release
286 if (AO_compare_and_swap_release(&GC_attached_thread
, TRUE
,
290 AO_nop_full(); /* Prior heap reads need to complete earlier. */
291 if (AO_load(&GC_attached_thread
)) {
292 AO_store(&GC_attached_thread
, FALSE
);
300 #endif /* !__GNUC__ */
302 /* Thread table used if GC_win32_dll_threads is set. */
303 /* This is a fixed size array. */
304 /* Since we use runtime conditionals, both versions */
305 /* are always defined. */
307 # define MAX_THREADS 512
310 /* Things may get quite slow for large numbers of threads, */
311 /* since we look them up with sequential search. */
312 volatile struct GC_Thread_Rep dll_thread_table
[MAX_THREADS
];
314 STATIC
volatile LONG GC_max_thread_index
= 0;
315 /* Largest index in dll_thread_table */
316 /* that was ever used. */
318 /* And now the version used if GC_win32_dll_threads is not set. */
319 /* This is a chained hash table, with much of the code borrowed */
320 /* From the Posix implementation. */
321 #ifndef THREAD_TABLE_SZ
322 # define THREAD_TABLE_SZ 256 /* Power of 2 (for speed). */
324 #define THREAD_TABLE_INDEX(id) (((word)(id) >> 2) % THREAD_TABLE_SZ)
325 STATIC GC_thread GC_threads
[THREAD_TABLE_SZ
];
327 /* It may not be safe to allocate when we register the first thread. */
328 /* Thus we allocated one statically. It does not contain any field we */
329 /* need to push ("next" and "status" fields are unused). */
330 static struct GC_Thread_Rep first_thread
;
331 static GC_bool first_thread_used
= FALSE
;
333 /* Add a thread to GC_threads. We assume it wasn't already there. */
334 /* Caller holds allocation lock. */
335 /* Unlike the pthreads version, the id field is set by the caller. */
336 STATIC GC_thread
GC_new_thread(DWORD id
)
338 word hv
= THREAD_TABLE_INDEX(id
);
341 GC_ASSERT(I_HOLD_LOCK());
342 if (!EXPECT(first_thread_used
, TRUE
)) {
343 result
= &first_thread
;
344 first_thread_used
= TRUE
;
346 GC_ASSERT(!GC_win32_dll_threads
);
347 result
= (struct GC_Thread_Rep
*)
348 GC_INTERNAL_MALLOC(sizeof(struct GC_Thread_Rep
), NORMAL
);
349 /* result can be NULL */
350 if (result
== 0) return(0);
352 /* result -> id = id; Done by caller. */
353 result
-> tm
.next
= GC_threads
[hv
];
354 GC_threads
[hv
] = result
;
356 GC_ASSERT(result
-> flags
== 0);
358 GC_ASSERT(result
-> thread_blocked_sp
== NULL
);
362 STATIC GC_bool GC_in_thread_creation
= FALSE
;
363 /* Protected by allocation lock. */
365 GC_INLINE
void GC_record_stack_base(GC_vthread me
,
366 const struct GC_stack_base
*sb
)
368 me
-> stack_base
= sb
-> mem_base
;
370 me
-> backing_store_end
= sb
-> reg_base
;
372 if (me
-> stack_base
== NULL
)
373 ABORT("Bad stack base in GC_register_my_thread");
376 /* This may be called from DllMain, and hence operates under unusual */
377 /* constraints. In particular, it must be lock-free if */
378 /* GC_win32_dll_threads is set. Always called from the thread being */
379 /* added. If GC_win32_dll_threads is not set, we already hold the */
380 /* allocation lock except possibly during single-threaded startup code. */
381 STATIC GC_thread
GC_register_my_thread_inner(const struct GC_stack_base
*sb
,
386 /* The following should be a no-op according to the win32 */
387 /* documentation. There is empirical evidence that it */
389 # if defined(MPROTECT_VDB)
392 && !GC_gww_dirty_init()
395 GC_set_write_fault_handler();
398 # ifndef GC_NO_THREADS_DISCOVERY
399 if (GC_win32_dll_threads
) {
401 /* It appears to be unsafe to acquire a lock here, since this */
402 /* code is apparently not preemptible on some systems. */
403 /* (This is based on complaints, not on Microsoft's official */
404 /* documentation, which says this should perform "only simple */
405 /* initialization tasks".) */
406 /* Hence we make do with nonblocking synchronization. */
407 /* It has been claimed that DllMain is really only executed with */
408 /* a particular system lock held, and thus careful use of locking */
409 /* around code that doesn't call back into the system libraries */
410 /* might be OK. But this hasn't been tested across all win32 */
412 /* cast away volatile qualifier */
414 InterlockedExchange((void*)&dll_thread_table
[i
].tm
.in_use
, 1) != 0;
416 /* Compare-and-swap would make this cleaner, but that's not */
417 /* supported before Windows 98 and NT 4.0. In Windows 2000, */
418 /* InterlockedExchange is supposed to be replaced by */
419 /* InterlockedExchangePointer, but that's not really what I */
421 /* FIXME: We should eventually declare Win95 dead and use AO_ */
422 /* primitives here. */
423 if (i
== MAX_THREADS
- 1)
424 ABORT("Too many threads");
426 /* Update GC_max_thread_index if necessary. The following is */
427 /* safe, and unlike CompareExchange-based solutions seems to work */
428 /* on all Windows95 and later platforms. */
429 /* Unfortunately, GC_max_thread_index may be temporarily out of */
430 /* bounds, so readers have to compensate. */
431 while (i
> GC_max_thread_index
) {
432 InterlockedIncrement((IE_t
)&GC_max_thread_index
);
434 if (GC_max_thread_index
>= MAX_THREADS
) {
435 /* We overshot due to simultaneous increments. */
436 /* Setting it to MAX_THREADS-1 is always safe. */
437 GC_max_thread_index
= MAX_THREADS
- 1;
439 me
= dll_thread_table
+ i
;
442 /* else */ /* Not using DllMain */ {
443 GC_ASSERT(I_HOLD_LOCK());
444 GC_in_thread_creation
= TRUE
; /* OK to collect from unknown thread. */
445 me
= GC_new_thread(thread_id
);
446 GC_in_thread_creation
= FALSE
;
448 ABORT("Failed to allocate memory for thread registering");
451 /* me can be NULL -> segfault */
452 me
-> pthread_id
= pthread_self();
455 /* GetCurrentThread() returns a pseudohandle (a const value). */
456 if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
458 (HANDLE
*)&(me
-> handle
),
459 0 /* dwDesiredAccess */, FALSE
/* bInheritHandle */,
460 DUPLICATE_SAME_ACCESS
)) {
461 GC_COND_LOG_PRINTF("DuplicateHandle failed with error code: %d\n",
462 (int)GetLastError());
463 ABORT("DuplicateHandle failed");
466 me
-> last_stack_min
= ADDR_LIMIT
;
467 GC_record_stack_base(me
, sb
);
468 /* Up until this point, GC_push_all_stacks considers this thread */
470 /* Up until this point, this entry is viewed as reserved but invalid */
471 /* by GC_delete_thread. */
472 me
-> id
= thread_id
;
473 # if defined(THREAD_LOCAL_ALLOC)
474 GC_init_thread_local((GC_tlfs
)(&(me
->tlfs
)));
476 # ifndef GC_NO_THREADS_DISCOVERY
477 if (GC_win32_dll_threads
) {
478 if (GC_please_stop
) {
479 AO_store(&GC_attached_thread
, TRUE
);
480 AO_nop_full(); /* Later updates must become visible after this. */
482 /* We'd like to wait here, but can't, since waiting in DllMain */
483 /* provokes deadlocks. */
484 /* Thus we force marking to be restarted instead. */
488 GC_ASSERT(!GC_please_stop
);
489 /* Otherwise both we and the thread stopping code would be */
490 /* holding the allocation lock. */
492 return (GC_thread
)(me
);
496 * GC_max_thread_index may temporarily be larger than MAX_THREADS.
497 * To avoid subscript errors, we check on access.
499 GC_INLINE LONG
GC_get_max_thread_index(void)
501 LONG my_max
= GC_max_thread_index
;
502 if (my_max
>= MAX_THREADS
) return MAX_THREADS
- 1;
506 /* Return the GC_thread corresponding to a thread id. May be called */
507 /* without a lock, but should be called in contexts in which the */
508 /* requested thread cannot be asynchronously deleted, e.g. from the */
510 /* This version assumes that either GC_win32_dll_threads is set, or */
511 /* we hold the allocator lock. */
512 /* Also used (for assertion checking only) from thread_local_alloc.c. */
513 STATIC GC_thread
GC_lookup_thread_inner(DWORD thread_id
)
515 # ifndef GC_NO_THREADS_DISCOVERY
516 if (GC_win32_dll_threads
) {
518 LONG my_max
= GC_get_max_thread_index();
519 for (i
= 0; i
<= my_max
&&
520 (!AO_load_acquire(&dll_thread_table
[i
].tm
.in_use
)
521 || dll_thread_table
[i
].id
!= thread_id
);
522 /* Must still be in_use, since nobody else can store our */
527 return i
<= my_max
? (GC_thread
)(dll_thread_table
+ i
) : NULL
;
531 word hv
= THREAD_TABLE_INDEX(thread_id
);
532 register GC_thread p
= GC_threads
[hv
];
534 GC_ASSERT(I_HOLD_LOCK());
535 while (p
!= 0 && p
-> id
!= thread_id
) p
= p
-> tm
.next
;
541 # define CHECK_LOOKUP_MY_THREAD(me) \
542 if (!(me)) ABORT("GC_lookup_thread_inner(GetCurrentThreadId) failed")
544 # define CHECK_LOOKUP_MY_THREAD(me) /* empty */
547 /* Called by GC_finalize() (in case of an allocation failure observed). */
548 /* GC_reset_finalizer_nested() is the same as in pthread_support.c. */
549 GC_INNER
void GC_reset_finalizer_nested(void)
551 GC_thread me
= GC_lookup_thread_inner(GetCurrentThreadId());
552 CHECK_LOOKUP_MY_THREAD(me
);
553 me
->finalizer_nested
= 0;
556 /* Checks and updates the thread-local level of finalizers recursion. */
557 /* Returns NULL if GC_invoke_finalizers() should not be called by the */
558 /* collector (to minimize the risk of a deep finalizers recursion), */
559 /* otherwise returns a pointer to the thread-local finalizer_nested. */
560 /* Called by GC_notify_or_invoke_finalizers() only (the lock is held). */
561 /* GC_check_finalizer_nested() is the same as in pthread_support.c. */
562 GC_INNER
unsigned char *GC_check_finalizer_nested(void)
564 GC_thread me
= GC_lookup_thread_inner(GetCurrentThreadId());
565 unsigned nesting_level
;
566 CHECK_LOOKUP_MY_THREAD(me
);
567 nesting_level
= me
->finalizer_nested
;
569 /* We are inside another GC_invoke_finalizers(). */
570 /* Skip some implicitly-called GC_invoke_finalizers() */
571 /* depending on the nesting (recursion) level. */
572 if (++me
->finalizer_skipped
< (1U << nesting_level
)) return NULL
;
573 me
->finalizer_skipped
= 0;
575 me
->finalizer_nested
= (unsigned char)(nesting_level
+ 1);
576 return &me
->finalizer_nested
;
579 #if defined(GC_ASSERTIONS) && defined(THREAD_LOCAL_ALLOC)
580 /* This is called from thread-local GC_malloc(). */
581 GC_bool
GC_is_thread_tsd_valid(void *tsd
)
587 me
= GC_lookup_thread_inner(GetCurrentThreadId());
589 return (word
)tsd
>= (word
)(&me
->tlfs
)
590 && (word
)tsd
< (word
)(&me
->tlfs
) + sizeof(me
->tlfs
);
592 #endif /* GC_ASSERTIONS && THREAD_LOCAL_ALLOC */
594 GC_API
int GC_CALL
GC_thread_is_registered(void)
596 DWORD thread_id
= GetCurrentThreadId();
601 me
= GC_lookup_thread_inner(thread_id
);
606 /* Make sure thread descriptor t is not protected by the VDB */
607 /* implementation. */
608 /* Used to prevent write faults when the world is (partially) stopped, */
609 /* since it may have been stopped with a system lock held, and that */
610 /* lock may be required for fault handling. */
611 #if defined(MPROTECT_VDB)
612 # define UNPROTECT_THREAD(t) \
613 if (!GC_win32_dll_threads && GC_dirty_maintained \
614 && t != &first_thread) { \
615 GC_ASSERT(SMALL_OBJ(GC_size(t))); \
616 GC_remove_protection(HBLKPTR(t), 1, FALSE); \
619 # define UNPROTECT_THREAD(t)
623 # define GC_PTHREAD_PTRVAL(pthread_id) pthread_id
624 #elif defined(GC_WIN32_PTHREADS)
625 # define GC_PTHREAD_PTRVAL(pthread_id) pthread_id.p
628 /* If a thread has been joined, but we have not yet */
629 /* been notified, then there may be more than one thread */
630 /* in the table with the same win32 id. */
631 /* This is OK, but we need a way to delete a specific one. */
632 /* Assumes we hold the allocation lock unless */
633 /* GC_win32_dll_threads is set. Does not actually free */
634 /* GC_thread entry (only unlinks it). */
635 /* If GC_win32_dll_threads is set it should be called from the */
636 /* thread being deleted. */
637 STATIC
void GC_delete_gc_thread_no_free(GC_vthread t
)
640 CloseHandle(t
->handle
);
642 # ifndef GC_NO_THREADS_DISCOVERY
643 if (GC_win32_dll_threads
) {
644 /* This is intended to be lock-free. */
645 /* It is either called synchronously from the thread being */
646 /* deleted, or by the joining thread. */
647 /* In this branch asynchronous changes to (*t) are possible. */
648 /* It's not allowed to call GC_printf (and the friends) here, */
649 /* see GC_stop_world() for the information. */
652 AO_store_release(&t
->tm
.in_use
, FALSE
);
656 DWORD id
= ((GC_thread
)t
) -> id
;
657 /* Cast away volatile qualifier, since we have lock. */
658 word hv
= THREAD_TABLE_INDEX(id
);
659 register GC_thread p
= GC_threads
[hv
];
660 register GC_thread prev
= 0;
662 GC_ASSERT(I_HOLD_LOCK());
663 while (p
!= (GC_thread
)t
) {
668 GC_threads
[hv
] = p
-> tm
.next
;
670 prev
-> tm
.next
= p
-> tm
.next
;
675 /* Delete a thread from GC_threads. We assume it is there. */
676 /* (The code intentionally traps if it wasn't.) Assumes we */
677 /* hold the allocation lock unless GC_win32_dll_threads is set. */
678 /* If GC_win32_dll_threads is set then it should be called from */
679 /* the thread being deleted. It is also safe to delete the */
680 /* main thread (unless GC_win32_dll_threads). */
681 STATIC
void GC_delete_thread(DWORD id
)
683 if (GC_win32_dll_threads
) {
684 GC_vthread t
= GC_lookup_thread_inner(id
);
687 WARN("Removing nonexistent thread, id = %" WARN_PRIdPTR
"\n", id
);
689 GC_delete_gc_thread_no_free(t
);
692 word hv
= THREAD_TABLE_INDEX(id
);
693 register GC_thread p
= GC_threads
[hv
];
694 register GC_thread prev
= 0;
696 GC_ASSERT(I_HOLD_LOCK());
697 while (p
-> id
!= id
) {
702 CloseHandle(p
->handle
);
705 GC_threads
[hv
] = p
-> tm
.next
;
707 prev
-> tm
.next
= p
-> tm
.next
;
709 if (p
!= &first_thread
) {
715 GC_API
void GC_CALL
GC_allow_register_threads(void)
717 /* Check GC is initialized and the current thread is registered. */
718 GC_ASSERT(GC_lookup_thread_inner(GetCurrentThreadId()) != 0);
720 # if !defined(GC_NO_THREADS_DISCOVERY) && !defined(PARALLEL_MARK)
721 /* GC_init() doesn't call GC_init_parallel() in this case. */
722 parallel_initialized
= TRUE
;
724 GC_need_to_lock
= TRUE
; /* We are multi-threaded now. */
727 GC_API
int GC_CALL
GC_register_my_thread(const struct GC_stack_base
*sb
)
730 DWORD thread_id
= GetCurrentThreadId();
733 if (GC_need_to_lock
== FALSE
)
734 ABORT("Threads explicit registering is not previously enabled");
736 /* We lock here, since we want to wait for an ongoing GC. */
738 me
= GC_lookup_thread_inner(thread_id
);
741 me
= GC_register_my_thread_inner(sb
, thread_id
);
742 me
-> flags
|= DETACHED
;
743 /* Treat as detached, since we do not need to worry about */
744 /* pointer results. */
746 GC_register_my_thread_inner(sb
, thread_id
);
752 /* else */ if ((me
-> flags
& FINISHED
) != 0) {
753 GC_record_stack_base(me
, sb
);
754 me
-> flags
&= ~FINISHED
; /* but not DETACHED */
755 # ifdef THREAD_LOCAL_ALLOC
756 GC_init_thread_local((GC_tlfs
)(&me
->tlfs
));
768 /* Similar to that in pthread_support.c. */
769 STATIC
void GC_wait_for_gc_completion(GC_bool wait_for_all
)
771 GC_ASSERT(I_HOLD_LOCK());
772 if (GC_incremental
&& GC_collection_in_progress()) {
773 word old_gc_no
= GC_gc_no
;
775 /* Make sure that no part of our stack is still on the mark stack, */
776 /* since it's about to be unmapped. */
779 GC_in_thread_creation
= TRUE
;
780 GC_collect_a_little_inner(1);
781 GC_in_thread_creation
= FALSE
;
785 Sleep(0); /* yield */
787 } while (GC_incremental
&& GC_collection_in_progress()
788 && (wait_for_all
|| old_gc_no
== GC_gc_no
));
792 GC_API
int GC_CALL
GC_unregister_my_thread(void)
796 # ifdef DEBUG_THREADS
797 GC_log_printf("Unregistering thread 0x%lx\n", (long)GetCurrentThreadId());
800 if (GC_win32_dll_threads
) {
801 # if defined(THREAD_LOCAL_ALLOC)
802 /* Can't happen: see GC_use_threads_discovery(). */
805 /* FIXME: Should we just ignore this? */
806 GC_delete_thread(GetCurrentThreadId());
809 # if defined(THREAD_LOCAL_ALLOC) || defined(GC_PTHREADS)
812 DWORD thread_id
= GetCurrentThreadId();
815 GC_wait_for_gc_completion(FALSE
);
816 # if defined(THREAD_LOCAL_ALLOC) || defined(GC_PTHREADS)
817 me
= GC_lookup_thread_inner(thread_id
);
818 CHECK_LOOKUP_MY_THREAD(me
);
819 GC_ASSERT(!KNOWN_FINISHED(me
));
821 # if defined(THREAD_LOCAL_ALLOC)
822 GC_ASSERT(GC_getspecific(GC_thread_key
) == &me
->tlfs
);
823 GC_destroy_thread_local(&(me
->tlfs
));
826 if ((me
-> flags
& DETACHED
) == 0) {
827 me
-> flags
|= FINISHED
;
831 GC_delete_thread(thread_id
);
833 # if defined(THREAD_LOCAL_ALLOC)
834 /* It is required to call remove_specific defined in specific.c. */
835 GC_remove_specific(GC_thread_key
);
842 /* Wrapper for functions that are likely to block for an appreciable */
843 /* length of time. */
845 /* GC_do_blocking_inner() is nearly the same as in pthread_support.c */
846 GC_INNER
void GC_do_blocking_inner(ptr_t data
, void * context GC_ATTR_UNUSED
)
848 struct blocking_data
* d
= (struct blocking_data
*) data
;
849 DWORD thread_id
= GetCurrentThreadId();
852 ptr_t stack_ptr
= GC_save_regs_in_stack();
857 me
= GC_lookup_thread_inner(thread_id
);
858 CHECK_LOOKUP_MY_THREAD(me
);
859 GC_ASSERT(me
-> thread_blocked_sp
== NULL
);
861 me
-> backing_store_ptr
= stack_ptr
;
863 me
-> thread_blocked_sp
= (ptr_t
) &d
; /* save approx. sp */
864 /* Save context here if we want to support precise stack marking */
866 d
-> client_data
= (d
-> fn
)(d
-> client_data
);
867 LOCK(); /* This will block if the world is stopped. */
868 me
-> thread_blocked_sp
= NULL
;
872 /* GC_call_with_gc_active() has the opposite to GC_do_blocking() */
873 /* functionality. It might be called from a user function invoked by */
874 /* GC_do_blocking() to temporarily back allow calling any GC function */
875 /* and/or manipulating pointers to the garbage collected heap. */
876 GC_API
void * GC_CALL
GC_call_with_gc_active(GC_fn_type fn
,
879 struct GC_traced_stack_sect_s stacksect
;
880 DWORD thread_id
= GetCurrentThreadId();
884 LOCK(); /* This will block if the world is stopped. */
885 me
= GC_lookup_thread_inner(thread_id
);
886 CHECK_LOOKUP_MY_THREAD(me
);
887 /* Adjust our stack base value (this could happen unless */
888 /* GC_get_stack_base() was used which returned GC_SUCCESS). */
889 GC_ASSERT(me
-> stack_base
!= NULL
);
890 if ((word
)me
->stack_base
< (word
)(&stacksect
))
891 me
-> stack_base
= (ptr_t
)(&stacksect
);
893 if (me
-> thread_blocked_sp
== NULL
) {
894 /* We are not inside GC_do_blocking() - do nothing more. */
896 return fn(client_data
);
899 /* Setup new "stack section". */
900 stacksect
.saved_stack_ptr
= me
-> thread_blocked_sp
;
902 /* This is the same as in GC_call_with_stack_base(). */
903 stacksect
.backing_store_end
= GC_save_regs_in_stack();
904 /* Unnecessarily flushes register stack, */
905 /* but that probably doesn't hurt. */
906 stacksect
.saved_backing_store_ptr
= me
-> backing_store_ptr
;
908 stacksect
.prev
= me
-> traced_stack_sect
;
909 me
-> thread_blocked_sp
= NULL
;
910 me
-> traced_stack_sect
= &stacksect
;
913 client_data
= fn(client_data
);
914 GC_ASSERT(me
-> thread_blocked_sp
== NULL
);
915 GC_ASSERT(me
-> traced_stack_sect
== &stacksect
);
917 /* Restore original "stack section". */
919 me
-> traced_stack_sect
= stacksect
.prev
;
921 me
-> backing_store_ptr
= stacksect
.saved_backing_store_ptr
;
923 me
-> thread_blocked_sp
= stacksect
.saved_stack_ptr
;
926 return client_data
; /* result */
931 /* A quick-and-dirty cache of the mapping between pthread_t */
932 /* and win32 thread id. */
933 # define PTHREAD_MAP_SIZE 512
934 DWORD GC_pthread_map_cache
[PTHREAD_MAP_SIZE
] = {0};
935 # define PTHREAD_MAP_INDEX(pthread_id) \
936 ((NUMERIC_THREAD_ID(pthread_id) >> 5) % PTHREAD_MAP_SIZE)
937 /* It appears pthread_t is really a pointer type ... */
938 # define SET_PTHREAD_MAP_CACHE(pthread_id, win32_id) \
939 (GC_pthread_map_cache[PTHREAD_MAP_INDEX(pthread_id)] = (win32_id))
940 # define GET_PTHREAD_MAP_CACHE(pthread_id) \
941 GC_pthread_map_cache[PTHREAD_MAP_INDEX(pthread_id)]
943 /* Return a GC_thread corresponding to a given pthread_t. */
944 /* Returns 0 if it's not there. */
945 /* We assume that this is only called for pthread ids that */
946 /* have not yet terminated or are still joinable, and */
947 /* cannot be concurrently terminated. */
948 /* Assumes we do NOT hold the allocation lock. */
949 STATIC GC_thread
GC_lookup_pthread(pthread_t id
)
951 # ifndef GC_NO_THREADS_DISCOVERY
952 if (GC_win32_dll_threads
) {
954 LONG my_max
= GC_get_max_thread_index();
956 for (i
= 0; i
<= my_max
&&
957 (!AO_load_acquire(&dll_thread_table
[i
].tm
.in_use
)
958 || THREAD_EQUAL(dll_thread_table
[i
].pthread_id
, id
));
959 /* Must still be in_use, since nobody else can */
960 /* store our thread_id. */
964 return i
<= my_max
? (GC_thread
)(dll_thread_table
+ i
) : NULL
;
968 /* We first try the cache. If that fails, we use a very slow */
970 word hv_guess
= THREAD_TABLE_INDEX(GET_PTHREAD_MAP_CACHE(id
));
976 for (p
= GC_threads
[hv_guess
]; 0 != p
; p
= p
-> tm
.next
) {
977 if (THREAD_EQUAL(p
-> pthread_id
, id
))
980 for (hv
= 0; hv
< THREAD_TABLE_SZ
; ++hv
) {
981 for (p
= GC_threads
[hv
]; 0 != p
; p
= p
-> tm
.next
) {
982 if (THREAD_EQUAL(p
-> pthread_id
, id
))
993 #endif /* GC_PTHREADS */
995 #ifdef CAN_HANDLE_FORK
996 /* Similar to that in pthread_support.c but also rehashes the table */
997 /* since hash map key (thread_id) differs from that in the parent. */
998 STATIC
void GC_remove_all_threads_but_me(void)
1001 GC_thread p
, next
, me
= NULL
;
1003 pthread_t pthread_id
= pthread_self(); /* same as in parent */
1005 GC_ASSERT(!GC_win32_dll_threads
);
1006 for (hv
= 0; hv
< THREAD_TABLE_SZ
; ++hv
) {
1007 for (p
= GC_threads
[hv
]; 0 != p
; p
= next
) {
1008 next
= p
-> tm
.next
;
1009 if (THREAD_EQUAL(p
-> pthread_id
, pthread_id
)) {
1010 GC_ASSERT(me
== NULL
);
1014 # ifdef THREAD_LOCAL_ALLOC
1015 if ((p
-> flags
& FINISHED
) == 0) {
1016 GC_destroy_thread_local(&p
->tlfs
);
1017 GC_remove_specific(GC_thread_key
);
1020 if (&first_thread
!= p
)
1021 GC_INTERNAL_FREE(p
);
1024 GC_threads
[hv
] = NULL
;
1027 /* Put "me" back to GC_threads. */
1028 GC_ASSERT(me
!= NULL
);
1029 thread_id
= GetCurrentThreadId(); /* differs from that in parent */
1030 GC_threads
[THREAD_TABLE_INDEX(thread_id
)] = me
;
1032 /* Update Win32 thread Id and handle. */
1033 me
-> id
= thread_id
;
1035 if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
1036 GetCurrentProcess(), (HANDLE
*)&me
->handle
,
1037 0 /* dwDesiredAccess */, FALSE
/* bInheritHandle */,
1038 DUPLICATE_SAME_ACCESS
))
1039 ABORT("DuplicateHandle failed");
1042 # if defined(THREAD_LOCAL_ALLOC) && !defined(USE_CUSTOM_SPECIFIC)
1043 /* For Cygwin, we need to re-assign thread-local pointer to */
1044 /* 'tlfs' (it is ok to call GC_destroy_thread_local and */
1045 /* GC_free_internal before this action). */
1046 if (GC_setspecific(GC_thread_key
, &me
->tlfs
) != 0)
1047 ABORT("GC_setspecific failed (in child)");
1051 static void fork_prepare_proc(void)
1054 # ifdef PARALLEL_MARK
1056 GC_wait_for_reclaim();
1058 GC_wait_for_gc_completion(TRUE
);
1059 # ifdef PARALLEL_MARK
1061 GC_acquire_mark_lock();
1065 static void fork_parent_proc(void)
1067 # ifdef PARALLEL_MARK
1069 GC_release_mark_lock();
1074 static void fork_child_proc(void)
1076 # ifdef PARALLEL_MARK
1078 GC_release_mark_lock();
1079 GC_parallel
= FALSE
; /* or GC_markers_m1 = 0 */
1080 /* Turn off parallel marking in the child, since we are */
1081 /* probably just going to exec, and we would have to */
1082 /* restart mark threads. */
1085 GC_remove_all_threads_but_me();
1089 /* Routines for fork handling by client (no-op if pthread_atfork works). */
1090 GC_API
void GC_CALL
GC_atfork_prepare(void)
1092 if (GC_handle_fork
<= 0)
1093 fork_prepare_proc();
1096 GC_API
void GC_CALL
GC_atfork_parent(void)
1098 if (GC_handle_fork
<= 0)
1102 GC_API
void GC_CALL
GC_atfork_child(void)
1104 if (GC_handle_fork
<= 0)
1107 #endif /* CAN_HANDLE_FORK */
1109 void GC_push_thread_structures(void)
1111 GC_ASSERT(I_HOLD_LOCK());
1112 # ifndef GC_NO_THREADS_DISCOVERY
1113 if (GC_win32_dll_threads
) {
1114 /* Unlike the other threads implementations, the thread table */
1115 /* here contains no pointers to the collectible heap (note also */
1116 /* that GC_PTHREADS is incompatible with DllMain-based thread */
1117 /* registration). Thus we have no private structures we need */
1122 GC_push_all((ptr_t
)(GC_threads
), (ptr_t
)(GC_threads
)+sizeof(GC_threads
));
1124 # if defined(THREAD_LOCAL_ALLOC)
1125 GC_push_all((ptr_t
)(&GC_thread_key
),
1126 (ptr_t
)(&GC_thread_key
) + sizeof(GC_thread_key
));
1127 /* Just in case we ever use our own TLS implementation. */
1131 /* Suspend the given thread, if it's still active. */
1132 STATIC
void GC_suspend(GC_thread t
)
1135 /* Apparently the Windows 95 GetOpenFileName call creates */
1136 /* a thread that does not properly get cleaned up, and */
1137 /* SuspendThread on its descriptor may provoke a crash. */
1138 /* This reduces the probability of that event, though it still */
1139 /* appears there's a race here. */
1142 UNPROTECT_THREAD(t
);
1144 if (GetExitCodeThread(t
-> handle
, &exitCode
) &&
1145 exitCode
!= STILL_ACTIVE
) {
1147 t
-> stack_base
= 0; /* prevent stack from being pushed */
1149 /* this breaks pthread_join on Cygwin, which is guaranteed to */
1150 /* only see user pthreads */
1151 GC_ASSERT(GC_win32_dll_threads
);
1152 GC_delete_gc_thread_no_free(t
);
1157 # if defined(MPROTECT_VDB)
1158 /* Acquire the spin lock we use to update dirty bits. */
1159 /* Threads shouldn't get stopped holding it. But we may */
1160 /* acquire and release it in the UNPROTECT_THREAD call. */
1161 while (AO_test_and_set_acquire(&GC_fault_handler_lock
) == AO_TS_SET
) {
1167 /* SuspendThread() will fail if thread is running kernel code. */
1168 while (SuspendThread(THREAD_HANDLE(t
)) == (DWORD
)-1)
1169 Sleep(10); /* in millis */
1171 if (SuspendThread(t
-> handle
) == (DWORD
)-1)
1172 ABORT("SuspendThread failed");
1173 # endif /* !MSWINCE */
1174 t
-> suspended
= (unsigned char)TRUE
;
1175 # if defined(MPROTECT_VDB)
1176 AO_CLEAR(&GC_fault_handler_lock
);
1180 #if defined(GC_ASSERTIONS) && !defined(CYGWIN32)
1181 GC_INNER GC_bool GC_write_disabled
= FALSE
;
1182 /* TRUE only if GC_stop_world() acquired GC_write_cs. */
1185 GC_INNER
void GC_stop_world(void)
1187 DWORD thread_id
= GetCurrentThreadId();
1189 if (!GC_thr_initialized
)
1190 ABORT("GC_stop_world() called before GC_thr_init()");
1191 GC_ASSERT(I_HOLD_LOCK());
1193 /* This code is the same as in pthread_stop_world.c */
1194 # ifdef PARALLEL_MARK
1196 GC_acquire_mark_lock();
1197 GC_ASSERT(GC_fl_builder_count
== 0);
1198 /* We should have previously waited for it to become zero. */
1200 # endif /* PARALLEL_MARK */
1202 # if !defined(GC_NO_THREADS_DISCOVERY) || defined(GC_ASSERTIONS)
1203 GC_please_stop
= TRUE
;
1206 GC_ASSERT(!GC_write_disabled
);
1207 EnterCriticalSection(&GC_write_cs
);
1208 /* It's not allowed to call GC_printf() (and friends) here down to */
1209 /* LeaveCriticalSection (same applies recursively to GC_suspend, */
1210 /* GC_delete_gc_thread_no_free, GC_get_max_thread_index, GC_size */
1211 /* and GC_remove_protection). */
1212 # ifdef GC_ASSERTIONS
1213 GC_write_disabled
= TRUE
;
1216 # ifndef GC_NO_THREADS_DISCOVERY
1217 if (GC_win32_dll_threads
) {
1220 /* Any threads being created during this loop will end up setting */
1221 /* GC_attached_thread when they start. This will force marking */
1222 /* to restart. This is not ideal, but hopefully correct. */
1223 AO_store(&GC_attached_thread
, FALSE
);
1224 my_max
= (int)GC_get_max_thread_index();
1225 for (i
= 0; i
<= my_max
; i
++) {
1226 GC_vthread t
= dll_thread_table
+ i
;
1227 if (t
-> stack_base
!= 0 && t
-> thread_blocked_sp
== NULL
1228 && t
-> id
!= thread_id
) {
1229 GC_suspend((GC_thread
)t
);
1238 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
1239 for (t
= GC_threads
[i
]; t
!= 0; t
= t
-> tm
.next
) {
1240 if (t
-> stack_base
!= 0 && t
-> thread_blocked_sp
== NULL
1241 && !KNOWN_FINISHED(t
) && t
-> id
!= thread_id
) {
1248 # ifdef GC_ASSERTIONS
1249 GC_write_disabled
= FALSE
;
1251 LeaveCriticalSection(&GC_write_cs
);
1253 # ifdef PARALLEL_MARK
1255 GC_release_mark_lock();
1259 GC_INNER
void GC_start_world(void)
1261 # ifdef GC_ASSERTIONS
1262 DWORD thread_id
= GetCurrentThreadId();
1266 GC_ASSERT(I_HOLD_LOCK());
1267 if (GC_win32_dll_threads
) {
1268 LONG my_max
= GC_get_max_thread_index();
1269 for (i
= 0; i
<= my_max
; i
++) {
1270 GC_thread t
= (GC_thread
)(dll_thread_table
+ i
);
1271 if (t
-> suspended
) {
1272 GC_ASSERT(t
-> stack_base
!= 0 && t
-> id
!= thread_id
);
1273 if (ResumeThread(THREAD_HANDLE(t
)) == (DWORD
)-1)
1274 ABORT("ResumeThread failed");
1275 t
-> suspended
= FALSE
;
1282 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
1283 for (t
= GC_threads
[i
]; t
!= 0; t
= t
-> tm
.next
) {
1284 if (t
-> suspended
) {
1285 GC_ASSERT(t
-> stack_base
!= 0 && t
-> id
!= thread_id
);
1286 if (ResumeThread(THREAD_HANDLE(t
)) == (DWORD
)-1)
1287 ABORT("ResumeThread failed");
1288 UNPROTECT_THREAD(t
);
1289 t
-> suspended
= FALSE
;
1294 # if !defined(GC_NO_THREADS_DISCOVERY) || defined(GC_ASSERTIONS)
1295 GC_please_stop
= FALSE
;
1300 /* The VirtualQuery calls below won't work properly on some old WinCE */
1301 /* versions, but since each stack is restricted to an aligned 64 KiB */
1302 /* region of virtual memory we can just take the next lowest multiple */
1303 /* of 64 KiB. The result of this macro must not be used as its */
1304 /* argument later and must not be used as the lower bound for sp */
1305 /* check (since the stack may be bigger than 64 KiB). */
1306 # define GC_wince_evaluate_stack_min(s) \
1307 (ptr_t)(((word)(s) - 1) & ~(word)0xFFFF)
1308 #elif defined(GC_ASSERTIONS)
1309 # define GC_dont_query_stack_min FALSE
1312 /* A cache holding the results of the recent VirtualQuery call. */
1313 /* Protected by the allocation lock. */
1314 static ptr_t last_address
= 0;
1315 static MEMORY_BASIC_INFORMATION last_info
;
1317 /* Probe stack memory region (starting at "s") to find out its */
1318 /* lowest address (i.e. stack top). */
1319 /* S must be a mapped address inside the region, NOT the first */
1320 /* unmapped address. */
1321 STATIC ptr_t
GC_get_stack_min(ptr_t s
)
1325 GC_ASSERT(I_HOLD_LOCK());
1326 if (s
!= last_address
) {
1327 VirtualQuery(s
, &last_info
, sizeof(last_info
));
1331 bottom
= last_info
.BaseAddress
;
1332 VirtualQuery(bottom
- 1, &last_info
, sizeof(last_info
));
1333 last_address
= bottom
- 1;
1334 } while ((last_info
.Protect
& PAGE_READWRITE
)
1335 && !(last_info
.Protect
& PAGE_GUARD
));
1339 /* Return true if the page at s has protections appropriate */
1340 /* for a stack page. */
1341 static GC_bool
may_be_in_stack(ptr_t s
)
1343 GC_ASSERT(I_HOLD_LOCK());
1344 if (s
!= last_address
) {
1345 VirtualQuery(s
, &last_info
, sizeof(last_info
));
1348 return (last_info
.Protect
& PAGE_READWRITE
)
1349 && !(last_info
.Protect
& PAGE_GUARD
);
1352 STATIC word
GC_push_stack_for(GC_thread thread
, DWORD me
)
1354 ptr_t sp
, stack_min
;
1356 struct GC_traced_stack_sect_s
*traced_stack_sect
=
1357 thread
-> traced_stack_sect
;
1358 if (thread
-> id
== me
) {
1359 GC_ASSERT(thread
-> thread_blocked_sp
== NULL
);
1360 sp
= GC_approx_sp();
1361 } else if ((sp
= thread
-> thread_blocked_sp
) == NULL
) {
1362 /* Use saved sp value for blocked threads. */
1363 /* For unblocked threads call GetThreadContext(). */
1365 context
.ContextFlags
= CONTEXT_INTEGER
|CONTEXT_CONTROL
;
1366 if (!GetThreadContext(THREAD_HANDLE(thread
), &context
))
1367 ABORT("GetThreadContext failed");
1369 /* Push all registers that might point into the heap. Frame */
1370 /* pointer registers are included in case client code was */
1371 /* compiled with the 'omit frame pointer' optimisation. */
1372 # define PUSH1(reg) GC_push_one((word)context.reg)
1373 # define PUSH2(r1,r2) PUSH1(r1), PUSH1(r2)
1374 # define PUSH4(r1,r2,r3,r4) PUSH2(r1,r2), PUSH2(r3,r4)
1376 PUSH4(Edi
,Esi
,Ebx
,Edx
), PUSH2(Ecx
,Eax
), PUSH1(Ebp
);
1377 sp
= (ptr_t
)context
.Esp
;
1378 # elif defined(X86_64)
1379 PUSH4(Rax
,Rcx
,Rdx
,Rbx
); PUSH2(Rbp
, Rsi
); PUSH1(Rdi
);
1380 PUSH4(R8
, R9
, R10
, R11
); PUSH4(R12
, R13
, R14
, R15
);
1381 sp
= (ptr_t
)context
.Rsp
;
1382 # elif defined(ARM32)
1383 PUSH4(R0
,R1
,R2
,R3
),PUSH4(R4
,R5
,R6
,R7
),PUSH4(R8
,R9
,R10
,R11
);
1385 sp
= (ptr_t
)context
.Sp
;
1387 PUSH4(R0
,R1
,R2
,R3
), PUSH4(R4
,R5
,R6
,R7
), PUSH4(R8
,R9
,R10
,R11
);
1388 PUSH2(R12
,R13
), PUSH1(R14
);
1389 sp
= (ptr_t
)context
.R15
;
1390 # elif defined(MIPS)
1391 PUSH4(IntAt
,IntV0
,IntV1
,IntA0
), PUSH4(IntA1
,IntA2
,IntA3
,IntT0
);
1392 PUSH4(IntT1
,IntT2
,IntT3
,IntT4
), PUSH4(IntT5
,IntT6
,IntT7
,IntS0
);
1393 PUSH4(IntS1
,IntS2
,IntS3
,IntS4
), PUSH4(IntS5
,IntS6
,IntS7
,IntT8
);
1394 PUSH4(IntT9
,IntK0
,IntK1
,IntS8
);
1395 sp
= (ptr_t
)context
.IntSp
;
1397 PUSH4(Gpr0
, Gpr3
, Gpr4
, Gpr5
), PUSH4(Gpr6
, Gpr7
, Gpr8
, Gpr9
);
1398 PUSH4(Gpr10
,Gpr11
,Gpr12
,Gpr14
), PUSH4(Gpr15
,Gpr16
,Gpr17
,Gpr18
);
1399 PUSH4(Gpr19
,Gpr20
,Gpr21
,Gpr22
), PUSH4(Gpr23
,Gpr24
,Gpr25
,Gpr26
);
1400 PUSH4(Gpr27
,Gpr28
,Gpr29
,Gpr30
), PUSH1(Gpr31
);
1401 sp
= (ptr_t
)context
.Gpr1
;
1402 # elif defined(ALPHA)
1403 PUSH4(IntV0
,IntT0
,IntT1
,IntT2
), PUSH4(IntT3
,IntT4
,IntT5
,IntT6
);
1404 PUSH4(IntT7
,IntS0
,IntS1
,IntS2
), PUSH4(IntS3
,IntS4
,IntS5
,IntFp
);
1405 PUSH4(IntA0
,IntA1
,IntA2
,IntA3
), PUSH4(IntA4
,IntA5
,IntT8
,IntT9
);
1406 PUSH4(IntT10
,IntT11
,IntT12
,IntAt
);
1407 sp
= (ptr_t
)context
.IntSp
;
1409 # error "architecture is not supported"
1411 } /* ! current thread */
1413 /* Set stack_min to the lowest address in the thread stack, */
1414 /* or to an address in the thread stack no larger than sp, */
1415 /* taking advantage of the old value to avoid slow traversals */
1416 /* of large stacks. */
1417 if (thread
-> last_stack_min
== ADDR_LIMIT
) {
1419 if (GC_dont_query_stack_min
) {
1420 stack_min
= GC_wince_evaluate_stack_min(traced_stack_sect
!= NULL
?
1421 (ptr_t
)traced_stack_sect
: thread
-> stack_base
);
1422 /* Keep last_stack_min value unmodified. */
1426 stack_min
= GC_get_stack_min(traced_stack_sect
!= NULL
?
1427 (ptr_t
)traced_stack_sect
: thread
-> stack_base
);
1428 UNPROTECT_THREAD(thread
);
1429 thread
-> last_stack_min
= stack_min
;
1432 /* First, adjust the latest known minimum stack address if we */
1433 /* are inside GC_call_with_gc_active(). */
1434 if (traced_stack_sect
!= NULL
&&
1435 (word
)thread
->last_stack_min
> (word
)traced_stack_sect
) {
1436 UNPROTECT_THREAD(thread
);
1437 thread
-> last_stack_min
= (ptr_t
)traced_stack_sect
;
1440 if ((word
)sp
< (word
)thread
->stack_base
1441 && (word
)sp
>= (word
)thread
->last_stack_min
) {
1444 /* In the current thread it is always safe to use sp value. */
1445 if (may_be_in_stack(thread
-> id
== me
&&
1446 (word
)sp
< (word
)thread
->last_stack_min
?
1447 sp
: thread
-> last_stack_min
)) {
1448 stack_min
= last_info
.BaseAddress
;
1449 /* Do not probe rest of the stack if sp is correct. */
1450 if ((word
)sp
< (word
)stack_min
1451 || (word
)sp
>= (word
)thread
->stack_base
)
1452 stack_min
= GC_get_stack_min(thread
-> last_stack_min
);
1454 /* Stack shrunk? Is this possible? */
1455 stack_min
= GC_get_stack_min(thread
-> stack_base
);
1457 UNPROTECT_THREAD(thread
);
1458 thread
-> last_stack_min
= stack_min
;
1462 GC_ASSERT(GC_dont_query_stack_min
1463 || stack_min
== GC_get_stack_min(thread
-> stack_base
)
1464 || ((word
)sp
>= (word
)stack_min
1465 && (word
)stack_min
< (word
)thread
->stack_base
1467 > (word
)GC_get_stack_min(thread
-> stack_base
)));
1469 if ((word
)sp
>= (word
)stack_min
&& (word
)sp
< (word
)thread
->stack_base
) {
1470 # ifdef DEBUG_THREADS
1471 GC_log_printf("Pushing stack for 0x%x from sp %p to %p from 0x%x\n",
1472 (int)thread
-> id
, sp
, thread
-> stack_base
, (int)me
);
1474 GC_push_all_stack_sections(sp
, thread
->stack_base
, traced_stack_sect
);
1476 /* If not current thread then it is possible for sp to point to */
1477 /* the guarded (untouched yet) page just below the current */
1478 /* stack_min of the thread. */
1479 if (thread
-> id
== me
|| (word
)sp
>= (word
)thread
->stack_base
1480 || (word
)(sp
+ GC_page_size
) < (word
)stack_min
)
1481 WARN("Thread stack pointer %p out of range, pushing everything\n",
1483 # ifdef DEBUG_THREADS
1484 GC_log_printf("Pushing stack for 0x%x from (min) %p to %p from 0x%x\n",
1485 (int)thread
->id
, stack_min
, thread
->stack_base
, (int)me
);
1487 /* Push everything - ignore "traced stack section" data. */
1488 GC_push_all_stack(stack_min
, thread
->stack_base
);
1490 return thread
->stack_base
- sp
; /* stack grows down */
1493 GC_INNER
void GC_push_all_stacks(void)
1495 DWORD thread_id
= GetCurrentThreadId();
1496 GC_bool found_me
= FALSE
;
1497 # ifndef SMALL_CONFIG
1498 unsigned nthreads
= 0;
1500 word total_size
= 0;
1501 # ifndef GC_NO_THREADS_DISCOVERY
1502 if (GC_win32_dll_threads
) {
1504 LONG my_max
= GC_get_max_thread_index();
1506 for (i
= 0; i
<= my_max
; i
++) {
1507 GC_thread t
= (GC_thread
)(dll_thread_table
+ i
);
1508 if (t
-> tm
.in_use
&& t
-> stack_base
) {
1509 # ifndef SMALL_CONFIG
1512 total_size
+= GC_push_stack_for(t
, thread_id
);
1513 if (t
-> id
== thread_id
) found_me
= TRUE
;
1520 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
1522 for (t
= GC_threads
[i
]; t
!= 0; t
= t
-> tm
.next
) {
1523 if (!KNOWN_FINISHED(t
) && t
-> stack_base
) {
1524 # ifndef SMALL_CONFIG
1527 total_size
+= GC_push_stack_for(t
, thread_id
);
1528 if (t
-> id
== thread_id
) found_me
= TRUE
;
1533 # ifndef SMALL_CONFIG
1534 GC_VERBOSE_LOG_PRINTF("Pushed %d thread stacks%s\n", nthreads
,
1535 GC_win32_dll_threads
?
1536 " based on DllMain thread tracking" : "");
1538 if (!found_me
&& !GC_in_thread_creation
)
1539 ABORT("Collecting from unknown thread");
1540 GC_total_stacksize
= total_size
;
1543 #ifdef PARALLEL_MARK
1545 # ifndef MAX_MARKERS
1546 # define MAX_MARKERS 16
1549 static ptr_t marker_sp
[MAX_MARKERS
- 1]; /* The cold end of the stack */
1552 static ptr_t marker_bsp
[MAX_MARKERS
- 1];
1555 static ptr_t marker_last_stack_min
[MAX_MARKERS
- 1];
1556 /* Last known minimum (hottest) address */
1557 /* in stack (or ADDR_LIMIT if unset) */
1560 #endif /* PARALLEL_MARK */
1562 /* Find stack with the lowest address which overlaps the */
1563 /* interval [start, limit). */
1564 /* Return stack bounds in *lo and *hi. If no such stack */
1565 /* is found, both *hi and *lo will be set to an address */
1566 /* higher than limit. */
1567 GC_INNER
void GC_get_next_stack(char *start
, char *limit
,
1568 char **lo
, char **hi
)
1571 char * current_min
= ADDR_LIMIT
; /* Least in-range stack base */
1572 ptr_t
*plast_stack_min
= NULL
; /* Address of last_stack_min */
1573 /* field for thread corresponding */
1574 /* to current_min. */
1575 GC_thread thread
= NULL
; /* Either NULL or points to the */
1576 /* thread's hash table entry */
1577 /* containing *plast_stack_min. */
1579 /* First set current_min, ignoring limit. */
1580 if (GC_win32_dll_threads
) {
1581 LONG my_max
= GC_get_max_thread_index();
1583 for (i
= 0; i
<= my_max
; i
++) {
1584 ptr_t s
= (ptr_t
)(dll_thread_table
[i
].stack_base
);
1586 if ((word
)s
> (word
)start
&& (word
)s
< (word
)current_min
) {
1587 /* Update address of last_stack_min. */
1588 plast_stack_min
= (ptr_t
* /* no volatile */)
1589 &dll_thread_table
[i
].last_stack_min
;
1594 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
1597 for (t
= GC_threads
[i
]; t
!= 0; t
= t
-> tm
.next
) {
1598 ptr_t s
= t
-> stack_base
;
1600 if ((word
)s
> (word
)start
&& (word
)s
< (word
)current_min
) {
1601 /* Update address of last_stack_min. */
1602 plast_stack_min
= &t
-> last_stack_min
;
1603 thread
= t
; /* Remember current thread to unprotect. */
1608 # ifdef PARALLEL_MARK
1609 for (i
= 0; i
< GC_markers_m1
; ++i
) {
1610 ptr_t s
= marker_sp
[i
];
1612 /* FIXME: not implemented */
1614 if ((word
)s
> (word
)start
&& (word
)s
< (word
)current_min
) {
1615 GC_ASSERT(marker_last_stack_min
[i
] != NULL
);
1616 plast_stack_min
= &marker_last_stack_min
[i
];
1618 thread
= NULL
; /* Not a thread's hash table entry. */
1625 if (current_min
== ADDR_LIMIT
) {
1630 GC_ASSERT((word
)current_min
> (word
)start
&& plast_stack_min
!= NULL
);
1632 if (GC_dont_query_stack_min
) {
1633 *lo
= GC_wince_evaluate_stack_min(current_min
);
1634 /* Keep last_stack_min value unmodified. */
1639 if ((word
)current_min
> (word
)limit
&& !may_be_in_stack(limit
)) {
1640 /* Skip the rest since the memory region at limit address is */
1641 /* not a stack (so the lowest address of the found stack would */
1642 /* be above the limit value anyway). */
1647 /* Get the minimum address of the found stack by probing its memory */
1648 /* region starting from the recent known minimum (if set). */
1649 if (*plast_stack_min
== ADDR_LIMIT
1650 || !may_be_in_stack(*plast_stack_min
)) {
1651 /* Unsafe to start from last_stack_min value. */
1652 *lo
= GC_get_stack_min(current_min
);
1654 /* Use the recent value to optimize search for min address. */
1655 *lo
= GC_get_stack_min(*plast_stack_min
);
1658 /* Remember current stack_min value. */
1659 if (thread
!= NULL
) {
1660 UNPROTECT_THREAD(thread
);
1662 *plast_stack_min
= *lo
;
1665 #ifdef PARALLEL_MARK
1667 # if defined(GC_PTHREADS) && !defined(GC_PTHREADS_PARAMARK)
1668 /* Use pthread-based parallel mark implementation. */
1669 # define GC_PTHREADS_PARAMARK
1672 # if !defined(GC_PTHREADS_PARAMARK) && defined(DONT_USE_SIGNALANDWAIT)
1673 STATIC HANDLE GC_marker_cv
[MAX_MARKERS
- 1] = {0};
1674 /* Events with manual reset (one for each */
1677 STATIC DWORD GC_marker_Id
[MAX_MARKERS
- 1] = {0};
1678 /* This table is used for mapping helper */
1679 /* threads ID to mark helper index (linear */
1680 /* search is used since the mapping contains */
1681 /* only a few entries). */
1684 /* GC_mark_thread() is the same as in pthread_support.c */
1685 # ifdef GC_PTHREADS_PARAMARK
1686 STATIC
void * GC_mark_thread(void * id
)
1689 STATIC DWORD WINAPI
GC_mark_thread(LPVOID id
)
1691 STATIC
unsigned __stdcall
GC_mark_thread(void * id
)
1695 word my_mark_no
= 0;
1697 if ((word
)id
== (word
)-1) return 0; /* to make compiler happy */
1698 marker_sp
[(word
)id
] = GC_approx_sp();
1700 marker_bsp
[(word
)id
] = GC_save_regs_in_stack();
1702 # if !defined(GC_PTHREADS_PARAMARK) && defined(DONT_USE_SIGNALANDWAIT)
1703 GC_marker_Id
[(word
)id
] = GetCurrentThreadId();
1706 for (;; ++my_mark_no
) {
1707 if (my_mark_no
- GC_mark_no
> (word
)2) {
1708 /* resynchronize if we get far off, e.g. because GC_mark_no */
1710 my_mark_no
= GC_mark_no
;
1712 # ifdef DEBUG_THREADS
1713 GC_log_printf("Starting mark helper for mark number %lu\n",
1714 (unsigned long)my_mark_no
);
1716 GC_help_marker(my_mark_no
);
1720 # ifdef GC_ASSERTIONS
1721 GC_INNER
unsigned long GC_mark_lock_holder
= NO_THREAD
;
1724 /* GC_mark_threads[] is unused here unlike that in pthread_support.c */
1726 # ifndef CAN_HANDLE_FORK
1727 # define available_markers_m1 GC_markers_m1
1730 # ifdef GC_PTHREADS_PARAMARK
1731 # include <pthread.h>
1733 # ifndef NUMERIC_THREAD_ID
1734 # define NUMERIC_THREAD_ID(id) (unsigned long)GC_PTHREAD_PTRVAL(id)
1737 /* start_mark_threads is the same as in pthread_support.c except */
1738 /* for thread stack that is assumed to be large enough. */
1739 # ifdef CAN_HANDLE_FORK
1740 static int available_markers_m1
= 0;
1741 # define start_mark_threads GC_start_mark_threads
1746 start_mark_threads(void)
1749 pthread_attr_t attr
;
1750 pthread_t new_thread
;
1752 GC_ASSERT(I_DONT_HOLD_LOCK());
1753 # ifdef CAN_HANDLE_FORK
1754 if (available_markers_m1
<= 0 || GC_parallel
) return;
1755 /* Skip if parallel markers disabled or already started. */
1758 if (0 != pthread_attr_init(&attr
)) ABORT("pthread_attr_init failed");
1759 if (0 != pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
))
1760 ABORT("pthread_attr_setdetachstate failed");
1762 for (i
= 0; i
< available_markers_m1
; ++i
) {
1763 marker_last_stack_min
[i
] = ADDR_LIMIT
;
1764 if (0 != pthread_create(&new_thread
, &attr
,
1765 GC_mark_thread
, (void *)(word
)i
)) {
1766 WARN("Marker thread creation failed.\n", 0);
1767 /* Don't try to create other marker threads. */
1772 pthread_attr_destroy(&attr
);
1773 GC_COND_LOG_PRINTF("Started %d mark helper threads\n", GC_markers_m1
);
1776 static pthread_mutex_t mark_mutex
= PTHREAD_MUTEX_INITIALIZER
;
1778 static pthread_cond_t builder_cv
= PTHREAD_COND_INITIALIZER
;
1780 /* GC_acquire/release_mark_lock(), GC_wait_builder/marker(), */
1781 /* GC_wait_for_reclaim(), GC_notify_all_builder/marker() are the same */
1782 /* as in pthread_support.c except that GC_generic_lock() is not used. */
1785 volatile AO_t GC_block_count
= 0;
1788 GC_INNER
void GC_acquire_mark_lock(void)
1790 if (pthread_mutex_lock(&mark_mutex
) != 0) {
1791 ABORT("pthread_mutex_lock failed");
1794 (void)AO_fetch_and_add1(&GC_block_count
);
1796 /* GC_generic_lock(&mark_mutex); */
1797 # ifdef GC_ASSERTIONS
1798 GC_mark_lock_holder
= NUMERIC_THREAD_ID(pthread_self());
1802 GC_INNER
void GC_release_mark_lock(void)
1804 GC_ASSERT(GC_mark_lock_holder
== NUMERIC_THREAD_ID(pthread_self()));
1805 # ifdef GC_ASSERTIONS
1806 GC_mark_lock_holder
= NO_THREAD
;
1808 if (pthread_mutex_unlock(&mark_mutex
) != 0) {
1809 ABORT("pthread_mutex_unlock failed");
1813 /* Collector must wait for a freelist builders for 2 reasons: */
1814 /* 1) Mark bits may still be getting examined without lock. */
1815 /* 2) Partial free lists referenced only by locals may not be */
1816 /* scanned correctly, e.g. if they contain "pointer-free" objects, */
1817 /* since the free-list link may be ignored. */
1818 STATIC
void GC_wait_builder(void)
1820 GC_ASSERT(GC_mark_lock_holder
== NUMERIC_THREAD_ID(pthread_self()));
1821 # ifdef GC_ASSERTIONS
1822 GC_mark_lock_holder
= NO_THREAD
;
1824 if (pthread_cond_wait(&builder_cv
, &mark_mutex
) != 0) {
1825 ABORT("pthread_cond_wait failed");
1827 GC_ASSERT(GC_mark_lock_holder
== NO_THREAD
);
1828 # ifdef GC_ASSERTIONS
1829 GC_mark_lock_holder
= NUMERIC_THREAD_ID(pthread_self());
1833 GC_INNER
void GC_wait_for_reclaim(void)
1835 GC_acquire_mark_lock();
1836 while (GC_fl_builder_count
> 0) {
1839 GC_release_mark_lock();
1842 GC_INNER
void GC_notify_all_builder(void)
1844 GC_ASSERT(GC_mark_lock_holder
== NUMERIC_THREAD_ID(pthread_self()));
1845 if (pthread_cond_broadcast(&builder_cv
) != 0) {
1846 ABORT("pthread_cond_broadcast failed");
1850 static pthread_cond_t mark_cv
= PTHREAD_COND_INITIALIZER
;
1852 GC_INNER
void GC_wait_marker(void)
1854 GC_ASSERT(GC_mark_lock_holder
== NUMERIC_THREAD_ID(pthread_self()));
1855 # ifdef GC_ASSERTIONS
1856 GC_mark_lock_holder
= NO_THREAD
;
1858 if (pthread_cond_wait(&mark_cv
, &mark_mutex
) != 0) {
1859 ABORT("pthread_cond_wait failed");
1861 GC_ASSERT(GC_mark_lock_holder
== NO_THREAD
);
1862 # ifdef GC_ASSERTIONS
1863 GC_mark_lock_holder
= NUMERIC_THREAD_ID(pthread_self());
1867 GC_INNER
void GC_notify_all_marker(void)
1869 if (pthread_cond_broadcast(&mark_cv
) != 0) {
1870 ABORT("pthread_cond_broadcast failed");
1874 # else /* ! GC_PTHREADS_PARAMARK */
1876 # ifndef MARK_THREAD_STACK_SIZE
1877 # define MARK_THREAD_STACK_SIZE 0 /* default value */
1880 /* mark_mutex_event, builder_cv, mark_cv are initialized in GC_thr_init */
1881 static HANDLE mark_mutex_event
= (HANDLE
)0; /* Event with auto-reset. */
1882 static HANDLE builder_cv
= (HANDLE
)0; /* Event with manual reset. */
1883 static HANDLE mark_cv
= (HANDLE
)0; /* Event with manual reset. */
1885 static void start_mark_threads(void)
1892 GC_uintptr_t handle
;
1896 # ifdef DONT_USE_SIGNALANDWAIT
1897 /* Initialize GC_marker_cv[] fully before starting the */
1898 /* first helper thread. */
1899 for (i
= 0; i
< GC_markers_m1
; ++i
) {
1900 if ((GC_marker_cv
[i
] = CreateEvent(NULL
/* attrs */,
1901 TRUE
/* isManualReset */,
1902 FALSE
/* initialState */,
1903 NULL
/* name (A/W) */)) == (HANDLE
)0)
1904 ABORT("CreateEvent failed");
1908 for (i
= 0; i
< GC_markers_m1
; ++i
) {
1909 marker_last_stack_min
[i
] = ADDR_LIMIT
;
1911 /* There is no _beginthreadex() in WinCE. */
1912 handle
= CreateThread(NULL
/* lpsa */,
1913 MARK_THREAD_STACK_SIZE
/* ignored */,
1914 GC_mark_thread
, (LPVOID
)(word
)i
,
1915 0 /* fdwCreate */, &thread_id
);
1916 if (handle
== NULL
) {
1917 WARN("Marker thread creation failed\n", 0);
1918 /* The most probable failure reason is "not enough memory". */
1919 /* Don't try to create other marker threads. */
1922 /* It's safe to detach the thread. */
1923 CloseHandle(handle
);
1926 handle
= _beginthreadex(NULL
/* security_attr */,
1927 MARK_THREAD_STACK_SIZE
, GC_mark_thread
,
1928 (void *)(word
)i
, 0 /* flags */, &thread_id
);
1929 if (!handle
|| handle
== (GC_uintptr_t
)-1L) {
1930 WARN("Marker thread creation failed\n", 0);
1931 /* Don't try to create other marker threads. */
1933 } else {/* We may detach the thread (if handle is of HANDLE type) */
1934 /* CloseHandle((HANDLE)handle); */
1939 /* Adjust GC_markers_m1 (and free unused resources) if failed. */
1940 # ifdef DONT_USE_SIGNALANDWAIT
1941 while (GC_markers_m1
> i
) {
1943 CloseHandle(GC_marker_cv
[GC_markers_m1
]);
1948 GC_COND_LOG_PRINTF("Started %d mark helper threads\n", GC_markers_m1
);
1950 CloseHandle(mark_cv
);
1951 CloseHandle(builder_cv
);
1952 CloseHandle(mark_mutex_event
);
1956 # ifdef DONT_USE_SIGNALANDWAIT
1957 STATIC
/* volatile */ LONG GC_mark_mutex_state
= 0;
1958 /* Mutex state: 0 - unlocked, */
1959 /* 1 - locked and no other waiters, */
1960 /* -1 - locked and waiters may exist. */
1961 /* Accessed by InterlockedExchange(). */
1963 STATIC
volatile AO_t GC_mark_mutex_waitcnt
= 0;
1964 /* Number of waiters + 1; 0 - unlocked. */
1967 /* #define LOCK_STATS */
1969 volatile AO_t GC_block_count
= 0;
1970 volatile AO_t GC_unlocked_count
= 0;
1973 GC_INNER
void GC_acquire_mark_lock(void)
1975 # ifdef DONT_USE_SIGNALANDWAIT
1976 if (InterlockedExchange(&GC_mark_mutex_state
, 1 /* locked */) != 0)
1978 if (AO_fetch_and_add1_acquire(&GC_mark_mutex_waitcnt
) != 0)
1982 (void)AO_fetch_and_add1(&GC_block_count
);
1984 # ifdef DONT_USE_SIGNALANDWAIT
1985 /* Repeatedly reset the state and wait until acquire the lock. */
1986 while (InterlockedExchange(&GC_mark_mutex_state
,
1987 -1 /* locked_and_has_waiters */) != 0)
1990 if (WaitForSingleObject(mark_mutex_event
, INFINITE
) == WAIT_FAILED
)
1991 ABORT("WaitForSingleObject failed");
1996 (void)AO_fetch_and_add1(&GC_unlocked_count
);
2000 GC_ASSERT(GC_mark_lock_holder
== NO_THREAD
);
2001 # ifdef GC_ASSERTIONS
2002 GC_mark_lock_holder
= (unsigned long)GetCurrentThreadId();
2006 GC_INNER
void GC_release_mark_lock(void)
2008 GC_ASSERT(GC_mark_lock_holder
== (unsigned long)GetCurrentThreadId());
2009 # ifdef GC_ASSERTIONS
2010 GC_mark_lock_holder
= NO_THREAD
;
2012 # ifdef DONT_USE_SIGNALANDWAIT
2013 if (InterlockedExchange(&GC_mark_mutex_state
, 0 /* unlocked */) < 0)
2015 GC_ASSERT(AO_load(&GC_mark_mutex_waitcnt
) != 0);
2016 if (AO_fetch_and_sub1_release(&GC_mark_mutex_waitcnt
) > 1)
2020 if (SetEvent(mark_mutex_event
) == FALSE
)
2021 ABORT("SetEvent failed");
2025 /* In GC_wait_for_reclaim/GC_notify_all_builder() we emulate POSIX */
2026 /* cond_wait/cond_broadcast() primitives with WinAPI Event object */
2027 /* (working in "manual reset" mode). This works here because */
2028 /* GC_notify_all_builder() is always called holding lock on */
2029 /* mark_mutex and the checked condition (GC_fl_builder_count == 0) */
2030 /* is the only one for which broadcasting on builder_cv is performed. */
2032 GC_INNER
void GC_wait_for_reclaim(void)
2034 GC_ASSERT(builder_cv
!= 0);
2036 GC_acquire_mark_lock();
2037 if (GC_fl_builder_count
== 0)
2039 if (ResetEvent(builder_cv
) == FALSE
)
2040 ABORT("ResetEvent failed");
2041 GC_release_mark_lock();
2042 if (WaitForSingleObject(builder_cv
, INFINITE
) == WAIT_FAILED
)
2043 ABORT("WaitForSingleObject failed");
2045 GC_release_mark_lock();
2048 GC_INNER
void GC_notify_all_builder(void)
2050 GC_ASSERT(GC_mark_lock_holder
== (unsigned long)GetCurrentThreadId());
2051 GC_ASSERT(builder_cv
!= 0);
2052 GC_ASSERT(GC_fl_builder_count
== 0);
2053 if (SetEvent(builder_cv
) == FALSE
)
2054 ABORT("SetEvent failed");
2057 # ifdef DONT_USE_SIGNALANDWAIT
2059 /* mark_cv is used (for waiting) by a non-helper thread. */
2061 GC_INNER
void GC_wait_marker(void)
2063 HANDLE event
= mark_cv
;
2064 DWORD thread_id
= GetCurrentThreadId();
2065 int i
= GC_markers_m1
;
2068 if (GC_marker_Id
[i
] == thread_id
) {
2069 event
= GC_marker_cv
[i
];
2074 if (ResetEvent(event
) == FALSE
)
2075 ABORT("ResetEvent failed");
2076 GC_release_mark_lock();
2077 if (WaitForSingleObject(event
, INFINITE
) == WAIT_FAILED
)
2078 ABORT("WaitForSingleObject failed");
2079 GC_acquire_mark_lock();
2082 GC_INNER
void GC_notify_all_marker(void)
2084 DWORD thread_id
= GetCurrentThreadId();
2085 int i
= GC_markers_m1
;
2088 /* Notify every marker ignoring self (for efficiency). */
2089 if (SetEvent(GC_marker_Id
[i
] != thread_id
? GC_marker_cv
[i
] :
2091 ABORT("SetEvent failed");
2095 # else /* DONT_USE_SIGNALANDWAIT */
2097 /* For GC_wait_marker/GC_notify_all_marker() the above technique */
2098 /* does not work because they are used with different checked */
2099 /* conditions in different places (and, in addition, notifying is */
2100 /* done after leaving critical section) and this could result in */
2101 /* a signal loosing between checking for a particular condition */
2102 /* and calling WaitForSingleObject. So, we use PulseEvent() and */
2103 /* NT SignalObjectAndWait() (which atomically sets mutex event to */
2104 /* signaled state and starts waiting on condvar). A special */
2105 /* case here is GC_mark_mutex_waitcnt == 1 (i.e. nobody waits for */
2106 /* mark lock at this moment) - we don't change it (otherwise we */
2107 /* may loose a signal sent between decrementing */
2108 /* GC_mark_mutex_waitcnt and calling WaitForSingleObject()). */
2111 /* SignalObjectAndWait() is missing in WinCE (for now), so you */
2112 /* should supply its emulation (externally) to use this code. */
2113 WINBASEAPI DWORD WINAPI
SignalObjectAndWait(HANDLE
, HANDLE
, DWORD
,
2115 # define signalObjectAndWait_func SignalObjectAndWait
2117 typedef DWORD (WINAPI
* SignalObjectAndWait_type
)(HANDLE
, HANDLE
,
2119 static SignalObjectAndWait_type signalObjectAndWait_func
= 0;
2122 GC_INNER
void GC_wait_marker(void)
2124 /* Here we assume that GC_wait_marker() is always called */
2125 /* from a while(check_cond) loop. */
2127 GC_ASSERT(mark_cv
!= 0);
2129 /* We inline GC_release_mark_lock() to have atomic */
2130 /* unlock-and-wait action here. */
2131 GC_ASSERT(GC_mark_lock_holder
== (unsigned long)GetCurrentThreadId());
2132 # ifdef GC_ASSERTIONS
2133 GC_mark_lock_holder
= NO_THREAD
;
2136 if ((waitcnt
= AO_load(&GC_mark_mutex_waitcnt
)) > 1) {
2137 (void)AO_fetch_and_sub1_release(&GC_mark_mutex_waitcnt
);
2139 GC_ASSERT(AO_load(&GC_mark_mutex_waitcnt
) != 0);
2142 /* The state of mark_cv is non-signaled here. */
2143 if (signalObjectAndWait_func(mark_mutex_event
/* hObjectToSignal */,
2144 mark_cv
/* hObjectToWaitOn */,
2145 INFINITE
/* timeout */,
2146 FALSE
/* isAlertable */) == WAIT_FAILED
)
2147 ABORT("SignalObjectAndWait failed");
2148 /* The state of mark_cv is non-signaled here again. */
2151 GC_acquire_mark_lock();
2153 GC_ASSERT(GC_mark_mutex_waitcnt
!= 0);
2154 /* Acquire mark lock */
2155 if (WaitForSingleObject(mark_mutex_event
, INFINITE
) == WAIT_FAILED
)
2156 ABORT("WaitForSingleObject failed");
2157 GC_ASSERT(GC_mark_lock_holder
== NO_THREAD
);
2158 # ifdef GC_ASSERTIONS
2159 GC_mark_lock_holder
= (unsigned long)GetCurrentThreadId();
2164 GC_INNER
void GC_notify_all_marker(void)
2166 GC_ASSERT(mark_cv
!= 0);
2167 if (PulseEvent(mark_cv
) == FALSE
)
2168 ABORT("PulseEvent failed");
2171 # endif /* !DONT_USE_SIGNALANDWAIT */
2173 # endif /* ! GC_PTHREADS_PARAMARK */
2175 #endif /* PARALLEL_MARK */
2177 /* We have no DllMain to take care of new threads. Thus we */
2178 /* must properly intercept thread creation. */
2181 LPTHREAD_START_ROUTINE start
;
2185 STATIC
void * GC_CALLBACK
GC_win32_start_inner(struct GC_stack_base
*sb
,
2189 LPTHREAD_START_ROUTINE start
= ((thread_args
*)arg
)->start
;
2190 LPVOID param
= ((thread_args
*)arg
)->param
;
2192 GC_register_my_thread(sb
); /* This waits for an in-progress GC. */
2194 # ifdef DEBUG_THREADS
2195 GC_log_printf("thread 0x%lx starting...\n", (long)GetCurrentThreadId());
2200 /* Clear the thread entry even if we exit with an exception. */
2201 /* This is probably pointless, since an uncaught exception is */
2202 /* supposed to result in the process being killed. */
2207 ret
= (void *)(word
)(*start
)(param
);
2213 GC_unregister_my_thread();
2216 # ifdef DEBUG_THREADS
2217 GC_log_printf("thread 0x%lx returned from start routine\n",
2218 (long)GetCurrentThreadId());
2223 STATIC DWORD WINAPI
GC_win32_start(LPVOID arg
)
2225 return (DWORD
)(word
)GC_call_with_stack_base(GC_win32_start_inner
, arg
);
2228 GC_API HANDLE WINAPI
GC_CreateThread(
2229 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
2230 GC_WIN32_SIZE_T dwStackSize
,
2231 LPTHREAD_START_ROUTINE lpStartAddress
,
2232 LPVOID lpParameter
, DWORD dwCreationFlags
,
2238 if (!EXPECT(parallel_initialized
, TRUE
))
2240 /* make sure GC is initialized (i.e. main thread is */
2241 /* attached, tls initialized). */
2243 # ifdef DEBUG_THREADS
2244 GC_log_printf("About to create a thread from 0x%lx\n",
2245 (long)GetCurrentThreadId());
2247 if (GC_win32_dll_threads
) {
2248 return CreateThread(lpThreadAttributes
, dwStackSize
, lpStartAddress
,
2249 lpParameter
, dwCreationFlags
, lpThreadId
);
2251 args
= GC_malloc_uncollectable(sizeof(thread_args
));
2252 /* Handed off to and deallocated by child thread. */
2254 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2258 /* set up thread arguments */
2259 args
-> start
= lpStartAddress
;
2260 args
-> param
= lpParameter
;
2262 GC_need_to_lock
= TRUE
;
2263 thread_h
= CreateThread(lpThreadAttributes
, dwStackSize
, GC_win32_start
,
2264 args
, dwCreationFlags
, lpThreadId
);
2265 if (thread_h
== 0) GC_free(args
);
2270 GC_API DECLSPEC_NORETURN
void WINAPI
GC_ExitThread(DWORD dwExitCode
)
2272 GC_unregister_my_thread();
2273 ExitThread(dwExitCode
);
2276 # if !defined(MSWINCE) && !defined(CYGWIN32)
2278 GC_API GC_uintptr_t GC_CALL
GC_beginthreadex(
2279 void *security
, unsigned stack_size
,
2280 unsigned (__stdcall
*start_address
)(void *),
2281 void *arglist
, unsigned initflag
,
2284 GC_uintptr_t thread_h
;
2287 if (!EXPECT(parallel_initialized
, TRUE
))
2289 /* make sure GC is initialized (i.e. main thread is */
2290 /* attached, tls initialized). */
2291 # ifdef DEBUG_THREADS
2292 GC_log_printf("About to create a thread from 0x%lx\n",
2293 (long)GetCurrentThreadId());
2296 if (GC_win32_dll_threads
) {
2297 return _beginthreadex(security
, stack_size
, start_address
,
2298 arglist
, initflag
, thrdaddr
);
2300 args
= GC_malloc_uncollectable(sizeof(thread_args
));
2301 /* Handed off to and deallocated by child thread. */
2303 /* MSDN docs say _beginthreadex() returns 0 on error and sets */
2304 /* errno to either EAGAIN (too many threads) or EINVAL (the */
2305 /* argument is invalid or the stack size is incorrect), so we */
2306 /* set errno to EAGAIN on "not enough memory". */
2311 /* set up thread arguments */
2312 args
-> start
= (LPTHREAD_START_ROUTINE
)start_address
;
2313 args
-> param
= arglist
;
2315 GC_need_to_lock
= TRUE
;
2316 thread_h
= _beginthreadex(security
, stack_size
,
2317 (unsigned (__stdcall
*)(void *))GC_win32_start
,
2318 args
, initflag
, thrdaddr
);
2319 if (thread_h
== 0) GC_free(args
);
2324 GC_API
void GC_CALL
GC_endthreadex(unsigned retval
)
2326 GC_unregister_my_thread();
2327 _endthreadex(retval
);
2330 # endif /* !MSWINCE && !CYGWIN32 */
2332 #ifdef GC_WINMAIN_REDIRECT
2333 /* This might be useful on WinCE. Shouldn't be used with GC_DLL. */
2335 # if defined(MSWINCE) && defined(UNDER_CE)
2336 # define WINMAIN_LPTSTR LPWSTR
2338 # define WINMAIN_LPTSTR LPSTR
2341 /* This is defined in gc.h. */
2344 /* Defined outside GC by an application. */
2345 int WINAPI
GC_WinMain(HINSTANCE
, HINSTANCE
, WINMAIN_LPTSTR
, int);
2348 HINSTANCE hInstance
;
2349 HINSTANCE hPrevInstance
;
2350 WINMAIN_LPTSTR lpCmdLine
;
2354 static DWORD WINAPI
main_thread_start(LPVOID arg
)
2356 main_thread_args
* args
= (main_thread_args
*) arg
;
2357 return (DWORD
)GC_WinMain(args
->hInstance
, args
->hPrevInstance
,
2358 args
->lpCmdLine
, args
->nShowCmd
);
2361 STATIC
void * GC_waitForSingleObjectInfinite(void * handle
)
2363 return (void *)(word
)WaitForSingleObject((HANDLE
)handle
, INFINITE
);
2366 # ifndef WINMAIN_THREAD_STACK_SIZE
2367 # define WINMAIN_THREAD_STACK_SIZE 0 /* default value */
2370 int WINAPI
WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
,
2371 WINMAIN_LPTSTR lpCmdLine
, int nShowCmd
)
2373 DWORD exit_code
= 1;
2375 main_thread_args args
= {
2376 hInstance
, hPrevInstance
, lpCmdLine
, nShowCmd
2381 /* initialize everything */
2384 /* start the main thread */
2385 thread_h
= GC_CreateThread(NULL
/* lpsa */,
2386 WINMAIN_THREAD_STACK_SIZE
/* ignored on WinCE */,
2387 main_thread_start
, &args
, 0 /* fdwCreate */,
2390 if (thread_h
!= NULL
) {
2391 if ((DWORD
)(word
)GC_do_blocking(GC_waitForSingleObjectInfinite
,
2392 (void *)thread_h
) == WAIT_FAILED
)
2393 ABORT("WaitForSingleObject(main_thread) failed");
2394 GetExitCodeThread (thread_h
, &exit_code
);
2395 CloseHandle (thread_h
);
2397 ABORT("GC_CreateThread(main_thread) failed");
2402 DeleteCriticalSection(&GC_allocate_ml
);
2404 return (int) exit_code
;
2407 #endif /* GC_WINMAIN_REDIRECT */
2409 /* Called by GC_init() - we hold the allocation lock. */
2410 GC_INNER
void GC_thr_init(void)
2412 struct GC_stack_base sb
;
2413 # ifdef GC_ASSERTIONS
2417 GC_ASSERT(I_HOLD_LOCK());
2418 if (GC_thr_initialized
) return;
2420 GC_ASSERT((word
)&GC_threads
% sizeof(word
) == 0);
2421 GC_main_thread
= GetCurrentThreadId();
2422 GC_thr_initialized
= TRUE
;
2424 # ifdef CAN_HANDLE_FORK
2425 /* Prepare for forks if requested. */
2426 if (GC_handle_fork
) {
2427 # ifdef CAN_CALL_ATFORK
2428 if (pthread_atfork(fork_prepare_proc
, fork_parent_proc
,
2429 fork_child_proc
) == 0) {
2430 /* Handlers successfully registered. */
2434 /* else */ if (GC_handle_fork
!= -1)
2435 ABORT("pthread_atfork failed");
2439 /* Add the initial thread, so we can stop it. */
2440 # ifdef GC_ASSERTIONS
2443 GC_get_stack_base(&sb
);
2444 GC_ASSERT(sb_result
== GC_SUCCESS
);
2446 # if defined(PARALLEL_MARK)
2448 char * markers_string
= GETENV("GC_MARKERS");
2451 if (markers_string
!= NULL
) {
2452 markers_m1
= atoi(markers_string
) - 1;
2453 if (markers_m1
>= MAX_MARKERS
) {
2454 WARN("Limiting number of mark threads\n", 0);
2455 markers_m1
= MAX_MARKERS
- 1;
2459 /* There is no GetProcessAffinityMask() in WinCE. */
2460 /* GC_sysinfo is already initialized. */
2461 markers_m1
= (int)GC_sysinfo
.dwNumberOfProcessors
- 1;
2464 DWORD_PTR procMask
= 0;
2471 if (GetProcessAffinityMask(GetCurrentProcess(),
2472 (void *)&procMask
, (void *)&sysMask
)
2476 } while ((procMask
&= procMask
- 1) != 0);
2478 markers_m1
= ncpu
- 1;
2480 # ifdef GC_MIN_MARKERS
2481 /* This is primarily for testing on systems without getenv(). */
2482 if (markers_m1
< GC_MIN_MARKERS
- 1)
2483 markers_m1
= GC_MIN_MARKERS
- 1;
2485 if (markers_m1
>= MAX_MARKERS
)
2486 markers_m1
= MAX_MARKERS
- 1; /* silently limit the value */
2488 available_markers_m1
= markers_m1
;
2491 /* Check whether parallel mode could be enabled. */
2493 # if !defined(GC_PTHREADS_PARAMARK) && !defined(MSWINCE) \
2494 && !defined(DONT_USE_SIGNALANDWAIT)
2496 /* SignalObjectAndWait() API call works only under NT. */
2498 if (GC_win32_dll_threads
|| available_markers_m1
<= 0
2499 # if !defined(GC_PTHREADS_PARAMARK) && !defined(MSWINCE) \
2500 && !defined(DONT_USE_SIGNALANDWAIT)
2502 || (hK32
= GetModuleHandle(TEXT("kernel32.dll"))) == (HMODULE
)0
2503 || (signalObjectAndWait_func
= (SignalObjectAndWait_type
)
2504 GetProcAddress(hK32
, "SignalObjectAndWait")) == 0
2507 /* Disable parallel marking. */
2508 GC_parallel
= FALSE
;
2510 "Single marker thread, turning off parallel marking\n");
2512 # ifndef GC_PTHREADS_PARAMARK
2513 /* Initialize Win32 event objects for parallel marking. */
2514 mark_mutex_event
= CreateEvent(NULL
/* attrs */,
2515 FALSE
/* isManualReset */,
2516 FALSE
/* initialState */, NULL
/* name */);
2517 builder_cv
= CreateEvent(NULL
/* attrs */,
2518 TRUE
/* isManualReset */,
2519 FALSE
/* initialState */, NULL
/* name */);
2520 mark_cv
= CreateEvent(NULL
/* attrs */, TRUE
/* isManualReset */,
2521 FALSE
/* initialState */, NULL
/* name */);
2522 if (mark_mutex_event
== (HANDLE
)0 || builder_cv
== (HANDLE
)0
2523 || mark_cv
== (HANDLE
)0)
2524 ABORT("CreateEvent failed");
2526 /* Disable true incremental collection, but generational is OK. */
2527 GC_time_limit
= GC_TIME_UNLIMITED
;
2530 # endif /* PARALLEL_MARK */
2532 GC_ASSERT(0 == GC_lookup_thread_inner(GC_main_thread
));
2533 GC_register_my_thread_inner(&sb
, GC_main_thread
);
2535 # ifdef PARALLEL_MARK
2536 # ifndef CAN_HANDLE_FORK
2540 /* If we are using a parallel marker, actually start helper threads. */
2541 start_mark_threads();
2549 void *(*start_routine
)(void *);
2554 GC_API
int GC_pthread_join(pthread_t pthread_id
, void **retval
)
2560 GC_ASSERT(!GC_win32_dll_threads
);
2561 # ifdef DEBUG_THREADS
2562 GC_log_printf("thread %p(0x%lx) is joining thread %p\n",
2563 GC_PTHREAD_PTRVAL(pthread_self()),
2564 (long)GetCurrentThreadId(), GC_PTHREAD_PTRVAL(pthread_id
));
2567 /* Thread being joined might not have registered itself yet. */
2568 /* After the join,thread id may have been recycled. */
2569 /* FIXME: It would be better if this worked more like */
2570 /* pthread_support.c. */
2571 # ifndef GC_WIN32_PTHREADS
2572 while ((t
= GC_lookup_pthread(pthread_id
)) == 0)
2576 result
= pthread_join(pthread_id
, retval
);
2578 # ifdef GC_WIN32_PTHREADS
2579 /* win32_pthreads id are unique */
2580 t
= GC_lookup_pthread(pthread_id
);
2581 if (NULL
== t
) ABORT("Thread not registered");
2584 GC_delete_gc_thread_no_free(t
);
2585 GC_INTERNAL_FREE(t
);
2588 # ifdef DEBUG_THREADS
2589 GC_log_printf("thread %p(0x%lx) completed join with thread %p\n",
2590 GC_PTHREAD_PTRVAL(pthread_self()),
2591 (long)GetCurrentThreadId(), GC_PTHREAD_PTRVAL(pthread_id
));
2596 /* Cygwin-pthreads calls CreateThread internally, but it's not easily */
2597 /* interceptible by us..., so intercept pthread_create instead. */
2598 GC_API
int GC_pthread_create(pthread_t
*new_thread
,
2599 GC_PTHREAD_CREATE_CONST pthread_attr_t
*attr
,
2600 void *(*start_routine
)(void *), void *arg
)
2603 struct start_info
* si
;
2605 if (!EXPECT(parallel_initialized
, TRUE
))
2607 /* make sure GC is initialized (i.e. main thread is attached) */
2608 GC_ASSERT(!GC_win32_dll_threads
);
2610 /* This is otherwise saved only in an area mmapped by the thread */
2611 /* library, which isn't visible to the collector. */
2612 si
= GC_malloc_uncollectable(sizeof(struct start_info
));
2613 if (0 == si
) return(EAGAIN
);
2615 si
-> start_routine
= start_routine
;
2618 pthread_attr_getdetachstate(attr
, &si
->detached
)
2619 == PTHREAD_CREATE_DETACHED
) {
2620 si
->detached
= TRUE
;
2623 # ifdef DEBUG_THREADS
2624 GC_log_printf("About to create a thread from %p(0x%lx)\n",
2625 GC_PTHREAD_PTRVAL(pthread_self()),
2626 (long)GetCurrentThreadId());
2628 GC_need_to_lock
= TRUE
;
2629 result
= pthread_create(new_thread
, attr
, GC_pthread_start
, si
);
2631 if (result
) { /* failure */
2637 STATIC
void * GC_CALLBACK
GC_pthread_start_inner(struct GC_stack_base
*sb
,
2640 struct start_info
* si
= arg
;
2642 void *(*start
)(void *);
2644 DWORD thread_id
= GetCurrentThreadId();
2645 pthread_t pthread_id
= pthread_self();
2649 # ifdef DEBUG_THREADS
2650 GC_log_printf("thread %p(0x%x) starting...\n",
2651 GC_PTHREAD_PTRVAL(pthread_id
), (int)thread_id
);
2654 GC_ASSERT(!GC_win32_dll_threads
);
2655 /* If a GC occurs before the thread is registered, that GC will */
2656 /* ignore this thread. That's fine, since it will block trying to */
2657 /* acquire the allocation lock, and won't yet hold interesting */
2660 /* We register the thread here instead of in the parent, so that */
2661 /* we don't need to hold the allocation lock during pthread_create. */
2662 me
= GC_register_my_thread_inner(sb
, thread_id
);
2663 SET_PTHREAD_MAP_CACHE(pthread_id
, thread_id
);
2664 me
-> pthread_id
= pthread_id
;
2665 if (si
->detached
) me
-> flags
|= DETACHED
;
2668 start
= si
-> start_routine
;
2669 start_arg
= si
-> arg
;
2671 GC_free(si
); /* was allocated uncollectable */
2673 pthread_cleanup_push(GC_thread_exit_proc
, (void *)me
);
2674 result
= (*start
)(start_arg
);
2675 me
-> status
= result
;
2676 pthread_cleanup_pop(1);
2678 # ifdef DEBUG_THREADS
2679 GC_log_printf("thread %p(0x%x) returned from start routine\n",
2680 GC_PTHREAD_PTRVAL(pthread_id
), (int)thread_id
);
2685 STATIC
void * GC_pthread_start(void * arg
)
2687 return GC_call_with_stack_base(GC_pthread_start_inner
, arg
);
2690 STATIC
void GC_thread_exit_proc(void *arg
)
2692 GC_thread me
= (GC_thread
)arg
;
2695 GC_ASSERT(!GC_win32_dll_threads
);
2696 # ifdef DEBUG_THREADS
2697 GC_log_printf("thread %p(0x%lx) called pthread_exit()\n",
2698 GC_PTHREAD_PTRVAL(pthread_self()),
2699 (long)GetCurrentThreadId());
2703 GC_wait_for_gc_completion(FALSE
);
2704 # if defined(THREAD_LOCAL_ALLOC)
2705 GC_ASSERT(GC_getspecific(GC_thread_key
) == &me
->tlfs
);
2706 GC_destroy_thread_local(&(me
->tlfs
));
2708 if (me
-> flags
& DETACHED
) {
2709 GC_delete_thread(GetCurrentThreadId());
2711 /* deallocate it as part of join */
2712 me
-> flags
|= FINISHED
;
2714 # if defined(THREAD_LOCAL_ALLOC)
2715 /* It is required to call remove_specific defined in specific.c. */
2716 GC_remove_specific(GC_thread_key
);
2721 # ifndef GC_NO_PTHREAD_SIGMASK
2722 /* Win32 pthread does not support sigmask. */
2723 /* So, nothing required here... */
2724 GC_API
int GC_pthread_sigmask(int how
, const sigset_t
*set
,
2727 return pthread_sigmask(how
, set
, oset
);
2729 # endif /* !GC_NO_PTHREAD_SIGMASK */
2731 GC_API
int GC_pthread_detach(pthread_t thread
)
2737 GC_ASSERT(!GC_win32_dll_threads
);
2739 t
= GC_lookup_pthread(thread
);
2741 result
= pthread_detach(thread
);
2743 if (NULL
== t
) ABORT("Thread not registered");
2745 t
-> flags
|= DETACHED
;
2746 /* Here the pthread thread id may have been recycled. */
2747 if ((t
-> flags
& FINISHED
) != 0) {
2748 GC_delete_gc_thread_no_free(t
);
2749 GC_INTERNAL_FREE(t
);
2756 #elif !defined(GC_NO_THREADS_DISCOVERY)
2757 /* We avoid acquiring locks here, since this doesn't seem to be */
2758 /* preemptible. This may run with an uninitialized collector, in */
2759 /* which case we don't do much. This implies that no threads other */
2760 /* than the main one should be created with an uninitialized */
2761 /* collector. (The alternative of initializing the collector here */
2762 /* seems dangerous, since DllMain is limited in what it can do.) */
2764 # ifdef GC_INSIDE_DLL
2765 /* Export only if needed by client. */
2768 # define GC_DllMain DllMain
2770 BOOL WINAPI
GC_DllMain(HINSTANCE inst GC_ATTR_UNUSED
, ULONG reason
,
2771 LPVOID reserved GC_ATTR_UNUSED
)
2774 static int entry_count
= 0;
2776 if (!GC_win32_dll_threads
&& parallel_initialized
) return TRUE
;
2779 case DLL_THREAD_ATTACH
:
2780 # ifdef PARALLEL_MARK
2781 /* Don't register marker threads. */
2783 /* We could reach here only if parallel_initialized == FALSE. */
2787 GC_ASSERT(entry_count
== 0 || parallel_initialized
);
2788 ++entry_count
; /* and fall through: */
2789 case DLL_PROCESS_ATTACH
:
2790 /* This may run with the collector uninitialized. */
2791 thread_id
= GetCurrentThreadId();
2792 if (parallel_initialized
&& GC_main_thread
!= thread_id
) {
2793 # ifdef PARALLEL_MARK
2794 ABORT("Cannot initialize parallel marker from DllMain");
2796 struct GC_stack_base sb
;
2797 /* Don't lock here. */
2798 # ifdef GC_ASSERTIONS
2801 GC_get_stack_base(&sb
);
2802 GC_ASSERT(sb_result
== GC_SUCCESS
);
2803 GC_register_my_thread_inner(&sb
, thread_id
);
2805 } /* o.w. we already did it during GC_thr_init, called by GC_init */
2808 case DLL_THREAD_DETACH
:
2809 /* We are hopefully running in the context of the exiting thread. */
2810 GC_ASSERT(parallel_initialized
);
2811 if (GC_win32_dll_threads
) {
2812 GC_delete_thread(GetCurrentThreadId());
2816 case DLL_PROCESS_DETACH
:
2817 if (GC_win32_dll_threads
) {
2819 int my_max
= (int)GC_get_max_thread_index();
2821 for (i
= 0; i
<= my_max
; ++i
) {
2822 if (AO_load(&(dll_thread_table
[i
].tm
.in_use
)))
2823 GC_delete_gc_thread_no_free(&dll_thread_table
[i
]);
2826 DeleteCriticalSection(&GC_allocate_ml
);
2832 #endif /* !GC_NO_THREADS_DISCOVERY && !GC_PTHREADS */
2834 /* Perform all initializations, including those that */
2835 /* may require allocation. */
2836 /* Called without allocation lock. */
2837 /* Must be called before a second thread is created. */
2838 GC_INNER
void GC_init_parallel(void)
2840 # if defined(THREAD_LOCAL_ALLOC)
2845 if (parallel_initialized
) return;
2846 parallel_initialized
= TRUE
;
2847 /* GC_init() calls us back, so set flag first. */
2849 if (!GC_is_initialized
) GC_init();
2850 if (GC_win32_dll_threads
) {
2851 GC_need_to_lock
= TRUE
;
2852 /* Cannot intercept thread creation. Hence we don't know if */
2853 /* other threads exist. However, client is not allowed to */
2854 /* create other threads before collector initialization. */
2855 /* Thus it's OK not to lock before this. */
2857 /* Initialize thread local free lists if used. */
2858 # if defined(THREAD_LOCAL_ALLOC)
2860 me
= GC_lookup_thread_inner(GetCurrentThreadId());
2861 CHECK_LOOKUP_MY_THREAD(me
);
2862 GC_init_thread_local(&me
->tlfs
);
2867 #if defined(USE_PTHREAD_LOCKS)
2868 /* Support for pthread locking code. */
2869 /* Pthread_mutex_try_lock may not win here, */
2870 /* due to builtin support for spinning first? */
2872 GC_INNER
volatile GC_bool GC_collecting
= 0;
2873 /* A hint that we're in the collector and */
2874 /* holding the allocation lock for an */
2875 /* extended period. */
2877 GC_INNER
void GC_lock(void)
2879 pthread_mutex_lock(&GC_allocate_ml
);
2881 #endif /* USE_PTHREAD_LOCKS */
2883 #if defined(THREAD_LOCAL_ALLOC)
2885 /* Add thread-local allocation support. VC++ uses __declspec(thread). */
2887 /* We must explicitly mark ptrfree and gcj free lists, since the free */
2888 /* list links wouldn't otherwise be found. We also set them in the */
2889 /* normal free lists, since that involves touching less memory than if */
2890 /* we scanned them normally. */
2891 GC_INNER
void GC_mark_thread_local_free_lists(void)
2896 for (i
= 0; i
< THREAD_TABLE_SZ
; ++i
) {
2897 for (p
= GC_threads
[i
]; 0 != p
; p
= p
-> tm
.next
) {
2898 if (!KNOWN_FINISHED(p
)) {
2899 # ifdef DEBUG_THREADS
2900 GC_log_printf("Marking thread locals for 0x%x\n", (int)p
-> id
);
2902 GC_mark_thread_local_fls_for(&(p
->tlfs
));
2908 # if defined(GC_ASSERTIONS)
2909 void GC_check_tls_for(GC_tlfs p
);
2910 # if defined(USE_CUSTOM_SPECIFIC)
2911 void GC_check_tsd_marks(tsd
*key
);
2913 /* Check that all thread-local free-lists are completely marked. */
2914 /* also check that thread-specific-data structures are marked. */
2915 void GC_check_tls(void)
2920 for (i
= 0; i
< THREAD_TABLE_SZ
; ++i
) {
2921 for (p
= GC_threads
[i
]; 0 != p
; p
= p
-> tm
.next
) {
2922 if (!KNOWN_FINISHED(p
))
2923 GC_check_tls_for(&(p
->tlfs
));
2926 # if defined(USE_CUSTOM_SPECIFIC)
2927 if (GC_thread_key
!= 0)
2928 GC_check_tsd_marks(GC_thread_key
);
2931 # endif /* GC_ASSERTIONS */
2933 #endif /* THREAD_LOCAL_ALLOC ... */
2935 # ifndef GC_NO_THREAD_REDIRECTS
2936 /* Restore thread calls redirection. */
2937 # define CreateThread GC_CreateThread
2938 # define ExitThread GC_ExitThread
2939 # undef _beginthreadex
2940 # define _beginthreadex GC_beginthreadex
2941 # undef _endthreadex
2942 # define _endthreadex GC_endthreadex
2943 # endif /* !GC_NO_THREAD_REDIRECTS */
2945 #endif /* GC_WIN32_THREADS */