| 1 |
/* |
| 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. |
| 6 |
* All rights reserved. |
| 7 |
* Copyright (c) 2008-2021 Ivan Maidanski |
| 8 |
* |
| 9 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 10 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 11 |
* |
| 12 |
* Permission is hereby granted to use or copy this program |
| 13 |
* for any purpose, provided the above notices are retained on all copies. |
| 14 |
* Permission to modify the code and to distribute modified code is granted, |
| 15 |
* provided the above notices are retained, and a notice that the code was |
| 16 |
* modified is included with the above copyright notice. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "private/gc_priv.h" |
| 20 |
|
| 21 |
#if defined(GC_WIN32_THREADS) |
| 22 |
|
| 23 |
#ifdef THREAD_LOCAL_ALLOC |
| 24 |
# include "private/thread_local_alloc.h" |
| 25 |
#endif /* THREAD_LOCAL_ALLOC */ |
| 26 |
|
| 27 |
/* Allocation lock declarations. */ |
| 28 |
#if !defined(USE_PTHREAD_LOCKS) |
| 29 |
GC_INNER CRITICAL_SECTION GC_allocate_ml; |
| 30 |
# ifdef GC_ASSERTIONS |
| 31 |
GC_INNER DWORD GC_lock_holder = NO_THREAD; |
| 32 |
/* Thread id for current holder of allocation lock */ |
| 33 |
# endif |
| 34 |
#else |
| 35 |
GC_INNER pthread_mutex_t GC_allocate_ml = PTHREAD_MUTEX_INITIALIZER; |
| 36 |
# ifdef GC_ASSERTIONS |
| 37 |
GC_INNER unsigned long GC_lock_holder = NO_THREAD; |
| 38 |
# endif |
| 39 |
#endif |
| 40 |
|
| 41 |
#undef CreateThread |
| 42 |
#undef ExitThread |
| 43 |
#undef _beginthreadex |
| 44 |
#undef _endthreadex |
| 45 |
|
| 46 |
#ifdef GC_PTHREADS |
| 47 |
# include <errno.h> /* for EINTR */ |
| 48 |
|
| 49 |
/* Cygwin-specific forward decls */ |
| 50 |
# undef pthread_create |
| 51 |
# undef pthread_join |
| 52 |
# undef pthread_detach |
| 53 |
|
| 54 |
# ifndef GC_NO_PTHREAD_SIGMASK |
| 55 |
# undef pthread_sigmask |
| 56 |
# endif |
| 57 |
|
| 58 |
STATIC void * GC_pthread_start(void * arg); |
| 59 |
STATIC void GC_thread_exit_proc(void *arg); |
| 60 |
|
| 61 |
# include <pthread.h> |
| 62 |
# ifdef CAN_CALL_ATFORK |
| 63 |
# include <unistd.h> |
| 64 |
# endif |
| 65 |
|
| 66 |
# ifdef EMULATE_PTHREAD_SEMAPHORE |
| 67 |
# include "private/darwin_semaphore.h" |
| 68 |
# else |
| 69 |
# include <semaphore.h> |
| 70 |
# endif |
| 71 |
|
| 72 |
#elif !defined(MSWINCE) |
| 73 |
# include <process.h> /* For _beginthreadex, _endthreadex */ |
| 74 |
# include <errno.h> /* for errno, EAGAIN */ |
| 75 |
|
| 76 |
#endif /* !GC_PTHREADS && !MSWINCE */ |
| 77 |
|
| 78 |
/* PUSHED_REGS_COUNT is the number of copied registers in copy_ptr_regs. */ |
| 79 |
static ptr_t copy_ptr_regs(word *regs, const CONTEXT *pcontext); |
| 80 |
#if defined(I386) |
| 81 |
# ifdef WOW64_THREAD_CONTEXT_WORKAROUND |
| 82 |
# define PUSHED_REGS_COUNT 9 |
| 83 |
# else |
| 84 |
# define PUSHED_REGS_COUNT 7 |
| 85 |
# endif |
| 86 |
#elif defined(X86_64) |
| 87 |
# ifdef XMM_CANT_STORE_PTRS |
| 88 |
/* If pointers can't be located in Xmm registers. */ |
| 89 |
# define PUSHED_REGS_COUNT 15 |
| 90 |
# else |
| 91 |
/* gcc-13 may store pointers into SIMD registers when */ |
| 92 |
/* certain compiler optimizations are enabled. */ |
| 93 |
# define PUSHED_REGS_COUNT (15+32) |
| 94 |
# endif |
| 95 |
#elif defined(SHx) |
| 96 |
# define PUSHED_REGS_COUNT 15 |
| 97 |
#elif defined(ARM32) |
| 98 |
# define PUSHED_REGS_COUNT 13 |
| 99 |
#elif defined(AARCH64) |
| 100 |
# define PUSHED_REGS_COUNT 30 |
| 101 |
#elif defined(MIPS) || defined(ALPHA) |
| 102 |
# define PUSHED_REGS_COUNT 28 |
| 103 |
#elif defined(PPC) |
| 104 |
# define PUSHED_REGS_COUNT 29 |
| 105 |
#endif |
| 106 |
|
| 107 |
/* DllMain-based thread registration is currently incompatible */ |
| 108 |
/* with thread-local allocation, pthreads and WinCE. */ |
| 109 |
#if (defined(GC_DLL) || defined(GC_INSIDE_DLL)) && !defined(NO_CRT) \ |
| 110 |
&& !defined(GC_NO_THREADS_DISCOVERY) && !defined(MSWINCE) \ |
| 111 |
&& !defined(THREAD_LOCAL_ALLOC) && !defined(GC_PTHREADS) |
| 112 |
|
| 113 |
/* This code operates in two distinct modes, depending on */ |
| 114 |
/* the setting of GC_win32_dll_threads. */ |
| 115 |
/* If GC_win32_dll_threads is set, all threads in the process */ |
| 116 |
/* are implicitly registered with the GC by DllMain. */ |
| 117 |
/* No explicit registration is required, and attempts at */ |
| 118 |
/* explicit registration are ignored. This mode is */ |
| 119 |
/* very different from the Posix operation of the collector. */ |
| 120 |
/* In this mode access to the thread table is lock-free. */ |
| 121 |
/* Hence there is a static limit on the number of threads. */ |
| 122 |
|
| 123 |
# ifdef GC_DISCOVER_TASK_THREADS |
| 124 |
/* GC_DISCOVER_TASK_THREADS should be used if DllMain-based */ |
| 125 |
/* thread registration is required but it is impossible to */ |
| 126 |
/* call GC_use_threads_discovery before other GC routines. */ |
| 127 |
# define GC_win32_dll_threads TRUE |
| 128 |
# else |
| 129 |
STATIC GC_bool GC_win32_dll_threads = FALSE; |
| 130 |
/* GC_win32_dll_threads must be set (if needed) at the */ |
| 131 |
/* application initialization time, i.e. before any */ |
| 132 |
/* collector or thread calls. We make it a "dynamic" */ |
| 133 |
/* option only to avoid multiple library versions. */ |
| 134 |
# endif |
| 135 |
|
| 136 |
#else |
| 137 |
/* If GC_win32_dll_threads is FALSE (or the collector is */ |
| 138 |
/* built without GC_DLL defined), things operate in a way */ |
| 139 |
/* that is very similar to Posix platforms, and new threads */ |
| 140 |
/* must be registered with the collector, e.g. by using */ |
| 141 |
/* preprocessor-based interception of the thread primitives. */ |
| 142 |
/* In this case, we use a real data structure for the thread */ |
| 143 |
/* table. Note that there is no equivalent of linker-based */ |
| 144 |
/* call interception, since we don't have ELF-like */ |
| 145 |
/* facilities. The Windows analog appears to be "API */ |
| 146 |
/* hooking", which really seems to be a standard way to */ |
| 147 |
/* do minor binary rewriting (?). I'd prefer not to have */ |
| 148 |
/* the basic collector rely on such facilities, but an */ |
| 149 |
/* optional package that intercepts thread calls this way */ |
| 150 |
/* would probably be nice. */ |
| 151 |
# ifndef GC_NO_THREADS_DISCOVERY |
| 152 |
# define GC_NO_THREADS_DISCOVERY |
| 153 |
# endif |
| 154 |
# define GC_win32_dll_threads FALSE |
| 155 |
# undef MAX_THREADS |
| 156 |
# define MAX_THREADS 1 /* dll_thread_table[] is always empty. */ |
| 157 |
#endif /* GC_NO_THREADS_DISCOVERY */ |
| 158 |
|
| 159 |
/* We have two versions of the thread table. Which one */ |
| 160 |
/* we use depends on whether GC_win32_dll_threads */ |
| 161 |
/* is set. Note that before initialization, we don't */ |
| 162 |
/* add any entries to either table, even if DllMain is */ |
| 163 |
/* called. The main thread will be added on */ |
| 164 |
/* initialization. */ |
| 165 |
|
| 166 |
/* The type of the first argument to InterlockedExchange. */ |
| 167 |
/* Documented to be LONG volatile *, but at least gcc likes */ |
| 168 |
/* this better. */ |
| 169 |
typedef LONG * IE_t; |
| 170 |
|
| 171 |
STATIC GC_bool GC_thr_initialized = FALSE; |
| 172 |
|
| 173 |
#ifndef GC_ALWAYS_MULTITHREADED |
| 174 |
GC_INNER GC_bool GC_need_to_lock = FALSE; |
| 175 |
#endif |
| 176 |
|
| 177 |
static GC_bool parallel_initialized = FALSE; |
| 178 |
|
| 179 |
/* GC_use_threads_discovery() is currently incompatible with pthreads */ |
| 180 |
/* and WinCE. It might be possible to get DllMain-based thread */ |
| 181 |
/* registration to work with Cygwin, but if you try it then you are on */ |
| 182 |
/* your own. */ |
| 183 |
GC_API void GC_CALL GC_use_threads_discovery(void) |
| 184 |
{ |
| 185 |
# ifdef GC_NO_THREADS_DISCOVERY |
| 186 |
ABORT("GC DllMain-based thread registration unsupported"); |
| 187 |
# else |
| 188 |
/* Turn on GC_win32_dll_threads. */ |
| 189 |
GC_ASSERT(!parallel_initialized); |
| 190 |
/* Note that GC_use_threads_discovery is expected to be called by */ |
| 191 |
/* the client application (not from DllMain) at start-up. */ |
| 192 |
# ifndef GC_DISCOVER_TASK_THREADS |
| 193 |
GC_win32_dll_threads = TRUE; |
| 194 |
# endif |
| 195 |
GC_init_parallel(); |
| 196 |
# endif |
| 197 |
} |
| 198 |
|
| 199 |
#define ADDR_LIMIT ((ptr_t)GC_WORD_MAX) |
| 200 |
|
| 201 |
struct GC_Thread_Rep { |
| 202 |
union { |
| 203 |
# ifndef GC_NO_THREADS_DISCOVERY |
| 204 |
volatile AO_t in_use; |
| 205 |
/* Updated without lock. */ |
| 206 |
/* We assert that unused */ |
| 207 |
/* entries have invalid ids of */ |
| 208 |
/* zero and zero stack fields. */ |
| 209 |
/* Used only with GC_win32_dll_threads. */ |
| 210 |
LONG long_in_use; /* The same but of the type that */ |
| 211 |
/* matches the first argument of */ |
| 212 |
/* InterlockedExchange(); volatile is */ |
| 213 |
/* omitted because the ancient version */ |
| 214 |
/* of the prototype lacks the qualifier.*/ |
| 215 |
# endif |
| 216 |
struct GC_Thread_Rep * next; |
| 217 |
/* Hash table link without */ |
| 218 |
/* GC_win32_dll_threads. */ |
| 219 |
/* More recently allocated threads */ |
| 220 |
/* with a given pthread id come */ |
| 221 |
/* first. (All but the first are */ |
| 222 |
/* guaranteed to be dead, but we may */ |
| 223 |
/* not yet have registered the join.) */ |
| 224 |
} tm; /* table_management */ |
| 225 |
DWORD id; |
| 226 |
|
| 227 |
# ifdef MSWINCE |
| 228 |
/* According to MSDN specs for WinCE targets: */ |
| 229 |
/* - DuplicateHandle() is not applicable to thread handles; and */ |
| 230 |
/* - the value returned by GetCurrentThreadId() could be used as */ |
| 231 |
/* a "real" thread handle (for SuspendThread(), ResumeThread() and */ |
| 232 |
/* GetThreadContext()). */ |
| 233 |
# define THREAD_HANDLE(t) (HANDLE)(word)(t)->id |
| 234 |
# else |
| 235 |
HANDLE handle; |
| 236 |
# define THREAD_HANDLE(t) (t)->handle |
| 237 |
# endif |
| 238 |
|
| 239 |
ptr_t stack_base; /* The cold end of the stack. */ |
| 240 |
/* 0 ==> entry not valid. */ |
| 241 |
/* !in_use ==> stack_base == 0 */ |
| 242 |
ptr_t last_stack_min; /* Last known minimum (hottest) address */ |
| 243 |
/* in stack or ADDR_LIMIT if unset */ |
| 244 |
# ifdef IA64 |
| 245 |
ptr_t backing_store_end; |
| 246 |
ptr_t backing_store_ptr; |
| 247 |
# elif defined(I386) |
| 248 |
ptr_t initial_stack_base; |
| 249 |
/* The cold end of the stack saved by */ |
| 250 |
/* GC_record_stack_base (never modified */ |
| 251 |
/* by GC_set_stackbottom). */ |
| 252 |
# endif |
| 253 |
|
| 254 |
ptr_t thread_blocked_sp; /* Protected by GC lock. */ |
| 255 |
/* NULL value means thread unblocked. */ |
| 256 |
/* If set to non-NULL, thread will */ |
| 257 |
/* acquire GC lock before doing any */ |
| 258 |
/* pointer manipulations. Thus it does */ |
| 259 |
/* not need to stop this thread. */ |
| 260 |
|
| 261 |
struct GC_traced_stack_sect_s *traced_stack_sect; |
| 262 |
/* Points to the "stack section" data */ |
| 263 |
/* held in stack by the innermost */ |
| 264 |
/* GC_call_with_gc_active() of this */ |
| 265 |
/* thread. May be NULL. */ |
| 266 |
|
| 267 |
# ifndef GC_NO_FINALIZATION |
| 268 |
unsigned short finalizer_skipped; |
| 269 |
unsigned char finalizer_nested; |
| 270 |
/* Used by GC_check_finalizer_nested() */ |
| 271 |
/* to minimize the level of recursion */ |
| 272 |
/* when a client finalizer allocates */ |
| 273 |
/* memory (initially both are 0). */ |
| 274 |
# endif |
| 275 |
|
| 276 |
unsigned char suspended; /* really of GC_bool type */ |
| 277 |
|
| 278 |
# ifdef GC_PTHREADS |
| 279 |
unsigned char flags; /* Protected by GC lock. */ |
| 280 |
# define FINISHED 1 /* Thread has exited. */ |
| 281 |
# define DETACHED 2 /* Thread is intended to be detached. */ |
| 282 |
# define KNOWN_FINISHED(t) (((t) -> flags) & FINISHED) |
| 283 |
pthread_t pthread_id; |
| 284 |
void *status; /* hold exit value until join in case it's a pointer */ |
| 285 |
# else |
| 286 |
# define KNOWN_FINISHED(t) 0 |
| 287 |
# endif |
| 288 |
|
| 289 |
# ifdef THREAD_LOCAL_ALLOC |
| 290 |
struct thread_local_freelists tlfs; |
| 291 |
# endif |
| 292 |
|
| 293 |
# if defined(WOW64_THREAD_CONTEXT_WORKAROUND) && defined(MSWINRT_FLAVOR) |
| 294 |
PNT_TIB tib; |
| 295 |
# endif |
| 296 |
|
| 297 |
# ifdef RETRY_GET_THREAD_CONTEXT |
| 298 |
ptr_t context_sp; |
| 299 |
word context_regs[PUSHED_REGS_COUNT]; |
| 300 |
/* Populated as part of GC_suspend() as */ |
| 301 |
/* resume/suspend loop may be needed for the */ |
| 302 |
/* call to GetThreadContext() to succeed. */ |
| 303 |
# endif |
| 304 |
}; |
| 305 |
|
| 306 |
typedef struct GC_Thread_Rep * GC_thread; |
| 307 |
typedef volatile struct GC_Thread_Rep * GC_vthread; |
| 308 |
|
| 309 |
STATIC DWORD GC_main_thread; |
| 310 |
|
| 311 |
#ifndef GC_NO_THREADS_DISCOVERY |
| 312 |
/* We track thread attachments while the world is supposed to be */ |
| 313 |
/* stopped. Unfortunately, we cannot stop them from starting, since */ |
| 314 |
/* blocking in DllMain seems to cause the world to deadlock. Thus, */ |
| 315 |
/* we have to recover if we notice this in the middle of marking. */ |
| 316 |
STATIC volatile AO_t GC_attached_thread = FALSE; |
| 317 |
|
| 318 |
/* We assumed that volatile ==> memory ordering, at least among */ |
| 319 |
/* volatiles. This code should consistently use atomic_ops. */ |
| 320 |
STATIC volatile GC_bool GC_please_stop = FALSE; |
| 321 |
#elif defined(GC_ASSERTIONS) |
| 322 |
STATIC GC_bool GC_please_stop = FALSE; |
| 323 |
#endif /* GC_NO_THREADS_DISCOVERY && GC_ASSERTIONS */ |
| 324 |
|
| 325 |
#if defined(WRAP_MARK_SOME) && !defined(GC_PTHREADS) |
| 326 |
/* Return TRUE if an thread was attached since we last asked or */ |
| 327 |
/* since GC_attached_thread was explicitly reset. */ |
| 328 |
GC_INNER GC_bool GC_started_thread_while_stopped(void) |
| 329 |
{ |
| 330 |
# ifndef GC_NO_THREADS_DISCOVERY |
| 331 |
if (GC_win32_dll_threads) { |
| 332 |
# ifdef AO_HAVE_compare_and_swap_release |
| 333 |
if (AO_compare_and_swap_release(&GC_attached_thread, TRUE, |
| 334 |
FALSE /* stored */)) |
| 335 |
return TRUE; |
| 336 |
# else |
| 337 |
AO_nop_full(); /* Prior heap reads need to complete earlier. */ |
| 338 |
if (AO_load(&GC_attached_thread)) { |
| 339 |
AO_store(&GC_attached_thread, FALSE); |
| 340 |
return TRUE; |
| 341 |
} |
| 342 |
# endif |
| 343 |
} |
| 344 |
# endif |
| 345 |
return FALSE; |
| 346 |
} |
| 347 |
#endif /* WRAP_MARK_SOME */ |
| 348 |
|
| 349 |
/* Thread table used if GC_win32_dll_threads is set. */ |
| 350 |
/* This is a fixed size array. */ |
| 351 |
/* Since we use runtime conditionals, both versions */ |
| 352 |
/* are always defined. */ |
| 353 |
# ifndef MAX_THREADS |
| 354 |
# define MAX_THREADS 512 |
| 355 |
# endif |
| 356 |
|
| 357 |
/* Things may get quite slow for large numbers of threads, */ |
| 358 |
/* since we look them up with sequential search. */ |
| 359 |
volatile struct GC_Thread_Rep dll_thread_table[MAX_THREADS]; |
| 360 |
|
| 361 |
STATIC volatile LONG GC_max_thread_index = 0; |
| 362 |
/* Largest index in dll_thread_table */ |
| 363 |
/* that was ever used. */ |
| 364 |
|
| 365 |
/* And now the version used if GC_win32_dll_threads is not set. */ |
| 366 |
/* This is a chained hash table, with much of the code borrowed */ |
| 367 |
/* from the Posix implementation. */ |
| 368 |
#ifndef THREAD_TABLE_SZ |
| 369 |
# define THREAD_TABLE_SZ 256 /* Power of 2 (for speed). */ |
| 370 |
#endif |
| 371 |
#define THREAD_TABLE_INDEX(id) /* id is of DWORD type */ \ |
| 372 |
(int)((((id) >> 8) ^ (id)) % THREAD_TABLE_SZ) |
| 373 |
STATIC GC_thread GC_threads[THREAD_TABLE_SZ]; |
| 374 |
|
| 375 |
/* It may not be safe to allocate when we register the first thread. */ |
| 376 |
/* Thus we allocated one statically. It does not contain any pointer */ |
| 377 |
/* field we need to push ("next" and "status" fields are unused). */ |
| 378 |
static struct GC_Thread_Rep first_thread; |
| 379 |
static GC_bool first_thread_used = FALSE; |
| 380 |
|
| 381 |
/* Add a thread to GC_threads. We assume it wasn't already there. */ |
| 382 |
/* Caller holds allocation lock. */ |
| 383 |
/* Unlike the pthreads version, the id field is set by the caller. */ |
| 384 |
STATIC GC_thread GC_new_thread(DWORD id) |
| 385 |
{ |
| 386 |
int hv = THREAD_TABLE_INDEX(id); |
| 387 |
GC_thread result; |
| 388 |
|
| 389 |
# ifdef DEBUG_THREADS |
| 390 |
GC_log_printf("Creating thread 0x%lx\n", (long)id); |
| 391 |
if (GC_threads[hv] != NULL) |
| 392 |
GC_log_printf("Hash collision at GC_threads[%d]\n", hv); |
| 393 |
# endif |
| 394 |
GC_ASSERT(I_HOLD_LOCK()); |
| 395 |
if (!EXPECT(first_thread_used, TRUE)) { |
| 396 |
result = &first_thread; |
| 397 |
first_thread_used = TRUE; |
| 398 |
GC_ASSERT(NULL == GC_threads[hv]); |
| 399 |
} else { |
| 400 |
GC_ASSERT(!GC_win32_dll_threads); |
| 401 |
result = (struct GC_Thread_Rep *) |
| 402 |
GC_INTERNAL_MALLOC(sizeof(struct GC_Thread_Rep), NORMAL); |
| 403 |
if (result == 0) return(0); |
| 404 |
} |
| 405 |
/* result -> id = id; Done by caller. */ |
| 406 |
result -> tm.next = GC_threads[hv]; |
| 407 |
GC_threads[hv] = result; |
| 408 |
# ifdef GC_PTHREADS |
| 409 |
GC_ASSERT(result -> flags == 0); |
| 410 |
# endif |
| 411 |
GC_ASSERT(result -> thread_blocked_sp == NULL); |
| 412 |
if (EXPECT(result != &first_thread, TRUE)) |
| 413 |
GC_dirty(result); |
| 414 |
return(result); |
| 415 |
} |
| 416 |
|
| 417 |
GC_INNER GC_bool GC_in_thread_creation = FALSE; |
| 418 |
/* Protected by allocation lock. */ |
| 419 |
|
| 420 |
GC_INLINE void GC_record_stack_base(GC_vthread me, |
| 421 |
const struct GC_stack_base *sb) |
| 422 |
{ |
| 423 |
me -> stack_base = (ptr_t)sb->mem_base; |
| 424 |
# ifdef IA64 |
| 425 |
me -> backing_store_end = (ptr_t)sb->reg_base; |
| 426 |
# elif defined(I386) |
| 427 |
me -> initial_stack_base = (ptr_t)sb->mem_base; |
| 428 |
# endif |
| 429 |
if (me -> stack_base == NULL) |
| 430 |
ABORT("Bad stack base in GC_register_my_thread"); |
| 431 |
} |
| 432 |
|
| 433 |
/* This may be called from DllMain, and hence operates under unusual */ |
| 434 |
/* constraints. In particular, it must be lock-free if */ |
| 435 |
/* GC_win32_dll_threads is set. Always called from the thread being */ |
| 436 |
/* added. If GC_win32_dll_threads is not set, we already hold the */ |
| 437 |
/* allocation lock except possibly during single-threaded startup code. */ |
| 438 |
/* Does not initialize thread local free lists. */ |
| 439 |
STATIC GC_thread GC_register_my_thread_inner(const struct GC_stack_base *sb, |
| 440 |
DWORD thread_id) |
| 441 |
{ |
| 442 |
GC_vthread me; |
| 443 |
|
| 444 |
/* The following should be a no-op according to the Win32 */ |
| 445 |
/* documentation. There is empirical evidence that it */ |
| 446 |
/* isn't. - HB */ |
| 447 |
# if defined(MPROTECT_VDB) && !defined(CYGWIN32) |
| 448 |
if (GC_auto_incremental |
| 449 |
# ifdef GWW_VDB |
| 450 |
&& !GC_gww_dirty_init() |
| 451 |
# endif |
| 452 |
) |
| 453 |
GC_set_write_fault_handler(); |
| 454 |
# endif |
| 455 |
|
| 456 |
# ifndef GC_NO_THREADS_DISCOVERY |
| 457 |
if (GC_win32_dll_threads) { |
| 458 |
int i; |
| 459 |
/* It appears to be unsafe to acquire a lock here, since this */ |
| 460 |
/* code is apparently not preemptible on some systems. */ |
| 461 |
/* (This is based on complaints, not on Microsoft's official */ |
| 462 |
/* documentation, which says this should perform "only simple */ |
| 463 |
/* initialization tasks".) */ |
| 464 |
/* Hence we make do with nonblocking synchronization. */ |
| 465 |
/* It has been claimed that DllMain is really only executed with */ |
| 466 |
/* a particular system lock held, and thus careful use of locking */ |
| 467 |
/* around code that doesn't call back into the system libraries */ |
| 468 |
/* might be OK. But this has not been tested across all Win32 */ |
| 469 |
/* variants. */ |
| 470 |
for (i = 0; |
| 471 |
InterlockedExchange(&dll_thread_table[i].tm.long_in_use, 1) != 0; |
| 472 |
i++) { |
| 473 |
/* Compare-and-swap would make this cleaner, but that's not */ |
| 474 |
/* supported before Windows 98 and NT 4.0. In Windows 2000, */ |
| 475 |
/* InterlockedExchange is supposed to be replaced by */ |
| 476 |
/* InterlockedExchangePointer, but that's not really what I */ |
| 477 |
/* want here. */ |
| 478 |
/* FIXME: We should eventually declare Windows 95 dead and use */ |
| 479 |
/* AO_ primitives here. */ |
| 480 |
if (i == MAX_THREADS - 1) |
| 481 |
ABORT("Too many threads"); |
| 482 |
} |
| 483 |
/* Update GC_max_thread_index if necessary. The following is */ |
| 484 |
/* safe, and unlike CompareExchange-based solutions seems to work */ |
| 485 |
/* on all Windows95 and later platforms. */ |
| 486 |
/* Unfortunately, GC_max_thread_index may be temporarily out of */ |
| 487 |
/* bounds, so readers have to compensate. */ |
| 488 |
while (i > GC_max_thread_index) { |
| 489 |
InterlockedIncrement((IE_t)&GC_max_thread_index); |
| 490 |
} |
| 491 |
if (GC_max_thread_index >= MAX_THREADS) { |
| 492 |
/* We overshot due to simultaneous increments. */ |
| 493 |
/* Setting it to MAX_THREADS-1 is always safe. */ |
| 494 |
GC_max_thread_index = MAX_THREADS - 1; |
| 495 |
} |
| 496 |
me = dll_thread_table + i; |
| 497 |
} else |
| 498 |
# endif |
| 499 |
/* else */ /* Not using DllMain */ { |
| 500 |
GC_ASSERT(I_HOLD_LOCK()); |
| 501 |
GC_in_thread_creation = TRUE; /* OK to collect from unknown thread. */ |
| 502 |
me = GC_new_thread(thread_id); |
| 503 |
GC_in_thread_creation = FALSE; |
| 504 |
if (me == 0) |
| 505 |
ABORT("Failed to allocate memory for thread registering"); |
| 506 |
} |
| 507 |
# ifdef GC_PTHREADS |
| 508 |
/* me can be NULL -> segfault */ |
| 509 |
me -> pthread_id = pthread_self(); |
| 510 |
# endif |
| 511 |
# ifndef MSWINCE |
| 512 |
/* GetCurrentThread() returns a pseudohandle (a const value). */ |
| 513 |
if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), |
| 514 |
GetCurrentProcess(), |
| 515 |
(HANDLE*)&(me -> handle), |
| 516 |
0 /* dwDesiredAccess */, FALSE /* bInheritHandle */, |
| 517 |
DUPLICATE_SAME_ACCESS)) { |
| 518 |
ABORT_ARG1("DuplicateHandle failed", |
| 519 |
": errcode= 0x%X", (unsigned)GetLastError()); |
| 520 |
} |
| 521 |
# endif |
| 522 |
# if defined(WOW64_THREAD_CONTEXT_WORKAROUND) && defined(MSWINRT_FLAVOR) |
| 523 |
/* Lookup TIB value via a call to NtCurrentTeb() on thread */ |
| 524 |
/* registration rather than calling GetThreadSelectorEntry() which */ |
| 525 |
/* is not available on UWP. */ |
| 526 |
me -> tib = (PNT_TIB)NtCurrentTeb(); |
| 527 |
# endif |
| 528 |
me -> last_stack_min = ADDR_LIMIT; |
| 529 |
GC_record_stack_base(me, sb); |
| 530 |
/* Up until this point, GC_push_all_stacks considers this thread */ |
| 531 |
/* invalid. */ |
| 532 |
/* Up until this point, this entry is viewed as reserved but invalid */ |
| 533 |
/* by GC_delete_thread. */ |
| 534 |
me -> id = thread_id; |
| 535 |
# ifndef GC_NO_THREADS_DISCOVERY |
| 536 |
if (GC_win32_dll_threads) { |
| 537 |
if (GC_please_stop) { |
| 538 |
AO_store(&GC_attached_thread, TRUE); |
| 539 |
AO_nop_full(); /* Later updates must become visible after this. */ |
| 540 |
} |
| 541 |
/* We'd like to wait here, but can't, since waiting in DllMain */ |
| 542 |
/* provokes deadlocks. */ |
| 543 |
/* Thus we force marking to be restarted instead. */ |
| 544 |
} else |
| 545 |
# endif |
| 546 |
/* else */ { |
| 547 |
GC_ASSERT(!GC_please_stop); |
| 548 |
/* Otherwise both we and the thread stopping code would be */ |
| 549 |
/* holding the allocation lock. */ |
| 550 |
} |
| 551 |
return (GC_thread)(me); |
| 552 |
} |
| 553 |
|
| 554 |
/* |
| 555 |
* GC_max_thread_index may temporarily be larger than MAX_THREADS. |
| 556 |
* To avoid subscript errors, we check on access. |
| 557 |
*/ |
| 558 |
GC_INLINE LONG GC_get_max_thread_index(void) |
| 559 |
{ |
| 560 |
LONG my_max = GC_max_thread_index; |
| 561 |
if (my_max >= MAX_THREADS) return MAX_THREADS - 1; |
| 562 |
return my_max; |
| 563 |
} |
| 564 |
|
| 565 |
/* Return the GC_thread corresponding to a thread id. May be called */ |
| 566 |
/* without a lock, but should be called in contexts in which the */ |
| 567 |
/* requested thread cannot be asynchronously deleted, e.g. from the */ |
| 568 |
/* thread itself. */ |
| 569 |
/* This version assumes that either GC_win32_dll_threads is set, or */ |
| 570 |
/* we hold the allocator lock. */ |
| 571 |
/* Also used (for assertion checking only) from thread_local_alloc.c. */ |
| 572 |
STATIC GC_thread GC_lookup_thread_inner(DWORD thread_id) |
| 573 |
{ |
| 574 |
# ifndef GC_NO_THREADS_DISCOVERY |
| 575 |
if (GC_win32_dll_threads) { |
| 576 |
int i; |
| 577 |
LONG my_max = GC_get_max_thread_index(); |
| 578 |
for (i = 0; i <= my_max && |
| 579 |
(!AO_load_acquire(&dll_thread_table[i].tm.in_use) |
| 580 |
|| dll_thread_table[i].id != thread_id); |
| 581 |
/* Must still be in_use, since nobody else can store our */ |
| 582 |
/* thread_id. */ |
| 583 |
i++) { |
| 584 |
/* empty */ |
| 585 |
} |
| 586 |
return i <= my_max ? (GC_thread)(dll_thread_table + i) : NULL; |
| 587 |
} else |
| 588 |
# endif |
| 589 |
/* else */ { |
| 590 |
GC_thread p = GC_threads[THREAD_TABLE_INDEX(thread_id)]; |
| 591 |
|
| 592 |
GC_ASSERT(I_HOLD_LOCK()); |
| 593 |
while (p != 0 && p -> id != thread_id) p = p -> tm.next; |
| 594 |
return(p); |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
#ifdef LINT2 |
| 599 |
# define CHECK_LOOKUP_MY_THREAD(me) \ |
| 600 |
if (!(me)) ABORT("GC_lookup_thread_inner(GetCurrentThreadId) failed") |
| 601 |
#else |
| 602 |
# define CHECK_LOOKUP_MY_THREAD(me) /* empty */ |
| 603 |
#endif |
| 604 |
|
| 605 |
#ifndef GC_NO_FINALIZATION |
| 606 |
/* Called by GC_finalize() (in case of an allocation failure observed). */ |
| 607 |
/* GC_reset_finalizer_nested() is the same as in pthread_support.c. */ |
| 608 |
GC_INNER void GC_reset_finalizer_nested(void) |
| 609 |
{ |
| 610 |
GC_thread me = GC_lookup_thread_inner(GetCurrentThreadId()); |
| 611 |
|
| 612 |
CHECK_LOOKUP_MY_THREAD(me); |
| 613 |
me->finalizer_nested = 0; |
| 614 |
} |
| 615 |
|
| 616 |
/* GC_check_finalizer_nested() is the same as in pthread_support.c. */ |
| 617 |
GC_INNER unsigned char *GC_check_finalizer_nested(void) |
| 618 |
{ |
| 619 |
GC_thread me = GC_lookup_thread_inner(GetCurrentThreadId()); |
| 620 |
unsigned nesting_level; |
| 621 |
|
| 622 |
CHECK_LOOKUP_MY_THREAD(me); |
| 623 |
nesting_level = me->finalizer_nested; |
| 624 |
if (nesting_level) { |
| 625 |
/* We are inside another GC_invoke_finalizers(). */ |
| 626 |
/* Skip some implicitly-called GC_invoke_finalizers() */ |
| 627 |
/* depending on the nesting (recursion) level. */ |
| 628 |
if (++me->finalizer_skipped < (1U << nesting_level)) return NULL; |
| 629 |
me->finalizer_skipped = 0; |
| 630 |
} |
| 631 |
me->finalizer_nested = (unsigned char)(nesting_level + 1); |
| 632 |
return &me->finalizer_nested; |
| 633 |
} |
| 634 |
#endif /* !GC_NO_FINALIZATION */ |
| 635 |
|
| 636 |
#if defined(GC_ASSERTIONS) && defined(THREAD_LOCAL_ALLOC) |
| 637 |
/* This is called from thread-local GC_malloc(). */ |
| 638 |
GC_bool GC_is_thread_tsd_valid(void *tsd) |
| 639 |
{ |
| 640 |
GC_thread me; |
| 641 |
DCL_LOCK_STATE; |
| 642 |
|
| 643 |
LOCK(); |
| 644 |
me = GC_lookup_thread_inner(GetCurrentThreadId()); |
| 645 |
UNLOCK(); |
| 646 |
return (word)tsd >= (word)(&me->tlfs) |
| 647 |
&& (word)tsd < (word)(&me->tlfs) + sizeof(me->tlfs); |
| 648 |
} |
| 649 |
#endif /* GC_ASSERTIONS && THREAD_LOCAL_ALLOC */ |
| 650 |
|
| 651 |
GC_API int GC_CALL GC_thread_is_registered(void) |
| 652 |
{ |
| 653 |
DWORD thread_id = GetCurrentThreadId(); |
| 654 |
GC_thread me; |
| 655 |
DCL_LOCK_STATE; |
| 656 |
|
| 657 |
LOCK(); |
| 658 |
me = GC_lookup_thread_inner(thread_id); |
| 659 |
UNLOCK(); |
| 660 |
return me != NULL && !KNOWN_FINISHED(me); |
| 661 |
} |
| 662 |
|
| 663 |
GC_API void GC_CALL GC_register_altstack(void *stack GC_ATTR_UNUSED, |
| 664 |
GC_word stack_size GC_ATTR_UNUSED, |
| 665 |
void *altstack GC_ATTR_UNUSED, |
| 666 |
GC_word altstack_size GC_ATTR_UNUSED) |
| 667 |
{ |
| 668 |
/* TODO: Implement */ |
| 669 |
} |
| 670 |
|
| 671 |
/* Make sure thread descriptor t is not protected by the VDB */ |
| 672 |
/* implementation. */ |
| 673 |
/* Used to prevent write faults when the world is (partially) stopped, */ |
| 674 |
/* since it may have been stopped with a system lock held, and that */ |
| 675 |
/* lock may be required for fault handling. */ |
| 676 |
#if defined(MPROTECT_VDB) |
| 677 |
# define UNPROTECT_THREAD(t) \ |
| 678 |
if (!GC_win32_dll_threads && GC_auto_incremental \ |
| 679 |
&& t != &first_thread) { \ |
| 680 |
GC_ASSERT(SMALL_OBJ(GC_size(t))); \ |
| 681 |
GC_remove_protection(HBLKPTR(t), 1, FALSE); \ |
| 682 |
} else (void)0 |
| 683 |
#else |
| 684 |
# define UNPROTECT_THREAD(t) (void)0 |
| 685 |
#endif |
| 686 |
|
| 687 |
#ifdef CYGWIN32 |
| 688 |
# define GC_PTHREAD_PTRVAL(pthread_id) pthread_id |
| 689 |
#elif defined(GC_WIN32_PTHREADS) || defined(GC_PTHREADS_PARAMARK) |
| 690 |
# include <pthread.h> /* to check for winpthreads */ |
| 691 |
# if defined(__WINPTHREADS_VERSION_MAJOR) |
| 692 |
# define GC_PTHREAD_PTRVAL(pthread_id) pthread_id |
| 693 |
# else |
| 694 |
# define GC_PTHREAD_PTRVAL(pthread_id) pthread_id.p |
| 695 |
# endif |
| 696 |
#endif |
| 697 |
|
| 698 |
/* If a thread has been joined, but we have not yet */ |
| 699 |
/* been notified, then there may be more than one thread */ |
| 700 |
/* in the table with the same Win32 thread id. */ |
| 701 |
/* This is OK, but we need a way to delete a specific one. */ |
| 702 |
/* Assumes we hold the allocation lock unless */ |
| 703 |
/* GC_win32_dll_threads is set. Does not actually free */ |
| 704 |
/* GC_thread entry (only unlinks it). */ |
| 705 |
/* If GC_win32_dll_threads is set it should be called from the */ |
| 706 |
/* thread being deleted. */ |
| 707 |
STATIC void GC_delete_gc_thread_no_free(GC_vthread t) |
| 708 |
{ |
| 709 |
# ifndef MSWINCE |
| 710 |
CloseHandle(t->handle); |
| 711 |
# endif |
| 712 |
# ifndef GC_NO_THREADS_DISCOVERY |
| 713 |
if (GC_win32_dll_threads) { |
| 714 |
/* This is intended to be lock-free. */ |
| 715 |
/* It is either called synchronously from the thread being */ |
| 716 |
/* deleted, or by the joining thread. */ |
| 717 |
/* In this branch asynchronous changes to (*t) are possible. */ |
| 718 |
/* It's not allowed to call GC_printf (and the friends) here, */ |
| 719 |
/* see GC_stop_world() for the information. */ |
| 720 |
t -> stack_base = 0; |
| 721 |
t -> id = 0; |
| 722 |
t -> suspended = FALSE; |
| 723 |
# ifdef RETRY_GET_THREAD_CONTEXT |
| 724 |
t -> context_sp = NULL; |
| 725 |
# endif |
| 726 |
AO_store_release(&t->tm.in_use, FALSE); |
| 727 |
} else |
| 728 |
# endif |
| 729 |
/* else */ { |
| 730 |
DWORD id = ((GC_thread)t) -> id; |
| 731 |
/* Cast away volatile qualifier, since we have lock. */ |
| 732 |
int hv = THREAD_TABLE_INDEX(id); |
| 733 |
GC_thread p = GC_threads[hv]; |
| 734 |
GC_thread prev = NULL; |
| 735 |
|
| 736 |
GC_ASSERT(I_HOLD_LOCK()); |
| 737 |
while (p != (GC_thread)t) { |
| 738 |
prev = p; |
| 739 |
p = p -> tm.next; |
| 740 |
} |
| 741 |
if (prev == 0) { |
| 742 |
GC_threads[hv] = p -> tm.next; |
| 743 |
} else { |
| 744 |
GC_ASSERT(prev != &first_thread); |
| 745 |
prev -> tm.next = p -> tm.next; |
| 746 |
GC_dirty(prev); |
| 747 |
} |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
/* Delete a thread from GC_threads. We assume it is there. */ |
| 752 |
/* (The code intentionally traps if it wasn't.) Assumes we */ |
| 753 |
/* hold the allocation lock unless GC_win32_dll_threads is set. */ |
| 754 |
/* If GC_win32_dll_threads is set then it should be called from */ |
| 755 |
/* the thread being deleted. It is also safe to delete the */ |
| 756 |
/* main thread (unless GC_win32_dll_threads). */ |
| 757 |
STATIC void GC_delete_thread(DWORD id) |
| 758 |
{ |
| 759 |
if (GC_win32_dll_threads) { |
| 760 |
GC_vthread t = GC_lookup_thread_inner(id); |
| 761 |
|
| 762 |
if (0 == t) { |
| 763 |
WARN("Removing nonexistent thread, id= %" WARN_PRIuPTR "\n", id); |
| 764 |
} else { |
| 765 |
GC_delete_gc_thread_no_free(t); |
| 766 |
} |
| 767 |
} else { |
| 768 |
int hv = THREAD_TABLE_INDEX(id); |
| 769 |
GC_thread p = GC_threads[hv]; |
| 770 |
GC_thread prev = NULL; |
| 771 |
|
| 772 |
GC_ASSERT(I_HOLD_LOCK()); |
| 773 |
while (p -> id != id) { |
| 774 |
prev = p; |
| 775 |
p = p -> tm.next; |
| 776 |
} |
| 777 |
# ifndef MSWINCE |
| 778 |
CloseHandle(p->handle); |
| 779 |
# endif |
| 780 |
if (prev == 0) { |
| 781 |
GC_threads[hv] = p -> tm.next; |
| 782 |
} else { |
| 783 |
GC_ASSERT(prev != &first_thread); |
| 784 |
prev -> tm.next = p -> tm.next; |
| 785 |
GC_dirty(prev); |
| 786 |
} |
| 787 |
if (EXPECT(p != &first_thread, TRUE)) { |
| 788 |
GC_INTERNAL_FREE(p); |
| 789 |
} |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
GC_API void GC_CALL GC_allow_register_threads(void) |
| 794 |
{ |
| 795 |
# ifdef GC_ASSERTIONS |
| 796 |
DCL_LOCK_STATE; |
| 797 |
|
| 798 |
/* Check GC is initialized and the current thread is registered. */ |
| 799 |
LOCK(); |
| 800 |
GC_ASSERT(GC_lookup_thread_inner(GetCurrentThreadId()) != 0); |
| 801 |
UNLOCK(); |
| 802 |
# endif |
| 803 |
# if !defined(GC_ALWAYS_MULTITHREADED) && !defined(PARALLEL_MARK) \ |
| 804 |
&& !defined(GC_NO_THREADS_DISCOVERY) |
| 805 |
/* GC_init() does not call GC_init_parallel() in this case. */ |
| 806 |
parallel_initialized = TRUE; |
| 807 |
# endif |
| 808 |
GC_start_mark_threads(); |
| 809 |
set_need_to_lock(); |
| 810 |
} |
| 811 |
|
| 812 |
GC_API int GC_CALL GC_register_my_thread(const struct GC_stack_base *sb) |
| 813 |
{ |
| 814 |
GC_thread me; |
| 815 |
DWORD thread_id = GetCurrentThreadId(); |
| 816 |
DCL_LOCK_STATE; |
| 817 |
|
| 818 |
if (GC_need_to_lock == FALSE) |
| 819 |
ABORT("Threads explicit registering is not previously enabled"); |
| 820 |
|
| 821 |
/* We lock here, since we want to wait for an ongoing GC. */ |
| 822 |
LOCK(); |
| 823 |
me = GC_lookup_thread_inner(thread_id); |
| 824 |
if (me == 0) { |
| 825 |
me = GC_register_my_thread_inner(sb, thread_id); |
| 826 |
# ifdef GC_PTHREADS |
| 827 |
# if defined(CPPCHECK) |
| 828 |
GC_noop1(me->flags); |
| 829 |
# endif |
| 830 |
me -> flags |= DETACHED; |
| 831 |
/* Treat as detached, since we do not need to worry about */ |
| 832 |
/* pointer results. */ |
| 833 |
# else |
| 834 |
(void)me; |
| 835 |
# endif |
| 836 |
} else |
| 837 |
# ifdef GC_PTHREADS |
| 838 |
/* else */ if ((me -> flags & FINISHED) != 0) { |
| 839 |
GC_record_stack_base(me, sb); |
| 840 |
me -> flags &= ~FINISHED; /* but not DETACHED */ |
| 841 |
} else |
| 842 |
# endif |
| 843 |
/* else */ { |
| 844 |
UNLOCK(); |
| 845 |
return GC_DUPLICATE; |
| 846 |
} |
| 847 |
|
| 848 |
# ifdef THREAD_LOCAL_ALLOC |
| 849 |
GC_init_thread_local(&me->tlfs); |
| 850 |
# endif |
| 851 |
UNLOCK(); |
| 852 |
return GC_SUCCESS; |
| 853 |
} |
| 854 |
|
| 855 |
#ifdef GC_DISABLE_INCREMENTAL |
| 856 |
# define GC_wait_for_gc_completion(wait_for_all) (void)(wait_for_all) |
| 857 |
#else |
| 858 |
/* Similar to that in pthread_support.c. */ |
| 859 |
STATIC void GC_wait_for_gc_completion(GC_bool wait_for_all) |
| 860 |
{ |
| 861 |
GC_ASSERT(I_HOLD_LOCK()); |
| 862 |
if (GC_incremental && GC_collection_in_progress()) { |
| 863 |
word old_gc_no = GC_gc_no; |
| 864 |
|
| 865 |
/* Make sure that no part of our stack is still on the mark stack, */ |
| 866 |
/* since it's about to be unmapped. */ |
| 867 |
do { |
| 868 |
ENTER_GC(); |
| 869 |
GC_in_thread_creation = TRUE; |
| 870 |
GC_collect_a_little_inner(1); |
| 871 |
GC_in_thread_creation = FALSE; |
| 872 |
EXIT_GC(); |
| 873 |
|
| 874 |
UNLOCK(); |
| 875 |
Sleep(0); /* yield */ |
| 876 |
LOCK(); |
| 877 |
} while (GC_incremental && GC_collection_in_progress() |
| 878 |
&& (wait_for_all || old_gc_no == GC_gc_no)); |
| 879 |
} |
| 880 |
} |
| 881 |
#endif /* !GC_DISABLE_INCREMENTAL */ |
| 882 |
|
| 883 |
GC_API int GC_CALL GC_unregister_my_thread(void) |
| 884 |
{ |
| 885 |
DCL_LOCK_STATE; |
| 886 |
|
| 887 |
# ifdef DEBUG_THREADS |
| 888 |
GC_log_printf("Unregistering thread 0x%lx\n", (long)GetCurrentThreadId()); |
| 889 |
# endif |
| 890 |
|
| 891 |
if (GC_win32_dll_threads) { |
| 892 |
# if defined(THREAD_LOCAL_ALLOC) |
| 893 |
/* Can't happen: see GC_use_threads_discovery(). */ |
| 894 |
GC_ASSERT(FALSE); |
| 895 |
# else |
| 896 |
/* FIXME: Should we just ignore this? */ |
| 897 |
GC_delete_thread(GetCurrentThreadId()); |
| 898 |
# endif |
| 899 |
} else { |
| 900 |
# if defined(THREAD_LOCAL_ALLOC) || defined(GC_PTHREADS) |
| 901 |
GC_thread me; |
| 902 |
# endif |
| 903 |
DWORD thread_id = GetCurrentThreadId(); |
| 904 |
|
| 905 |
LOCK(); |
| 906 |
GC_wait_for_gc_completion(FALSE); |
| 907 |
# if defined(THREAD_LOCAL_ALLOC) || defined(GC_PTHREADS) |
| 908 |
me = GC_lookup_thread_inner(thread_id); |
| 909 |
CHECK_LOOKUP_MY_THREAD(me); |
| 910 |
GC_ASSERT(!KNOWN_FINISHED(me)); |
| 911 |
# endif |
| 912 |
# if defined(THREAD_LOCAL_ALLOC) |
| 913 |
GC_ASSERT(GC_getspecific(GC_thread_key) == &me->tlfs); |
| 914 |
GC_destroy_thread_local(&(me->tlfs)); |
| 915 |
# endif |
| 916 |
# ifdef GC_PTHREADS |
| 917 |
if ((me -> flags & DETACHED) == 0) { |
| 918 |
me -> flags |= FINISHED; |
| 919 |
} else |
| 920 |
# endif |
| 921 |
/* else */ { |
| 922 |
GC_delete_thread(thread_id); |
| 923 |
} |
| 924 |
# if defined(THREAD_LOCAL_ALLOC) |
| 925 |
/* It is required to call remove_specific defined in specific.c. */ |
| 926 |
GC_remove_specific(GC_thread_key); |
| 927 |
# endif |
| 928 |
UNLOCK(); |
| 929 |
} |
| 930 |
return GC_SUCCESS; |
| 931 |
} |
| 932 |
|
| 933 |
/* Wrapper for functions that are likely to block for an appreciable */ |
| 934 |
/* length of time. */ |
| 935 |
|
| 936 |
/* GC_do_blocking_inner() is nearly the same as in pthread_support.c */ |
| 937 |
GC_INNER void GC_do_blocking_inner(ptr_t data, void * context GC_ATTR_UNUSED) |
| 938 |
{ |
| 939 |
struct blocking_data * d = (struct blocking_data *) data; |
| 940 |
DWORD thread_id = GetCurrentThreadId(); |
| 941 |
GC_thread me; |
| 942 |
# ifdef IA64 |
| 943 |
ptr_t stack_ptr = GC_save_regs_in_stack(); |
| 944 |
# endif |
| 945 |
DCL_LOCK_STATE; |
| 946 |
|
| 947 |
LOCK(); |
| 948 |
me = GC_lookup_thread_inner(thread_id); |
| 949 |
CHECK_LOOKUP_MY_THREAD(me); |
| 950 |
GC_ASSERT(me -> thread_blocked_sp == NULL); |
| 951 |
# ifdef IA64 |
| 952 |
me -> backing_store_ptr = stack_ptr; |
| 953 |
# endif |
| 954 |
me -> thread_blocked_sp = (ptr_t) &d; /* save approx. sp */ |
| 955 |
/* Save context here if we want to support precise stack marking */ |
| 956 |
UNLOCK(); |
| 957 |
d -> client_data = (d -> fn)(d -> client_data); |
| 958 |
LOCK(); /* This will block if the world is stopped. */ |
| 959 |
# if defined(CPPCHECK) |
| 960 |
GC_noop1((word)me->thread_blocked_sp); |
| 961 |
# endif |
| 962 |
me -> thread_blocked_sp = NULL; |
| 963 |
UNLOCK(); |
| 964 |
} |
| 965 |
|
| 966 |
/* GC_call_with_gc_active() has the opposite to GC_do_blocking() */ |
| 967 |
/* functionality. It might be called from a user function invoked by */ |
| 968 |
/* GC_do_blocking() to temporarily back allow calling any GC function */ |
| 969 |
/* and/or manipulating pointers to the garbage collected heap. */ |
| 970 |
GC_API void * GC_CALL GC_call_with_gc_active(GC_fn_type fn, |
| 971 |
void * client_data) |
| 972 |
{ |
| 973 |
struct GC_traced_stack_sect_s stacksect; |
| 974 |
DWORD thread_id = GetCurrentThreadId(); |
| 975 |
GC_thread me; |
| 976 |
DCL_LOCK_STATE; |
| 977 |
|
| 978 |
LOCK(); /* This will block if the world is stopped. */ |
| 979 |
me = GC_lookup_thread_inner(thread_id); |
| 980 |
CHECK_LOOKUP_MY_THREAD(me); |
| 981 |
/* Adjust our stack bottom pointer (this could happen unless */ |
| 982 |
/* GC_get_stack_base() was used which returned GC_SUCCESS). */ |
| 983 |
GC_ASSERT(me -> stack_base != NULL); |
| 984 |
if ((word)me->stack_base < (word)(&stacksect)) { |
| 985 |
me -> stack_base = (ptr_t)(&stacksect); |
| 986 |
# if defined(I386) |
| 987 |
me -> initial_stack_base = me -> stack_base; |
| 988 |
# endif |
| 989 |
} |
| 990 |
|
| 991 |
if (me -> thread_blocked_sp == NULL) { |
| 992 |
/* We are not inside GC_do_blocking() - do nothing more. */ |
| 993 |
UNLOCK(); |
| 994 |
client_data = fn(client_data); |
| 995 |
/* Prevent treating the above as a tail call. */ |
| 996 |
GC_noop1(COVERT_DATAFLOW(&stacksect)); |
| 997 |
return client_data; /* result */ |
| 998 |
} |
| 999 |
|
| 1000 |
/* Setup new "stack section". */ |
| 1001 |
stacksect.saved_stack_ptr = me -> thread_blocked_sp; |
| 1002 |
# ifdef IA64 |
| 1003 |
/* This is the same as in GC_call_with_stack_base(). */ |
| 1004 |
stacksect.backing_store_end = GC_save_regs_in_stack(); |
| 1005 |
/* Unnecessarily flushes register stack, */ |
| 1006 |
/* but that probably doesn't hurt. */ |
| 1007 |
stacksect.saved_backing_store_ptr = me -> backing_store_ptr; |
| 1008 |
# endif |
| 1009 |
stacksect.prev = me -> traced_stack_sect; |
| 1010 |
me -> thread_blocked_sp = NULL; |
| 1011 |
me -> traced_stack_sect = &stacksect; |
| 1012 |
|
| 1013 |
UNLOCK(); |
| 1014 |
client_data = fn(client_data); |
| 1015 |
GC_ASSERT(me -> thread_blocked_sp == NULL); |
| 1016 |
GC_ASSERT(me -> traced_stack_sect == &stacksect); |
| 1017 |
|
| 1018 |
/* Restore original "stack section". */ |
| 1019 |
LOCK(); |
| 1020 |
# if defined(CPPCHECK) |
| 1021 |
GC_noop1((word)me->traced_stack_sect); |
| 1022 |
# endif |
| 1023 |
me -> traced_stack_sect = stacksect.prev; |
| 1024 |
# ifdef IA64 |
| 1025 |
me -> backing_store_ptr = stacksect.saved_backing_store_ptr; |
| 1026 |
# endif |
| 1027 |
me -> thread_blocked_sp = stacksect.saved_stack_ptr; |
| 1028 |
UNLOCK(); |
| 1029 |
|
| 1030 |
return client_data; /* result */ |
| 1031 |
} |
| 1032 |
|
| 1033 |
GC_API void GC_CALL GC_set_stackbottom(void *gc_thread_handle, |
| 1034 |
const struct GC_stack_base *sb) |
| 1035 |
{ |
| 1036 |
GC_thread t = (GC_thread)gc_thread_handle; |
| 1037 |
|
| 1038 |
GC_ASSERT(sb -> mem_base != NULL); |
| 1039 |
if (!EXPECT(GC_is_initialized, TRUE)) { |
| 1040 |
GC_ASSERT(NULL == t); |
| 1041 |
GC_stackbottom = (char *)sb->mem_base; |
| 1042 |
# ifdef IA64 |
| 1043 |
GC_register_stackbottom = (ptr_t)sb->reg_base; |
| 1044 |
# endif |
| 1045 |
return; |
| 1046 |
} |
| 1047 |
|
| 1048 |
GC_ASSERT(I_HOLD_LOCK()); |
| 1049 |
if (NULL == t) { /* current thread? */ |
| 1050 |
t = GC_lookup_thread_inner(GetCurrentThreadId()); |
| 1051 |
CHECK_LOOKUP_MY_THREAD(t); |
| 1052 |
} |
| 1053 |
GC_ASSERT(!KNOWN_FINISHED(t)); |
| 1054 |
GC_ASSERT(NULL == t -> thread_blocked_sp |
| 1055 |
&& NULL == t -> traced_stack_sect); /* for now */ |
| 1056 |
t -> stack_base = (ptr_t)sb->mem_base; |
| 1057 |
t -> last_stack_min = ADDR_LIMIT; /* reset the known minimum */ |
| 1058 |
# ifdef IA64 |
| 1059 |
t -> backing_store_end = (ptr_t)sb->reg_base; |
| 1060 |
# endif |
| 1061 |
} |
| 1062 |
|
| 1063 |
GC_API void * GC_CALL GC_get_my_stackbottom(struct GC_stack_base *sb) |
| 1064 |
{ |
| 1065 |
DWORD thread_id = GetCurrentThreadId(); |
| 1066 |
GC_thread me; |
| 1067 |
DCL_LOCK_STATE; |
| 1068 |
|
| 1069 |
LOCK(); |
| 1070 |
me = GC_lookup_thread_inner(thread_id); |
| 1071 |
CHECK_LOOKUP_MY_THREAD(me); /* the thread is assumed to be registered */ |
| 1072 |
sb -> mem_base = me -> stack_base; |
| 1073 |
# ifdef IA64 |
| 1074 |
sb -> reg_base = me -> backing_store_end; |
| 1075 |
# endif |
| 1076 |
UNLOCK(); |
| 1077 |
return (void *)me; /* gc_thread_handle */ |
| 1078 |
} |
| 1079 |
|
| 1080 |
#ifdef GC_PTHREADS |
| 1081 |
|
| 1082 |
/* A quick-and-dirty cache of the mapping between pthread_t */ |
| 1083 |
/* and Win32 thread id. */ |
| 1084 |
# define PTHREAD_MAP_SIZE 512 |
| 1085 |
DWORD GC_pthread_map_cache[PTHREAD_MAP_SIZE] = {0}; |
| 1086 |
# define PTHREAD_MAP_INDEX(pthread_id) \ |
| 1087 |
((NUMERIC_THREAD_ID(pthread_id) >> 5) % PTHREAD_MAP_SIZE) |
| 1088 |
/* It appears pthread_t is really a pointer type ... */ |
| 1089 |
# define SET_PTHREAD_MAP_CACHE(pthread_id, win32_id) \ |
| 1090 |
(void)(GC_pthread_map_cache[PTHREAD_MAP_INDEX(pthread_id)] = (win32_id)) |
| 1091 |
# define GET_PTHREAD_MAP_CACHE(pthread_id) \ |
| 1092 |
GC_pthread_map_cache[PTHREAD_MAP_INDEX(pthread_id)] |
| 1093 |
|
| 1094 |
/* Return a GC_thread corresponding to a given pthread_t. */ |
| 1095 |
/* Returns 0 if it's not there. */ |
| 1096 |
/* We assume that this is only called for pthread ids that */ |
| 1097 |
/* have not yet terminated or are still joinable, and */ |
| 1098 |
/* cannot be concurrently terminated. */ |
| 1099 |
/* Assumes we do NOT hold the allocation lock. */ |
| 1100 |
STATIC GC_thread GC_lookup_pthread(pthread_t id) |
| 1101 |
{ |
| 1102 |
# ifndef GC_NO_THREADS_DISCOVERY |
| 1103 |
if (GC_win32_dll_threads) { |
| 1104 |
int i; |
| 1105 |
LONG my_max = GC_get_max_thread_index(); |
| 1106 |
|
| 1107 |
for (i = 0; i <= my_max && |
| 1108 |
(!AO_load_acquire(&dll_thread_table[i].tm.in_use) |
| 1109 |
|| !THREAD_EQUAL(dll_thread_table[i].pthread_id, id)); |
| 1110 |
/* Must still be in_use, since nobody else can */ |
| 1111 |
/* store our thread_id. */ |
| 1112 |
i++) { |
| 1113 |
/* empty */ |
| 1114 |
} |
| 1115 |
return i <= my_max ? (GC_thread)(dll_thread_table + i) : NULL; |
| 1116 |
} else |
| 1117 |
# endif |
| 1118 |
/* else */ { |
| 1119 |
/* We first try the cache. If that fails, we use a very slow */ |
| 1120 |
/* approach. */ |
| 1121 |
DWORD win32_id = GET_PTHREAD_MAP_CACHE(id); |
| 1122 |
int hv_guess = THREAD_TABLE_INDEX(win32_id); |
| 1123 |
int hv; |
| 1124 |
GC_thread p; |
| 1125 |
DCL_LOCK_STATE; |
| 1126 |
|
| 1127 |
LOCK(); |
| 1128 |
for (p = GC_threads[hv_guess]; 0 != p; p = p -> tm.next) { |
| 1129 |
if (THREAD_EQUAL(p -> pthread_id, id)) |
| 1130 |
goto foundit; |
| 1131 |
} |
| 1132 |
for (hv = 0; hv < THREAD_TABLE_SZ; ++hv) { |
| 1133 |
for (p = GC_threads[hv]; 0 != p; p = p -> tm.next) { |
| 1134 |
if (THREAD_EQUAL(p -> pthread_id, id)) |
| 1135 |
goto foundit; |
| 1136 |
} |
| 1137 |
} |
| 1138 |
p = 0; |
| 1139 |
foundit: |
| 1140 |
UNLOCK(); |
| 1141 |
return p; |
| 1142 |
} |
| 1143 |
} |
| 1144 |
|
| 1145 |
#endif /* GC_PTHREADS */ |
| 1146 |
|
| 1147 |
#ifdef CAN_HANDLE_FORK |
| 1148 |
/* Similar to that in pthread_support.c but also rehashes the table */ |
| 1149 |
/* since hash map key (thread_id) differs from that in the parent. */ |
| 1150 |
STATIC void GC_remove_all_threads_but_me(void) |
| 1151 |
{ |
| 1152 |
int hv; |
| 1153 |
GC_thread me = NULL; |
| 1154 |
DWORD thread_id; |
| 1155 |
pthread_t pthread_id = pthread_self(); /* same as in parent */ |
| 1156 |
|
| 1157 |
GC_ASSERT(!GC_win32_dll_threads); |
| 1158 |
for (hv = 0; hv < THREAD_TABLE_SZ; ++hv) { |
| 1159 |
GC_thread p, next; |
| 1160 |
|
| 1161 |
for (p = GC_threads[hv]; 0 != p; p = next) { |
| 1162 |
next = p -> tm.next; |
| 1163 |
if (THREAD_EQUAL(p -> pthread_id, pthread_id) |
| 1164 |
&& me == NULL) { /* ignore dead threads with the same id */ |
| 1165 |
me = p; |
| 1166 |
p -> tm.next = 0; |
| 1167 |
} else { |
| 1168 |
# ifdef THREAD_LOCAL_ALLOC |
| 1169 |
if ((p -> flags & FINISHED) == 0) { |
| 1170 |
/* Cannot call GC_destroy_thread_local here (see the */ |
| 1171 |
/* corresponding comment in pthread_support.c). */ |
| 1172 |
GC_remove_specific_after_fork(GC_thread_key, p -> pthread_id); |
| 1173 |
} |
| 1174 |
# endif |
| 1175 |
if (&first_thread != p) |
| 1176 |
GC_INTERNAL_FREE(p); |
| 1177 |
} |
| 1178 |
} |
| 1179 |
GC_threads[hv] = NULL; |
| 1180 |
} |
| 1181 |
|
| 1182 |
/* Put "me" back to GC_threads. */ |
| 1183 |
GC_ASSERT(me != NULL); |
| 1184 |
thread_id = GetCurrentThreadId(); /* differs from that in parent */ |
| 1185 |
GC_threads[THREAD_TABLE_INDEX(thread_id)] = me; |
| 1186 |
|
| 1187 |
/* Update Win32 thread Id and handle. */ |
| 1188 |
me -> id = thread_id; |
| 1189 |
# ifndef MSWINCE |
| 1190 |
if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), |
| 1191 |
GetCurrentProcess(), (HANDLE *)&me->handle, |
| 1192 |
0 /* dwDesiredAccess */, FALSE /* bInheritHandle */, |
| 1193 |
DUPLICATE_SAME_ACCESS)) |
| 1194 |
ABORT("DuplicateHandle failed"); |
| 1195 |
# endif |
| 1196 |
|
| 1197 |
# if defined(THREAD_LOCAL_ALLOC) && !defined(USE_CUSTOM_SPECIFIC) |
| 1198 |
/* For Cygwin, we need to re-assign thread-local pointer to */ |
| 1199 |
/* 'tlfs' (it is OK to call GC_destroy_thread_local and */ |
| 1200 |
/* GC_free_inner before this action). */ |
| 1201 |
if (GC_setspecific(GC_thread_key, &me->tlfs) != 0) |
| 1202 |
ABORT("GC_setspecific failed (in child)"); |
| 1203 |
# endif |
| 1204 |
} |
| 1205 |
|
| 1206 |
static void fork_prepare_proc(void) |
| 1207 |
{ |
| 1208 |
LOCK(); |
| 1209 |
# ifdef PARALLEL_MARK |
| 1210 |
if (GC_parallel) |
| 1211 |
GC_wait_for_reclaim(); |
| 1212 |
# endif |
| 1213 |
GC_wait_for_gc_completion(TRUE); |
| 1214 |
# ifdef PARALLEL_MARK |
| 1215 |
if (GC_parallel) |
| 1216 |
GC_acquire_mark_lock(); |
| 1217 |
# endif |
| 1218 |
} |
| 1219 |
|
| 1220 |
static void fork_parent_proc(void) |
| 1221 |
{ |
| 1222 |
# ifdef PARALLEL_MARK |
| 1223 |
if (GC_parallel) |
| 1224 |
GC_release_mark_lock(); |
| 1225 |
# endif |
| 1226 |
UNLOCK(); |
| 1227 |
} |
| 1228 |
|
| 1229 |
static void fork_child_proc(void) |
| 1230 |
{ |
| 1231 |
# ifdef PARALLEL_MARK |
| 1232 |
if (GC_parallel) { |
| 1233 |
GC_release_mark_lock(); |
| 1234 |
GC_parallel = FALSE; /* or GC_markers_m1 = 0 */ |
| 1235 |
/* Turn off parallel marking in the child, since we are */ |
| 1236 |
/* probably just going to exec, and we would have to */ |
| 1237 |
/* restart mark threads. */ |
| 1238 |
} |
| 1239 |
# endif |
| 1240 |
GC_remove_all_threads_but_me(); |
| 1241 |
UNLOCK(); |
| 1242 |
} |
| 1243 |
|
| 1244 |
/* Routines for fork handling by client (no-op if pthread_atfork works). */ |
| 1245 |
GC_API void GC_CALL GC_atfork_prepare(void) |
| 1246 |
{ |
| 1247 |
if (!EXPECT(GC_is_initialized, TRUE)) GC_init(); |
| 1248 |
if (GC_handle_fork <= 0) |
| 1249 |
fork_prepare_proc(); |
| 1250 |
} |
| 1251 |
|
| 1252 |
GC_API void GC_CALL GC_atfork_parent(void) |
| 1253 |
{ |
| 1254 |
if (GC_handle_fork <= 0) |
| 1255 |
fork_parent_proc(); |
| 1256 |
} |
| 1257 |
|
| 1258 |
GC_API void GC_CALL GC_atfork_child(void) |
| 1259 |
{ |
| 1260 |
if (GC_handle_fork <= 0) |
| 1261 |
fork_child_proc(); |
| 1262 |
} |
| 1263 |
#endif /* CAN_HANDLE_FORK */ |
| 1264 |
|
| 1265 |
void GC_push_thread_structures(void) |
| 1266 |
{ |
| 1267 |
GC_ASSERT(I_HOLD_LOCK()); |
| 1268 |
# ifndef GC_NO_THREADS_DISCOVERY |
| 1269 |
if (GC_win32_dll_threads) { |
| 1270 |
/* Unlike the other threads implementations, the thread table */ |
| 1271 |
/* here contains no pointers to the collectible heap (note also */ |
| 1272 |
/* that GC_PTHREADS is incompatible with DllMain-based thread */ |
| 1273 |
/* registration). Thus we have no private structures we need */ |
| 1274 |
/* to preserve. */ |
| 1275 |
} else |
| 1276 |
# endif |
| 1277 |
/* else */ { |
| 1278 |
GC_push_all(&GC_threads, (ptr_t)(&GC_threads) + sizeof(GC_threads)); |
| 1279 |
} |
| 1280 |
# if defined(THREAD_LOCAL_ALLOC) && defined(USE_CUSTOM_SPECIFIC) |
| 1281 |
GC_PUSH_ALL_SYM(GC_thread_key); |
| 1282 |
/* Just in case we ever use our own TLS implementation. */ |
| 1283 |
# endif |
| 1284 |
} |
| 1285 |
|
| 1286 |
#ifdef WOW64_THREAD_CONTEXT_WORKAROUND |
| 1287 |
# ifndef CONTEXT_EXCEPTION_ACTIVE |
| 1288 |
# define CONTEXT_EXCEPTION_ACTIVE 0x08000000 |
| 1289 |
# define CONTEXT_EXCEPTION_REQUEST 0x40000000 |
| 1290 |
# define CONTEXT_EXCEPTION_REPORTING 0x80000000 |
| 1291 |
# endif |
| 1292 |
static GC_bool isWow64; /* Is running 32-bit code on Win64? */ |
| 1293 |
# define GET_THREAD_CONTEXT_FLAGS (isWow64 \ |
| 1294 |
? CONTEXT_INTEGER | CONTEXT_CONTROL \ |
| 1295 |
| CONTEXT_EXCEPTION_REQUEST | CONTEXT_SEGMENTS \ |
| 1296 |
: CONTEXT_INTEGER | CONTEXT_CONTROL) |
| 1297 |
#elif defined(I386) || defined(XMM_CANT_STORE_PTRS) |
| 1298 |
# define GET_THREAD_CONTEXT_FLAGS (CONTEXT_INTEGER | CONTEXT_CONTROL) |
| 1299 |
#else |
| 1300 |
# define GET_THREAD_CONTEXT_FLAGS (CONTEXT_INTEGER | CONTEXT_CONTROL \ |
| 1301 |
| CONTEXT_FLOATING_POINT) |
| 1302 |
#endif /* !WOW64_THREAD_CONTEXT_WORKAROUND && !I386 */ |
| 1303 |
|
| 1304 |
/* Suspend the given thread, if it's still active. */ |
| 1305 |
STATIC void GC_suspend(GC_thread t) |
| 1306 |
{ |
| 1307 |
# ifndef MSWINCE |
| 1308 |
DWORD exitCode; |
| 1309 |
# ifdef RETRY_GET_THREAD_CONTEXT |
| 1310 |
int retry_cnt; |
| 1311 |
# define MAX_SUSPEND_THREAD_RETRIES (1000 * 1000) |
| 1312 |
# endif |
| 1313 |
# endif |
| 1314 |
|
| 1315 |
# ifdef DEBUG_THREADS |
| 1316 |
GC_log_printf("Suspending 0x%x\n", (int)t->id); |
| 1317 |
# endif |
| 1318 |
UNPROTECT_THREAD(t); |
| 1319 |
GC_acquire_dirty_lock(); |
| 1320 |
|
| 1321 |
# ifdef MSWINCE |
| 1322 |
/* SuspendThread() will fail if thread is running kernel code. */ |
| 1323 |
while (SuspendThread(THREAD_HANDLE(t)) == (DWORD)-1) { |
| 1324 |
GC_release_dirty_lock(); |
| 1325 |
Sleep(10); /* in millis */ |
| 1326 |
GC_acquire_dirty_lock(); |
| 1327 |
} |
| 1328 |
# elif defined(RETRY_GET_THREAD_CONTEXT) |
| 1329 |
for (retry_cnt = 0;;) { |
| 1330 |
/* Apparently the Windows 95 GetOpenFileName call creates */ |
| 1331 |
/* a thread that does not properly get cleaned up, and */ |
| 1332 |
/* SuspendThread on its descriptor may provoke a crash. */ |
| 1333 |
/* This reduces the probability of that event, though it still */ |
| 1334 |
/* appears there is a race here. */ |
| 1335 |
if (GetExitCodeThread(t -> handle, &exitCode) |
| 1336 |
&& exitCode != STILL_ACTIVE) { |
| 1337 |
GC_release_dirty_lock(); |
| 1338 |
# ifdef GC_PTHREADS |
| 1339 |
t -> stack_base = 0; /* prevent stack from being pushed */ |
| 1340 |
# else |
| 1341 |
/* This breaks pthread_join on Cygwin, which is guaranteed to */ |
| 1342 |
/* only see user threads. */ |
| 1343 |
GC_ASSERT(GC_win32_dll_threads); |
| 1344 |
GC_delete_gc_thread_no_free(t); |
| 1345 |
# endif |
| 1346 |
return; |
| 1347 |
} |
| 1348 |
|
| 1349 |
if (SuspendThread(t->handle) != (DWORD)-1) { |
| 1350 |
CONTEXT context; |
| 1351 |
|
| 1352 |
context.ContextFlags = GET_THREAD_CONTEXT_FLAGS; |
| 1353 |
if (GetThreadContext(t->handle, &context)) { |
| 1354 |
/* TODO: WoW64 extra workaround: if CONTEXT_EXCEPTION_ACTIVE */ |
| 1355 |
/* then Sleep(1) and retry. */ |
| 1356 |
t->context_sp = copy_ptr_regs(t->context_regs, &context); |
| 1357 |
break; /* success; the context pointer registers are saved */ |
| 1358 |
} |
| 1359 |
|
| 1360 |
/* Resume the thread, try to suspend it in a better location. */ |
| 1361 |
if (ResumeThread(t->handle) == (DWORD)-1) |
| 1362 |
ABORT("ResumeThread failed in suspend loop"); |
| 1363 |
} |
| 1364 |
if (retry_cnt > 1) { |
| 1365 |
GC_release_dirty_lock(); |
| 1366 |
Sleep(0); /* yield */ |
| 1367 |
GC_acquire_dirty_lock(); |
| 1368 |
} |
| 1369 |
if (++retry_cnt >= MAX_SUSPEND_THREAD_RETRIES) |
| 1370 |
ABORT("SuspendThread loop failed"); /* something must be wrong */ |
| 1371 |
} |
| 1372 |
# else |
| 1373 |
if (GetExitCodeThread(t -> handle, &exitCode) |
| 1374 |
&& exitCode != STILL_ACTIVE) { |
| 1375 |
GC_release_dirty_lock(); |
| 1376 |
# ifdef GC_PTHREADS |
| 1377 |
t -> stack_base = 0; /* prevent stack from being pushed */ |
| 1378 |
# else |
| 1379 |
GC_ASSERT(GC_win32_dll_threads); |
| 1380 |
GC_delete_gc_thread_no_free(t); |
| 1381 |
# endif |
| 1382 |
return; |
| 1383 |
} |
| 1384 |
if (SuspendThread(t -> handle) == (DWORD)-1) |
| 1385 |
ABORT("SuspendThread failed"); |
| 1386 |
# endif |
| 1387 |
t -> suspended = (unsigned char)TRUE; |
| 1388 |
GC_release_dirty_lock(); |
| 1389 |
if (GC_on_thread_event) |
| 1390 |
GC_on_thread_event(GC_EVENT_THREAD_SUSPENDED, THREAD_HANDLE(t)); |
| 1391 |
} |
| 1392 |
|
| 1393 |
#if defined(GC_ASSERTIONS) \ |
| 1394 |
&& ((defined(MSWIN32) && !defined(CONSOLE_LOG)) || defined(MSWINCE)) |
| 1395 |
GC_INNER GC_bool GC_write_disabled = FALSE; |
| 1396 |
/* TRUE only if GC_stop_world() acquired GC_write_cs. */ |
| 1397 |
#endif |
| 1398 |
|
| 1399 |
GC_INNER void GC_stop_world(void) |
| 1400 |
{ |
| 1401 |
DWORD thread_id = GetCurrentThreadId(); |
| 1402 |
|
| 1403 |
if (!GC_thr_initialized) |
| 1404 |
ABORT("GC_stop_world() called before GC_thr_init()"); |
| 1405 |
GC_ASSERT(I_HOLD_LOCK()); |
| 1406 |
|
| 1407 |
/* This code is the same as in pthread_stop_world.c */ |
| 1408 |
# ifdef PARALLEL_MARK |
| 1409 |
if (GC_parallel) { |
| 1410 |
GC_acquire_mark_lock(); |
| 1411 |
GC_ASSERT(GC_fl_builder_count == 0); |
| 1412 |
/* We should have previously waited for it to become zero. */ |
| 1413 |
} |
| 1414 |
# endif /* PARALLEL_MARK */ |
| 1415 |
|
| 1416 |
# if !defined(GC_NO_THREADS_DISCOVERY) || defined(GC_ASSERTIONS) |
| 1417 |
GC_please_stop = TRUE; |
| 1418 |
# endif |
| 1419 |
# if (defined(MSWIN32) && !defined(CONSOLE_LOG)) || defined(MSWINCE) |
| 1420 |
GC_ASSERT(!GC_write_disabled); |
| 1421 |
EnterCriticalSection(&GC_write_cs); |
| 1422 |
/* It's not allowed to call GC_printf() (and friends) here down to */ |
| 1423 |
/* LeaveCriticalSection (same applies recursively to GC_suspend, */ |
| 1424 |
/* GC_delete_gc_thread_no_free, GC_get_max_thread_index, GC_size */ |
| 1425 |
/* and GC_remove_protection). */ |
| 1426 |
# ifdef GC_ASSERTIONS |
| 1427 |
GC_write_disabled = TRUE; |
| 1428 |
# endif |
| 1429 |
# endif |
| 1430 |
# ifndef GC_NO_THREADS_DISCOVERY |
| 1431 |
if (GC_win32_dll_threads) { |
| 1432 |
int i; |
| 1433 |
int my_max; |
| 1434 |
/* Any threads being created during this loop will end up setting */ |
| 1435 |
/* GC_attached_thread when they start. This will force marking */ |
| 1436 |
/* to restart. This is not ideal, but hopefully correct. */ |
| 1437 |
AO_store(&GC_attached_thread, FALSE); |
| 1438 |
my_max = (int)GC_get_max_thread_index(); |
| 1439 |
for (i = 0; i <= my_max; i++) { |
| 1440 |
GC_vthread t = dll_thread_table + i; |
| 1441 |
if (t -> stack_base != 0 && t -> thread_blocked_sp == NULL |
| 1442 |
&& t -> id != thread_id) { |
| 1443 |
GC_suspend((GC_thread)t); |
| 1444 |
} |
| 1445 |
} |
| 1446 |
} else |
| 1447 |
# endif |
| 1448 |
/* else */ { |
| 1449 |
GC_thread t; |
| 1450 |
int i; |
| 1451 |
|
| 1452 |
for (i = 0; i < THREAD_TABLE_SZ; i++) { |
| 1453 |
for (t = GC_threads[i]; t != 0; t = t -> tm.next) { |
| 1454 |
if (t -> stack_base != 0 && t -> thread_blocked_sp == NULL |
| 1455 |
&& !KNOWN_FINISHED(t) && t -> id != thread_id) { |
| 1456 |
GC_suspend(t); |
| 1457 |
} |
| 1458 |
} |
| 1459 |
} |
| 1460 |
} |
| 1461 |
# if (defined(MSWIN32) && !defined(CONSOLE_LOG)) || defined(MSWINCE) |
| 1462 |
# ifdef GC_ASSERTIONS |
| 1463 |
GC_write_disabled = FALSE; |
| 1464 |
# endif |
| 1465 |
LeaveCriticalSection(&GC_write_cs); |
| 1466 |
# endif |
| 1467 |
# ifdef PARALLEL_MARK |
| 1468 |
if (GC_parallel) |
| 1469 |
GC_release_mark_lock(); |
| 1470 |
# endif |
| 1471 |
} |
| 1472 |
|
| 1473 |
GC_INNER void GC_start_world(void) |
| 1474 |
{ |
| 1475 |
# ifdef GC_ASSERTIONS |
| 1476 |
DWORD thread_id = GetCurrentThreadId(); |
| 1477 |
# endif |
| 1478 |
|
| 1479 |
GC_ASSERT(I_HOLD_LOCK()); |
| 1480 |
if (GC_win32_dll_threads) { |
| 1481 |
LONG my_max = GC_get_max_thread_index(); |
| 1482 |
int i; |
| 1483 |
|
| 1484 |
for (i = 0; i <= my_max; i++) { |
| 1485 |
GC_thread t = (GC_thread)(dll_thread_table + i); |
| 1486 |
if (t -> suspended) { |
| 1487 |
# ifdef DEBUG_THREADS |
| 1488 |
GC_log_printf("Resuming 0x%x\n", (int)t->id); |
| 1489 |
# endif |
| 1490 |
GC_ASSERT(t -> stack_base != 0 && t -> id != thread_id); |
| 1491 |
if (ResumeThread(THREAD_HANDLE(t)) == (DWORD)-1) |
| 1492 |
ABORT("ResumeThread failed"); |
| 1493 |
t -> suspended = FALSE; |
| 1494 |
if (GC_on_thread_event) |
| 1495 |
GC_on_thread_event(GC_EVENT_THREAD_UNSUSPENDED, THREAD_HANDLE(t)); |
| 1496 |
} |
| 1497 |
/* Else thread is unregistered or not suspended. */ |
| 1498 |
} |
| 1499 |
} else { |
| 1500 |
GC_thread t; |
| 1501 |
int i; |
| 1502 |
|
| 1503 |
for (i = 0; i < THREAD_TABLE_SZ; i++) { |
| 1504 |
for (t = GC_threads[i]; t != 0; t = t -> tm.next) { |
| 1505 |
if (t -> suspended) { |
| 1506 |
# ifdef DEBUG_THREADS |
| 1507 |
GC_log_printf("Resuming 0x%x\n", (int)t->id); |
| 1508 |
# endif |
| 1509 |
GC_ASSERT(t -> stack_base != 0 && t -> id != thread_id); |
| 1510 |
if (ResumeThread(THREAD_HANDLE(t)) == (DWORD)-1) |
| 1511 |
ABORT("ResumeThread failed"); |
| 1512 |
UNPROTECT_THREAD(t); |
| 1513 |
t -> suspended = FALSE; |
| 1514 |
if (GC_on_thread_event) |
| 1515 |
GC_on_thread_event(GC_EVENT_THREAD_UNSUSPENDED, THREAD_HANDLE(t)); |
| 1516 |
} else { |
| 1517 |
# ifdef DEBUG_THREADS |
| 1518 |
GC_log_printf("Not resuming thread 0x%x as it is not suspended\n", |
| 1519 |
(int)t->id); |
| 1520 |
# endif |
| 1521 |
} |
| 1522 |
} |
| 1523 |
} |
| 1524 |
} |
| 1525 |
# if !defined(GC_NO_THREADS_DISCOVERY) || defined(GC_ASSERTIONS) |
| 1526 |
GC_please_stop = FALSE; |
| 1527 |
# endif |
| 1528 |
} |
| 1529 |
|
| 1530 |
#ifdef MSWINCE |
| 1531 |
/* The VirtualQuery calls below won't work properly on some old WinCE */ |
| 1532 |
/* versions, but since each stack is restricted to an aligned 64 KiB */ |
| 1533 |
/* region of virtual memory we can just take the next lowest multiple */ |
| 1534 |
/* of 64 KiB. The result of this macro must not be used as its */ |
| 1535 |
/* argument later and must not be used as the lower bound for sp */ |
| 1536 |
/* check (since the stack may be bigger than 64 KiB). */ |
| 1537 |
# define GC_wince_evaluate_stack_min(s) \ |
| 1538 |
(ptr_t)(((word)(s) - 1) & ~(word)0xFFFF) |
| 1539 |
#elif defined(GC_ASSERTIONS) |
| 1540 |
# define GC_dont_query_stack_min FALSE |
| 1541 |
#endif |
| 1542 |
|
| 1543 |
/* A cache holding the results of the recent VirtualQuery call. */ |
| 1544 |
/* Protected by the allocation lock. */ |
| 1545 |
static ptr_t last_address = 0; |
| 1546 |
static MEMORY_BASIC_INFORMATION last_info; |
| 1547 |
|
| 1548 |
/* Probe stack memory region (starting at "s") to find out its */ |
| 1549 |
/* lowest address (i.e. stack top). */ |
| 1550 |
/* S must be a mapped address inside the region, NOT the first */ |
| 1551 |
/* unmapped address. */ |
| 1552 |
STATIC ptr_t GC_get_stack_min(ptr_t s) |
| 1553 |
{ |
| 1554 |
ptr_t bottom; |
| 1555 |
|
| 1556 |
GC_ASSERT(I_HOLD_LOCK()); |
| 1557 |
if (s != last_address) { |
| 1558 |
VirtualQuery(s, &last_info, sizeof(last_info)); |
| 1559 |
last_address = s; |
| 1560 |
} |
| 1561 |
do { |
| 1562 |
bottom = (ptr_t)last_info.BaseAddress; |
| 1563 |
VirtualQuery(bottom - 1, &last_info, sizeof(last_info)); |
| 1564 |
last_address = bottom - 1; |
| 1565 |
} while ((last_info.Protect & PAGE_READWRITE) |
| 1566 |
&& !(last_info.Protect & PAGE_GUARD)); |
| 1567 |
return(bottom); |
| 1568 |
} |
| 1569 |
|
| 1570 |
/* Return true if the page at s has protections appropriate */ |
| 1571 |
/* for a stack page. */ |
| 1572 |
static GC_bool may_be_in_stack(ptr_t s) |
| 1573 |
{ |
| 1574 |
GC_ASSERT(I_HOLD_LOCK()); |
| 1575 |
if (s != last_address) { |
| 1576 |
VirtualQuery(s, &last_info, sizeof(last_info)); |
| 1577 |
last_address = s; |
| 1578 |
} |
| 1579 |
return (last_info.Protect & PAGE_READWRITE) |
| 1580 |
&& !(last_info.Protect & PAGE_GUARD); |
| 1581 |
} |
| 1582 |
|
| 1583 |
/* Copy all registers that might point into the heap. Frame */ |
| 1584 |
/* pointer registers are included in case client code was */ |
| 1585 |
/* compiled with the 'omit frame pointer' optimization. */ |
| 1586 |
/* The context register values are stored to regs argument */ |
| 1587 |
/* which is expected to be of PUSHED_REGS_COUNT length exactly. */ |
| 1588 |
/* The functions returns the context stack pointer value. */ |
| 1589 |
static ptr_t copy_ptr_regs(word *regs, const CONTEXT *pcontext) { |
| 1590 |
ptr_t sp; |
| 1591 |
int cnt = 0; |
| 1592 |
# define context (*pcontext) |
| 1593 |
# define PUSH1(reg) (regs[cnt++] = (word)pcontext->reg) |
| 1594 |
# define PUSH2(r1,r2) (PUSH1(r1), PUSH1(r2)) |
| 1595 |
# define PUSH4(r1,r2,r3,r4) (PUSH2(r1,r2), PUSH2(r3,r4)) |
| 1596 |
# define PUSH8_LH(r1,r2,r3,r4) (PUSH4(r1.Low,r1.High,r2.Low,r2.High), \ |
| 1597 |
PUSH4(r3.Low,r3.High,r4.Low,r4.High)) |
| 1598 |
# if defined(I386) |
| 1599 |
# ifdef WOW64_THREAD_CONTEXT_WORKAROUND |
| 1600 |
PUSH2(ContextFlags, SegFs); /* cannot contain pointers */ |
| 1601 |
# endif |
| 1602 |
PUSH4(Edi,Esi,Ebx,Edx), PUSH2(Ecx,Eax), PUSH1(Ebp); |
| 1603 |
sp = (ptr_t)context.Esp; |
| 1604 |
# elif defined(X86_64) |
| 1605 |
PUSH4(Rax,Rcx,Rdx,Rbx); PUSH2(Rbp, Rsi); PUSH1(Rdi); |
| 1606 |
PUSH4(R8, R9, R10, R11); PUSH4(R12, R13, R14, R15); |
| 1607 |
# ifndef XMM_CANT_STORE_PTRS |
| 1608 |
PUSH8_LH(Xmm0, Xmm1, Xmm2, Xmm3); |
| 1609 |
PUSH8_LH(Xmm4, Xmm5, Xmm6, Xmm7); |
| 1610 |
PUSH8_LH(Xmm8, Xmm9, Xmm10, Xmm11); |
| 1611 |
PUSH8_LH(Xmm12, Xmm13, Xmm14, Xmm15); |
| 1612 |
# endif |
| 1613 |
sp = (ptr_t)context.Rsp; |
| 1614 |
# elif defined(ARM32) |
| 1615 |
PUSH4(R0,R1,R2,R3),PUSH4(R4,R5,R6,R7),PUSH4(R8,R9,R10,R11); |
| 1616 |
PUSH1(R12); |
| 1617 |
sp = (ptr_t)context.Sp; |
| 1618 |
# elif defined(AARCH64) |
| 1619 |
PUSH4(X0,X1,X2,X3),PUSH4(X4,X5,X6,X7),PUSH4(X8,X9,X10,X11); |
| 1620 |
PUSH4(X12,X13,X14,X15),PUSH4(X16,X17,X18,X19),PUSH4(X20,X21,X22,X23); |
| 1621 |
PUSH4(X24,X25,X26,X27),PUSH1(X28); |
| 1622 |
PUSH1(Lr); |
| 1623 |
sp = (ptr_t)context.Sp; |
| 1624 |
# elif defined(SHx) |
| 1625 |
PUSH4(R0,R1,R2,R3), PUSH4(R4,R5,R6,R7), PUSH4(R8,R9,R10,R11); |
| 1626 |
PUSH2(R12,R13), PUSH1(R14); |
| 1627 |
sp = (ptr_t)context.R15; |
| 1628 |
# elif defined(MIPS) |
| 1629 |
PUSH4(IntAt,IntV0,IntV1,IntA0), PUSH4(IntA1,IntA2,IntA3,IntT0); |
| 1630 |
PUSH4(IntT1,IntT2,IntT3,IntT4), PUSH4(IntT5,IntT6,IntT7,IntS0); |
| 1631 |
PUSH4(IntS1,IntS2,IntS3,IntS4), PUSH4(IntS5,IntS6,IntS7,IntT8); |
| 1632 |
PUSH4(IntT9,IntK0,IntK1,IntS8); |
| 1633 |
sp = (ptr_t)context.IntSp; |
| 1634 |
# elif defined(PPC) |
| 1635 |
PUSH4(Gpr0, Gpr3, Gpr4, Gpr5), PUSH4(Gpr6, Gpr7, Gpr8, Gpr9); |
| 1636 |
PUSH4(Gpr10,Gpr11,Gpr12,Gpr14), PUSH4(Gpr15,Gpr16,Gpr17,Gpr18); |
| 1637 |
PUSH4(Gpr19,Gpr20,Gpr21,Gpr22), PUSH4(Gpr23,Gpr24,Gpr25,Gpr26); |
| 1638 |
PUSH4(Gpr27,Gpr28,Gpr29,Gpr30), PUSH1(Gpr31); |
| 1639 |
sp = (ptr_t)context.Gpr1; |
| 1640 |
# elif defined(ALPHA) |
| 1641 |
PUSH4(IntV0,IntT0,IntT1,IntT2), PUSH4(IntT3,IntT4,IntT5,IntT6); |
| 1642 |
PUSH4(IntT7,IntS0,IntS1,IntS2), PUSH4(IntS3,IntS4,IntS5,IntFp); |
| 1643 |
PUSH4(IntA0,IntA1,IntA2,IntA3), PUSH4(IntA4,IntA5,IntT8,IntT9); |
| 1644 |
PUSH4(IntT10,IntT11,IntT12,IntAt); |
| 1645 |
sp = (ptr_t)context.IntSp; |
| 1646 |
# elif defined(CPPCHECK) |
| 1647 |
sp = (ptr_t)(word)cnt; /* to workaround "cnt not used" false positive */ |
| 1648 |
# else |
| 1649 |
# error Architecture is not supported |
| 1650 |
# endif |
| 1651 |
# undef context |
| 1652 |
GC_ASSERT(cnt == PUSHED_REGS_COUNT); |
| 1653 |
return sp; |
| 1654 |
} |
| 1655 |
|
| 1656 |
STATIC word GC_push_stack_for(GC_thread thread, DWORD me) |
| 1657 |
{ |
| 1658 |
ptr_t sp, stack_min; |
| 1659 |
|
| 1660 |
struct GC_traced_stack_sect_s *traced_stack_sect = |
| 1661 |
thread -> traced_stack_sect; |
| 1662 |
if (thread -> id == me) { |
| 1663 |
GC_ASSERT(thread -> thread_blocked_sp == NULL); |
| 1664 |
sp = GC_approx_sp(); |
| 1665 |
} else if ((sp = thread -> thread_blocked_sp) == NULL) { |
| 1666 |
/* Use saved sp value for blocked threads. */ |
| 1667 |
# ifdef RETRY_GET_THREAD_CONTEXT |
| 1668 |
/* We cache context when suspending the thread since it may */ |
| 1669 |
/* require looping. */ |
| 1670 |
word *regs = thread->context_regs; |
| 1671 |
|
| 1672 |
if (thread->suspended) { |
| 1673 |
sp = thread->context_sp; |
| 1674 |
} else |
| 1675 |
# else |
| 1676 |
word regs[PUSHED_REGS_COUNT]; |
| 1677 |
# endif |
| 1678 |
|
| 1679 |
/* else */ { |
| 1680 |
CONTEXT context; |
| 1681 |
|
| 1682 |
/* For unblocked threads call GetThreadContext(). */ |
| 1683 |
context.ContextFlags = GET_THREAD_CONTEXT_FLAGS; |
| 1684 |
if (GetThreadContext(THREAD_HANDLE(thread), &context)) { |
| 1685 |
sp = copy_ptr_regs(regs, &context); |
| 1686 |
} else { |
| 1687 |
# ifdef RETRY_GET_THREAD_CONTEXT |
| 1688 |
/* At least, try to use the stale context if saved. */ |
| 1689 |
sp = thread->context_sp; |
| 1690 |
if (NULL == sp) { |
| 1691 |
/* Skip the current thread, anyway its stack will */ |
| 1692 |
/* be pushed when the world is stopped. */ |
| 1693 |
return 0; |
| 1694 |
} |
| 1695 |
# else |
| 1696 |
ABORT("GetThreadContext failed"); |
| 1697 |
# endif |
| 1698 |
} |
| 1699 |
} |
| 1700 |
# ifdef THREAD_LOCAL_ALLOC |
| 1701 |
GC_ASSERT(thread->suspended || !GC_world_stopped); |
| 1702 |
# endif |
| 1703 |
|
| 1704 |
# ifndef WOW64_THREAD_CONTEXT_WORKAROUND |
| 1705 |
GC_push_many_regs(regs, PUSHED_REGS_COUNT); |
| 1706 |
# else |
| 1707 |
GC_push_many_regs(regs + 2, PUSHED_REGS_COUNT - 2); |
| 1708 |
/* skip ContextFlags and SegFs */ |
| 1709 |
|
| 1710 |
/* WoW64 workaround. */ |
| 1711 |
if (isWow64) { |
| 1712 |
DWORD ContextFlags = (DWORD)regs[0]; |
| 1713 |
|
| 1714 |
if ((ContextFlags & CONTEXT_EXCEPTION_REPORTING) != 0 |
| 1715 |
&& (ContextFlags & (CONTEXT_EXCEPTION_ACTIVE |
| 1716 |
/* | CONTEXT_SERVICE_ACTIVE */)) != 0) { |
| 1717 |
PNT_TIB tib; |
| 1718 |
|
| 1719 |
# ifdef MSWINRT_FLAVOR |
| 1720 |
tib = thread -> tib; |
| 1721 |
# else |
| 1722 |
WORD SegFs = (WORD)regs[1]; |
| 1723 |
LDT_ENTRY selector; |
| 1724 |
|
| 1725 |
if (!GetThreadSelectorEntry(THREAD_HANDLE(thread), SegFs, |
| 1726 |
&selector)) |
| 1727 |
ABORT("GetThreadSelectorEntry failed"); |
| 1728 |
tib = (PNT_TIB)(selector.BaseLow |
| 1729 |
| (selector.HighWord.Bits.BaseMid << 16) |
| 1730 |
| (selector.HighWord.Bits.BaseHi << 24)); |
| 1731 |
# endif |
| 1732 |
# ifdef DEBUG_THREADS |
| 1733 |
GC_log_printf("TIB stack limit/base: %p .. %p\n", |
| 1734 |
(void *)tib->StackLimit, (void *)tib->StackBase); |
| 1735 |
# endif |
| 1736 |
GC_ASSERT(!((word)thread->stack_base |
| 1737 |
COOLER_THAN (word)tib->StackBase)); |
| 1738 |
if (thread->stack_base != thread->initial_stack_base |
| 1739 |
/* We are in a coroutine. */ |
| 1740 |
&& ((word)thread->stack_base <= (word)tib->StackLimit |
| 1741 |
|| (word)tib->StackBase < (word)thread->stack_base)) { |
| 1742 |
/* The coroutine stack is not within TIB stack. */ |
| 1743 |
WARN("GetThreadContext might return stale register values" |
| 1744 |
" including ESP= %p\n", sp); |
| 1745 |
/* TODO: Because of WoW64 bug, there is no guarantee that */ |
| 1746 |
/* sp really points to the stack top but, for now, we do */ |
| 1747 |
/* our best as the TIB stack limit/base cannot be used */ |
| 1748 |
/* while we are inside a coroutine. */ |
| 1749 |
} else { |
| 1750 |
/* GetThreadContext() might return stale register values, */ |
| 1751 |
/* so we scan the entire stack region (down to the stack */ |
| 1752 |
/* limit). There is no 100% guarantee that all the */ |
| 1753 |
/* registers are pushed but we do our best (the proper */ |
| 1754 |
/* solution would be to fix it inside Windows). */ |
| 1755 |
sp = (ptr_t)tib->StackLimit; |
| 1756 |
} |
| 1757 |
} /* else */ |
| 1758 |
# ifdef DEBUG_THREADS |
| 1759 |
else { |
| 1760 |
static GC_bool logged; |
| 1761 |
if (!logged |
| 1762 |
&& (ContextFlags & CONTEXT_EXCEPTION_REPORTING) == 0) { |
| 1763 |
GC_log_printf("CONTEXT_EXCEPTION_REQUEST not supported\n"); |
| 1764 |
logged = TRUE; |
| 1765 |
} |
| 1766 |
} |
| 1767 |
# endif |
| 1768 |
} |
| 1769 |
# endif /* WOW64_THREAD_CONTEXT_WORKAROUND */ |
| 1770 |
} /* ! current thread */ |
| 1771 |
# ifdef STACKPTR_CORRECTOR_AVAILABLE |
| 1772 |
if (GC_sp_corrector != 0) |
| 1773 |
GC_sp_corrector((void **)&sp, (void *)(thread -> pthread_id)); |
| 1774 |
# endif |
| 1775 |
|
| 1776 |
/* Set stack_min to the lowest address in the thread stack, */ |
| 1777 |
/* or to an address in the thread stack no larger than sp, */ |
| 1778 |
/* taking advantage of the old value to avoid slow traversals */ |
| 1779 |
/* of large stacks. */ |
| 1780 |
if (thread -> last_stack_min == ADDR_LIMIT) { |
| 1781 |
# ifdef MSWINCE |
| 1782 |
if (GC_dont_query_stack_min) { |
| 1783 |
stack_min = GC_wince_evaluate_stack_min(traced_stack_sect != NULL ? |
| 1784 |
(ptr_t)traced_stack_sect : thread -> stack_base); |
| 1785 |
/* Keep last_stack_min value unmodified. */ |
| 1786 |
} else |
| 1787 |
# endif |
| 1788 |
/* else */ { |
| 1789 |
stack_min = GC_get_stack_min(traced_stack_sect != NULL ? |
| 1790 |
(ptr_t)traced_stack_sect : thread -> stack_base); |
| 1791 |
UNPROTECT_THREAD(thread); |
| 1792 |
thread -> last_stack_min = stack_min; |
| 1793 |
} |
| 1794 |
} else { |
| 1795 |
/* First, adjust the latest known minimum stack address if we */ |
| 1796 |
/* are inside GC_call_with_gc_active(). */ |
| 1797 |
if (traced_stack_sect != NULL && |
| 1798 |
(word)thread->last_stack_min > (word)traced_stack_sect) { |
| 1799 |
UNPROTECT_THREAD(thread); |
| 1800 |
thread -> last_stack_min = (ptr_t)traced_stack_sect; |
| 1801 |
} |
| 1802 |
|
| 1803 |
if ((word)sp < (word)thread->stack_base |
| 1804 |
&& (word)sp >= (word)thread->last_stack_min) { |
| 1805 |
stack_min = sp; |
| 1806 |
} else { |
| 1807 |
/* In the current thread it is always safe to use sp value. */ |
| 1808 |
if (may_be_in_stack(thread -> id == me && |
| 1809 |
(word)sp < (word)thread->last_stack_min ? |
| 1810 |
sp : thread -> last_stack_min)) { |
| 1811 |
stack_min = (ptr_t)last_info.BaseAddress; |
| 1812 |
/* Do not probe rest of the stack if sp is correct. */ |
| 1813 |
if ((word)sp < (word)stack_min |
| 1814 |
|| (word)sp >= (word)thread->stack_base) |
| 1815 |
stack_min = GC_get_stack_min(thread -> last_stack_min); |
| 1816 |
} else { |
| 1817 |
/* Stack shrunk? Is this possible? */ |
| 1818 |
stack_min = GC_get_stack_min(thread -> stack_base); |
| 1819 |
} |
| 1820 |
UNPROTECT_THREAD(thread); |
| 1821 |
thread -> last_stack_min = stack_min; |
| 1822 |
} |
| 1823 |
} |
| 1824 |
|
| 1825 |
GC_ASSERT(GC_dont_query_stack_min |
| 1826 |
|| stack_min == GC_get_stack_min(thread -> stack_base) |
| 1827 |
|| ((word)sp >= (word)stack_min |
| 1828 |
&& (word)stack_min < (word)thread->stack_base |
| 1829 |
&& (word)stack_min |
| 1830 |
> (word)GC_get_stack_min(thread -> stack_base))); |
| 1831 |
|
| 1832 |
if ((word)sp >= (word)stack_min && (word)sp < (word)thread->stack_base) { |
| 1833 |
# ifdef DEBUG_THREADS |
| 1834 |
GC_log_printf("Pushing stack for 0x%x from sp %p to %p from 0x%x\n", |
| 1835 |
(int)thread->id, (void *)sp, (void *)thread->stack_base, |
| 1836 |
(int)me); |
| 1837 |
# endif |
| 1838 |
GC_push_all_stack_sections(sp, thread->stack_base, traced_stack_sect); |
| 1839 |
} else { |
| 1840 |
/* If not current thread then it is possible for sp to point to */ |
| 1841 |
/* the guarded (untouched yet) page just below the current */ |
| 1842 |
/* stack_min of the thread. */ |
| 1843 |
if (thread -> id == me || (word)sp >= (word)thread->stack_base |
| 1844 |
|| (word)(sp + GC_page_size) < (word)stack_min) |
| 1845 |
WARN("Thread stack pointer %p out of range, pushing everything\n", |
| 1846 |
sp); |
| 1847 |
# ifdef DEBUG_THREADS |
| 1848 |
GC_log_printf("Pushing stack for 0x%x from (min) %p to %p from 0x%x\n", |
| 1849 |
(int)thread->id, (void *)stack_min, |
| 1850 |
(void *)thread->stack_base, (int)me); |
| 1851 |
# endif |
| 1852 |
/* Push everything - ignore "traced stack section" data. */ |
| 1853 |
GC_push_all_stack(stack_min, thread->stack_base); |
| 1854 |
} |
| 1855 |
return thread->stack_base - sp; /* stack grows down */ |
| 1856 |
} |
| 1857 |
|
| 1858 |
/* We hold allocation lock. Should do exactly the right thing if the */ |
| 1859 |
/* world is stopped. Should not fail if it isn't. */ |
| 1860 |
GC_INNER void GC_push_all_stacks(void) |
| 1861 |
{ |
| 1862 |
DWORD thread_id = GetCurrentThreadId(); |
| 1863 |
GC_bool found_me = FALSE; |
| 1864 |
# ifndef SMALL_CONFIG |
| 1865 |
unsigned nthreads = 0; |
| 1866 |
# endif |
| 1867 |
word total_size = 0; |
| 1868 |
# ifndef GC_NO_THREADS_DISCOVERY |
| 1869 |
if (GC_win32_dll_threads) { |
| 1870 |
int i; |
| 1871 |
LONG my_max = GC_get_max_thread_index(); |
| 1872 |
|
| 1873 |
for (i = 0; i <= my_max; i++) { |
| 1874 |
GC_thread t = (GC_thread)(dll_thread_table + i); |
| 1875 |
if (t -> tm.in_use && t -> stack_base) { |
| 1876 |
# ifndef SMALL_CONFIG |
| 1877 |
++nthreads; |
| 1878 |
# endif |
| 1879 |
total_size += GC_push_stack_for(t, thread_id); |
| 1880 |
if (t -> id == thread_id) found_me = TRUE; |
| 1881 |
} |
| 1882 |
} |
| 1883 |
} else |
| 1884 |
# endif |
| 1885 |
/* else */ { |
| 1886 |
int i; |
| 1887 |
for (i = 0; i < THREAD_TABLE_SZ; i++) { |
| 1888 |
GC_thread t; |
| 1889 |
for (t = GC_threads[i]; t != 0; t = t -> tm.next) { |
| 1890 |
if (!KNOWN_FINISHED(t) && t -> stack_base) { |
| 1891 |
# ifndef SMALL_CONFIG |
| 1892 |
++nthreads; |
| 1893 |
# endif |
| 1894 |
total_size += GC_push_stack_for(t, thread_id); |
| 1895 |
if (t -> id == thread_id) found_me = TRUE; |
| 1896 |
} |
| 1897 |
} |
| 1898 |
} |
| 1899 |
} |
| 1900 |
# ifndef SMALL_CONFIG |
| 1901 |
GC_VERBOSE_LOG_PRINTF("Pushed %d thread stacks%s\n", nthreads, |
| 1902 |
GC_win32_dll_threads ? |
| 1903 |
" based on DllMain thread tracking" : ""); |
| 1904 |
# endif |
| 1905 |
if (!found_me && !GC_in_thread_creation) |
| 1906 |
ABORT("Collecting from unknown thread"); |
| 1907 |
GC_total_stacksize = total_size; |
| 1908 |
} |
| 1909 |
|
| 1910 |
#ifdef PARALLEL_MARK |
| 1911 |
|
| 1912 |
# ifndef MAX_MARKERS |
| 1913 |
# define MAX_MARKERS 16 |
| 1914 |
# endif |
| 1915 |
|
| 1916 |
static ptr_t marker_sp[MAX_MARKERS - 1]; /* The cold end of the stack */ |
| 1917 |
/* for markers. */ |
| 1918 |
# ifdef IA64 |
| 1919 |
static ptr_t marker_bsp[MAX_MARKERS - 1]; |
| 1920 |
# endif |
| 1921 |
|
| 1922 |
static ptr_t marker_last_stack_min[MAX_MARKERS - 1]; |
| 1923 |
/* Last known minimum (hottest) address */ |
| 1924 |
/* in stack (or ADDR_LIMIT if unset) */ |
| 1925 |
/* for markers. */ |
| 1926 |
|
| 1927 |
#endif /* PARALLEL_MARK */ |
| 1928 |
|
| 1929 |
/* Find stack with the lowest address which overlaps the */ |
| 1930 |
/* interval [start, limit). */ |
| 1931 |
/* Return stack bounds in *lo and *hi. If no such stack */ |
| 1932 |
/* is found, both *hi and *lo will be set to an address */ |
| 1933 |
/* higher than limit. */ |
| 1934 |
GC_INNER void GC_get_next_stack(char *start, char *limit, |
| 1935 |
char **lo, char **hi) |
| 1936 |
{ |
| 1937 |
int i; |
| 1938 |
char * current_min = ADDR_LIMIT; /* Least in-range stack base */ |
| 1939 |
ptr_t *plast_stack_min = NULL; /* Address of last_stack_min */ |
| 1940 |
/* field for thread corresponding */ |
| 1941 |
/* to current_min. */ |
| 1942 |
GC_thread thread = NULL; /* Either NULL or points to the */ |
| 1943 |
/* thread's hash table entry */ |
| 1944 |
/* containing *plast_stack_min. */ |
| 1945 |
|
| 1946 |
/* First set current_min, ignoring limit. */ |
| 1947 |
if (GC_win32_dll_threads) { |
| 1948 |
LONG my_max = GC_get_max_thread_index(); |
| 1949 |
|
| 1950 |
for (i = 0; i <= my_max; i++) { |
| 1951 |
ptr_t s = (ptr_t)(dll_thread_table[i].stack_base); |
| 1952 |
|
| 1953 |
if ((word)s > (word)start && (word)s < (word)current_min) { |
| 1954 |
/* Update address of last_stack_min. */ |
| 1955 |
plast_stack_min = (ptr_t * /* no volatile */) |
| 1956 |
&dll_thread_table[i].last_stack_min; |
| 1957 |
current_min = s; |
| 1958 |
# if defined(CPPCHECK) |
| 1959 |
/* To avoid a warning that thread is always null. */ |
| 1960 |
thread = (GC_thread)&dll_thread_table[i]; |
| 1961 |
# endif |
| 1962 |
} |
| 1963 |
} |
| 1964 |
} else { |
| 1965 |
for (i = 0; i < THREAD_TABLE_SZ; i++) { |
| 1966 |
GC_thread t; |
| 1967 |
|
| 1968 |
for (t = GC_threads[i]; t != 0; t = t -> tm.next) { |
| 1969 |
ptr_t s = t -> stack_base; |
| 1970 |
|
| 1971 |
if ((word)s > (word)start && (word)s < (word)current_min) { |
| 1972 |
/* Update address of last_stack_min. */ |
| 1973 |
plast_stack_min = &t -> last_stack_min; |
| 1974 |
thread = t; /* Remember current thread to unprotect. */ |
| 1975 |
current_min = s; |
| 1976 |
} |
| 1977 |
} |
| 1978 |
} |
| 1979 |
# ifdef PARALLEL_MARK |
| 1980 |
for (i = 0; i < GC_markers_m1; ++i) { |
| 1981 |
ptr_t s = marker_sp[i]; |
| 1982 |
# ifdef IA64 |
| 1983 |
/* FIXME: not implemented */ |
| 1984 |
# endif |
| 1985 |
if ((word)s > (word)start && (word)s < (word)current_min) { |
| 1986 |
GC_ASSERT(marker_last_stack_min[i] != NULL); |
| 1987 |
plast_stack_min = &marker_last_stack_min[i]; |
| 1988 |
current_min = s; |
| 1989 |
thread = NULL; /* Not a thread's hash table entry. */ |
| 1990 |
} |
| 1991 |
} |
| 1992 |
# endif |
| 1993 |
} |
| 1994 |
|
| 1995 |
*hi = current_min; |
| 1996 |
if (current_min == ADDR_LIMIT) { |
| 1997 |
*lo = ADDR_LIMIT; |
| 1998 |
return; |
| 1999 |
} |
| 2000 |
|
| 2001 |
GC_ASSERT((word)current_min > (word)start && plast_stack_min != NULL); |
| 2002 |
# ifdef MSWINCE |
| 2003 |
if (GC_dont_query_stack_min) { |
| 2004 |
*lo = GC_wince_evaluate_stack_min(current_min); |
| 2005 |
/* Keep last_stack_min value unmodified. */ |
| 2006 |
return; |
| 2007 |
} |
| 2008 |
# endif |
| 2009 |
|
| 2010 |
if ((word)current_min > (word)limit && !may_be_in_stack(limit)) { |
| 2011 |
/* Skip the rest since the memory region at limit address is */ |
| 2012 |
/* not a stack (so the lowest address of the found stack would */ |
| 2013 |
/* be above the limit value anyway). */ |
| 2014 |
*lo = ADDR_LIMIT; |
| 2015 |
return; |
| 2016 |
} |
| 2017 |
|
| 2018 |
/* Get the minimum address of the found stack by probing its memory */ |
| 2019 |
/* region starting from the recent known minimum (if set). */ |
| 2020 |
if (*plast_stack_min == ADDR_LIMIT |
| 2021 |
|| !may_be_in_stack(*plast_stack_min)) { |
| 2022 |
/* Unsafe to start from last_stack_min value. */ |
| 2023 |
*lo = GC_get_stack_min(current_min); |
| 2024 |
} else { |
| 2025 |
/* Use the recent value to optimize search for min address. */ |
| 2026 |
*lo = GC_get_stack_min(*plast_stack_min); |
| 2027 |
} |
| 2028 |
|
| 2029 |
/* Remember current stack_min value. */ |
| 2030 |
if (thread != NULL) { |
| 2031 |
UNPROTECT_THREAD(thread); |
| 2032 |
} |
| 2033 |
*plast_stack_min = *lo; |
| 2034 |
} |
| 2035 |
|
| 2036 |
#ifdef PARALLEL_MARK |
| 2037 |
|
| 2038 |
# if !defined(GC_PTHREADS_PARAMARK) |
| 2039 |
STATIC HANDLE GC_marker_cv[MAX_MARKERS - 1] = {0}; |
| 2040 |
/* Events with manual reset (one for each */ |
| 2041 |
/* mark helper). */ |
| 2042 |
|
| 2043 |
STATIC DWORD GC_marker_Id[MAX_MARKERS - 1] = {0}; |
| 2044 |
/* This table is used for mapping helper */ |
| 2045 |
/* threads ID to mark helper index (linear */ |
| 2046 |
/* search is used since the mapping contains */ |
| 2047 |
/* only a few entries). */ |
| 2048 |
# endif |
| 2049 |
|
| 2050 |
# if defined(GC_PTHREADS) && defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) |
| 2051 |
static void set_marker_thread_name(unsigned id) |
| 2052 |
{ |
| 2053 |
/* This code is the same as in pthread_support.c. */ |
| 2054 |
char name_buf[16]; /* pthread_setname_np may fail for longer names */ |
| 2055 |
int len = sizeof("GC-marker-") - 1; |
| 2056 |
|
| 2057 |
/* Compose the name manually as snprintf may be unavailable or */ |
| 2058 |
/* "%u directive output may be truncated" warning may occur. */ |
| 2059 |
BCOPY("GC-marker-", name_buf, len); |
| 2060 |
if (id >= 10) |
| 2061 |
name_buf[len++] = (char)('0' + (id / 10) % 10); |
| 2062 |
name_buf[len] = (char)('0' + id % 10); |
| 2063 |
name_buf[len + 1] = '\0'; |
| 2064 |
|
| 2065 |
if (pthread_setname_np(pthread_self(), name_buf) != 0) |
| 2066 |
WARN("pthread_setname_np failed\n", 0); |
| 2067 |
} |
| 2068 |
|
| 2069 |
# elif !defined(MSWINCE) |
| 2070 |
/* A pointer to SetThreadDescription() which is available since */ |
| 2071 |
/* Windows 10. The function prototype is in processthreadsapi.h. */ |
| 2072 |
static FARPROC setThreadDescription_fn; |
| 2073 |
|
| 2074 |
static void set_marker_thread_name(unsigned id) |
| 2075 |
{ |
| 2076 |
WCHAR name_buf[16]; |
| 2077 |
int len = sizeof(L"GC-marker-") / sizeof(WCHAR) - 1; |
| 2078 |
HRESULT hr; |
| 2079 |
|
| 2080 |
if (!setThreadDescription_fn) return; /* missing SetThreadDescription */ |
| 2081 |
|
| 2082 |
/* Compose the name manually as swprintf may be unavailable. */ |
| 2083 |
BCOPY(L"GC-marker-", name_buf, len * sizeof(WCHAR)); |
| 2084 |
if (id >= 10) |
| 2085 |
name_buf[len++] = (WCHAR)('0' + (id / 10) % 10); |
| 2086 |
name_buf[len] = (WCHAR)('0' + id % 10); |
| 2087 |
name_buf[len + 1] = 0; |
| 2088 |
|
| 2089 |
/* Invoke SetThreadDescription(). Cast the function pointer to word */ |
| 2090 |
/* first to avoid "incompatible function types" compiler warning. */ |
| 2091 |
hr = (*(HRESULT (WINAPI *)(HANDLE, const WCHAR *)) |
| 2092 |
(word)setThreadDescription_fn)(GetCurrentThread(), name_buf); |
| 2093 |
if (FAILED(hr)) |
| 2094 |
WARN("SetThreadDescription failed\n", 0); |
| 2095 |
} |
| 2096 |
# else |
| 2097 |
# define set_marker_thread_name(id) (void)(id) |
| 2098 |
# endif |
| 2099 |
|
| 2100 |
/* GC_mark_thread() is the same as in pthread_support.c */ |
| 2101 |
# ifdef GC_PTHREADS_PARAMARK |
| 2102 |
STATIC void * GC_mark_thread(void * id) |
| 2103 |
# elif defined(MSWINCE) |
| 2104 |
STATIC DWORD WINAPI GC_mark_thread(LPVOID id) |
| 2105 |
# else |
| 2106 |
STATIC unsigned __stdcall GC_mark_thread(void * id) |
| 2107 |
# endif |
| 2108 |
{ |
| 2109 |
word my_mark_no = 0; |
| 2110 |
|
| 2111 |
if ((word)id == GC_WORD_MAX) return 0; /* to prevent a compiler warning */ |
| 2112 |
set_marker_thread_name((unsigned)(word)id); |
| 2113 |
marker_sp[(word)id] = GC_approx_sp(); |
| 2114 |
# ifdef IA64 |
| 2115 |
marker_bsp[(word)id] = GC_save_regs_in_stack(); |
| 2116 |
# endif |
| 2117 |
# if !defined(GC_PTHREADS_PARAMARK) |
| 2118 |
GC_marker_Id[(word)id] = GetCurrentThreadId(); |
| 2119 |
# endif |
| 2120 |
|
| 2121 |
/* Inform GC_start_mark_threads about completion of marker data init. */ |
| 2122 |
GC_acquire_mark_lock(); |
| 2123 |
if (0 == --GC_fl_builder_count) /* count may have a negative value */ |
| 2124 |
GC_notify_all_builder(); |
| 2125 |
|
| 2126 |
for (;; ++my_mark_no) { |
| 2127 |
if (my_mark_no - GC_mark_no > (word)2) { |
| 2128 |
/* resynchronize if we get far off, e.g. because GC_mark_no */ |
| 2129 |
/* wrapped. */ |
| 2130 |
my_mark_no = GC_mark_no; |
| 2131 |
} |
| 2132 |
# ifdef DEBUG_THREADS |
| 2133 |
GC_log_printf("Starting mark helper for mark number %lu\n", |
| 2134 |
(unsigned long)my_mark_no); |
| 2135 |
# endif |
| 2136 |
GC_help_marker(my_mark_no); |
| 2137 |
} |
| 2138 |
} |
| 2139 |
|
| 2140 |
# ifndef GC_ASSERTIONS |
| 2141 |
# define SET_MARK_LOCK_HOLDER (void)0 |
| 2142 |
# define UNSET_MARK_LOCK_HOLDER (void)0 |
| 2143 |
# endif |
| 2144 |
|
| 2145 |
/* GC_mark_threads[] is unused here unlike that in pthread_support.c */ |
| 2146 |
|
| 2147 |
static int available_markers_m1 = 0; |
| 2148 |
|
| 2149 |
# ifdef GC_PTHREADS_PARAMARK |
| 2150 |
# include <pthread.h> |
| 2151 |
|
| 2152 |
# if defined(GC_ASSERTIONS) && !defined(NUMERIC_THREAD_ID) |
| 2153 |
# define NUMERIC_THREAD_ID(id) (unsigned long)(word)GC_PTHREAD_PTRVAL(id) |
| 2154 |
/* Id not guaranteed to be unique. */ |
| 2155 |
# endif |
| 2156 |
|
| 2157 |
# ifdef CAN_HANDLE_FORK |
| 2158 |
static pthread_cond_t mark_cv; |
| 2159 |
/* initialized by GC_start_mark_threads_inner */ |
| 2160 |
# else |
| 2161 |
static pthread_cond_t mark_cv = PTHREAD_COND_INITIALIZER; |
| 2162 |
# endif |
| 2163 |
|
| 2164 |
/* GC_start_mark_threads is the same as in pthread_support.c except */ |
| 2165 |
/* for thread stack that is assumed to be large enough. */ |
| 2166 |
|
| 2167 |
GC_INNER void GC_start_mark_threads_inner(void) |
| 2168 |
{ |
| 2169 |
int i; |
| 2170 |
pthread_attr_t attr; |
| 2171 |
pthread_t new_thread; |
| 2172 |
# ifndef NO_MARKER_SPECIAL_SIGMASK |
| 2173 |
sigset_t set, oldset; |
| 2174 |
# endif |
| 2175 |
|
| 2176 |
GC_ASSERT(I_HOLD_LOCK()); |
| 2177 |
ASSERT_CANCEL_DISABLED(); |
| 2178 |
if (available_markers_m1 <= 0 || GC_parallel) return; |
| 2179 |
/* Skip if parallel markers disabled or already started. */ |
| 2180 |
GC_wait_for_gc_completion(TRUE); |
| 2181 |
|
| 2182 |
# ifdef CAN_HANDLE_FORK |
| 2183 |
/* Reset mark_cv state after forking (as in pthread_support.c). */ |
| 2184 |
{ |
| 2185 |
pthread_cond_t mark_cv_local = PTHREAD_COND_INITIALIZER; |
| 2186 |
BCOPY(&mark_cv_local, &mark_cv, sizeof(mark_cv)); |
| 2187 |
} |
| 2188 |
# endif |
| 2189 |
|
| 2190 |
GC_ASSERT(GC_fl_builder_count == 0); |
| 2191 |
if (0 != pthread_attr_init(&attr)) ABORT("pthread_attr_init failed"); |
| 2192 |
if (0 != pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) |
| 2193 |
ABORT("pthread_attr_setdetachstate failed"); |
| 2194 |
|
| 2195 |
# ifndef NO_MARKER_SPECIAL_SIGMASK |
| 2196 |
/* Apply special signal mask to GC marker threads, and don't drop */ |
| 2197 |
/* user defined signals by GC marker threads. */ |
| 2198 |
if (sigfillset(&set) != 0) |
| 2199 |
ABORT("sigfillset failed"); |
| 2200 |
if (pthread_sigmask(SIG_BLOCK, &set, &oldset) < 0) { |
| 2201 |
WARN("pthread_sigmask set failed, no markers started\n", 0); |
| 2202 |
GC_markers_m1 = 0; |
| 2203 |
(void)pthread_attr_destroy(&attr); |
| 2204 |
return; |
| 2205 |
} |
| 2206 |
# endif /* !NO_MARKER_SPECIAL_SIGMASK */ |
| 2207 |
|
| 2208 |
/* To have proper GC_parallel value in GC_help_marker. */ |
| 2209 |
GC_markers_m1 = available_markers_m1; |
| 2210 |
|
| 2211 |
for (i = 0; i < available_markers_m1; ++i) { |
| 2212 |
marker_last_stack_min[i] = ADDR_LIMIT; |
| 2213 |
if (0 != pthread_create(&new_thread, &attr, |
| 2214 |
GC_mark_thread, (void *)(word)i)) { |
| 2215 |
WARN("Marker thread creation failed\n", 0); |
| 2216 |
/* Don't try to create other marker threads. */ |
| 2217 |
GC_markers_m1 = i; |
| 2218 |
break; |
| 2219 |
} |
| 2220 |
} |
| 2221 |
|
| 2222 |
# ifndef NO_MARKER_SPECIAL_SIGMASK |
| 2223 |
/* Restore previous signal mask. */ |
| 2224 |
if (pthread_sigmask(SIG_SETMASK, &oldset, NULL) < 0) { |
| 2225 |
WARN("pthread_sigmask restore failed\n", 0); |
| 2226 |
} |
| 2227 |
# endif |
| 2228 |
|
| 2229 |
(void)pthread_attr_destroy(&attr); |
| 2230 |
GC_wait_for_markers_init(); |
| 2231 |
GC_COND_LOG_PRINTF("Started %d mark helper threads\n", GC_markers_m1); |
| 2232 |
} |
| 2233 |
|
| 2234 |
# ifdef GC_ASSERTIONS |
| 2235 |
STATIC unsigned long GC_mark_lock_holder = NO_THREAD; |
| 2236 |
# define SET_MARK_LOCK_HOLDER \ |
| 2237 |
(void)(GC_mark_lock_holder = NUMERIC_THREAD_ID(pthread_self())) |
| 2238 |
# define UNSET_MARK_LOCK_HOLDER \ |
| 2239 |
do { \ |
| 2240 |
GC_ASSERT(GC_mark_lock_holder \ |
| 2241 |
== NUMERIC_THREAD_ID(pthread_self())); \ |
| 2242 |
GC_mark_lock_holder = NO_THREAD; \ |
| 2243 |
} while (0) |
| 2244 |
# endif /* GC_ASSERTIONS */ |
| 2245 |
|
| 2246 |
static pthread_mutex_t mark_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 2247 |
|
| 2248 |
static pthread_cond_t builder_cv = PTHREAD_COND_INITIALIZER; |
| 2249 |
|
| 2250 |
/* GC_acquire/release_mark_lock(), GC_wait_builder/marker(), */ |
| 2251 |
/* GC_wait_for_reclaim(), GC_notify_all_builder/marker() are the same */ |
| 2252 |
/* as in pthread_support.c except that GC_generic_lock() is not used. */ |
| 2253 |
|
| 2254 |
# ifdef LOCK_STATS |
| 2255 |
volatile AO_t GC_block_count = 0; |
| 2256 |
# endif |
| 2257 |
|
| 2258 |
GC_INNER void GC_acquire_mark_lock(void) |
| 2259 |
{ |
| 2260 |
# ifdef NUMERIC_THREAD_ID_UNIQUE |
| 2261 |
GC_ASSERT(GC_mark_lock_holder != NUMERIC_THREAD_ID(pthread_self())); |
| 2262 |
# endif |
| 2263 |
if (pthread_mutex_lock(&mark_mutex) != 0) { |
| 2264 |
ABORT("pthread_mutex_lock failed"); |
| 2265 |
} |
| 2266 |
# ifdef LOCK_STATS |
| 2267 |
(void)AO_fetch_and_add1(&GC_block_count); |
| 2268 |
# endif |
| 2269 |
/* GC_generic_lock(&mark_mutex); */ |
| 2270 |
SET_MARK_LOCK_HOLDER; |
| 2271 |
} |
| 2272 |
|
| 2273 |
GC_INNER void GC_release_mark_lock(void) |
| 2274 |
{ |
| 2275 |
UNSET_MARK_LOCK_HOLDER; |
| 2276 |
if (pthread_mutex_unlock(&mark_mutex) != 0) { |
| 2277 |
ABORT("pthread_mutex_unlock failed"); |
| 2278 |
} |
| 2279 |
} |
| 2280 |
|
| 2281 |
/* Collector must wait for a freelist builders for 2 reasons: */ |
| 2282 |
/* 1) Mark bits may still be getting examined without lock. */ |
| 2283 |
/* 2) Partial free lists referenced only by locals may not be */ |
| 2284 |
/* scanned correctly, e.g. if they contain "pointer-free" objects, */ |
| 2285 |
/* since the free-list link may be ignored. */ |
| 2286 |
STATIC void GC_wait_builder(void) |
| 2287 |
{ |
| 2288 |
UNSET_MARK_LOCK_HOLDER; |
| 2289 |
if (pthread_cond_wait(&builder_cv, &mark_mutex) != 0) { |
| 2290 |
ABORT("pthread_cond_wait failed"); |
| 2291 |
} |
| 2292 |
GC_ASSERT(GC_mark_lock_holder == NO_THREAD); |
| 2293 |
SET_MARK_LOCK_HOLDER; |
| 2294 |
} |
| 2295 |
|
| 2296 |
GC_INNER void GC_wait_for_reclaim(void) |
| 2297 |
{ |
| 2298 |
GC_acquire_mark_lock(); |
| 2299 |
while (GC_fl_builder_count > 0) { |
| 2300 |
GC_wait_builder(); |
| 2301 |
} |
| 2302 |
GC_release_mark_lock(); |
| 2303 |
} |
| 2304 |
|
| 2305 |
GC_INNER void GC_notify_all_builder(void) |
| 2306 |
{ |
| 2307 |
GC_ASSERT(GC_mark_lock_holder == NUMERIC_THREAD_ID(pthread_self())); |
| 2308 |
if (pthread_cond_broadcast(&builder_cv) != 0) { |
| 2309 |
ABORT("pthread_cond_broadcast failed"); |
| 2310 |
} |
| 2311 |
} |
| 2312 |
|
| 2313 |
GC_INNER void GC_wait_marker(void) |
| 2314 |
{ |
| 2315 |
GC_ASSERT(GC_parallel); |
| 2316 |
UNSET_MARK_LOCK_HOLDER; |
| 2317 |
if (pthread_cond_wait(&mark_cv, &mark_mutex) != 0) { |
| 2318 |
ABORT("pthread_cond_wait failed"); |
| 2319 |
} |
| 2320 |
GC_ASSERT(GC_mark_lock_holder == NO_THREAD); |
| 2321 |
SET_MARK_LOCK_HOLDER; |
| 2322 |
} |
| 2323 |
|
| 2324 |
GC_INNER void GC_notify_all_marker(void) |
| 2325 |
{ |
| 2326 |
GC_ASSERT(GC_parallel); |
| 2327 |
if (pthread_cond_broadcast(&mark_cv) != 0) { |
| 2328 |
ABORT("pthread_cond_broadcast failed"); |
| 2329 |
} |
| 2330 |
} |
| 2331 |
|
| 2332 |
# else /* ! GC_PTHREADS_PARAMARK */ |
| 2333 |
|
| 2334 |
# ifndef MARK_THREAD_STACK_SIZE |
| 2335 |
# define MARK_THREAD_STACK_SIZE 0 /* default value */ |
| 2336 |
# endif |
| 2337 |
|
| 2338 |
/* mark_mutex_event, builder_cv, mark_cv are initialized in GC_thr_init */ |
| 2339 |
static HANDLE mark_mutex_event = (HANDLE)0; /* Event with auto-reset. */ |
| 2340 |
static HANDLE builder_cv = (HANDLE)0; /* Event with manual reset. */ |
| 2341 |
static HANDLE mark_cv = (HANDLE)0; /* Event with manual reset. */ |
| 2342 |
|
| 2343 |
GC_INNER void GC_start_mark_threads_inner(void) |
| 2344 |
{ |
| 2345 |
int i; |
| 2346 |
|
| 2347 |
GC_ASSERT(I_HOLD_LOCK()); |
| 2348 |
ASSERT_CANCEL_DISABLED(); |
| 2349 |
if (available_markers_m1 <= 0 || GC_parallel) return; |
| 2350 |
GC_wait_for_gc_completion(TRUE); |
| 2351 |
|
| 2352 |
GC_ASSERT(GC_fl_builder_count == 0); |
| 2353 |
/* Initialize GC_marker_cv[] fully before starting the */ |
| 2354 |
/* first helper thread. */ |
| 2355 |
GC_markers_m1 = available_markers_m1; |
| 2356 |
for (i = 0; i < GC_markers_m1; ++i) { |
| 2357 |
if ((GC_marker_cv[i] = CreateEvent(NULL /* attrs */, |
| 2358 |
TRUE /* isManualReset */, |
| 2359 |
FALSE /* initialState */, |
| 2360 |
NULL /* name (A/W) */)) == (HANDLE)0) |
| 2361 |
ABORT("CreateEvent failed"); |
| 2362 |
} |
| 2363 |
|
| 2364 |
for (i = 0; i < GC_markers_m1; ++i) { |
| 2365 |
# if defined(MSWINCE) || defined(MSWIN_XBOX1) |
| 2366 |
HANDLE handle; |
| 2367 |
DWORD thread_id; |
| 2368 |
|
| 2369 |
marker_last_stack_min[i] = ADDR_LIMIT; |
| 2370 |
/* There is no _beginthreadex() in WinCE. */ |
| 2371 |
handle = CreateThread(NULL /* lpsa */, |
| 2372 |
MARK_THREAD_STACK_SIZE /* ignored */, |
| 2373 |
GC_mark_thread, (LPVOID)(word)i, |
| 2374 |
0 /* fdwCreate */, &thread_id); |
| 2375 |
if (handle == NULL) { |
| 2376 |
WARN("Marker thread creation failed\n", 0); |
| 2377 |
/* The most probable failure reason is "not enough memory". */ |
| 2378 |
/* Don't try to create other marker threads. */ |
| 2379 |
break; |
| 2380 |
} else { |
| 2381 |
/* It's safe to detach the thread. */ |
| 2382 |
CloseHandle(handle); |
| 2383 |
} |
| 2384 |
# else |
| 2385 |
GC_uintptr_t handle; |
| 2386 |
unsigned thread_id; |
| 2387 |
|
| 2388 |
marker_last_stack_min[i] = ADDR_LIMIT; |
| 2389 |
handle = _beginthreadex(NULL /* security_attr */, |
| 2390 |
MARK_THREAD_STACK_SIZE, GC_mark_thread, |
| 2391 |
(void *)(word)i, 0 /* flags */, &thread_id); |
| 2392 |
if (!handle || handle == (GC_uintptr_t)-1L) { |
| 2393 |
WARN("Marker thread creation failed\n", 0); |
| 2394 |
/* Don't try to create other marker threads. */ |
| 2395 |
break; |
| 2396 |
} else {/* We may detach the thread (if handle is of HANDLE type) */ |
| 2397 |
/* CloseHandle((HANDLE)handle); */ |
| 2398 |
} |
| 2399 |
# endif |
| 2400 |
} |
| 2401 |
|
| 2402 |
/* Adjust GC_markers_m1 (and free unused resources) if failed. */ |
| 2403 |
while (GC_markers_m1 > i) { |
| 2404 |
GC_markers_m1--; |
| 2405 |
CloseHandle(GC_marker_cv[GC_markers_m1]); |
| 2406 |
} |
| 2407 |
GC_wait_for_markers_init(); |
| 2408 |
GC_COND_LOG_PRINTF("Started %d mark helper threads\n", GC_markers_m1); |
| 2409 |
if (i == 0) { |
| 2410 |
CloseHandle(mark_cv); |
| 2411 |
CloseHandle(builder_cv); |
| 2412 |
CloseHandle(mark_mutex_event); |
| 2413 |
} |
| 2414 |
} |
| 2415 |
|
| 2416 |
# ifdef GC_ASSERTIONS |
| 2417 |
STATIC DWORD GC_mark_lock_holder = NO_THREAD; |
| 2418 |
# define SET_MARK_LOCK_HOLDER \ |
| 2419 |
(void)(GC_mark_lock_holder = GetCurrentThreadId()) |
| 2420 |
# define UNSET_MARK_LOCK_HOLDER \ |
| 2421 |
do { \ |
| 2422 |
GC_ASSERT(GC_mark_lock_holder == GetCurrentThreadId()); \ |
| 2423 |
GC_mark_lock_holder = NO_THREAD; \ |
| 2424 |
} while (0) |
| 2425 |
# endif /* GC_ASSERTIONS */ |
| 2426 |
|
| 2427 |
STATIC /* volatile */ LONG GC_mark_mutex_state = 0; |
| 2428 |
/* Mutex state: 0 - unlocked, */ |
| 2429 |
/* 1 - locked and no other waiters, */ |
| 2430 |
/* -1 - locked and waiters may exist. */ |
| 2431 |
/* Accessed by InterlockedExchange(). */ |
| 2432 |
|
| 2433 |
/* #define LOCK_STATS */ |
| 2434 |
# ifdef LOCK_STATS |
| 2435 |
volatile AO_t GC_block_count = 0; |
| 2436 |
volatile AO_t GC_unlocked_count = 0; |
| 2437 |
# endif |
| 2438 |
|
| 2439 |
GC_INNER void GC_acquire_mark_lock(void) |
| 2440 |
{ |
| 2441 |
GC_ASSERT(GC_mark_lock_holder != GetCurrentThreadId()); |
| 2442 |
if (InterlockedExchange(&GC_mark_mutex_state, 1 /* locked */) != 0) { |
| 2443 |
# ifdef LOCK_STATS |
| 2444 |
(void)AO_fetch_and_add1(&GC_block_count); |
| 2445 |
# endif |
| 2446 |
/* Repeatedly reset the state and wait until acquire the lock. */ |
| 2447 |
while (InterlockedExchange(&GC_mark_mutex_state, |
| 2448 |
-1 /* locked_and_has_waiters */) != 0) { |
| 2449 |
if (WaitForSingleObject(mark_mutex_event, INFINITE) == WAIT_FAILED) |
| 2450 |
ABORT("WaitForSingleObject failed"); |
| 2451 |
} |
| 2452 |
} |
| 2453 |
# ifdef LOCK_STATS |
| 2454 |
else { |
| 2455 |
(void)AO_fetch_and_add1(&GC_unlocked_count); |
| 2456 |
} |
| 2457 |
# endif |
| 2458 |
|
| 2459 |
GC_ASSERT(GC_mark_lock_holder == NO_THREAD); |
| 2460 |
SET_MARK_LOCK_HOLDER; |
| 2461 |
} |
| 2462 |
|
| 2463 |
GC_INNER void GC_release_mark_lock(void) |
| 2464 |
{ |
| 2465 |
UNSET_MARK_LOCK_HOLDER; |
| 2466 |
if (InterlockedExchange(&GC_mark_mutex_state, 0 /* unlocked */) < 0) { |
| 2467 |
/* wake a waiter */ |
| 2468 |
if (SetEvent(mark_mutex_event) == FALSE) |
| 2469 |
ABORT("SetEvent failed"); |
| 2470 |
} |
| 2471 |
} |
| 2472 |
|
| 2473 |
/* In GC_wait_for_reclaim/GC_notify_all_builder() we emulate POSIX */ |
| 2474 |
/* cond_wait/cond_broadcast() primitives with WinAPI Event object */ |
| 2475 |
/* (working in "manual reset" mode). This works here because */ |
| 2476 |
/* GC_notify_all_builder() is always called holding lock on */ |
| 2477 |
/* mark_mutex and the checked condition (GC_fl_builder_count == 0) */ |
| 2478 |
/* is the only one for which broadcasting on builder_cv is performed. */ |
| 2479 |
|
| 2480 |
GC_INNER void GC_wait_for_reclaim(void) |
| 2481 |
{ |
| 2482 |
GC_ASSERT(builder_cv != 0); |
| 2483 |
for (;;) { |
| 2484 |
GC_acquire_mark_lock(); |
| 2485 |
if (GC_fl_builder_count == 0) |
| 2486 |
break; |
| 2487 |
if (ResetEvent(builder_cv) == FALSE) |
| 2488 |
ABORT("ResetEvent failed"); |
| 2489 |
GC_release_mark_lock(); |
| 2490 |
if (WaitForSingleObject(builder_cv, INFINITE) == WAIT_FAILED) |
| 2491 |
ABORT("WaitForSingleObject failed"); |
| 2492 |
} |
| 2493 |
GC_release_mark_lock(); |
| 2494 |
} |
| 2495 |
|
| 2496 |
GC_INNER void GC_notify_all_builder(void) |
| 2497 |
{ |
| 2498 |
GC_ASSERT(GC_mark_lock_holder == GetCurrentThreadId()); |
| 2499 |
GC_ASSERT(builder_cv != 0); |
| 2500 |
GC_ASSERT(GC_fl_builder_count == 0); |
| 2501 |
if (SetEvent(builder_cv) == FALSE) |
| 2502 |
ABORT("SetEvent failed"); |
| 2503 |
} |
| 2504 |
|
| 2505 |
/* mark_cv is used (for waiting) by a non-helper thread. */ |
| 2506 |
|
| 2507 |
GC_INNER void GC_wait_marker(void) |
| 2508 |
{ |
| 2509 |
HANDLE event = mark_cv; |
| 2510 |
DWORD thread_id = GetCurrentThreadId(); |
| 2511 |
int i = GC_markers_m1; |
| 2512 |
|
| 2513 |
while (i-- > 0) { |
| 2514 |
if (GC_marker_Id[i] == thread_id) { |
| 2515 |
event = GC_marker_cv[i]; |
| 2516 |
break; |
| 2517 |
} |
| 2518 |
} |
| 2519 |
|
| 2520 |
if (ResetEvent(event) == FALSE) |
| 2521 |
ABORT("ResetEvent failed"); |
| 2522 |
GC_release_mark_lock(); |
| 2523 |
if (WaitForSingleObject(event, INFINITE) == WAIT_FAILED) |
| 2524 |
ABORT("WaitForSingleObject failed"); |
| 2525 |
GC_acquire_mark_lock(); |
| 2526 |
} |
| 2527 |
|
| 2528 |
GC_INNER void GC_notify_all_marker(void) |
| 2529 |
{ |
| 2530 |
DWORD thread_id = GetCurrentThreadId(); |
| 2531 |
int i = GC_markers_m1; |
| 2532 |
|
| 2533 |
while (i-- > 0) { |
| 2534 |
/* Notify every marker ignoring self (for efficiency). */ |
| 2535 |
if (SetEvent(GC_marker_Id[i] != thread_id ? GC_marker_cv[i] : |
| 2536 |
mark_cv) == FALSE) |
| 2537 |
ABORT("SetEvent failed"); |
| 2538 |
} |
| 2539 |
} |
| 2540 |
|
| 2541 |
# endif /* ! GC_PTHREADS_PARAMARK */ |
| 2542 |
|
| 2543 |
static unsigned required_markers_cnt = 0; |
| 2544 |
/* The default value (0) means the number of */ |
| 2545 |
/* markers should be selected automatically. */ |
| 2546 |
|
| 2547 |
# define START_MARK_THREADS() \ |
| 2548 |
if (EXPECT(GC_parallel || available_markers_m1 <= 0, TRUE)) {} \ |
| 2549 |
else GC_start_mark_threads() |
| 2550 |
#else |
| 2551 |
|
| 2552 |
# define START_MARK_THREADS() (void)0 |
| 2553 |
#endif /* !PARALLEL_MARK */ |
| 2554 |
|
| 2555 |
/* We have no DllMain to take care of new threads. Thus we */ |
| 2556 |
/* must properly intercept thread creation. */ |
| 2557 |
|
| 2558 |
typedef struct { |
| 2559 |
LPTHREAD_START_ROUTINE start; |
| 2560 |
LPVOID param; |
| 2561 |
} thread_args; |
| 2562 |
|
| 2563 |
STATIC void * GC_CALLBACK GC_win32_start_inner(struct GC_stack_base *sb, |
| 2564 |
void *arg) |
| 2565 |
{ |
| 2566 |
void * ret; |
| 2567 |
LPTHREAD_START_ROUTINE start = ((thread_args *)arg)->start; |
| 2568 |
LPVOID param = ((thread_args *)arg)->param; |
| 2569 |
|
| 2570 |
GC_register_my_thread(sb); /* This waits for an in-progress GC. */ |
| 2571 |
|
| 2572 |
# ifdef DEBUG_THREADS |
| 2573 |
GC_log_printf("thread 0x%lx starting...\n", (long)GetCurrentThreadId()); |
| 2574 |
# endif |
| 2575 |
|
| 2576 |
GC_free(arg); |
| 2577 |
|
| 2578 |
/* Clear the thread entry even if we exit with an exception. */ |
| 2579 |
/* This is probably pointless, since an uncaught exception is */ |
| 2580 |
/* supposed to result in the process being killed. */ |
| 2581 |
# if !defined(__GNUC__) && !defined(NO_CRT) |
| 2582 |
ret = NULL; /* to suppress "might be uninitialized" compiler warning */ |
| 2583 |
__try |
| 2584 |
# endif |
| 2585 |
{ |
| 2586 |
ret = (void *)(word)(*start)(param); |
| 2587 |
} |
| 2588 |
# if !defined(__GNUC__) && !defined(NO_CRT) |
| 2589 |
__finally |
| 2590 |
# endif |
| 2591 |
{ |
| 2592 |
GC_unregister_my_thread(); |
| 2593 |
} |
| 2594 |
|
| 2595 |
# ifdef DEBUG_THREADS |
| 2596 |
GC_log_printf("thread 0x%lx returned from start routine\n", |
| 2597 |
(long)GetCurrentThreadId()); |
| 2598 |
# endif |
| 2599 |
return ret; |
| 2600 |
} |
| 2601 |
|
| 2602 |
STATIC DWORD WINAPI GC_win32_start(LPVOID arg) |
| 2603 |
{ |
| 2604 |
return (DWORD)(word)GC_call_with_stack_base(GC_win32_start_inner, arg); |
| 2605 |
} |
| 2606 |
|
| 2607 |
GC_API HANDLE WINAPI GC_CreateThread( |
| 2608 |
LPSECURITY_ATTRIBUTES lpThreadAttributes, |
| 2609 |
GC_WIN32_SIZE_T dwStackSize, |
| 2610 |
LPTHREAD_START_ROUTINE lpStartAddress, |
| 2611 |
LPVOID lpParameter, DWORD dwCreationFlags, |
| 2612 |
LPDWORD lpThreadId) |
| 2613 |
{ |
| 2614 |
if (!EXPECT(parallel_initialized, TRUE)) |
| 2615 |
GC_init_parallel(); |
| 2616 |
/* make sure GC is initialized (i.e. main thread is */ |
| 2617 |
/* attached, tls initialized). */ |
| 2618 |
|
| 2619 |
# ifdef DEBUG_THREADS |
| 2620 |
GC_log_printf("About to create a thread from 0x%lx\n", |
| 2621 |
(long)GetCurrentThreadId()); |
| 2622 |
# endif |
| 2623 |
if (GC_win32_dll_threads) { |
| 2624 |
return CreateThread(lpThreadAttributes, dwStackSize, lpStartAddress, |
| 2625 |
lpParameter, dwCreationFlags, lpThreadId); |
| 2626 |
} else { |
| 2627 |
thread_args *args = |
| 2628 |
(thread_args *)GC_malloc_uncollectable(sizeof(thread_args)); |
| 2629 |
/* Handed off to and deallocated by child thread. */ |
| 2630 |
HANDLE thread_h; |
| 2631 |
|
| 2632 |
if (NULL == args) { |
| 2633 |
SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
| 2634 |
return NULL; |
| 2635 |
} |
| 2636 |
|
| 2637 |
/* set up thread arguments */ |
| 2638 |
args -> start = lpStartAddress; |
| 2639 |
args -> param = lpParameter; |
| 2640 |
GC_dirty(args); |
| 2641 |
REACHABLE_AFTER_DIRTY(lpParameter); |
| 2642 |
|
| 2643 |
START_MARK_THREADS(); |
| 2644 |
set_need_to_lock(); |
| 2645 |
thread_h = CreateThread(lpThreadAttributes, dwStackSize, GC_win32_start, |
| 2646 |
args, dwCreationFlags, lpThreadId); |
| 2647 |
if (thread_h == 0) GC_free(args); |
| 2648 |
return thread_h; |
| 2649 |
} |
| 2650 |
} |
| 2651 |
|
| 2652 |
GC_API DECLSPEC_NORETURN void WINAPI GC_ExitThread(DWORD dwExitCode) |
| 2653 |
{ |
| 2654 |
GC_unregister_my_thread(); |
| 2655 |
ExitThread(dwExitCode); |
| 2656 |
} |
| 2657 |
|
| 2658 |
# if !defined(CYGWIN32) && !defined(MSWINCE) && !defined(MSWIN_XBOX1) \ |
| 2659 |
&& !defined(NO_CRT) |
| 2660 |
GC_API GC_uintptr_t GC_CALL GC_beginthreadex( |
| 2661 |
void *security, unsigned stack_size, |
| 2662 |
unsigned (__stdcall *start_address)(void *), |
| 2663 |
void *arglist, unsigned initflag, |
| 2664 |
unsigned *thrdaddr) |
| 2665 |
{ |
| 2666 |
if (!EXPECT(parallel_initialized, TRUE)) |
| 2667 |
GC_init_parallel(); |
| 2668 |
/* make sure GC is initialized (i.e. main thread is */ |
| 2669 |
/* attached, tls initialized). */ |
| 2670 |
# ifdef DEBUG_THREADS |
| 2671 |
GC_log_printf("About to create a thread from 0x%lx\n", |
| 2672 |
(long)GetCurrentThreadId()); |
| 2673 |
# endif |
| 2674 |
|
| 2675 |
if (GC_win32_dll_threads) { |
| 2676 |
return _beginthreadex(security, stack_size, start_address, |
| 2677 |
arglist, initflag, thrdaddr); |
| 2678 |
} else { |
| 2679 |
GC_uintptr_t thread_h; |
| 2680 |
thread_args *args = |
| 2681 |
(thread_args *)GC_malloc_uncollectable(sizeof(thread_args)); |
| 2682 |
/* Handed off to and deallocated by child thread. */ |
| 2683 |
|
| 2684 |
if (NULL == args) { |
| 2685 |
/* MSDN docs say _beginthreadex() returns 0 on error and sets */ |
| 2686 |
/* errno to either EAGAIN (too many threads) or EINVAL (the */ |
| 2687 |
/* argument is invalid or the stack size is incorrect), so we */ |
| 2688 |
/* set errno to EAGAIN on "not enough memory". */ |
| 2689 |
errno = EAGAIN; |
| 2690 |
return 0; |
| 2691 |
} |
| 2692 |
|
| 2693 |
/* set up thread arguments */ |
| 2694 |
args -> start = (LPTHREAD_START_ROUTINE)start_address; |
| 2695 |
args -> param = arglist; |
| 2696 |
GC_dirty(args); |
| 2697 |
REACHABLE_AFTER_DIRTY(arglist); |
| 2698 |
|
| 2699 |
START_MARK_THREADS(); |
| 2700 |
set_need_to_lock(); |
| 2701 |
thread_h = _beginthreadex(security, stack_size, |
| 2702 |
(unsigned (__stdcall *)(void *))GC_win32_start, |
| 2703 |
args, initflag, thrdaddr); |
| 2704 |
if (thread_h == 0) GC_free(args); |
| 2705 |
return thread_h; |
| 2706 |
} |
| 2707 |
} |
| 2708 |
|
| 2709 |
GC_API void GC_CALL GC_endthreadex(unsigned retval) |
| 2710 |
{ |
| 2711 |
GC_unregister_my_thread(); |
| 2712 |
_endthreadex(retval); |
| 2713 |
} |
| 2714 |
# endif /* !CYGWIN32 && !MSWINCE && !MSWIN_XBOX1 && !NO_CRT */ |
| 2715 |
|
| 2716 |
#ifdef GC_WINMAIN_REDIRECT |
| 2717 |
/* This might be useful on WinCE. Shouldn't be used with GC_DLL. */ |
| 2718 |
|
| 2719 |
# if defined(MSWINCE) && defined(UNDER_CE) |
| 2720 |
# define WINMAIN_LPTSTR LPWSTR |
| 2721 |
# else |
| 2722 |
# define WINMAIN_LPTSTR LPSTR |
| 2723 |
# endif |
| 2724 |
|
| 2725 |
/* This is defined in gc.h. */ |
| 2726 |
# undef WinMain |
| 2727 |
|
| 2728 |
/* Defined outside GC by an application. */ |
| 2729 |
int WINAPI GC_WinMain(HINSTANCE, HINSTANCE, WINMAIN_LPTSTR, int); |
| 2730 |
|
| 2731 |
typedef struct { |
| 2732 |
HINSTANCE hInstance; |
| 2733 |
HINSTANCE hPrevInstance; |
| 2734 |
WINMAIN_LPTSTR lpCmdLine; |
| 2735 |
int nShowCmd; |
| 2736 |
} main_thread_args; |
| 2737 |
|
| 2738 |
static DWORD WINAPI main_thread_start(LPVOID arg) |
| 2739 |
{ |
| 2740 |
main_thread_args * args = (main_thread_args *) arg; |
| 2741 |
return (DWORD)GC_WinMain(args->hInstance, args->hPrevInstance, |
| 2742 |
args->lpCmdLine, args->nShowCmd); |
| 2743 |
} |
| 2744 |
|
| 2745 |
STATIC void *GC_CALLBACK GC_waitForSingleObjectInfinite(void *handle) |
| 2746 |
{ |
| 2747 |
return (void *)(word)WaitForSingleObject((HANDLE)handle, INFINITE); |
| 2748 |
} |
| 2749 |
|
| 2750 |
# ifndef WINMAIN_THREAD_STACK_SIZE |
| 2751 |
# define WINMAIN_THREAD_STACK_SIZE 0 /* default value */ |
| 2752 |
# endif |
| 2753 |
|
| 2754 |
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, |
| 2755 |
WINMAIN_LPTSTR lpCmdLine, int nShowCmd) |
| 2756 |
{ |
| 2757 |
DWORD exit_code = 1; |
| 2758 |
|
| 2759 |
main_thread_args args = { |
| 2760 |
hInstance, hPrevInstance, lpCmdLine, nShowCmd |
| 2761 |
}; |
| 2762 |
HANDLE thread_h; |
| 2763 |
DWORD thread_id; |
| 2764 |
|
| 2765 |
/* initialize everything */ |
| 2766 |
GC_INIT(); |
| 2767 |
|
| 2768 |
/* start the main thread */ |
| 2769 |
thread_h = GC_CreateThread(NULL /* lpsa */, |
| 2770 |
WINMAIN_THREAD_STACK_SIZE /* ignored on WinCE */, |
| 2771 |
main_thread_start, &args, 0 /* fdwCreate */, |
| 2772 |
&thread_id); |
| 2773 |
|
| 2774 |
if (thread_h != NULL) { |
| 2775 |
if ((DWORD)(word)GC_do_blocking(GC_waitForSingleObjectInfinite, |
| 2776 |
(void *)thread_h) == WAIT_FAILED) |
| 2777 |
ABORT("WaitForSingleObject(main_thread) failed"); |
| 2778 |
GetExitCodeThread (thread_h, &exit_code); |
| 2779 |
CloseHandle (thread_h); |
| 2780 |
} else { |
| 2781 |
ABORT("GC_CreateThread(main_thread) failed"); |
| 2782 |
} |
| 2783 |
|
| 2784 |
# ifdef MSWINCE |
| 2785 |
GC_deinit(); |
| 2786 |
# endif |
| 2787 |
return (int) exit_code; |
| 2788 |
} |
| 2789 |
|
| 2790 |
#endif /* GC_WINMAIN_REDIRECT */ |
| 2791 |
|
| 2792 |
GC_API void GC_CALL GC_set_markers_count(unsigned markers GC_ATTR_UNUSED) |
| 2793 |
{ |
| 2794 |
/* The same implementation as in pthread_support.c. */ |
| 2795 |
# ifdef PARALLEL_MARK |
| 2796 |
required_markers_cnt = markers < MAX_MARKERS ? markers : MAX_MARKERS; |
| 2797 |
# endif |
| 2798 |
} |
| 2799 |
|
| 2800 |
#ifdef WOW64_THREAD_CONTEXT_WORKAROUND |
| 2801 |
# ifdef MSWINRT_FLAVOR |
| 2802 |
/* Available on WinRT but we have to declare it manually. */ |
| 2803 |
__declspec(dllimport) HMODULE WINAPI GetModuleHandleW(LPCWSTR); |
| 2804 |
# endif |
| 2805 |
|
| 2806 |
static GC_bool is_wow64_process(HMODULE hK32 GC_ATTR_UNUSED) |
| 2807 |
{ |
| 2808 |
BOOL is_wow64; |
| 2809 |
# ifdef MSWINRT_FLAVOR |
| 2810 |
/* Try to use IsWow64Process2 as it handles different WoW64 cases. */ |
| 2811 |
HMODULE hWow64 = GetModuleHandleW(L"api-ms-win-core-wow64-l1-1-1.dll"); |
| 2812 |
|
| 2813 |
if (hWow64) { |
| 2814 |
FARPROC pfn2 = GetProcAddress(hWow64, "IsWow64Process2"); |
| 2815 |
USHORT process_machine, native_machine; |
| 2816 |
|
| 2817 |
if (pfn2 |
| 2818 |
&& (*(BOOL (WINAPI*)(HANDLE, USHORT*, USHORT*))(word)pfn2)( |
| 2819 |
GetCurrentProcess(), &process_machine, &native_machine)) |
| 2820 |
return process_machine != native_machine; |
| 2821 |
} |
| 2822 |
if (IsWow64Process(GetCurrentProcess(), &is_wow64)) |
| 2823 |
return (GC_bool)is_wow64; |
| 2824 |
# else |
| 2825 |
if (hK32) { |
| 2826 |
FARPROC pfn = GetProcAddress(hK32, "IsWow64Process"); |
| 2827 |
|
| 2828 |
if (pfn |
| 2829 |
&& (*(BOOL (WINAPI*)(HANDLE, BOOL*))(word)pfn)( |
| 2830 |
GetCurrentProcess(), &is_wow64)) |
| 2831 |
return (GC_bool)is_wow64; |
| 2832 |
} |
| 2833 |
# endif |
| 2834 |
return FALSE; /* IsWow64Process failed */ |
| 2835 |
} |
| 2836 |
#endif /* WOW64_THREAD_CONTEXT_WORKAROUND */ |
| 2837 |
|
| 2838 |
#ifndef DONT_USE_ATEXIT |
| 2839 |
GC_INNER GC_bool GC_is_main_thread(void) |
| 2840 |
{ |
| 2841 |
GC_ASSERT(GC_thr_initialized); |
| 2842 |
return GC_main_thread == GetCurrentThreadId(); |
| 2843 |
} |
| 2844 |
#endif /* !DONT_USE_ATEXIT */ |
| 2845 |
|
| 2846 |
GC_INNER void GC_thr_init(void) |
| 2847 |
{ |
| 2848 |
struct GC_stack_base sb; |
| 2849 |
# if (!defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) && !defined(MSWINCE) \ |
| 2850 |
&& defined(PARALLEL_MARK)) || defined(WOW64_THREAD_CONTEXT_WORKAROUND) |
| 2851 |
HMODULE hK32; |
| 2852 |
# ifdef MSWINRT_FLAVOR |
| 2853 |
MEMORY_BASIC_INFORMATION memInfo; |
| 2854 |
|
| 2855 |
if (VirtualQuery((void*)(word)GetProcAddress, &memInfo, sizeof(memInfo)) |
| 2856 |
!= sizeof(memInfo)) |
| 2857 |
ABORT("Weird VirtualQuery result"); |
| 2858 |
hK32 = (HMODULE)memInfo.AllocationBase; |
| 2859 |
# else |
| 2860 |
hK32 = GetModuleHandle(TEXT("kernel32.dll")); |
| 2861 |
# endif |
| 2862 |
# endif |
| 2863 |
|
| 2864 |
GC_ASSERT(I_HOLD_LOCK()); |
| 2865 |
if (GC_thr_initialized) return; |
| 2866 |
|
| 2867 |
GC_ASSERT((word)&GC_threads % sizeof(word) == 0); |
| 2868 |
GC_main_thread = GetCurrentThreadId(); |
| 2869 |
GC_thr_initialized = TRUE; |
| 2870 |
|
| 2871 |
# ifdef CAN_HANDLE_FORK |
| 2872 |
/* Prepare for forks if requested. */ |
| 2873 |
if (GC_handle_fork) { |
| 2874 |
# ifdef CAN_CALL_ATFORK |
| 2875 |
if (pthread_atfork(fork_prepare_proc, fork_parent_proc, |
| 2876 |
fork_child_proc) == 0) { |
| 2877 |
/* Handlers successfully registered. */ |
| 2878 |
GC_handle_fork = 1; |
| 2879 |
} else |
| 2880 |
# endif |
| 2881 |
/* else */ if (GC_handle_fork != -1) |
| 2882 |
ABORT("pthread_atfork failed"); |
| 2883 |
} |
| 2884 |
# endif |
| 2885 |
# ifdef WOW64_THREAD_CONTEXT_WORKAROUND |
| 2886 |
/* Set isWow64 flag. */ |
| 2887 |
isWow64 = is_wow64_process(hK32); |
| 2888 |
# endif |
| 2889 |
/* Add the initial thread, so we can stop it. */ |
| 2890 |
sb.mem_base = GC_stackbottom; |
| 2891 |
GC_ASSERT(sb.mem_base != NULL); |
| 2892 |
# ifdef IA64 |
| 2893 |
sb.reg_base = GC_register_stackbottom; |
| 2894 |
# endif |
| 2895 |
|
| 2896 |
# if defined(PARALLEL_MARK) |
| 2897 |
{ |
| 2898 |
char * markers_string = GETENV("GC_MARKERS"); |
| 2899 |
int markers = required_markers_cnt; |
| 2900 |
|
| 2901 |
if (markers_string != NULL) { |
| 2902 |
markers = atoi(markers_string); |
| 2903 |
if (markers <= 0 || markers > MAX_MARKERS) { |
| 2904 |
WARN("Too big or invalid number of mark threads: %" WARN_PRIdPTR |
| 2905 |
"; using maximum threads\n", (signed_word)markers); |
| 2906 |
markers = MAX_MARKERS; |
| 2907 |
} |
| 2908 |
} else if (0 == markers) { |
| 2909 |
/* Unless the client sets the desired number of */ |
| 2910 |
/* parallel markers, it is determined based on the */ |
| 2911 |
/* number of CPU cores. */ |
| 2912 |
# ifdef MSWINCE |
| 2913 |
/* There is no GetProcessAffinityMask() in WinCE. */ |
| 2914 |
/* GC_sysinfo is already initialized. */ |
| 2915 |
markers = (int)GC_sysinfo.dwNumberOfProcessors; |
| 2916 |
# else |
| 2917 |
# ifdef _WIN64 |
| 2918 |
DWORD_PTR procMask = 0; |
| 2919 |
DWORD_PTR sysMask; |
| 2920 |
# else |
| 2921 |
DWORD procMask = 0; |
| 2922 |
DWORD sysMask; |
| 2923 |
# endif |
| 2924 |
int ncpu = 0; |
| 2925 |
if ( |
| 2926 |
# ifdef __cplusplus |
| 2927 |
GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask) |
| 2928 |
# else |
| 2929 |
/* Cast args to void* for compatibility with some old SDKs. */ |
| 2930 |
GetProcessAffinityMask(GetCurrentProcess(), |
| 2931 |
(void *)&procMask, (void *)&sysMask) |
| 2932 |
# endif |
| 2933 |
&& procMask) { |
| 2934 |
do { |
| 2935 |
ncpu++; |
| 2936 |
} while ((procMask &= procMask - 1) != 0); |
| 2937 |
} |
| 2938 |
markers = ncpu; |
| 2939 |
# endif |
| 2940 |
# if defined(GC_MIN_MARKERS) && !defined(CPPCHECK) |
| 2941 |
/* This is primarily for testing on systems without getenv(). */ |
| 2942 |
if (markers < GC_MIN_MARKERS) |
| 2943 |
markers = GC_MIN_MARKERS; |
| 2944 |
# endif |
| 2945 |
if (markers > MAX_MARKERS) |
| 2946 |
markers = MAX_MARKERS; /* silently limit the value */ |
| 2947 |
} |
| 2948 |
available_markers_m1 = markers - 1; |
| 2949 |
} |
| 2950 |
|
| 2951 |
/* Check whether parallel mode could be enabled. */ |
| 2952 |
if (GC_win32_dll_threads || available_markers_m1 <= 0) { |
| 2953 |
/* Disable parallel marking. */ |
| 2954 |
GC_parallel = FALSE; |
| 2955 |
GC_COND_LOG_PRINTF( |
| 2956 |
"Single marker thread, turning off parallel marking\n"); |
| 2957 |
} else { |
| 2958 |
# ifndef GC_PTHREADS_PARAMARK |
| 2959 |
/* Initialize Win32 event objects for parallel marking. */ |
| 2960 |
mark_mutex_event = CreateEvent(NULL /* attrs */, |
| 2961 |
FALSE /* isManualReset */, |
| 2962 |
FALSE /* initialState */, NULL /* name */); |
| 2963 |
builder_cv = CreateEvent(NULL /* attrs */, |
| 2964 |
TRUE /* isManualReset */, |
| 2965 |
FALSE /* initialState */, NULL /* name */); |
| 2966 |
mark_cv = CreateEvent(NULL /* attrs */, TRUE /* isManualReset */, |
| 2967 |
FALSE /* initialState */, NULL /* name */); |
| 2968 |
if (mark_mutex_event == (HANDLE)0 || builder_cv == (HANDLE)0 |
| 2969 |
|| mark_cv == (HANDLE)0) |
| 2970 |
ABORT("CreateEvent failed"); |
| 2971 |
# endif |
| 2972 |
# if !defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) && !defined(MSWINCE) |
| 2973 |
if (hK32) |
| 2974 |
setThreadDescription_fn = GetProcAddress(hK32, |
| 2975 |
"SetThreadDescription"); |
| 2976 |
# endif |
| 2977 |
} |
| 2978 |
# endif /* PARALLEL_MARK */ |
| 2979 |
|
| 2980 |
GC_ASSERT(0 == GC_lookup_thread_inner(GC_main_thread)); |
| 2981 |
GC_register_my_thread_inner(&sb, GC_main_thread); |
| 2982 |
} |
| 2983 |
|
| 2984 |
#ifdef GC_PTHREADS |
| 2985 |
|
| 2986 |
struct start_info { |
| 2987 |
void *(*start_routine)(void *); |
| 2988 |
void *arg; |
| 2989 |
sem_t registered; /* 1 ==> in our thread table, but */ |
| 2990 |
/* parent hasn't yet noticed. */ |
| 2991 |
int detached; |
| 2992 |
}; |
| 2993 |
|
| 2994 |
GC_API int GC_pthread_join(pthread_t pthread_id, void **retval) |
| 2995 |
{ |
| 2996 |
int result; |
| 2997 |
GC_thread t; |
| 2998 |
DCL_LOCK_STATE; |
| 2999 |
|
| 3000 |
GC_ASSERT(!GC_win32_dll_threads); |
| 3001 |
# ifdef DEBUG_THREADS |
| 3002 |
GC_log_printf("thread %p(0x%lx) is joining thread %p\n", |
| 3003 |
(void *)GC_PTHREAD_PTRVAL(pthread_self()), |
| 3004 |
(long)GetCurrentThreadId(), |
| 3005 |
(void *)GC_PTHREAD_PTRVAL(pthread_id)); |
| 3006 |
# endif |
| 3007 |
|
| 3008 |
/* After the join, thread id may have been recycled. */ |
| 3009 |
t = GC_lookup_pthread(pthread_id); |
| 3010 |
result = pthread_join(pthread_id, retval); |
| 3011 |
if (0 == result) { |
| 3012 |
LOCK(); |
| 3013 |
if ((t -> flags & FINISHED) != 0) { |
| 3014 |
GC_delete_gc_thread_no_free(t); |
| 3015 |
GC_INTERNAL_FREE(t); |
| 3016 |
} |
| 3017 |
UNLOCK(); |
| 3018 |
} |
| 3019 |
|
| 3020 |
# ifdef DEBUG_THREADS |
| 3021 |
GC_log_printf("thread %p(0x%lx) join with thread %p %s\n", |
| 3022 |
(void *)GC_PTHREAD_PTRVAL(pthread_self()), |
| 3023 |
(long)GetCurrentThreadId(), |
| 3024 |
(void *)GC_PTHREAD_PTRVAL(pthread_id), |
| 3025 |
result != 0 ? "failed" : "succeeded"); |
| 3026 |
# endif |
| 3027 |
return result; |
| 3028 |
} |
| 3029 |
|
| 3030 |
/* Cygwin-pthreads calls CreateThread internally, but it's not easily */ |
| 3031 |
/* interceptable by us..., so intercept pthread_create instead. */ |
| 3032 |
GC_API int GC_pthread_create(pthread_t *new_thread, |
| 3033 |
GC_PTHREAD_CREATE_CONST pthread_attr_t *attr, |
| 3034 |
void *(*start_routine)(void *), void *arg) |
| 3035 |
{ |
| 3036 |
int result; |
| 3037 |
struct start_info si; |
| 3038 |
|
| 3039 |
if (!EXPECT(parallel_initialized, TRUE)) |
| 3040 |
GC_init_parallel(); |
| 3041 |
/* make sure GC is initialized (i.e. main thread is attached) */ |
| 3042 |
GC_ASSERT(!GC_win32_dll_threads); |
| 3043 |
|
| 3044 |
if (sem_init(&si.registered, GC_SEM_INIT_PSHARED, 0) != 0) |
| 3045 |
ABORT("sem_init failed"); |
| 3046 |
|
| 3047 |
si.start_routine = start_routine; |
| 3048 |
si.arg = arg; |
| 3049 |
si.detached = 0; |
| 3050 |
if (attr != NULL |
| 3051 |
&& pthread_attr_getdetachstate(attr, &si.detached) != 0) |
| 3052 |
ABORT("pthread_attr_getdetachstate failed"); |
| 3053 |
# ifdef DEBUG_THREADS |
| 3054 |
GC_log_printf("About to create a thread from %p(0x%lx)\n", |
| 3055 |
(void *)GC_PTHREAD_PTRVAL(pthread_self()), |
| 3056 |
(long)GetCurrentThreadId()); |
| 3057 |
# endif |
| 3058 |
START_MARK_THREADS(); |
| 3059 |
set_need_to_lock(); |
| 3060 |
result = pthread_create(new_thread, attr, GC_pthread_start, &si); |
| 3061 |
|
| 3062 |
/* Wait until child has been added to the thread table. */ |
| 3063 |
/* This also ensures that we hold onto the stack-allocated si */ |
| 3064 |
/* until the child is done with it. */ |
| 3065 |
if (0 == result) { |
| 3066 |
while (0 != sem_wait(&si.registered)) { |
| 3067 |
if (EINTR != errno) ABORT("sem_wait failed"); |
| 3068 |
} |
| 3069 |
} |
| 3070 |
sem_destroy(&si.registered); |
| 3071 |
return(result); |
| 3072 |
} |
| 3073 |
|
| 3074 |
STATIC void * GC_CALLBACK GC_pthread_start_inner(struct GC_stack_base *sb, |
| 3075 |
void * arg) |
| 3076 |
{ |
| 3077 |
struct start_info * si = (struct start_info *)arg; |
| 3078 |
void * result; |
| 3079 |
void *(*start)(void *); |
| 3080 |
void *start_arg; |
| 3081 |
DWORD thread_id = GetCurrentThreadId(); |
| 3082 |
pthread_t pthread_id = pthread_self(); |
| 3083 |
GC_thread me; |
| 3084 |
DCL_LOCK_STATE; |
| 3085 |
|
| 3086 |
# ifdef DEBUG_THREADS |
| 3087 |
GC_log_printf("thread %p(0x%x) starting...\n", |
| 3088 |
(void *)GC_PTHREAD_PTRVAL(pthread_id), (int)thread_id); |
| 3089 |
# endif |
| 3090 |
|
| 3091 |
GC_ASSERT(!GC_win32_dll_threads); |
| 3092 |
/* If a GC occurs before the thread is registered, that GC will */ |
| 3093 |
/* ignore this thread. That's fine, since it will block trying to */ |
| 3094 |
/* acquire the allocation lock, and won't yet hold interesting */ |
| 3095 |
/* pointers. */ |
| 3096 |
LOCK(); |
| 3097 |
/* We register the thread here instead of in the parent, so that */ |
| 3098 |
/* we don't need to hold the allocation lock during pthread_create. */ |
| 3099 |
me = GC_register_my_thread_inner(sb, thread_id); |
| 3100 |
SET_PTHREAD_MAP_CACHE(pthread_id, thread_id); |
| 3101 |
GC_ASSERT(me != &first_thread); |
| 3102 |
me -> pthread_id = pthread_id; |
| 3103 |
if (si->detached) me -> flags |= DETACHED; |
| 3104 |
# ifdef THREAD_LOCAL_ALLOC |
| 3105 |
GC_init_thread_local(&me->tlfs); |
| 3106 |
# endif |
| 3107 |
UNLOCK(); |
| 3108 |
|
| 3109 |
start = si -> start_routine; |
| 3110 |
start_arg = si -> arg; |
| 3111 |
sem_post(&(si -> registered)); /* Last action on si. */ |
| 3112 |
/* OK to deallocate. */ |
| 3113 |
|
| 3114 |
pthread_cleanup_push(GC_thread_exit_proc, (void *)me); |
| 3115 |
result = (*start)(start_arg); |
| 3116 |
me -> status = result; |
| 3117 |
GC_dirty(me); |
| 3118 |
pthread_cleanup_pop(1); |
| 3119 |
|
| 3120 |
# ifdef DEBUG_THREADS |
| 3121 |
GC_log_printf("thread %p(0x%x) returned from start routine\n", |
| 3122 |
(void *)GC_PTHREAD_PTRVAL(pthread_id), (int)thread_id); |
| 3123 |
# endif |
| 3124 |
return(result); |
| 3125 |
} |
| 3126 |
|
| 3127 |
STATIC void * GC_pthread_start(void * arg) |
| 3128 |
{ |
| 3129 |
return GC_call_with_stack_base(GC_pthread_start_inner, arg); |
| 3130 |
} |
| 3131 |
|
| 3132 |
STATIC void GC_thread_exit_proc(void *arg) |
| 3133 |
{ |
| 3134 |
GC_thread me = (GC_thread)arg; |
| 3135 |
DCL_LOCK_STATE; |
| 3136 |
|
| 3137 |
GC_ASSERT(!GC_win32_dll_threads); |
| 3138 |
# ifdef DEBUG_THREADS |
| 3139 |
GC_log_printf("thread %p(0x%lx) called pthread_exit()\n", |
| 3140 |
(void *)GC_PTHREAD_PTRVAL(pthread_self()), |
| 3141 |
(long)GetCurrentThreadId()); |
| 3142 |
# endif |
| 3143 |
|
| 3144 |
LOCK(); |
| 3145 |
GC_wait_for_gc_completion(FALSE); |
| 3146 |
# if defined(THREAD_LOCAL_ALLOC) |
| 3147 |
GC_ASSERT(GC_getspecific(GC_thread_key) == &me->tlfs); |
| 3148 |
GC_destroy_thread_local(&(me->tlfs)); |
| 3149 |
# endif |
| 3150 |
if (me -> flags & DETACHED) { |
| 3151 |
GC_delete_thread(GetCurrentThreadId()); |
| 3152 |
} else { |
| 3153 |
/* deallocate it as part of join */ |
| 3154 |
me -> flags |= FINISHED; |
| 3155 |
} |
| 3156 |
# if defined(THREAD_LOCAL_ALLOC) |
| 3157 |
/* It is required to call remove_specific defined in specific.c. */ |
| 3158 |
GC_remove_specific(GC_thread_key); |
| 3159 |
# endif |
| 3160 |
UNLOCK(); |
| 3161 |
} |
| 3162 |
|
| 3163 |
# ifndef GC_NO_PTHREAD_SIGMASK |
| 3164 |
/* pthreads-win32 does not support sigmask. */ |
| 3165 |
/* So, nothing required here... */ |
| 3166 |
GC_API int GC_pthread_sigmask(int how, const sigset_t *set, |
| 3167 |
sigset_t *oset) |
| 3168 |
{ |
| 3169 |
return pthread_sigmask(how, set, oset); |
| 3170 |
} |
| 3171 |
# endif /* !GC_NO_PTHREAD_SIGMASK */ |
| 3172 |
|
| 3173 |
GC_API int GC_pthread_detach(pthread_t thread) |
| 3174 |
{ |
| 3175 |
int result; |
| 3176 |
GC_thread t; |
| 3177 |
DCL_LOCK_STATE; |
| 3178 |
|
| 3179 |
GC_ASSERT(!GC_win32_dll_threads); |
| 3180 |
t = GC_lookup_pthread(thread); |
| 3181 |
result = pthread_detach(thread); |
| 3182 |
if (result == 0) { |
| 3183 |
LOCK(); |
| 3184 |
t -> flags |= DETACHED; |
| 3185 |
/* Here the pthread thread id may have been recycled. */ |
| 3186 |
if ((t -> flags & FINISHED) != 0) { |
| 3187 |
GC_delete_gc_thread_no_free(t); |
| 3188 |
GC_INTERNAL_FREE(t); |
| 3189 |
} |
| 3190 |
UNLOCK(); |
| 3191 |
} |
| 3192 |
return result; |
| 3193 |
} |
| 3194 |
|
| 3195 |
#elif !defined(GC_NO_THREADS_DISCOVERY) |
| 3196 |
/* We avoid acquiring locks here, since this doesn't seem to be */ |
| 3197 |
/* preemptible. This may run with an uninitialized collector, in */ |
| 3198 |
/* which case we don't do much. This implies that no threads other */ |
| 3199 |
/* than the main one should be created with an uninitialized */ |
| 3200 |
/* collector. (The alternative of initializing the collector here */ |
| 3201 |
/* seems dangerous, since DllMain is limited in what it can do.) */ |
| 3202 |
|
| 3203 |
# ifdef GC_INSIDE_DLL |
| 3204 |
/* Export only if needed by client. */ |
| 3205 |
GC_API |
| 3206 |
# else |
| 3207 |
# define GC_DllMain DllMain |
| 3208 |
# endif |
| 3209 |
BOOL WINAPI GC_DllMain(HINSTANCE inst GC_ATTR_UNUSED, ULONG reason, |
| 3210 |
LPVOID reserved GC_ATTR_UNUSED) |
| 3211 |
{ |
| 3212 |
DWORD thread_id; |
| 3213 |
|
| 3214 |
/* Note that GC_use_threads_discovery should be called by the */ |
| 3215 |
/* client application at start-up to activate automatic thread */ |
| 3216 |
/* registration (it is the default GC behavior); */ |
| 3217 |
/* to always have automatic thread registration turned on, the GC */ |
| 3218 |
/* should be compiled with -D GC_DISCOVER_TASK_THREADS. */ |
| 3219 |
if (!GC_win32_dll_threads && parallel_initialized) return TRUE; |
| 3220 |
|
| 3221 |
switch (reason) { |
| 3222 |
case DLL_THREAD_ATTACH: |
| 3223 |
# ifdef PARALLEL_MARK |
| 3224 |
/* Don't register marker threads. */ |
| 3225 |
if (GC_parallel) { |
| 3226 |
/* We could reach here only if parallel_initialized == FALSE. */ |
| 3227 |
break; |
| 3228 |
} |
| 3229 |
# endif |
| 3230 |
/* FALLTHRU */ |
| 3231 |
case DLL_PROCESS_ATTACH: |
| 3232 |
/* This may run with the collector uninitialized. */ |
| 3233 |
thread_id = GetCurrentThreadId(); |
| 3234 |
if (parallel_initialized && GC_main_thread != thread_id) { |
| 3235 |
struct GC_stack_base sb; |
| 3236 |
/* Don't lock here. */ |
| 3237 |
# ifdef GC_ASSERTIONS |
| 3238 |
int sb_result = |
| 3239 |
# endif |
| 3240 |
GC_get_stack_base(&sb); |
| 3241 |
GC_ASSERT(sb_result == GC_SUCCESS); |
| 3242 |
GC_register_my_thread_inner(&sb, thread_id); |
| 3243 |
} /* o.w. we already did it during GC_thr_init, called by GC_init */ |
| 3244 |
break; |
| 3245 |
|
| 3246 |
case DLL_THREAD_DETACH: |
| 3247 |
/* We are hopefully running in the context of the exiting thread. */ |
| 3248 |
if (GC_win32_dll_threads) { |
| 3249 |
GC_delete_thread(GetCurrentThreadId()); |
| 3250 |
} |
| 3251 |
break; |
| 3252 |
|
| 3253 |
case DLL_PROCESS_DETACH: |
| 3254 |
if (GC_win32_dll_threads) { |
| 3255 |
int i; |
| 3256 |
int my_max = (int)GC_get_max_thread_index(); |
| 3257 |
|
| 3258 |
for (i = 0; i <= my_max; ++i) { |
| 3259 |
if (AO_load(&(dll_thread_table[i].tm.in_use))) |
| 3260 |
GC_delete_gc_thread_no_free(&dll_thread_table[i]); |
| 3261 |
} |
| 3262 |
GC_deinit(); |
| 3263 |
} |
| 3264 |
break; |
| 3265 |
} |
| 3266 |
return TRUE; |
| 3267 |
} |
| 3268 |
#endif /* !GC_NO_THREADS_DISCOVERY && !GC_PTHREADS */ |
| 3269 |
|
| 3270 |
/* Perform all initializations, including those that */ |
| 3271 |
/* may require allocation. */ |
| 3272 |
/* Called without allocation lock. */ |
| 3273 |
/* Must be called before a second thread is created. */ |
| 3274 |
GC_INNER void GC_init_parallel(void) |
| 3275 |
{ |
| 3276 |
# if defined(THREAD_LOCAL_ALLOC) |
| 3277 |
GC_thread me; |
| 3278 |
DCL_LOCK_STATE; |
| 3279 |
# endif |
| 3280 |
|
| 3281 |
if (parallel_initialized) return; |
| 3282 |
parallel_initialized = TRUE; |
| 3283 |
/* GC_init() calls us back, so set flag first. */ |
| 3284 |
|
| 3285 |
if (!GC_is_initialized) GC_init(); |
| 3286 |
# if defined(CPPCHECK) && !defined(GC_NO_THREADS_DISCOVERY) |
| 3287 |
GC_noop1((word)&GC_DllMain); |
| 3288 |
# endif |
| 3289 |
if (GC_win32_dll_threads) { |
| 3290 |
set_need_to_lock(); |
| 3291 |
/* Cannot intercept thread creation. Hence we don't know if */ |
| 3292 |
/* other threads exist. However, client is not allowed to */ |
| 3293 |
/* create other threads before collector initialization. */ |
| 3294 |
/* Thus it's OK not to lock before this. */ |
| 3295 |
} |
| 3296 |
/* Initialize thread local free lists if used. */ |
| 3297 |
# if defined(THREAD_LOCAL_ALLOC) |
| 3298 |
LOCK(); |
| 3299 |
me = GC_lookup_thread_inner(GetCurrentThreadId()); |
| 3300 |
CHECK_LOOKUP_MY_THREAD(me); |
| 3301 |
GC_init_thread_local(&me->tlfs); |
| 3302 |
UNLOCK(); |
| 3303 |
# endif |
| 3304 |
} |
| 3305 |
|
| 3306 |
#if defined(USE_PTHREAD_LOCKS) |
| 3307 |
/* Support for pthread locking code. */ |
| 3308 |
/* pthread_mutex_trylock may not win here, */ |
| 3309 |
/* due to builtin support for spinning first? */ |
| 3310 |
|
| 3311 |
GC_INNER void GC_lock(void) |
| 3312 |
{ |
| 3313 |
pthread_mutex_lock(&GC_allocate_ml); |
| 3314 |
} |
| 3315 |
#endif /* USE_PTHREAD_LOCKS */ |
| 3316 |
|
| 3317 |
#if defined(THREAD_LOCAL_ALLOC) |
| 3318 |
|
| 3319 |
/* Add thread-local allocation support. VC++ uses __declspec(thread). */ |
| 3320 |
|
| 3321 |
/* We must explicitly mark ptrfree and gcj free lists, since the free */ |
| 3322 |
/* list links wouldn't otherwise be found. We also set them in the */ |
| 3323 |
/* normal free lists, since that involves touching less memory than if */ |
| 3324 |
/* we scanned them normally. */ |
| 3325 |
GC_INNER void GC_mark_thread_local_free_lists(void) |
| 3326 |
{ |
| 3327 |
int i; |
| 3328 |
GC_thread p; |
| 3329 |
|
| 3330 |
for (i = 0; i < THREAD_TABLE_SZ; ++i) { |
| 3331 |
for (p = GC_threads[i]; 0 != p; p = p -> tm.next) { |
| 3332 |
if (!KNOWN_FINISHED(p)) { |
| 3333 |
# ifdef DEBUG_THREADS |
| 3334 |
GC_log_printf("Marking thread locals for 0x%x\n", (int)p -> id); |
| 3335 |
# endif |
| 3336 |
GC_mark_thread_local_fls_for(&(p->tlfs)); |
| 3337 |
} |
| 3338 |
} |
| 3339 |
} |
| 3340 |
} |
| 3341 |
|
| 3342 |
# if defined(GC_ASSERTIONS) |
| 3343 |
/* Check that all thread-local free-lists are completely marked. */ |
| 3344 |
/* also check that thread-specific-data structures are marked. */ |
| 3345 |
void GC_check_tls(void) |
| 3346 |
{ |
| 3347 |
int i; |
| 3348 |
GC_thread p; |
| 3349 |
|
| 3350 |
for (i = 0; i < THREAD_TABLE_SZ; ++i) { |
| 3351 |
for (p = GC_threads[i]; 0 != p; p = p -> tm.next) { |
| 3352 |
if (!KNOWN_FINISHED(p)) |
| 3353 |
GC_check_tls_for(&(p->tlfs)); |
| 3354 |
} |
| 3355 |
} |
| 3356 |
# if defined(USE_CUSTOM_SPECIFIC) |
| 3357 |
if (GC_thread_key != 0) |
| 3358 |
GC_check_tsd_marks(GC_thread_key); |
| 3359 |
# endif |
| 3360 |
} |
| 3361 |
# endif /* GC_ASSERTIONS */ |
| 3362 |
|
| 3363 |
#endif /* THREAD_LOCAL_ALLOC ... */ |
| 3364 |
|
| 3365 |
# ifndef GC_NO_THREAD_REDIRECTS |
| 3366 |
/* Restore thread calls redirection. */ |
| 3367 |
# define CreateThread GC_CreateThread |
| 3368 |
# define ExitThread GC_ExitThread |
| 3369 |
# undef _beginthreadex |
| 3370 |
# define _beginthreadex GC_beginthreadex |
| 3371 |
# undef _endthreadex |
| 3372 |
# define _endthreadex GC_endthreadex |
| 3373 |
# endif /* !GC_NO_THREAD_REDIRECTS */ |
| 3374 |
|
| 3375 |
#endif /* GC_WIN32_THREADS */ |