/* * Copyright (c) 1994 by Xerox Corporation. All rights reserved. * Copyright (c) 1996 by Silicon Graphics. All rights reserved. * Copyright (c) 1998 by Fergus Henderson. All rights reserved. * Copyright (c) 2000-2008 by Hewlett-Packard Development Company. * All rights reserved. * Copyright (c) 2008-2021 Ivan Maidanski * * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. * * Permission is hereby granted to use or copy this program * for any purpose, provided the above notices are retained on all copies. * Permission to modify the code and to distribute modified code is granted, * provided the above notices are retained, and a notice that the code was * modified is included with the above copyright notice.
*/
#include"private/gc_priv.h"
#ifdefined(GC_WIN32_THREADS)
#ifdef THREAD_LOCAL_ALLOC # include "private/thread_local_alloc.h" #endif/* THREAD_LOCAL_ALLOC */
/* DllMain-based thread registration is currently incompatible */ /* with thread-local allocation, pthreads and WinCE. */ #if (defined(GC_DLL) || defined(GC_INSIDE_DLL)) && !defined(NO_CRT) \
&& !defined(GC_NO_THREADS_DISCOVERY) && !defined(MSWINCE) \
&& !defined(THREAD_LOCAL_ALLOC) && !defined(GC_PTHREADS)
/* This code operates in two distinct modes, depending on */ /* the setting of GC_win32_dll_threads. */ /* If GC_win32_dll_threads is set, all threads in the process */ /* are implicitly registered with the GC by DllMain. */ /* No explicit registration is required, and attempts at */ /* explicit registration are ignored. This mode is */ /* very different from the Posix operation of the collector. */ /* In this mode access to the thread table is lock-free. */ /* Hence there is a static limit on the number of threads. */
# ifdef GC_DISCOVER_TASK_THREADS /* GC_DISCOVER_TASK_THREADS should be used if DllMain-based */ /* thread registration is required but it is impossible to */ /* call GC_use_threads_discovery before other GC routines. */ # define GC_win32_dll_threads TRUE # else STATIC GC_bool GC_win32_dll_threads = FALSE; /* GC_win32_dll_threads must be set (if needed) at the */ /* application initialization time, i.e. before any */ /* collector or thread calls. We make it a "dynamic" */ /* option only to avoid multiple library versions. */ # endif
#else /* If GC_win32_dll_threads is FALSE (or the collector is */ /* built without GC_DLL defined), things operate in a way */ /* that is very similar to Posix platforms, and new threads */ /* must be registered with the collector, e.g. by using */ /* preprocessor-based interception of the thread primitives. */ /* In this case, we use a real data structure for the thread */ /* table. Note that there is no equivalent of linker-based */ /* call interception, since we don't have ELF-like */ /* facilities. The Windows analog appears to be "API */ /* hooking", which really seems to be a standard way to */ /* do minor binary rewriting (?). I'd prefer not to have */ /* the basic collector rely on such facilities, but an */ /* optional package that intercepts thread calls this way */ /* would probably be nice. */ # ifndef GC_NO_THREADS_DISCOVERY # define GC_NO_THREADS_DISCOVERY # endif # define GC_win32_dll_threads FALSE # undef MAX_THREADS # define MAX_THREADS 1 /* dll_thread_table[] is always empty. */ #endif/* GC_NO_THREADS_DISCOVERY */
/* We have two versions of the thread table. Which one */ /* we us depends on whether or not GC_win32_dll_threads */ /* is set. Note that before initialization, we don't */ /* add any entries to either table, even if DllMain is */ /* called. The main thread will be added on */ /* initialization. */
/* The type of the first argument to InterlockedExchange. */ /* Documented to be LONG volatile *, but at least gcc likes */ /* this better. */ typedefLONG * IE_t;
/* GC_use_threads_discovery() is currently incompatible with pthreads */ /* and WinCE. It might be possible to get DllMain-based thread */ /* registration to work with Cygwin, but if you try it then you are on */ /* your own. */
GC_API void GC_CALL GC_use_threads_discovery(void)
{ # ifdef GC_NO_THREADS_DISCOVERY
ABORT("GC DllMain-based thread registration unsupported"); # else /* Turn on GC_win32_dll_threads. */
GC_ASSERT(!parallel_initialized); /* Note that GC_use_threads_discovery is expected to be called by */ /* the client application (not from DllMain) at start-up. */ # ifndef GC_DISCOVER_TASK_THREADS
GC_win32_dll_threads = TRUE; # endif
GC_init_parallel(); # endif
}
#define ADDR_LIMIT ((ptr_t)GC_WORD_MAX)
struct GC_Thread_Rep { union { # ifndef GC_NO_THREADS_DISCOVERY volatile AO_t in_use; /* Updated without lock. */ /* We assert that unused */ /* entries have invalid ids of */ /* zero and zero stack fields. */ /* Used only with GC_win32_dll_threads. */ LONG long_in_use; /* The same but of the type that */ /* matches the first argument of */ /* InterlockedExchange(); volatile is */ /* omitted because the ancient version */ /* of the prototype lacks the qualifier.*/ # endif struct GC_Thread_Rep * next; /* Hash table link without */ /* GC_win32_dll_threads. */ /* More recently allocated threads */ /* with a given pthread id come */ /* first. (All but the first are */ /* guaranteed to be dead, but we may */ /* not yet have registered the join.) */
} tm; /* table_management */
DWORD id;
# ifdef MSWINCE /* According to MSDN specs for WinCE targets: */ /* - DuplicateHandle() is not applicable to thread handles; and */ /* - the value returned by GetCurrentThreadId() could be used as */ /* a "real" thread handle (for SuspendThread(), ResumeThread() and */ /* GetThreadContext()). */ # define THREAD_HANDLE(t) (HANDLE)(word)(t)->id # else
HANDLE handle; # define THREAD_HANDLE(t) (t)->handle # endif
ptr_t stack_base; /* The cold end of the stack. */ /* 0 ==> entry not valid. */ /* !in_use ==> stack_base == 0 */
ptr_t last_stack_min; /* Last known minimum (hottest) address */ /* in stack or ADDR_LIMIT if unset */ # ifdef IA64
ptr_t backing_store_end;
ptr_t backing_store_ptr; # elif defined(I386)
ptr_t initial_stack_base; /* The cold end of the stack saved by */ /* GC_record_stack_base (never modified */ /* by GC_set_stackbottom). */ # endif
ptr_t thread_blocked_sp; /* Protected by GC lock. */ /* NULL value means thread unblocked. */ /* If set to non-NULL, thread will */ /* acquire GC lock before doing any */ /* pointer manipulations. Thus it does */ /* not need to stop this thread. */
struct GC_traced_stack_sect_s *traced_stack_sect; /* Points to the "stack section" data */ /* held in stack by the innermost */ /* GC_call_with_gc_active() of this */ /* thread. May be NULL. */
# ifndef GC_NO_FINALIZATION unsignedshort finalizer_skipped; unsignedchar finalizer_nested; /* Used by GC_check_finalizer_nested() */ /* to minimize the level of recursion */ /* when a client finalizer allocates */ /* memory (initially both are 0). */ # endif
unsignedchar suspended; /* really of GC_bool type */
# ifdef GC_PTHREADS unsignedchar flags; /* Protected by GC lock. */ # define FINISHED 1 /* Thread has exited. */ # define DETACHED 2 /* Thread is intended to be detached. */ # define KNOWN_FINISHED(t) (((t) -> flags) & FINISHED)
pthread_t pthread_id; void *status; /* hold exit value until join in case it's a pointer */ # else # define KNOWN_FINISHED(t) 0 # endif
# ifdef RETRY_GET_THREAD_CONTEXT
ptr_t context_sp;
word context_regs[PUSHED_REGS_COUNT]; /* Populated as part of GC_suspend() as */ /* resume/suspend loop may be needed for the */ /* call to GetThreadContext() to succeed. */ # endif
};
/* We track thread attachments while the world is supposed to be */ /* stopped. Unfortunately, we cannot stop them from starting, since */ /* blocking in DllMain seems to cause the world to deadlock. Thus, */ /* we have to recover if we notice this in the middle of marking. */ STATICvolatile AO_t GC_attached_thread = FALSE;
/* We assumed that volatile ==> memory ordering, at least among */ /* volatiles. This code should consistently use atomic_ops. */ STATICvolatile GC_bool GC_please_stop = FALSE; #elifdefined(GC_ASSERTIONS) STATIC GC_bool GC_please_stop = FALSE; #endif/* GC_NO_THREADS_DISCOVERY && GC_ASSERTIONS */
#ifdefined(WRAP_MARK_SOME) && !defined(GC_PTHREADS) /* Return TRUE if an thread was attached since we last asked or */ /* since GC_attached_thread was explicitly reset. */
GC_INNER GC_bool GC_started_thread_while_stopped(void)
{ # ifndef GC_NO_THREADS_DISCOVERY if (GC_win32_dll_threads) { # ifdef AO_HAVE_compare_and_swap_release if (AO_compare_and_swap_release(&GC_attached_thread, TRUE, FALSE/* stored */)) returnTRUE; # else
AO_nop_full(); /* Prior heap reads need to complete earlier. */ if (AO_load(&GC_attached_thread)) {
AO_store(&GC_attached_thread, FALSE); returnTRUE;
} # endif
} # endif returnFALSE;
} #endif/* WRAP_MARK_SOME */
/* Thread table used if GC_win32_dll_threads is set. */ /* This is a fixed size array. */ /* Since we use runtime conditionals, both versions */ /* are always defined. */ # ifndef MAX_THREADS # define MAX_THREADS 512 # endif
/* Things may get quite slow for large numbers of threads, */ /* since we look them up with sequential search. */ volatilestruct GC_Thread_Rep dll_thread_table[MAX_THREADS];
STATICvolatileLONG GC_max_thread_index = 0; /* Largest index in dll_thread_table */ /* that was ever used. */
/* And now the version used if GC_win32_dll_threads is not set. */ /* This is a chained hash table, with much of the code borrowed */ /* from the Posix implementation. */ #ifndef THREAD_TABLE_SZ # define THREAD_TABLE_SZ 256 /* Power of 2 (for speed). */ #endif #define THREAD_TABLE_INDEX(id) /* id is of DWORD type */ \
(int)((((id) >> 8) ^ (id)) % THREAD_TABLE_SZ) STATIC GC_thread GC_threads[THREAD_TABLE_SZ];
/* It may not be safe to allocate when we register the first thread. */ /* Thus we allocated one statically. It does not contain any pointer */ /* field we need to push ("next" and "status" fields are unused). */ staticstruct GC_Thread_Rep first_thread; static GC_bool first_thread_used = FALSE;
/* Add a thread to GC_threads. We assume it wasn't already there. */ /* Caller holds allocation lock. */ /* Unlike the pthreads version, the id field is set by the caller. */ STATIC GC_thread GC_new_thread(DWORD id)
{ int hv = THREAD_TABLE_INDEX(id);
GC_thread result;
# ifdef DEBUG_THREADS
GC_log_printf("Creating thread 0x%lx\n", (long)id); if (GC_threads[hv] != NULL)
GC_log_printf("Hash collision at GC_threads[%d]\n", hv); # endif
GC_ASSERT(I_HOLD_LOCK()); if (!EXPECT(first_thread_used, TRUE)) {
result = &first_thread;
first_thread_used = TRUE;
GC_ASSERT(NULL == GC_threads[hv]);
} else {
GC_ASSERT(!GC_win32_dll_threads);
result = (struct GC_Thread_Rep *)
GC_INTERNAL_MALLOC(sizeof(struct GC_Thread_Rep), NORMAL); if (result == 0) return(0);
} /* result -> id = id; Done by caller. */
result -> tm.next = GC_threads[hv];
GC_threads[hv] = result; # ifdef GC_PTHREADS
GC_ASSERT(result -> flags == 0); # endif
GC_ASSERT(result -> thread_blocked_sp == NULL); if (EXPECT(result != &first_thread, TRUE))
GC_dirty(result); return(result);
}
GC_INLINE void GC_record_stack_base(GC_vthread me, conststruct GC_stack_base *sb)
{
me -> stack_base = (ptr_t)sb->mem_base; # ifdef IA64
me -> backing_store_end = (ptr_t)sb->reg_base; # elif defined(I386)
me -> initial_stack_base = (ptr_t)sb->mem_base; # endif if (me -> stack_base == NULL)
ABORT("Bad stack base in GC_register_my_thread");
}
/* This may be called from DllMain, and hence operates under unusual */ /* constraints. In particular, it must be lock-free if */ /* GC_win32_dll_threads is set. Always called from the thread being */ /* added. If GC_win32_dll_threads is not set, we already hold the */ /* allocation lock except possibly during single-threaded startup code. */ STATIC GC_thread GC_register_my_thread_inner(conststruct GC_stack_base *sb,
DWORD thread_id)
{
GC_vthread me;
/* The following should be a no-op according to the win32 */ /* documentation. There is empirical evidence that it */ /* isn't. - HB */ # ifdefined(MPROTECT_VDB) && !defined(CYGWIN32) if (GC_auto_incremental # ifdef GWW_VDB
&& !GC_gww_dirty_init() # endif
)
GC_set_write_fault_handler(); # endif
# ifndef GC_NO_THREADS_DISCOVERY if (GC_win32_dll_threads) { int i; /* It appears to be unsafe to acquire a lock here, since this */ /* code is apparently not preemptible on some systems. */ /* (This is based on complaints, not on Microsoft's official */ /* documentation, which says this should perform "only simple */ /* initialization tasks".) */ /* Hence we make do with nonblocking synchronization. */ /* It has been claimed that DllMain is really only executed with */ /* a particular system lock held, and thus careful use of locking */ /* around code that doesn't call back into the system libraries */ /* might be OK. But this hasn't been tested across all win32 */ /* variants. */ for (i = 0;
InterlockedExchange(&dll_thread_table[i].tm.long_in_use, 1) != 0;
i++) { /* Compare-and-swap would make this cleaner, but that's not */ /* supported before Windows 98 and NT 4.0. In Windows 2000, */ /* InterlockedExchange is supposed to be replaced by */ /* InterlockedExchangePointer, but that's not really what I */ /* want here. */ /* FIXME: We should eventually declare Win95 dead and use AO_ */ /* primitives here. */ if (i == MAX_THREADS - 1)
ABORT("Too many threads");
} /* Update GC_max_thread_index if necessary. The following is */ /* safe, and unlike CompareExchange-based solutions seems to work */ /* on all Windows95 and later platforms. */ /* Unfortunately, GC_max_thread_index may be temporarily out of */ /* bounds, so readers have to compensate. */ while (i > GC_max_thread_index) {
InterlockedIncrement((IE_t)&GC_max_thread_index);
} if (GC_max_thread_index >= MAX_THREADS) { /* We overshot due to simultaneous increments. */ /* Setting it to MAX_THREADS-1 is always safe. */
GC_max_thread_index = MAX_THREADS - 1;
}
me = dll_thread_table + i;
} else # endif /* else */ /* Not using DllMain */ {
GC_ASSERT(I_HOLD_LOCK());
GC_in_thread_creation = TRUE; /* OK to collect from unknown thread. */
me = GC_new_thread(thread_id);
GC_in_thread_creation = FALSE; if (me == 0)
ABORT("Failed to allocate memory for thread registering");
} # ifdef GC_PTHREADS /* me can be NULL -> segfault */
me -> pthread_id = pthread_self(); # endif # ifndef MSWINCE /* GetCurrentThread() returns a pseudohandle (a const value). */ if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
GetCurrentProcess(),
(HANDLE*)&(me -> handle),
0 /* dwDesiredAccess */, FALSE /* bInheritHandle */,
DUPLICATE_SAME_ACCESS)) {
ABORT_ARG1("DuplicateHandle failed", ": errcode= 0x%X", (unsigned)GetLastError());
} # endif
me -> last_stack_min = ADDR_LIMIT;
GC_record_stack_base(me, sb); /* Up until this point, GC_push_all_stacks considers this thread */ /* invalid. */ /* Up until this point, this entry is viewed as reserved but invalid */ /* by GC_delete_thread. */
me -> id = thread_id; # ifdefined(THREAD_LOCAL_ALLOC)
GC_init_thread_local((GC_tlfs)(&(me->tlfs))); # endif # ifndef GC_NO_THREADS_DISCOVERY if (GC_win32_dll_threads) { if (GC_please_stop) {
AO_store(&GC_attached_thread, TRUE);
AO_nop_full(); /* Later updates must become visible after this. */
} /* We'd like to wait here, but can't, since waiting in DllMain */ /* provokes deadlocks. */ /* Thus we force marking to be restarted instead. */
} else # endif /* else */ {
GC_ASSERT(!GC_please_stop); /* Otherwise both we and the thread stopping code would be */ /* holding the allocation lock. */
} return (GC_thread)(me);
}
/* * GC_max_thread_index may temporarily be larger than MAX_THREADS. * To avoid subscript errors, we check on access.
*/
GC_INLINE LONG GC_get_max_thread_index(void)
{ LONG my_max = GC_max_thread_index; if (my_max >= MAX_THREADS) return MAX_THREADS - 1; return my_max;
}
/* Return the GC_thread corresponding to a thread id. May be called */ /* without a lock, but should be called in contexts in which the */ /* requested thread cannot be asynchronously deleted, e.g. from the */ /* thread itself. */ /* This version assumes that either GC_win32_dll_threads is set, or */ /* we hold the allocator lock. */ /* Also used (for assertion checking only) from thread_local_alloc.c. */ STATIC GC_thread GC_lookup_thread_inner(DWORD thread_id)
{ # ifndef GC_NO_THREADS_DISCOVERY if (GC_win32_dll_threads) { int i; LONG my_max = GC_get_max_thread_index(); for (i = 0; i <= my_max &&
(!AO_load_acquire(&dll_thread_table[i].tm.in_use)
|| dll_thread_table[i].id != thread_id); /* Must still be in_use, since nobody else can store our */ /* thread_id. */
i++) { /* empty */
} return i <= my_max ? (GC_thread)(dll_thread_table + i) : NULL;
} else # endif /* else */ {
GC_thread p = GC_threads[THREAD_TABLE_INDEX(thread_id)];
GC_ASSERT(I_HOLD_LOCK()); while (p != 0 && p -> id != thread_id) p = p -> tm.next; return(p);
}
}
#ifndef GC_NO_FINALIZATION /* Called by GC_finalize() (in case of an allocation failure observed). */ /* GC_reset_finalizer_nested() is the same as in pthread_support.c. */
GC_INNER void GC_reset_finalizer_nested(void)
{
GC_thread me = GC_lookup_thread_inner(GetCurrentThreadId());
/* GC_check_finalizer_nested() is the same as in pthread_support.c. */
GC_INNER unsignedchar *GC_check_finalizer_nested(void)
{
GC_thread me = GC_lookup_thread_inner(GetCurrentThreadId()); unsigned nesting_level;
CHECK_LOOKUP_MY_THREAD(me);
nesting_level = me->finalizer_nested; if (nesting_level) { /* We are inside another GC_invoke_finalizers(). */ /* Skip some implicitly-called GC_invoke_finalizers() */ /* depending on the nesting (recursion) level. */ if (++me->finalizer_skipped < (1U << nesting_level)) return NULL;
me->finalizer_skipped = 0;
}
me->finalizer_nested = (unsignedchar)(nesting_level + 1); return &me->finalizer_nested;
} #endif/* !GC_NO_FINALIZATION */
#ifdefined(GC_ASSERTIONS) && defined(THREAD_LOCAL_ALLOC) /* This is called from thread-local GC_malloc(). */
GC_bool GC_is_thread_tsd_valid(void *tsd)
{
GC_thread me;
DCL_LOCK_STATE;
/* Make sure thread descriptor t is not protected by the VDB */ /* implementation. */ /* Used to prevent write faults when the world is (partially) stopped, */ /* since it may have been stopped with a system lock held, and that */ /* lock may be required for fault handling. */ #ifdefined(MPROTECT_VDB) # define UNPROTECT_THREAD(t) \ if (!GC_win32_dll_threads && GC_auto_incremental \
&& t != &first_thread) { \
GC_ASSERT(SMALL_OBJ(GC_size(t))); \
GC_remove_protection(HBLKPTR(t), 1, FALSE); \
} else (void)0 #else # define UNPROTECT_THREAD(t) (void)0 #endif
/* If a thread has been joined, but we have not yet */ /* been notified, then there may be more than one thread */ /* in the table with the same win32 id. */ /* This is OK, but we need a way to delete a specific one. */ /* Assumes we hold the allocation lock unless */ /* GC_win32_dll_threads is set. Does not actually free */ /* GC_thread entry (only unlinks it). */ /* If GC_win32_dll_threads is set it should be called from the */ /* thread being deleted. */ STATICvoid GC_delete_gc_thread_no_free(GC_vthread t)
{ # ifndef MSWINCE
CloseHandle(t->handle); # endif # ifndef GC_NO_THREADS_DISCOVERY if (GC_win32_dll_threads) { /* This is intended to be lock-free. */ /* It is either called synchronously from the thread being */ /* deleted, or by the joining thread. */ /* In this branch asynchronous changes to (*t) are possible. */ /* It's not allowed to call GC_printf (and the friends) here, */ /* see GC_stop_world() for the information. */
t -> stack_base = 0;
t -> id = 0;
t -> suspended = FALSE; # ifdef RETRY_GET_THREAD_CONTEXT
t -> context_sp = NULL; # endif
AO_store_release(&t->tm.in_use, FALSE);
} else # endif /* else */ {
DWORD id = ((GC_thread)t) -> id; /* Cast away volatile qualifier, since we have lock. */ int hv = THREAD_TABLE_INDEX(id);
GC_thread p = GC_threads[hv];
GC_thread prev = NULL;
GC_ASSERT(I_HOLD_LOCK()); while (p != (GC_thread)t) {
prev = p;
p = p -> tm.next;
} if (prev == 0) {
GC_threads[hv] = p -> tm.next;
} else {
GC_ASSERT(prev != &first_thread);
prev -> tm.next = p -> tm.next;
GC_dirty(prev);
}
}
}
/* Delete a thread from GC_threads. We assume it is there. */ /* (The code intentionally traps if it wasn't.) Assumes we */ /* hold the allocation lock unless GC_win32_dll_threads is set. */ /* If GC_win32_dll_threads is set then it should be called from */ /* the thread being deleted. It is also safe to delete the */ /* main thread (unless GC_win32_dll_threads). */ STATICvoid GC_delete_thread(DWORD id)
{ if (GC_win32_dll_threads) {
GC_vthread t = GC_lookup_thread_inner(id);
/* Check GC is initialized and the current thread is registered. */
LOCK();
GC_ASSERT(GC_lookup_thread_inner(GetCurrentThreadId()) != 0);
UNLOCK(); # endif # if !defined(GC_ALWAYS_MULTITHREADED) && !defined(PARALLEL_MARK) \
&& !defined(GC_NO_THREADS_DISCOVERY) /* GC_init() does not call GC_init_parallel() in this case. */
parallel_initialized = TRUE; # endif
GC_start_mark_threads();
set_need_to_lock();
}
if (GC_need_to_lock == FALSE)
ABORT("Threads explicit registering is not previously enabled");
/* We lock here, since we want to wait for an ongoing GC. */
LOCK();
me = GC_lookup_thread_inner(thread_id); if (me == 0) { # ifdef GC_PTHREADS
me = GC_register_my_thread_inner(sb, thread_id); # ifdefined(CPPCHECK)
GC_noop1(me->flags); # endif
me -> flags |= DETACHED; /* Treat as detached, since we do not need to worry about */ /* pointer results. */ # else
GC_register_my_thread_inner(sb, thread_id); # endif
UNLOCK(); return GC_SUCCESS;
} else # ifdef GC_PTHREADS /* else */ if ((me -> flags & FINISHED) != 0) {
GC_record_stack_base(me, sb);
me -> flags &= ~FINISHED; /* but not DETACHED */ # ifdef THREAD_LOCAL_ALLOC
GC_init_thread_local((GC_tlfs)(&me->tlfs)); # endif
UNLOCK(); return GC_SUCCESS;
} else # endif /* else */ {
UNLOCK(); return GC_DUPLICATE;
}
}
#ifdef GC_DISABLE_INCREMENTAL # define GC_wait_for_gc_completion(wait_for_all) (void)(wait_for_all) #else /* Similar to that in pthread_support.c. */ STATICvoid GC_wait_for_gc_completion(GC_bool wait_for_all)
{
GC_ASSERT(I_HOLD_LOCK()); if (GC_incremental && GC_collection_in_progress()) {
word old_gc_no = GC_gc_no;
/* Make sure that no part of our stack is still on the mark stack, */ /* since it's about to be unmapped. */ do {
ENTER_GC();
GC_in_thread_creation = TRUE;
GC_collect_a_little_inner(1);
GC_in_thread_creation = FALSE;
EXIT_GC();
if (GC_win32_dll_threads) { # ifdefined(THREAD_LOCAL_ALLOC) /* Can't happen: see GC_use_threads_discovery(). */
GC_ASSERT(FALSE); # else /* FIXME: Should we just ignore this? */
GC_delete_thread(GetCurrentThreadId()); # endif
} else { # ifdefined(THREAD_LOCAL_ALLOC) || defined(GC_PTHREADS)
GC_thread me; # endif
DWORD thread_id = GetCurrentThreadId();
LOCK();
GC_wait_for_gc_completion(FALSE); # ifdefined(THREAD_LOCAL_ALLOC) || defined(GC_PTHREADS)
me = GC_lookup_thread_inner(thread_id);
CHECK_LOOKUP_MY_THREAD(me);
GC_ASSERT(!KNOWN_FINISHED(me)); # endif # ifdefined(THREAD_LOCAL_ALLOC)
GC_ASSERT(GC_getspecific(GC_thread_key) == &me->tlfs);
GC_destroy_thread_local(&(me->tlfs)); # endif # ifdef GC_PTHREADS if ((me -> flags & DETACHED) == 0) {
me -> flags |= FINISHED;
} else # endif /* else */ {
GC_delete_thread(thread_id);
} # ifdefined(THREAD_LOCAL_ALLOC) /* It is required to call remove_specific defined in specific.c. */
GC_remove_specific(GC_thread_key); # endif
UNLOCK();
} return GC_SUCCESS;
}
/* Wrapper for functions that are likely to block for an appreciable */ /* length of time. */
/* GC_do_blocking_inner() is nearly the same as in pthread_support.c */
GC_INNER void GC_do_blocking_inner(ptr_t data, void * context GC_ATTR_UNUSED)
{ struct blocking_data * d = (struct blocking_data *) data;
DWORD thread_id = GetCurrentThreadId();
GC_thread me; # ifdef IA64
ptr_t stack_ptr = GC_save_regs_in_stack(); # endif
DCL_LOCK_STATE;
LOCK();
me = GC_lookup_thread_inner(thread_id);
CHECK_LOOKUP_MY_THREAD(me);
GC_ASSERT(me -> thread_blocked_sp == NULL); # ifdef IA64
me -> backing_store_ptr = stack_ptr; # endif
me -> thread_blocked_sp = (ptr_t) &d; /* save approx. sp */ /* Save context here if we want to support precise stack marking */
UNLOCK();
d -> client_data = (d -> fn)(d -> client_data);
LOCK(); /* This will block if the world is stopped. */ # ifdefined(CPPCHECK)
GC_noop1((word)me->thread_blocked_sp); # endif
me -> thread_blocked_sp = NULL;
UNLOCK();
}
/* GC_call_with_gc_active() has the opposite to GC_do_blocking() */ /* functionality. It might be called from a user function invoked by */ /* GC_do_blocking() to temporarily back allow calling any GC function */ /* and/or manipulating pointers to the garbage collected heap. */
GC_API void * GC_CALL GC_call_with_gc_active(GC_fn_type fn, void * client_data)
{ struct GC_traced_stack_sect_s stacksect;
DWORD thread_id = GetCurrentThreadId();
GC_thread me;
DCL_LOCK_STATE;
LOCK(); /* This will block if the world is stopped. */
me = GC_lookup_thread_inner(thread_id);
CHECK_LOOKUP_MY_THREAD(me); /* Adjust our stack bottom pointer (this could happen unless */ /* GC_get_stack_base() was used which returned GC_SUCCESS). */
GC_ASSERT(me -> stack_base != NULL); if ((word)me->stack_base < (word)(&stacksect)) {
me -> stack_base = (ptr_t)(&stacksect); # ifdefined(I386)
me -> initial_stack_base = me -> stack_base; # endif
}
if (me -> thread_blocked_sp == NULL) { /* We are not inside GC_do_blocking() - do nothing more. */
UNLOCK();
client_data = fn(client_data); /* Prevent treating the above as a tail call. */
GC_noop1(COVERT_DATAFLOW(&stacksect)); return client_data; /* result */
}
/* Setup new "stack section". */
stacksect.saved_stack_ptr = me -> thread_blocked_sp; # ifdef IA64 /* This is the same as in GC_call_with_stack_base(). */
stacksect.backing_store_end = GC_save_regs_in_stack(); /* Unnecessarily flushes register stack, */ /* but that probably doesn't hurt. */
stacksect.saved_backing_store_ptr = me -> backing_store_ptr; # endif
stacksect.prev = me -> traced_stack_sect;
me -> thread_blocked_sp = NULL;
me -> traced_stack_sect = &stacksect;
GC_ASSERT(I_HOLD_LOCK()); if (NULL == t) { /* current thread? */
t = GC_lookup_thread_inner(GetCurrentThreadId());
CHECK_LOOKUP_MY_THREAD(t);
}
GC_ASSERT(!KNOWN_FINISHED(t));
GC_ASSERT(NULL == t -> thread_blocked_sp
&& NULL == t -> traced_stack_sect); /* for now */
t -> stack_base = (ptr_t)sb->mem_base;
t -> last_stack_min = ADDR_LIMIT; /* reset the known minimum */ # ifdef IA64
t -> backing_store_end = (ptr_t)sb->reg_base; # endif
}
LOCK();
me = GC_lookup_thread_inner(thread_id);
CHECK_LOOKUP_MY_THREAD(me); /* the thread is assumed to be registered */
sb -> mem_base = me -> stack_base; # ifdef IA64
sb -> reg_base = me -> backing_store_end; # endif
UNLOCK(); return (void *)me; /* gc_thread_handle */
}
#ifdef GC_PTHREADS
/* A quick-and-dirty cache of the mapping between pthread_t */ /* and win32 thread id. */ # define PTHREAD_MAP_SIZE 512
DWORD GC_pthread_map_cache[PTHREAD_MAP_SIZE] = {0}; # define PTHREAD_MAP_INDEX(pthread_id) \
((NUMERIC_THREAD_ID(pthread_id) >> 5) % PTHREAD_MAP_SIZE) /* It appears pthread_t is really a pointer type ... */ # define SET_PTHREAD_MAP_CACHE(pthread_id, win32_id) \
(void)(GC_pthread_map_cache[PTHREAD_MAP_INDEX(pthread_id)] = (win32_id)) # define GET_PTHREAD_MAP_CACHE(pthread_id) \
GC_pthread_map_cache[PTHREAD_MAP_INDEX(pthread_id)]
/* Return a GC_thread corresponding to a given pthread_t. */ /* Returns 0 if it's not there. */ /* We assume that this is only called for pthread ids that */ /* have not yet terminated or are still joinable, and */ /* cannot be concurrently terminated. */ /* Assumes we do NOT hold the allocation lock. */ STATIC GC_thread GC_lookup_pthread(pthread_t id)
{ # ifndef GC_NO_THREADS_DISCOVERY if (GC_win32_dll_threads) { int i; LONG my_max = GC_get_max_thread_index();
for (i = 0; i <= my_max &&
(!AO_load_acquire(&dll_thread_table[i].tm.in_use)
|| THREAD_EQUAL(dll_thread_table[i].pthread_id, id)); /* Must still be in_use, since nobody else can */ /* store our thread_id. */
i++) { /* empty */
} return i <= my_max ? (GC_thread)(dll_thread_table + i) : NULL;
} else # endif /* else */ { /* We first try the cache. If that fails, we use a very slow */ /* approach. */
DWORD win32_id = GET_PTHREAD_MAP_CACHE(id); int hv_guess = THREAD_TABLE_INDEX(win32_id); int hv;
GC_thread p;
DCL_LOCK_STATE;
LOCK(); for (p = GC_threads[hv_guess]; 0 != p; p = p -> tm.next) { if (THREAD_EQUAL(p -> pthread_id, id)) goto foundit;
} for (hv = 0; hv < THREAD_TABLE_SZ; ++hv) { for (p = GC_threads[hv]; 0 != p; p = p -> tm.next) { if (THREAD_EQUAL(p -> pthread_id, id)) goto foundit;
}
}
p = 0;
foundit:
UNLOCK(); return p;
}
}
#endif/* GC_PTHREADS */
#ifdef CAN_HANDLE_FORK /* Similar to that in pthread_support.c but also rehashes the table */ /* since hash map key (thread_id) differs from that in the parent. */ STATICvoid GC_remove_all_threads_but_me(void)
{ int hv;
GC_thread me = NULL;
DWORD thread_id;
pthread_t pthread_id = pthread_self(); /* same as in parent */
for (p = GC_threads[hv]; 0 != p; p = next) {
next = p -> tm.next; if (THREAD_EQUAL(p -> pthread_id, pthread_id)
&& me == NULL) { /* ignore dead threads with the same id */
me = p;
p -> tm.next = 0;
} else { # ifdef THREAD_LOCAL_ALLOC if ((p -> flags & FINISHED) == 0) { /* Cannot call GC_destroy_thread_local here (see the */ /* corresponding comment in pthread_support.c). */
GC_remove_specific_after_fork(GC_thread_key, p -> pthread_id);
} # endif if (&first_thread != p)
GC_INTERNAL_FREE(p);
}
}
GC_threads[hv] = NULL;
}
/* Put "me" back to GC_threads. */
GC_ASSERT(me != NULL);
thread_id = GetCurrentThreadId(); /* differs from that in parent */
GC_threads[THREAD_TABLE_INDEX(thread_id)] = me;
/* Update Win32 thread Id and handle. */
me -> id = thread_id; # ifndef MSWINCE if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
GetCurrentProcess(), (HANDLE *)&me->handle,
0 /* dwDesiredAccess */, FALSE /* bInheritHandle */,
DUPLICATE_SAME_ACCESS))
ABORT("DuplicateHandle failed"); # endif
# ifdefined(THREAD_LOCAL_ALLOC) && !defined(USE_CUSTOM_SPECIFIC) /* For Cygwin, we need to re-assign thread-local pointer to */ /* 'tlfs' (it is OK to call GC_destroy_thread_local and */ /* GC_free_internal before this action). */ if (GC_setspecific(GC_thread_key, &me->tlfs) != 0)
ABORT("GC_setspecific failed (in child)"); # endif
}
staticvoid fork_child_proc(void)
{ # ifdef PARALLEL_MARK if (GC_parallel) {
GC_release_mark_lock();
GC_parallel = FALSE; /* or GC_markers_m1 = 0 */ /* Turn off parallel marking in the child, since we are */ /* probably just going to exec, and we would have to */ /* restart mark threads. */
} # endif
GC_remove_all_threads_but_me();
UNLOCK();
}
/* Routines for fork handling by client (no-op if pthread_atfork works). */
GC_API void GC_CALL GC_atfork_prepare(void)
{ if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); if (GC_handle_fork <= 0)
fork_prepare_proc();
}
void GC_push_thread_structures(void)
{
GC_ASSERT(I_HOLD_LOCK()); # ifndef GC_NO_THREADS_DISCOVERY if (GC_win32_dll_threads) { /* Unlike the other threads implementations, the thread table */ /* here contains no pointers to the collectible heap (note also */ /* that GC_PTHREADS is incompatible with DllMain-based thread */ /* registration). Thus we have no private structures we need */ /* to preserve. */
} else # endif /* else */ {
GC_PUSH_ALL_SYM(GC_threads);
} # ifdefined(THREAD_LOCAL_ALLOC) && defined(USE_CUSTOM_SPECIFIC)
GC_PUSH_ALL_SYM(GC_thread_key); /* Just in case we ever use our own TLS implementation. */ # endif
}
# ifdef MSWINCE /* SuspendThread() will fail if thread is running kernel code. */ while (SuspendThread(THREAD_HANDLE(t)) == (DWORD)-1) {
GC_release_dirty_lock();
Sleep(10); /* in millis */
GC_acquire_dirty_lock();
} # elif defined(RETRY_GET_THREAD_CONTEXT) for (retry_cnt = 0;;) { /* Apparently the Windows 95 GetOpenFileName call creates */ /* a thread that does not properly get cleaned up, and */ /* SuspendThread on its descriptor may provoke a crash. */ /* This reduces the probability of that event, though it still */ /* appears there is a race here. */ if (GetExitCodeThread(t -> handle, &exitCode)
&& exitCode != STILL_ACTIVE) {
GC_release_dirty_lock(); # ifdef GC_PTHREADS
t -> stack_base = 0; /* prevent stack from being pushed */ # else /* This breaks pthread_join on Cygwin, which is guaranteed to */ /* only see user threads. */
GC_ASSERT(GC_win32_dll_threads);
GC_delete_gc_thread_no_free(t); # endif return;
}
if (SuspendThread(t->handle) != (DWORD)-1) {
CONTEXT context;
context.ContextFlags = GET_THREAD_CONTEXT_FLAGS; if (GetThreadContext(t->handle, &context)) { /* TODO: WoW64 extra workaround: if CONTEXT_EXCEPTION_ACTIVE */ /* then Sleep(1) and retry. */
t->context_sp = copy_ptr_regs(t->context_regs, &context); break; /* success; the context pointer registers are saved */
}
/* Resume the thread, try to suspend it in a better location. */ if (ResumeThread(t->handle) == (DWORD)-1)
ABORT("ResumeThread failed in suspend loop");
} if (retry_cnt > 1) {
GC_release_dirty_lock();
Sleep(0); /* yield */
GC_acquire_dirty_lock();
} if (++retry_cnt >= MAX_SUSPEND_THREAD_RETRIES)
ABORT("SuspendThread loop failed"); /* something must be wrong */
} # else if (GetExitCodeThread(t -> handle, &exitCode)
&& exitCode != STILL_ACTIVE) {
GC_release_dirty_lock(); # ifdef GC_PTHREADS
t -> stack_base = 0; /* prevent stack from being pushed */ # else
GC_ASSERT(GC_win32_dll_threads);
GC_delete_gc_thread_no_free(t); # endif return;
} if (SuspendThread(t -> handle) == (DWORD)-1)
ABORT("SuspendThread failed"); # endif
t -> suspended = (unsignedchar)TRUE;
GC_release_dirty_lock(); if (GC_on_thread_event)
GC_on_thread_event(GC_EVENT_THREAD_SUSPENDED, THREAD_HANDLE(t));
}
if (!GC_thr_initialized)
ABORT("GC_stop_world() called before GC_thr_init()");
GC_ASSERT(I_HOLD_LOCK());
/* This code is the same as in pthread_stop_world.c */ # ifdef PARALLEL_MARK if (GC_parallel) {
GC_acquire_mark_lock();
GC_ASSERT(GC_fl_builder_count == 0); /* We should have previously waited for it to become zero. */
} # endif /* PARALLEL_MARK */
# if !defined(GC_NO_THREADS_DISCOVERY) || defined(GC_ASSERTIONS)
GC_please_stop = TRUE; # endif # if (defined(MSWIN32) && !defined(CONSOLE_LOG)) || defined(MSWINCE)
GC_ASSERT(!GC_write_disabled);
EnterCriticalSection(&GC_write_cs); /* It's not allowed to call GC_printf() (and friends) here down to */ /* LeaveCriticalSection (same applies recursively to GC_suspend, */ /* GC_delete_gc_thread_no_free, GC_get_max_thread_index, GC_size */ /* and GC_remove_protection). */ # ifdef GC_ASSERTIONS
GC_write_disabled = TRUE; # endif # endif # ifndef GC_NO_THREADS_DISCOVERY if (GC_win32_dll_threads) { int i; int my_max; /* Any threads being created during this loop will end up setting */ /* GC_attached_thread when they start. This will force marking */ /* to restart. This is not ideal, but hopefully correct. */
AO_store(&GC_attached_thread, FALSE);
my_max = (int)GC_get_max_thread_index(); for (i = 0; i <= my_max; i++) {
GC_vthread t = dll_thread_table + i; if (t -> stack_base != 0 && t -> thread_blocked_sp == NULL
&& t -> id != thread_id) {
GC_suspend((GC_thread)t);
}
}
} else # endif /* else */ {
GC_thread t; int i;
for (i = 0; i < THREAD_TABLE_SZ; i++) { for (t = GC_threads[i]; t != 0; t = t -> tm.next) { if (t -> stack_base != 0 && t -> thread_blocked_sp == NULL
&& !KNOWN_FINISHED(t) && t -> id != thread_id) {
GC_suspend(t);
}
}
}
} # if (defined(MSWIN32) && !defined(CONSOLE_LOG)) || defined(MSWINCE) # ifdef GC_ASSERTIONS
GC_write_disabled = FALSE; # endif
LeaveCriticalSection(&GC_write_cs); # endif # ifdef PARALLEL_MARK if (GC_parallel)
GC_release_mark_lock(); # endif
}
GC_ASSERT(I_HOLD_LOCK()); if (GC_win32_dll_threads) { LONG my_max = GC_get_max_thread_index(); int i;
for (i = 0; i <= my_max; i++) {
GC_thread t = (GC_thread)(dll_thread_table + i); if (t -> suspended) { # ifdef DEBUG_THREADS
GC_log_printf("Resuming 0x%x\n", (int)t->id); # endif
GC_ASSERT(t -> stack_base != 0 && t -> id != thread_id); if (ResumeThread(THREAD_HANDLE(t)) == (DWORD)-1)
ABORT("ResumeThread failed");
t -> suspended = FALSE; if (GC_on_thread_event)
GC_on_thread_event(GC_EVENT_THREAD_UNSUSPENDED, THREAD_HANDLE(t));
} /* Else thread is unregistered or not suspended. */
}
} else {
GC_thread t; int i;
for (i = 0; i < THREAD_TABLE_SZ; i++) { for (t = GC_threads[i]; t != 0; t = t -> tm.next) { if (t -> suspended) { # ifdef DEBUG_THREADS
GC_log_printf("Resuming 0x%x\n", (int)t->id); # endif
GC_ASSERT(t -> stack_base != 0 && t -> id != thread_id); if (ResumeThread(THREAD_HANDLE(t)) == (DWORD)-1)
ABORT("ResumeThread failed");
UNPROTECT_THREAD(t);
t -> suspended = FALSE; if (GC_on_thread_event)
GC_on_thread_event(GC_EVENT_THREAD_UNSUSPENDED, THREAD_HANDLE(t));
} else { # ifdef DEBUG_THREADS
GC_log_printf("Not resuming thread 0x%x as it is not suspended\n",
(int)t->id); # endif
}
}
}
} # if !defined(GC_NO_THREADS_DISCOVERY) || defined(GC_ASSERTIONS)
GC_please_stop = FALSE; # endif
}
#ifdef MSWINCE /* The VirtualQuery calls below won't work properly on some old WinCE */ /* versions, but since each stack is restricted to an aligned 64 KiB */ /* region of virtual memory we can just take the next lowest multiple */ /* of 64 KiB. The result of this macro must not be used as its */ /* argument later and must not be used as the lower bound for sp */ /* check (since the stack may be bigger than 64 KiB). */ # define GC_wince_evaluate_stack_min(s) \
(ptr_t)(((word)(s) - 1) & ~(word)0xFFFF) #elifdefined(GC_ASSERTIONS) # define GC_dont_query_stack_min FALSE #endif
/* A cache holding the results of the recent VirtualQuery call. */ /* Protected by the allocation lock. */ static ptr_t last_address = 0; static MEMORY_BASIC_INFORMATION last_info;
/* Probe stack memory region (starting at "s") to find out its */ /* lowest address (i.e. stack top). */ /* S must be a mapped address inside the region, NOT the first */ /* unmapped address. */ STATIC ptr_t GC_get_stack_min(ptr_t s)
{
ptr_t bottom;
GC_ASSERT(I_HOLD_LOCK()); if (s != last_address) {
VirtualQuery(s, &last_info, sizeof(last_info));
last_address = s;
} do {
bottom = (ptr_t)last_info.BaseAddress;
VirtualQuery(bottom - 1, &last_info, sizeof(last_info));
last_address = bottom - 1;
} while ((last_info.Protect & PAGE_READWRITE)
&& !(last_info.Protect & PAGE_GUARD)); return(bottom);
}
/* Return true if the page at s has protections appropriate */ /* for a stack page. */ static GC_bool may_be_in_stack(ptr_t s)
{
GC_ASSERT(I_HOLD_LOCK()); if (s != last_address) {
VirtualQuery(s, &last_info, sizeof(last_info));
last_address = s;
} return (last_info.Protect & PAGE_READWRITE)
&& !(last_info.Protect & PAGE_GUARD);
}
/* Copy all registers that might point into the heap. Frame */ /* pointer registers are included in case client code was */ /* compiled with the 'omit frame pointer' optimization. */ /* The context register values are stored to regs argument */ /* which is expected to be of PUSHED_REGS_COUNT length exactly. */ /* The functions returns the context stack pointer value. */ static ptr_t copy_ptr_regs(word *regs, const CONTEXT *pcontext) {
ptr_t sp; int cnt = 0; # define context (*pcontext) # define PUSH1(reg) (regs[cnt++] = (word)pcontext->reg) # define PUSH2(r1,r2) (PUSH1(r1), PUSH1(r2)) # define PUSH4(r1,r2,r3,r4) (PUSH2(r1,r2), PUSH2(r3,r4)) # ifdefined(I386) # ifdef WOW64_THREAD_CONTEXT_WORKAROUND
PUSH2(ContextFlags, SegFs); /* cannot contain pointers */ # endif
PUSH4(Edi,Esi,Ebx,Edx), PUSH2(Ecx,Eax), PUSH1(Ebp);
sp = (ptr_t)context.Esp; # elif defined(X86_64)
PUSH4(Rax,Rcx,Rdx,Rbx); PUSH2(Rbp, Rsi); PUSH1(Rdi);
PUSH4(R8, R9, R10, R11); PUSH4(R12, R13, R14, R15);
sp = (ptr_t)context.Rsp; # elif defined(ARM32)
PUSH4(R0,R1,R2,R3),PUSH4(R4,R5,R6,R7),PUSH4(R8,R9,R10,R11);
PUSH1(R12);
sp = (ptr_t)context.Sp; # elif defined(AARCH64)
PUSH4(X0,X1,X2,X3),PUSH4(X4,X5,X6,X7),PUSH4(X8,X9,X10,X11);
PUSH4(X12,X13,X14,X15),PUSH4(X16,X17,X18,X19),PUSH4(X20,X21,X22,X23);
PUSH4(X24,X25,X26,X27),PUSH1(X28);
PUSH1(Lr);
sp = (ptr_t)context.Sp; # elif defined(SHx)
PUSH4(R0,R1,R2,R3), PUSH4(R4,R5,R6,R7), PUSH4(R8,R9,R10,R11);
PUSH2(R12,R13), PUSH1(R14);
sp = (ptr_t)context.R15; # elif defined(MIPS)
PUSH4(IntAt,IntV0,IntV1,IntA0), PUSH4(IntA1,IntA2,IntA3,IntT0);
PUSH4(IntT1,IntT2,IntT3,IntT4), PUSH4(IntT5,IntT6,IntT7,IntS0);
PUSH4(IntS1,IntS2,IntS3,IntS4), PUSH4(IntS5,IntS6,IntS7,IntT8);
PUSH4(IntT9,IntK0,IntK1,IntS8);
sp = (ptr_t)context.IntSp; # elif defined(PPC)
PUSH4(Gpr0, Gpr3, Gpr4, Gpr5), PUSH4(Gpr6, Gpr7, Gpr8, Gpr9);
PUSH4(Gpr10,Gpr11,Gpr12,Gpr14), PUSH4(Gpr15,Gpr16,Gpr17,Gpr18);
PUSH4(Gpr19,Gpr20,Gpr21,Gpr22), PUSH4(Gpr23,Gpr24,Gpr25,Gpr26);
PUSH4(Gpr27,Gpr28,Gpr29,Gpr30), PUSH1(Gpr31);
sp = (ptr_t)context.Gpr1; # elif defined(ALPHA)
PUSH4(IntV0,IntT0,IntT1,IntT2), PUSH4(IntT3,IntT4,IntT5,IntT6);
PUSH4(IntT7,IntS0,IntS1,IntS2), PUSH4(IntS3,IntS4,IntS5,IntFp);
PUSH4(IntA0,IntA1,IntA2,IntA3), PUSH4(IntA4,IntA5,IntT8,IntT9);
PUSH4(IntT10,IntT11,IntT12,IntAt);
sp = (ptr_t)context.IntSp; # elif defined(CPPCHECK)
sp = (ptr_t)(word)cnt; /* to workaround "cnt not used" false positive */ # else # error Architecture is not supported # endif # undef context
GC_ASSERT(cnt == PUSHED_REGS_COUNT); return sp;
}
STATIC word GC_push_stack_for(GC_thread thread, DWORD me)
{
ptr_t sp, stack_min;
struct GC_traced_stack_sect_s *traced_stack_sect =
thread -> traced_stack_sect; if (thread -> id == me) {
GC_ASSERT(thread -> thread_blocked_sp == NULL);
sp = GC_approx_sp();
} elseif ((sp = thread -> thread_blocked_sp) == NULL) { /* Use saved sp value for blocked threads. */ # ifdef RETRY_GET_THREAD_CONTEXT /* We cache context when suspending the thread since it may */ /* require looping. */
word *regs = thread->context_regs;
if (thread->suspended) {
sp = thread->context_sp;
} else # else
word regs[PUSHED_REGS_COUNT]; # endif
/* else */ {
CONTEXT context;
/* For unblocked threads call GetThreadContext(). */
context.ContextFlags = GET_THREAD_CONTEXT_FLAGS; if (GetThreadContext(THREAD_HANDLE(thread), &context)) {
sp = copy_ptr_regs(regs, &context);
} else { # ifdef RETRY_GET_THREAD_CONTEXT /* At least, try to use the stale context if saved. */
sp = thread->context_sp; if (NULL == sp) { /* Skip the current thread, anyway its stack will */ /* be pushed when the world is stopped. */ return 0;
} # else
ABORT("GetThreadContext failed"); # endif
}
} # ifdef THREAD_LOCAL_ALLOC
GC_ASSERT(thread->suspended || !GC_world_stopped); # endif
if (!GetThreadSelectorEntry(THREAD_HANDLE(thread), SegFs, &selector))
ABORT("GetThreadSelectorEntry failed");
tib = (PNT_TIB)(selector.BaseLow
| (selector.HighWord.Bits.BaseMid << 16)
| (selector.HighWord.Bits.BaseHi << 24)); # ifdef DEBUG_THREADS
GC_log_printf("TIB stack limit/base: %p .. %p\n",
(void *)tib->StackLimit, (void *)tib->StackBase); # endif
GC_ASSERT(!((word)thread->stack_base
COOLER_THAN (word)tib->StackBase)); if (thread->stack_base != thread->initial_stack_base /* We are in a coroutine. */
&& ((word)thread->stack_base <= (word)tib->StackLimit
|| (word)tib->StackBase < (word)thread->stack_base)) { /* The coroutine stack is not within TIB stack. */
WARN("GetThreadContext might return stale register values" " including ESP= %p\n", sp); /* TODO: Because of WoW64 bug, there is no guarantee that */ /* sp really points to the stack top but, for now, we do */ /* our best as the TIB stack limit/base cannot be used */ /* while we are inside a coroutine. */
} else { /* GetThreadContext() might return stale register values, */ /* so we scan the entire stack region (down to the stack */ /* limit). There is no 100% guarantee that all the */ /* registers are pushed but we do our best (the proper */ /* solution would be to fix it inside Windows OS). */
sp = (ptr_t)tib->StackLimit;
}
} /* else */ # ifdef DEBUG_THREADS else { static GC_bool logged; if (!logged
&& (ContextFlags & CONTEXT_EXCEPTION_REPORTING) == 0) {
GC_log_printf("CONTEXT_EXCEPTION_REQUEST not supported\n");
logged = TRUE;
}
} # endif
} # endif /* WOW64_THREAD_CONTEXT_WORKAROUND */
} /* ! current thread */
/* Set stack_min to the lowest address in the thread stack, */ /* or to an address in the thread stack no larger than sp, */ /* taking advantage of the old value to avoid slow traversals */ /* of large stacks. */ if (thread -> last_stack_min == ADDR_LIMIT) { # ifdef MSWINCE if (GC_dont_query_stack_min) {
stack_min = GC_wince_evaluate_stack_min(traced_stack_sect != NULL ?
(ptr_t)traced_stack_sect : thread -> stack_base); /* Keep last_stack_min value unmodified. */
} else # endif /* else */ {
stack_min = GC_get_stack_min(traced_stack_sect != NULL ?
(ptr_t)traced_stack_sect : thread -> stack_base);
UNPROTECT_THREAD(thread);
thread -> last_stack_min = stack_min;
}
} else { /* First, adjust the latest known minimum stack address if we */ /* are inside GC_call_with_gc_active(). */ if (traced_stack_sect != NULL &&
(word)thread->last_stack_min > (word)traced_stack_sect) {
UNPROTECT_THREAD(thread);
thread -> last_stack_min = (ptr_t)traced_stack_sect;
}
if ((word)sp < (word)thread->stack_base
&& (word)sp >= (word)thread->last_stack_min) {
stack_min = sp;
} else { /* In the current thread it is always safe to use sp value. */ if (may_be_in_stack(thread -> id == me &&
(word)sp < (word)thread->last_stack_min ?
sp : thread -> last_stack_min)) {
stack_min = (ptr_t)last_info.BaseAddress; /* Do not probe rest of the stack if sp is correct. */ if ((word)sp < (word)stack_min
|| (word)sp >= (word)thread->stack_base)
stack_min = GC_get_stack_min(thread -> last_stack_min);
} else { /* Stack shrunk? Is this possible? */
stack_min = GC_get_stack_min(thread -> stack_base);
}
UNPROTECT_THREAD(thread);
thread -> last_stack_min = stack_min;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.