| 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-2009 by Hewlett-Packard Development Company. |
| 6 |
* All rights reserved. |
| 7 |
* Copyright (c) 2008-2020 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/pthread_support.h" |
| 20 |
|
| 21 |
#if defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS) && \ |
| 22 |
!defined(GC_DARWIN_THREADS) && !defined(PLATFORM_STOP_WORLD) \ |
| 23 |
&& !defined(SN_TARGET_PSP2) |
| 24 |
|
| 25 |
#ifdef NACL |
| 26 |
# include <unistd.h> |
| 27 |
# include <sys/time.h> |
| 28 |
#elif defined(GC_OPENBSD_UTHREADS) |
| 29 |
# include <pthread_np.h> |
| 30 |
#else |
| 31 |
# include <signal.h> |
| 32 |
# include <semaphore.h> |
| 33 |
# include <errno.h> |
| 34 |
# include <time.h> /* for nanosleep() */ |
| 35 |
# include <unistd.h> |
| 36 |
#endif /* !GC_OPENBSD_UTHREADS && !NACL */ |
| 37 |
|
| 38 |
#ifndef GC_OPENBSD_UTHREADS |
| 39 |
GC_INLINE void GC_usleep(unsigned us) |
| 40 |
{ |
| 41 |
# if defined(LINT2) || defined(THREAD_SANITIZER) |
| 42 |
/* Workaround "waiting while holding a lock" static analyzer warning. */ |
| 43 |
/* Workaround a rare hang in usleep() trying to acquire TSan Lock. */ |
| 44 |
while (us-- > 0) |
| 45 |
sched_yield(); /* pretending it takes 1us */ |
| 46 |
# elif defined(CPPCHECK) /* || _POSIX_C_SOURCE >= 199309L */ |
| 47 |
struct timespec ts; |
| 48 |
|
| 49 |
ts.tv_sec = 0; |
| 50 |
ts.tv_nsec = us * 1000; |
| 51 |
/* This requires _POSIX_TIMERS feature. */ |
| 52 |
(void)nanosleep(&ts, NULL); |
| 53 |
# else |
| 54 |
usleep(us); |
| 55 |
# endif |
| 56 |
} |
| 57 |
#endif /* !GC_OPENBSD_UTHREADS */ |
| 58 |
|
| 59 |
#ifdef NACL |
| 60 |
|
| 61 |
STATIC int GC_nacl_num_gc_threads = 0; |
| 62 |
STATIC __thread int GC_nacl_thread_idx = -1; |
| 63 |
STATIC volatile int GC_nacl_park_threads_now = 0; |
| 64 |
STATIC volatile pthread_t GC_nacl_thread_parker = -1; |
| 65 |
|
| 66 |
GC_INNER __thread GC_thread GC_nacl_gc_thread_self = NULL; |
| 67 |
|
| 68 |
volatile int GC_nacl_thread_parked[MAX_NACL_GC_THREADS]; |
| 69 |
int GC_nacl_thread_used[MAX_NACL_GC_THREADS]; |
| 70 |
|
| 71 |
#elif !defined(GC_OPENBSD_UTHREADS) |
| 72 |
|
| 73 |
#if (!defined(AO_HAVE_load_acquire) || !defined(AO_HAVE_store_release)) \ |
| 74 |
&& !defined(CPPCHECK) |
| 75 |
# error AO_load_acquire and/or AO_store_release are missing; |
| 76 |
# error please define AO_REQUIRE_CAS manually |
| 77 |
#endif |
| 78 |
|
| 79 |
/* It's safe to call original pthread_sigmask() here. */ |
| 80 |
#undef pthread_sigmask |
| 81 |
|
| 82 |
#ifdef DEBUG_THREADS |
| 83 |
# ifndef NSIG |
| 84 |
# ifdef CPPCHECK |
| 85 |
# define NSIG 32 |
| 86 |
# elif defined(MAXSIG) |
| 87 |
# define NSIG (MAXSIG+1) |
| 88 |
# elif defined(_NSIG) |
| 89 |
# define NSIG _NSIG |
| 90 |
# elif defined(__SIGRTMAX) |
| 91 |
# define NSIG (__SIGRTMAX+1) |
| 92 |
# else |
| 93 |
# error define NSIG |
| 94 |
# endif |
| 95 |
# endif /* !NSIG */ |
| 96 |
|
| 97 |
void GC_print_sig_mask(void) |
| 98 |
{ |
| 99 |
sigset_t blocked; |
| 100 |
int i; |
| 101 |
|
| 102 |
if (pthread_sigmask(SIG_BLOCK, NULL, &blocked) != 0) |
| 103 |
ABORT("pthread_sigmask failed"); |
| 104 |
for (i = 1; i < NSIG; i++) { |
| 105 |
if (sigismember(&blocked, i)) |
| 106 |
GC_printf("Signal blocked: %d\n", i); |
| 107 |
} |
| 108 |
} |
| 109 |
#endif /* DEBUG_THREADS */ |
| 110 |
|
| 111 |
/* Remove the signals that we want to allow in thread stopping */ |
| 112 |
/* handler from a set. */ |
| 113 |
STATIC void GC_remove_allowed_signals(sigset_t *set) |
| 114 |
{ |
| 115 |
if (sigdelset(set, SIGINT) != 0 |
| 116 |
|| sigdelset(set, SIGQUIT) != 0 |
| 117 |
|| sigdelset(set, SIGABRT) != 0 |
| 118 |
|| sigdelset(set, SIGTERM) != 0) { |
| 119 |
ABORT("sigdelset failed"); |
| 120 |
} |
| 121 |
|
| 122 |
# ifdef MPROTECT_VDB |
| 123 |
/* Handlers write to the thread structure, which is in the heap, */ |
| 124 |
/* and hence can trigger a protection fault. */ |
| 125 |
if (sigdelset(set, SIGSEGV) != 0 |
| 126 |
# ifdef HAVE_SIGBUS |
| 127 |
|| sigdelset(set, SIGBUS) != 0 |
| 128 |
# endif |
| 129 |
) { |
| 130 |
ABORT("sigdelset failed"); |
| 131 |
} |
| 132 |
# endif |
| 133 |
} |
| 134 |
|
| 135 |
static sigset_t suspend_handler_mask; |
| 136 |
|
| 137 |
#define THREAD_RESTARTED 0x1 |
| 138 |
|
| 139 |
STATIC volatile AO_t GC_stop_count = 0; |
| 140 |
/* Incremented by two (not to alter */ |
| 141 |
/* THREAD_RESTARTED bit) at the beginning of */ |
| 142 |
/* GC_stop_world. */ |
| 143 |
|
| 144 |
STATIC volatile AO_t GC_world_is_stopped = FALSE; |
| 145 |
/* FALSE ==> it is safe for threads to restart, */ |
| 146 |
/* i.e. they will see another suspend signal */ |
| 147 |
/* before they are expected to stop (unless */ |
| 148 |
/* they have stopped voluntarily). */ |
| 149 |
|
| 150 |
#ifndef NO_RETRY_SIGNALS |
| 151 |
/* Any platform could lose signals, so let's be conservative and */ |
| 152 |
/* always enable signals retry logic. */ |
| 153 |
STATIC GC_bool GC_retry_signals = TRUE; |
| 154 |
#else |
| 155 |
STATIC GC_bool GC_retry_signals = FALSE; |
| 156 |
#endif |
| 157 |
|
| 158 |
/* |
| 159 |
* We use signals to stop threads during GC. |
| 160 |
* |
| 161 |
* Suspended threads wait in signal handler for SIG_THR_RESTART. |
| 162 |
* That's more portable than semaphores or condition variables. |
| 163 |
* (We do use sem_post from a signal handler, but that should be portable.) |
| 164 |
* |
| 165 |
* The thread suspension signal SIG_SUSPEND is now defined in gc_priv.h. |
| 166 |
* Note that we can't just stop a thread; we need it to save its stack |
| 167 |
* pointer(s) and acknowledge. |
| 168 |
*/ |
| 169 |
#ifndef SIG_THR_RESTART |
| 170 |
# if defined(GC_HPUX_THREADS) || defined(GC_OSF1_THREADS) \ |
| 171 |
|| defined(GC_NETBSD_THREADS) || defined(GC_USESIGRT_SIGNALS) |
| 172 |
# if defined(_SIGRTMIN) && !defined(CPPCHECK) |
| 173 |
# define SIG_THR_RESTART _SIGRTMIN + 5 |
| 174 |
# else |
| 175 |
# define SIG_THR_RESTART SIGRTMIN + 5 |
| 176 |
# endif |
| 177 |
# elif defined(GC_FREEBSD_THREADS) && defined(__GLIBC__) |
| 178 |
# define SIG_THR_RESTART (32+5) |
| 179 |
# elif defined(GC_FREEBSD_THREADS) || defined(HURD) || defined(RTEMS) |
| 180 |
# define SIG_THR_RESTART SIGUSR2 |
| 181 |
# else |
| 182 |
# define SIG_THR_RESTART SIGXCPU |
| 183 |
# endif |
| 184 |
#endif |
| 185 |
|
| 186 |
#define SIGNAL_UNSET (-1) |
| 187 |
/* Since SIG_SUSPEND and/or SIG_THR_RESTART could represent */ |
| 188 |
/* a non-constant expression (e.g., in case of SIGRTMIN), */ |
| 189 |
/* actual signal numbers are determined by GC_stop_init() */ |
| 190 |
/* unless manually set (before GC initialization). */ |
| 191 |
STATIC int GC_sig_suspend = SIGNAL_UNSET; |
| 192 |
STATIC int GC_sig_thr_restart = SIGNAL_UNSET; |
| 193 |
|
| 194 |
GC_API void GC_CALL GC_set_suspend_signal(int sig) |
| 195 |
{ |
| 196 |
if (GC_is_initialized) return; |
| 197 |
|
| 198 |
GC_sig_suspend = sig; |
| 199 |
} |
| 200 |
|
| 201 |
GC_API void GC_CALL GC_set_thr_restart_signal(int sig) |
| 202 |
{ |
| 203 |
if (GC_is_initialized) return; |
| 204 |
|
| 205 |
GC_sig_thr_restart = sig; |
| 206 |
} |
| 207 |
|
| 208 |
GC_API int GC_CALL GC_get_suspend_signal(void) |
| 209 |
{ |
| 210 |
return GC_sig_suspend != SIGNAL_UNSET ? GC_sig_suspend : SIG_SUSPEND; |
| 211 |
} |
| 212 |
|
| 213 |
GC_API int GC_CALL GC_get_thr_restart_signal(void) |
| 214 |
{ |
| 215 |
return GC_sig_thr_restart != SIGNAL_UNSET |
| 216 |
? GC_sig_thr_restart : SIG_THR_RESTART; |
| 217 |
} |
| 218 |
|
| 219 |
#if defined(GC_EXPLICIT_SIGNALS_UNBLOCK) \ |
| 220 |
|| !defined(NO_SIGNALS_UNBLOCK_IN_MAIN) |
| 221 |
/* Some targets (e.g., Solaris) might require this to be called when */ |
| 222 |
/* doing thread registering from the thread destructor. */ |
| 223 |
GC_INNER void GC_unblock_gc_signals(void) |
| 224 |
{ |
| 225 |
sigset_t set; |
| 226 |
sigemptyset(&set); |
| 227 |
GC_ASSERT(GC_sig_suspend != SIGNAL_UNSET); |
| 228 |
GC_ASSERT(GC_sig_thr_restart != SIGNAL_UNSET); |
| 229 |
sigaddset(&set, GC_sig_suspend); |
| 230 |
sigaddset(&set, GC_sig_thr_restart); |
| 231 |
if (pthread_sigmask(SIG_UNBLOCK, &set, NULL) != 0) |
| 232 |
ABORT("pthread_sigmask failed"); |
| 233 |
} |
| 234 |
#endif /* GC_EXPLICIT_SIGNALS_UNBLOCK */ |
| 235 |
|
| 236 |
STATIC sem_t GC_suspend_ack_sem; /* also used to acknowledge restart */ |
| 237 |
|
| 238 |
STATIC void GC_suspend_handler_inner(ptr_t dummy, void *context); |
| 239 |
|
| 240 |
#ifndef NO_SA_SIGACTION |
| 241 |
STATIC void GC_suspend_handler(int sig, siginfo_t * info GC_ATTR_UNUSED, |
| 242 |
void * context GC_ATTR_UNUSED) |
| 243 |
#else |
| 244 |
STATIC void GC_suspend_handler(int sig) |
| 245 |
#endif |
| 246 |
{ |
| 247 |
int old_errno = errno; |
| 248 |
|
| 249 |
if (sig != GC_sig_suspend) { |
| 250 |
# if defined(GC_FREEBSD_THREADS) |
| 251 |
/* Workaround "deferred signal handling" bug in FreeBSD 9.2. */ |
| 252 |
if (0 == sig) return; |
| 253 |
# endif |
| 254 |
ABORT("Bad signal in suspend_handler"); |
| 255 |
} |
| 256 |
|
| 257 |
# if defined(E2K) || defined(HP_PA) || defined(IA64) || defined(M68K) \ |
| 258 |
|| defined(NO_SA_SIGACTION) |
| 259 |
GC_with_callee_saves_pushed(GC_suspend_handler_inner, NULL); |
| 260 |
# else |
| 261 |
/* We believe that in this case the full context is already */ |
| 262 |
/* in the signal handler frame. */ |
| 263 |
GC_suspend_handler_inner(NULL, context); |
| 264 |
# endif |
| 265 |
errno = old_errno; |
| 266 |
} |
| 267 |
|
| 268 |
#ifdef BASE_ATOMIC_OPS_EMULATED |
| 269 |
/* The AO primitives emulated with locks cannot be used inside signal */ |
| 270 |
/* handlers as this could cause a deadlock or a double lock. */ |
| 271 |
/* The following "async" macro definitions are correct only for */ |
| 272 |
/* an uniprocessor case and are provided for a test purpose. */ |
| 273 |
# define ao_load_acquire_async(p) (*(p)) |
| 274 |
# define ao_load_async(p) ao_load_acquire_async(p) |
| 275 |
# define ao_store_release_async(p, v) (void)(*(p) = (v)) |
| 276 |
# define ao_store_async(p, v) ao_store_release_async(p, v) |
| 277 |
#else |
| 278 |
# define ao_load_acquire_async(p) AO_load_acquire(p) |
| 279 |
# define ao_load_async(p) AO_load(p) |
| 280 |
# define ao_store_release_async(p, v) AO_store_release(p, v) |
| 281 |
# define ao_store_async(p, v) AO_store(p, v) |
| 282 |
#endif /* !BASE_ATOMIC_OPS_EMULATED */ |
| 283 |
|
| 284 |
/* The lookup here is safe, since this is done on behalf */ |
| 285 |
/* of a thread which holds the allocation lock in order */ |
| 286 |
/* to stop the world. Thus concurrent modification of the */ |
| 287 |
/* data structure is impossible. Unfortunately, we have to */ |
| 288 |
/* instruct TSan that the lookup is safe. */ |
| 289 |
#ifdef THREAD_SANITIZER |
| 290 |
/* The implementation of the function is the same as that of */ |
| 291 |
/* GC_lookup_thread except for the attribute added here. */ |
| 292 |
GC_ATTR_NO_SANITIZE_THREAD |
| 293 |
static GC_thread GC_lookup_thread_async(pthread_t id) |
| 294 |
{ |
| 295 |
GC_thread p = GC_threads[THREAD_TABLE_INDEX(id)]; |
| 296 |
|
| 297 |
while (p != NULL && !THREAD_EQUAL(p->id, id)) |
| 298 |
p = p->next; |
| 299 |
return p; |
| 300 |
} |
| 301 |
#else |
| 302 |
# define GC_lookup_thread_async GC_lookup_thread |
| 303 |
#endif |
| 304 |
|
| 305 |
GC_INLINE void GC_store_stack_ptr(GC_thread me) |
| 306 |
{ |
| 307 |
/* There is no data race between the suspend handler (storing */ |
| 308 |
/* stack_ptr) and GC_push_all_stacks (fetching stack_ptr) because */ |
| 309 |
/* GC_push_all_stacks is executed after GC_stop_world exits and the */ |
| 310 |
/* latter runs sem_wait repeatedly waiting for all the suspended */ |
| 311 |
/* threads to call sem_post. Nonetheless, stack_ptr is stored (here) */ |
| 312 |
/* and fetched (by GC_push_all_stacks) using the atomic primitives to */ |
| 313 |
/* avoid the related TSan warning. */ |
| 314 |
# ifdef SPARC |
| 315 |
ao_store_async((volatile AO_t *)&me->stop_info.stack_ptr, |
| 316 |
(AO_t)GC_save_regs_in_stack()); |
| 317 |
/* TODO: regs saving already done by GC_with_callee_saves_pushed */ |
| 318 |
# else |
| 319 |
# ifdef IA64 |
| 320 |
me -> backing_store_ptr = GC_save_regs_in_stack(); |
| 321 |
# endif |
| 322 |
ao_store_async((volatile AO_t *)&me->stop_info.stack_ptr, |
| 323 |
(AO_t)GC_approx_sp()); |
| 324 |
# endif |
| 325 |
} |
| 326 |
|
| 327 |
STATIC void GC_suspend_handler_inner(ptr_t dummy GC_ATTR_UNUSED, |
| 328 |
void * context GC_ATTR_UNUSED) |
| 329 |
{ |
| 330 |
pthread_t self = pthread_self(); |
| 331 |
GC_thread me; |
| 332 |
# ifdef E2K |
| 333 |
ptr_t bs_lo; |
| 334 |
size_t stack_size; |
| 335 |
# endif |
| 336 |
IF_CANCEL(int cancel_state;) |
| 337 |
# ifdef GC_ENABLE_SUSPEND_THREAD |
| 338 |
word suspend_cnt; |
| 339 |
# endif |
| 340 |
AO_t my_stop_count = ao_load_acquire_async(&GC_stop_count); |
| 341 |
/* After the barrier, this thread should see */ |
| 342 |
/* the actual content of GC_threads. */ |
| 343 |
|
| 344 |
DISABLE_CANCEL(cancel_state); |
| 345 |
/* pthread_setcancelstate is not defined to be async-signal-safe. */ |
| 346 |
/* But the glibc version appears to be in the absence of */ |
| 347 |
/* asynchronous cancellation. And since this signal handler */ |
| 348 |
/* to block on sigsuspend, which is both async-signal-safe */ |
| 349 |
/* and a cancellation point, there seems to be no obvious way */ |
| 350 |
/* out of it. In fact, it looks to me like an async-signal-safe */ |
| 351 |
/* cancellation point is inherently a problem, unless there is */ |
| 352 |
/* some way to disable cancellation in the handler. */ |
| 353 |
# ifdef DEBUG_THREADS |
| 354 |
GC_log_printf("Suspending %p\n", (void *)self); |
| 355 |
# endif |
| 356 |
GC_ASSERT(((word)my_stop_count & THREAD_RESTARTED) == 0); |
| 357 |
|
| 358 |
me = GC_lookup_thread_async(self); |
| 359 |
|
| 360 |
# ifdef GC_ENABLE_SUSPEND_THREAD |
| 361 |
# if defined(__GNUC__) && !defined(__clang__) |
| 362 |
/* Workaround "writing 8 bytes into a region of size 0" bogus */ |
| 363 |
/* gcc warning (produced by gcc-12.2.0/aarch64, at least). */ |
| 364 |
if (NULL == me) ABORT("Lookup self failed"); |
| 365 |
# endif |
| 366 |
suspend_cnt = (word)ao_load_async(&(me -> stop_info.ext_suspend_cnt)); |
| 367 |
# endif |
| 368 |
if (((word)me->stop_info.last_stop_count & ~(word)THREAD_RESTARTED) |
| 369 |
== (word)my_stop_count |
| 370 |
# ifdef GC_ENABLE_SUSPEND_THREAD |
| 371 |
&& (suspend_cnt & 1) == 0 |
| 372 |
# endif |
| 373 |
) { |
| 374 |
/* Duplicate signal. OK if we are retrying. */ |
| 375 |
if (!GC_retry_signals) { |
| 376 |
WARN("Duplicate suspend signal in thread %p\n", self); |
| 377 |
} |
| 378 |
RESTORE_CANCEL(cancel_state); |
| 379 |
return; |
| 380 |
} |
| 381 |
GC_store_stack_ptr(me); |
| 382 |
# ifdef E2K |
| 383 |
GC_ASSERT(NULL == me -> backing_store_end); |
| 384 |
GET_PROCEDURE_STACK_LOCAL(&bs_lo, &stack_size); |
| 385 |
me -> backing_store_end = bs_lo; |
| 386 |
me -> backing_store_ptr = bs_lo + stack_size; |
| 387 |
# endif |
| 388 |
|
| 389 |
/* Tell the thread that wants to stop the world that this */ |
| 390 |
/* thread has been stopped. Note that sem_post() is */ |
| 391 |
/* the only async-signal-safe primitive in LinuxThreads. */ |
| 392 |
sem_post(&GC_suspend_ack_sem); |
| 393 |
ao_store_release_async(&me->stop_info.last_stop_count, my_stop_count); |
| 394 |
|
| 395 |
/* Wait until that thread tells us to restart by sending */ |
| 396 |
/* this thread a GC_sig_thr_restart signal (should be masked */ |
| 397 |
/* at this point thus there is no race). */ |
| 398 |
/* We do not continue until we receive that signal, */ |
| 399 |
/* but we do not take that as authoritative. (We may be */ |
| 400 |
/* accidentally restarted by one of the user signals we */ |
| 401 |
/* don't block.) After we receive the signal, we use a */ |
| 402 |
/* primitive and expensive mechanism to wait until it's */ |
| 403 |
/* really safe to proceed. Under normal circumstances, */ |
| 404 |
/* this code should not be executed. */ |
| 405 |
do { |
| 406 |
sigsuspend(&suspend_handler_mask); |
| 407 |
/* Iterate while not restarting the world or thread is suspended. */ |
| 408 |
} while ((ao_load_acquire_async(&GC_world_is_stopped) |
| 409 |
&& ao_load_async(&GC_stop_count) == my_stop_count) |
| 410 |
# ifdef GC_ENABLE_SUSPEND_THREAD |
| 411 |
|| ((suspend_cnt & 1) != 0 |
| 412 |
&& (word)ao_load_async(&(me -> stop_info.ext_suspend_cnt)) |
| 413 |
== suspend_cnt) |
| 414 |
# endif |
| 415 |
); |
| 416 |
|
| 417 |
# ifdef DEBUG_THREADS |
| 418 |
GC_log_printf("Continuing %p\n", (void *)self); |
| 419 |
# endif |
| 420 |
# ifdef E2K |
| 421 |
GC_ASSERT(me -> backing_store_end == bs_lo); |
| 422 |
FREE_PROCEDURE_STACK_LOCAL(bs_lo, stack_size); |
| 423 |
me -> backing_store_ptr = NULL; |
| 424 |
me -> backing_store_end = NULL; |
| 425 |
# endif |
| 426 |
# ifndef GC_NETBSD_THREADS_WORKAROUND |
| 427 |
if (GC_retry_signals) |
| 428 |
# endif |
| 429 |
{ |
| 430 |
/* If the RESTART signal loss is possible (though it should be */ |
| 431 |
/* less likely than losing the SUSPEND signal as we do not do */ |
| 432 |
/* much between the first sem_post and sigsuspend calls), more */ |
| 433 |
/* handshaking is provided to work around it. */ |
| 434 |
sem_post(&GC_suspend_ack_sem); |
| 435 |
# ifdef GC_NETBSD_THREADS_WORKAROUND |
| 436 |
if (GC_retry_signals) |
| 437 |
# endif |
| 438 |
{ |
| 439 |
/* Set the flag that the thread has been restarted. */ |
| 440 |
ao_store_release_async(&me->stop_info.last_stop_count, |
| 441 |
(AO_t)((word)my_stop_count | THREAD_RESTARTED)); |
| 442 |
} |
| 443 |
} |
| 444 |
RESTORE_CANCEL(cancel_state); |
| 445 |
} |
| 446 |
|
| 447 |
static void suspend_restart_barrier(int n_live_threads) |
| 448 |
{ |
| 449 |
int i; |
| 450 |
|
| 451 |
for (i = 0; i < n_live_threads; i++) { |
| 452 |
while (0 != sem_wait(&GC_suspend_ack_sem)) { |
| 453 |
/* On Linux, sem_wait is documented to always return zero. */ |
| 454 |
/* But the documentation appears to be incorrect. */ |
| 455 |
/* EINTR seems to happen with some versions of gdb. */ |
| 456 |
if (errno != EINTR) |
| 457 |
ABORT("sem_wait failed"); |
| 458 |
} |
| 459 |
} |
| 460 |
# ifdef GC_ASSERTIONS |
| 461 |
sem_getvalue(&GC_suspend_ack_sem, &i); |
| 462 |
GC_ASSERT(0 == i); |
| 463 |
# endif |
| 464 |
} |
| 465 |
|
| 466 |
# define WAIT_UNIT 3000 /* us */ |
| 467 |
|
| 468 |
static int resend_lost_signals(int n_live_threads, |
| 469 |
int (*suspend_restart_all)(void)) |
| 470 |
{ |
| 471 |
# define RETRY_INTERVAL 100000 /* us */ |
| 472 |
# define RESEND_SIGNALS_LIMIT 150 |
| 473 |
|
| 474 |
if (n_live_threads > 0) { |
| 475 |
unsigned long wait_usecs = 0; /* Total wait since retry. */ |
| 476 |
int retry = 0; |
| 477 |
int prev_sent = 0; |
| 478 |
|
| 479 |
for (;;) { |
| 480 |
int ack_count; |
| 481 |
|
| 482 |
sem_getvalue(&GC_suspend_ack_sem, &ack_count); |
| 483 |
if (ack_count == n_live_threads) |
| 484 |
break; |
| 485 |
if (wait_usecs > RETRY_INTERVAL) { |
| 486 |
int newly_sent = suspend_restart_all(); |
| 487 |
|
| 488 |
if (newly_sent != prev_sent) { |
| 489 |
retry = 0; /* restart the counter */ |
| 490 |
} else if (++retry >= RESEND_SIGNALS_LIMIT) /* no progress */ |
| 491 |
ABORT_ARG1("Signals delivery fails constantly", |
| 492 |
" at GC #%lu", (unsigned long)GC_gc_no); |
| 493 |
|
| 494 |
GC_COND_LOG_PRINTF("Resent %d signals after timeout, retry: %d\n", |
| 495 |
newly_sent, retry); |
| 496 |
sem_getvalue(&GC_suspend_ack_sem, &ack_count); |
| 497 |
if (newly_sent < n_live_threads - ack_count) { |
| 498 |
WARN("Lost some threads while stopping or starting world?!\n", 0); |
| 499 |
n_live_threads = ack_count + newly_sent; |
| 500 |
} |
| 501 |
prev_sent = newly_sent; |
| 502 |
wait_usecs = 0; |
| 503 |
} |
| 504 |
GC_usleep(WAIT_UNIT); |
| 505 |
wait_usecs += WAIT_UNIT; |
| 506 |
} |
| 507 |
} |
| 508 |
return n_live_threads; |
| 509 |
} |
| 510 |
|
| 511 |
#ifdef HAVE_CLOCK_GETTIME |
| 512 |
# define TS_NSEC_ADD(ts, ns) \ |
| 513 |
(ts.tv_nsec += (ns), \ |
| 514 |
(void)(ts.tv_nsec >= 1000000L*1000 ? \ |
| 515 |
(ts.tv_nsec -= 1000000L*1000, ts.tv_sec++, 0) : 0)) |
| 516 |
#endif |
| 517 |
|
| 518 |
static void resend_lost_signals_retry(int n_live_threads, |
| 519 |
int (*suspend_restart_all)(void)) |
| 520 |
{ |
| 521 |
# if defined(HAVE_CLOCK_GETTIME) && !defined(DONT_TIMEDWAIT_ACK_SEM) |
| 522 |
# define TIMEOUT_BEFORE_RESEND 10000 /* us */ |
| 523 |
struct timespec ts; |
| 524 |
|
| 525 |
if (n_live_threads > 0 && clock_gettime(CLOCK_REALTIME, &ts) == 0) { |
| 526 |
int i; |
| 527 |
|
| 528 |
TS_NSEC_ADD(ts, TIMEOUT_BEFORE_RESEND * 1000); |
| 529 |
/* First, try to wait for the semaphore with some timeout. */ |
| 530 |
/* On failure, fallback to WAIT_UNIT pause and resend of the signal. */ |
| 531 |
for (i = 0; i < n_live_threads; i++) { |
| 532 |
if (0 != sem_timedwait(&GC_suspend_ack_sem, &ts)) |
| 533 |
break; /* Wait timed out or any other error. */ |
| 534 |
} |
| 535 |
/* Update the count of threads to wait the ack from. */ |
| 536 |
n_live_threads -= i; |
| 537 |
} |
| 538 |
# endif |
| 539 |
n_live_threads = resend_lost_signals(n_live_threads, suspend_restart_all); |
| 540 |
suspend_restart_barrier(n_live_threads); |
| 541 |
} |
| 542 |
|
| 543 |
STATIC void GC_restart_handler(int sig) |
| 544 |
{ |
| 545 |
# if defined(DEBUG_THREADS) |
| 546 |
int old_errno = errno; /* Preserve errno value. */ |
| 547 |
# endif |
| 548 |
|
| 549 |
if (sig != GC_sig_thr_restart) |
| 550 |
ABORT("Bad signal in restart handler"); |
| 551 |
|
| 552 |
/* |
| 553 |
** Note: even if we don't do anything useful here, |
| 554 |
** it would still be necessary to have a signal handler, |
| 555 |
** rather than ignoring the signals, otherwise |
| 556 |
** the signals will not be delivered at all, and |
| 557 |
** will thus not interrupt the sigsuspend() above. |
| 558 |
*/ |
| 559 |
# ifdef DEBUG_THREADS |
| 560 |
GC_log_printf("In GC_restart_handler for %p\n", (void *)pthread_self()); |
| 561 |
errno = old_errno; |
| 562 |
# endif |
| 563 |
} |
| 564 |
|
| 565 |
# ifdef USE_TKILL_ON_ANDROID |
| 566 |
EXTERN_C_BEGIN |
| 567 |
extern int tkill(pid_t tid, int sig); /* from sys/linux-unistd.h */ |
| 568 |
EXTERN_C_END |
| 569 |
# define THREAD_SYSTEM_ID(t) (t)->kernel_id |
| 570 |
# else |
| 571 |
# define THREAD_SYSTEM_ID(t) (t)->id |
| 572 |
# endif |
| 573 |
|
| 574 |
# ifndef RETRY_TKILL_EAGAIN_LIMIT |
| 575 |
# define RETRY_TKILL_EAGAIN_LIMIT 16 |
| 576 |
# endif |
| 577 |
|
| 578 |
static int raise_signal(GC_thread p, int sig) |
| 579 |
{ |
| 580 |
int res; |
| 581 |
# ifdef RETRY_TKILL_ON_EAGAIN |
| 582 |
int retry; |
| 583 |
|
| 584 |
for (retry = 0; ; retry++) |
| 585 |
# endif |
| 586 |
{ |
| 587 |
# ifdef USE_TKILL_ON_ANDROID |
| 588 |
int old_errno = errno; |
| 589 |
|
| 590 |
res = tkill(THREAD_SYSTEM_ID(p), sig); |
| 591 |
if (res < 0) { |
| 592 |
res = errno; |
| 593 |
errno = old_errno; |
| 594 |
} |
| 595 |
# else |
| 596 |
res = pthread_kill(THREAD_SYSTEM_ID(p), sig); |
| 597 |
# endif |
| 598 |
# ifdef RETRY_TKILL_ON_EAGAIN |
| 599 |
if (res != EAGAIN || retry >= RETRY_TKILL_EAGAIN_LIMIT) break; |
| 600 |
/* A temporal overflow of the real-time signal queue. */ |
| 601 |
GC_usleep(WAIT_UNIT); |
| 602 |
# endif |
| 603 |
} |
| 604 |
return res; |
| 605 |
} |
| 606 |
|
| 607 |
# ifdef GC_ENABLE_SUSPEND_THREAD |
| 608 |
# include <sys/time.h> |
| 609 |
# include "javaxfc.h" /* to get the prototypes as extern "C" */ |
| 610 |
|
| 611 |
STATIC void GC_brief_async_signal_safe_sleep(void) |
| 612 |
{ |
| 613 |
struct timeval tv; |
| 614 |
tv.tv_sec = 0; |
| 615 |
# if defined(GC_TIME_LIMIT) && !defined(CPPCHECK) |
| 616 |
tv.tv_usec = 1000 * GC_TIME_LIMIT / 2; |
| 617 |
# else |
| 618 |
tv.tv_usec = 1000 * 50 / 2; |
| 619 |
# endif |
| 620 |
(void)select(0, 0, 0, 0, &tv); |
| 621 |
} |
| 622 |
|
| 623 |
GC_INNER void GC_suspend_self_inner(GC_thread me, word suspend_cnt) { |
| 624 |
IF_CANCEL(int cancel_state;) |
| 625 |
|
| 626 |
GC_ASSERT((suspend_cnt & 1) != 0); |
| 627 |
DISABLE_CANCEL(cancel_state); |
| 628 |
# ifdef DEBUG_THREADS |
| 629 |
GC_log_printf("Suspend self: %p\n", (void *)(me -> id)); |
| 630 |
# endif |
| 631 |
while ((word)ao_load_acquire_async(&(me -> stop_info.ext_suspend_cnt)) |
| 632 |
== suspend_cnt) { |
| 633 |
/* TODO: Use sigsuspend() even for self-suspended threads. */ |
| 634 |
GC_brief_async_signal_safe_sleep(); |
| 635 |
} |
| 636 |
# ifdef DEBUG_THREADS |
| 637 |
GC_log_printf("Resume self: %p\n", (void *)(me -> id)); |
| 638 |
# endif |
| 639 |
RESTORE_CANCEL(cancel_state); |
| 640 |
} |
| 641 |
|
| 642 |
GC_API void GC_CALL GC_suspend_thread(GC_SUSPEND_THREAD_ID thread) { |
| 643 |
GC_thread t; |
| 644 |
word suspend_cnt; |
| 645 |
IF_CANCEL(int cancel_state;) |
| 646 |
DCL_LOCK_STATE; |
| 647 |
|
| 648 |
LOCK(); |
| 649 |
t = GC_lookup_thread((pthread_t)thread); |
| 650 |
if (NULL == t) { |
| 651 |
UNLOCK(); |
| 652 |
return; |
| 653 |
} |
| 654 |
suspend_cnt = (word)(t -> stop_info.ext_suspend_cnt); |
| 655 |
if ((suspend_cnt & 1) != 0) /* already suspended? */ { |
| 656 |
GC_ASSERT(!THREAD_EQUAL((pthread_t)thread, pthread_self())); |
| 657 |
UNLOCK(); |
| 658 |
return; |
| 659 |
} |
| 660 |
if ((t -> flags & FINISHED) != 0 || t -> thread_blocked) { |
| 661 |
t -> stop_info.ext_suspend_cnt = (AO_t)(suspend_cnt | 1); /* suspend */ |
| 662 |
/* Terminated but not joined yet, or in do-blocking state. */ |
| 663 |
UNLOCK(); |
| 664 |
return; |
| 665 |
} |
| 666 |
|
| 667 |
if (THREAD_EQUAL((pthread_t)thread, pthread_self())) { |
| 668 |
t -> stop_info.ext_suspend_cnt = (AO_t)(suspend_cnt | 1); |
| 669 |
GC_with_callee_saves_pushed(GC_suspend_self_blocked, (ptr_t)t); |
| 670 |
UNLOCK(); |
| 671 |
return; |
| 672 |
} |
| 673 |
|
| 674 |
DISABLE_CANCEL(cancel_state); |
| 675 |
/* GC_suspend_thread is not a cancellation point. */ |
| 676 |
# ifdef PARALLEL_MARK |
| 677 |
/* Ensure we do not suspend a thread while it is rebuilding */ |
| 678 |
/* a free list, otherwise such a dead-lock is possible: */ |
| 679 |
/* thread 1 is blocked in GC_wait_for_reclaim holding */ |
| 680 |
/* the allocation lock, thread 2 is suspended in */ |
| 681 |
/* GC_reclaim_generic invoked from GC_generic_malloc_many */ |
| 682 |
/* (with GC_fl_builder_count > 0), and thread 3 is blocked */ |
| 683 |
/* acquiring the allocation lock in GC_resume_thread. */ |
| 684 |
if (GC_parallel) |
| 685 |
GC_wait_for_reclaim(); |
| 686 |
# endif |
| 687 |
|
| 688 |
if (GC_manual_vdb) { |
| 689 |
/* See the relevant comment in GC_stop_world. */ |
| 690 |
GC_acquire_dirty_lock(); |
| 691 |
} |
| 692 |
/* Else do not acquire the lock as the write fault handler might */ |
| 693 |
/* be trying to acquire this lock too, and the suspend handler */ |
| 694 |
/* execution is deferred until the write fault handler completes. */ |
| 695 |
|
| 696 |
/* Set the flag making the change visible to the signal handler. */ |
| 697 |
AO_store_release(&(t -> stop_info.ext_suspend_cnt), |
| 698 |
(AO_t)(suspend_cnt | 1)); |
| 699 |
|
| 700 |
/* TODO: Support GC_retry_signals (not needed for TSan) */ |
| 701 |
switch (raise_signal(t, GC_sig_suspend)) { |
| 702 |
/* ESRCH cannot happen as terminated threads are handled above. */ |
| 703 |
case 0: |
| 704 |
break; |
| 705 |
default: |
| 706 |
ABORT("pthread_kill failed"); |
| 707 |
} |
| 708 |
|
| 709 |
/* Wait for the thread to complete threads table lookup and */ |
| 710 |
/* stack_ptr assignment. */ |
| 711 |
GC_ASSERT(GC_thr_initialized); |
| 712 |
suspend_restart_barrier(1); |
| 713 |
if (GC_manual_vdb) |
| 714 |
GC_release_dirty_lock(); |
| 715 |
RESTORE_CANCEL(cancel_state); |
| 716 |
UNLOCK(); |
| 717 |
} |
| 718 |
|
| 719 |
GC_API void GC_CALL GC_resume_thread(GC_SUSPEND_THREAD_ID thread) { |
| 720 |
GC_thread t; |
| 721 |
DCL_LOCK_STATE; |
| 722 |
|
| 723 |
LOCK(); |
| 724 |
t = GC_lookup_thread((pthread_t)thread); |
| 725 |
if (t != NULL) { |
| 726 |
word suspend_cnt = (word)(t -> stop_info.ext_suspend_cnt); |
| 727 |
|
| 728 |
if ((suspend_cnt & 1) != 0) /* is suspended? */ { |
| 729 |
/* Mark the thread as not suspended - it will be resumed shortly. */ |
| 730 |
AO_store(&(t -> stop_info.ext_suspend_cnt), (AO_t)(suspend_cnt + 1)); |
| 731 |
|
| 732 |
if ((t -> flags & FINISHED) == 0 && !(t -> thread_blocked)) { |
| 733 |
int result = raise_signal(t, GC_sig_thr_restart); |
| 734 |
|
| 735 |
/* TODO: Support signal resending on GC_retry_signals */ |
| 736 |
if (result != 0) |
| 737 |
ABORT_ARG1("pthread_kill failed in GC_resume_thread", |
| 738 |
": errcode= %d", result); |
| 739 |
# ifndef GC_NETBSD_THREADS_WORKAROUND |
| 740 |
if (GC_retry_signals) |
| 741 |
# endif |
| 742 |
{ |
| 743 |
IF_CANCEL(int cancel_state;) |
| 744 |
|
| 745 |
DISABLE_CANCEL(cancel_state); |
| 746 |
suspend_restart_barrier(1); |
| 747 |
RESTORE_CANCEL(cancel_state); |
| 748 |
} |
| 749 |
} |
| 750 |
} |
| 751 |
} |
| 752 |
UNLOCK(); |
| 753 |
} |
| 754 |
|
| 755 |
GC_API int GC_CALL GC_is_thread_suspended(GC_SUSPEND_THREAD_ID thread) { |
| 756 |
GC_thread t; |
| 757 |
int is_suspended = 0; |
| 758 |
DCL_LOCK_STATE; |
| 759 |
|
| 760 |
LOCK(); |
| 761 |
t = GC_lookup_thread((pthread_t)thread); |
| 762 |
if (t != NULL && (t -> stop_info.ext_suspend_cnt & 1) != 0) |
| 763 |
is_suspended = (int)TRUE; |
| 764 |
UNLOCK(); |
| 765 |
return is_suspended; |
| 766 |
} |
| 767 |
# endif /* GC_ENABLE_SUSPEND_THREAD */ |
| 768 |
|
| 769 |
# undef ao_load_acquire_async |
| 770 |
# undef ao_load_async |
| 771 |
# undef ao_store_async |
| 772 |
# undef ao_store_release_async |
| 773 |
#endif /* !GC_OPENBSD_UTHREADS && !NACL */ |
| 774 |
|
| 775 |
/* We hold allocation lock. Should do exactly the right thing if the */ |
| 776 |
/* world is stopped. Should not fail if it isn't. */ |
| 777 |
GC_INNER void GC_push_all_stacks(void) |
| 778 |
{ |
| 779 |
GC_bool found_me = FALSE; |
| 780 |
size_t nthreads = 0; |
| 781 |
int i; |
| 782 |
GC_thread p; |
| 783 |
ptr_t lo; /* stack top (sp) */ |
| 784 |
ptr_t hi; /* bottom */ |
| 785 |
# if defined(E2K) || defined(IA64) |
| 786 |
/* We also need to scan the register backing store. */ |
| 787 |
ptr_t bs_lo, bs_hi; |
| 788 |
# endif |
| 789 |
struct GC_traced_stack_sect_s *traced_stack_sect; |
| 790 |
pthread_t self = pthread_self(); |
| 791 |
word total_size = 0; |
| 792 |
# ifdef E2K |
| 793 |
GC_bool is_stopped = (GC_bool)GC_world_is_stopped; |
| 794 |
# endif |
| 795 |
|
| 796 |
if (!EXPECT(GC_thr_initialized, TRUE)) |
| 797 |
GC_thr_init(); |
| 798 |
# ifdef DEBUG_THREADS |
| 799 |
GC_log_printf("Pushing stacks from thread %p\n", (void *)self); |
| 800 |
# endif |
| 801 |
for (i = 0; i < THREAD_TABLE_SZ; i++) { |
| 802 |
for (p = GC_threads[i]; p != 0; p = p -> next) { |
| 803 |
if (p -> flags & FINISHED) continue; |
| 804 |
++nthreads; |
| 805 |
traced_stack_sect = p -> traced_stack_sect; |
| 806 |
if (THREAD_EQUAL(p -> id, self)) { |
| 807 |
GC_ASSERT(!p->thread_blocked); |
| 808 |
# ifdef SPARC |
| 809 |
lo = GC_save_regs_in_stack(); |
| 810 |
# else |
| 811 |
lo = GC_approx_sp(); |
| 812 |
# ifdef IA64 |
| 813 |
bs_hi = GC_save_regs_in_stack(); |
| 814 |
# elif defined(E2K) |
| 815 |
GC_ASSERT(NULL == p -> backing_store_end); |
| 816 |
(void)GC_save_regs_in_stack(); |
| 817 |
{ |
| 818 |
size_t stack_size; |
| 819 |
GET_PROCEDURE_STACK_LOCAL(&bs_lo, &stack_size); |
| 820 |
bs_hi = bs_lo + stack_size; |
| 821 |
} |
| 822 |
# endif |
| 823 |
# endif |
| 824 |
found_me = TRUE; |
| 825 |
} else { |
| 826 |
lo = (ptr_t)AO_load((volatile AO_t *)&p->stop_info.stack_ptr); |
| 827 |
# ifdef IA64 |
| 828 |
bs_hi = p -> backing_store_ptr; |
| 829 |
# elif defined(E2K) |
| 830 |
bs_lo = p -> backing_store_end; |
| 831 |
bs_hi = p -> backing_store_ptr; |
| 832 |
# endif |
| 833 |
if (traced_stack_sect != NULL |
| 834 |
&& traced_stack_sect->saved_stack_ptr == lo) { |
| 835 |
/* If the thread has never been stopped since the recent */ |
| 836 |
/* GC_call_with_gc_active invocation then skip the top */ |
| 837 |
/* "stack section" as stack_ptr already points to. */ |
| 838 |
traced_stack_sect = traced_stack_sect->prev; |
| 839 |
} |
| 840 |
} |
| 841 |
if ((p -> flags & MAIN_THREAD) == 0) { |
| 842 |
hi = p -> stack_end; |
| 843 |
# ifdef IA64 |
| 844 |
bs_lo = p -> backing_store_end; |
| 845 |
# endif |
| 846 |
} else { |
| 847 |
/* The original stack. */ |
| 848 |
hi = GC_stackbottom; |
| 849 |
# ifdef IA64 |
| 850 |
bs_lo = BACKING_STORE_BASE; |
| 851 |
# endif |
| 852 |
} |
| 853 |
# ifdef DEBUG_THREADS |
| 854 |
# ifdef STACK_GROWS_UP |
| 855 |
GC_log_printf("Stack for thread %p is (%p,%p]\n", |
| 856 |
(void *)(p -> id), (void *)hi, (void *)lo); |
| 857 |
# else |
| 858 |
GC_log_printf("Stack for thread %p is [%p,%p)\n", |
| 859 |
(void *)(p -> id), (void *)lo, (void *)hi); |
| 860 |
# endif |
| 861 |
# endif |
| 862 |
if (0 == lo) ABORT("GC_push_all_stacks: sp not set!"); |
| 863 |
if (p->altstack != NULL && (word)p->altstack <= (word)lo |
| 864 |
&& (word)lo <= (word)p->altstack + p->altstack_size) { |
| 865 |
# ifdef STACK_GROWS_UP |
| 866 |
hi = p->altstack; |
| 867 |
# else |
| 868 |
hi = p->altstack + p->altstack_size; |
| 869 |
# endif |
| 870 |
/* FIXME: Need to scan the normal stack too, but how ? */ |
| 871 |
} |
| 872 |
# ifdef STACKPTR_CORRECTOR_AVAILABLE |
| 873 |
if (GC_sp_corrector != 0) |
| 874 |
GC_sp_corrector((void **)&lo, (void *)(p -> id)); |
| 875 |
# endif |
| 876 |
GC_push_all_stack_sections(lo, hi, traced_stack_sect); |
| 877 |
# ifdef STACK_GROWS_UP |
| 878 |
total_size += lo - hi; |
| 879 |
# else |
| 880 |
total_size += hi - lo; /* lo <= hi */ |
| 881 |
# endif |
| 882 |
# ifdef NACL |
| 883 |
/* Push reg_storage as roots, this will cover the reg context. */ |
| 884 |
GC_push_all_stack((ptr_t)p -> stop_info.reg_storage, |
| 885 |
(ptr_t)(p -> stop_info.reg_storage + NACL_GC_REG_STORAGE_SIZE)); |
| 886 |
total_size += NACL_GC_REG_STORAGE_SIZE * sizeof(ptr_t); |
| 887 |
# endif |
| 888 |
# ifdef E2K |
| 889 |
if (!is_stopped && !p->thread_blocked |
| 890 |
# ifdef GC_ENABLE_SUSPEND_THREAD |
| 891 |
&& (p -> stop_info.ext_suspend_cnt & 1) == 0 |
| 892 |
# endif |
| 893 |
&& !THREAD_EQUAL(p -> id, self)) |
| 894 |
continue; /* procedure stack buffer has already been freed */ |
| 895 |
# endif |
| 896 |
# if defined(E2K) || defined(IA64) |
| 897 |
# ifdef DEBUG_THREADS |
| 898 |
GC_log_printf("Reg stack for thread %p is [%p,%p)\n", |
| 899 |
(void *)(p -> id), (void *)bs_lo, (void *)bs_hi); |
| 900 |
# endif |
| 901 |
GC_ASSERT(bs_lo != NULL && bs_hi != NULL); |
| 902 |
/* FIXME: This (if p->id==self) may add an unbounded number of */ |
| 903 |
/* entries, and hence overflow the mark stack, which is bad. */ |
| 904 |
GC_push_all_register_sections(bs_lo, bs_hi, |
| 905 |
THREAD_EQUAL(p -> id, self), |
| 906 |
traced_stack_sect); |
| 907 |
total_size += bs_hi - bs_lo; /* bs_lo <= bs_hi */ |
| 908 |
# endif |
| 909 |
# ifdef E2K |
| 910 |
if (THREAD_EQUAL(p -> id, self)) |
| 911 |
FREE_PROCEDURE_STACK_LOCAL(bs_lo, (size_t)(bs_hi - bs_lo)); |
| 912 |
# endif |
| 913 |
} |
| 914 |
} |
| 915 |
GC_VERBOSE_LOG_PRINTF("Pushed %d thread stacks\n", (int)nthreads); |
| 916 |
if (!found_me && !GC_in_thread_creation) |
| 917 |
ABORT("Collecting from unknown thread"); |
| 918 |
GC_total_stacksize = total_size; |
| 919 |
} |
| 920 |
|
| 921 |
#ifdef DEBUG_THREADS |
| 922 |
/* There seems to be a very rare thread stopping problem. To help us */ |
| 923 |
/* debug that, we save the ids of the stopping thread. */ |
| 924 |
pthread_t GC_stopping_thread; |
| 925 |
int GC_stopping_pid = 0; |
| 926 |
#endif |
| 927 |
|
| 928 |
/* We hold the allocation lock. Suspend all threads that might */ |
| 929 |
/* still be running. Return the number of suspend signals that */ |
| 930 |
/* were sent. */ |
| 931 |
STATIC int GC_suspend_all(void) |
| 932 |
{ |
| 933 |
int n_live_threads = 0; |
| 934 |
int i; |
| 935 |
# ifndef NACL |
| 936 |
GC_thread p; |
| 937 |
# ifndef GC_OPENBSD_UTHREADS |
| 938 |
int result; |
| 939 |
# endif |
| 940 |
pthread_t self = pthread_self(); |
| 941 |
|
| 942 |
for (i = 0; i < THREAD_TABLE_SZ; i++) { |
| 943 |
for (p = GC_threads[i]; p != 0; p = p -> next) { |
| 944 |
if (!THREAD_EQUAL(p -> id, self)) { |
| 945 |
if ((p -> flags & FINISHED) != 0) continue; |
| 946 |
if (p -> thread_blocked) /* Will wait */ continue; |
| 947 |
# ifndef GC_OPENBSD_UTHREADS |
| 948 |
# ifdef GC_ENABLE_SUSPEND_THREAD |
| 949 |
if ((p -> stop_info.ext_suspend_cnt & 1) != 0) continue; |
| 950 |
# endif |
| 951 |
if (AO_load(&p->stop_info.last_stop_count) == GC_stop_count) |
| 952 |
continue; /* matters only if GC_retry_signals */ |
| 953 |
n_live_threads++; |
| 954 |
# endif |
| 955 |
# ifdef DEBUG_THREADS |
| 956 |
GC_log_printf("Sending suspend signal to %p\n", (void *)p->id); |
| 957 |
# endif |
| 958 |
|
| 959 |
# ifdef GC_OPENBSD_UTHREADS |
| 960 |
{ |
| 961 |
stack_t stack; |
| 962 |
|
| 963 |
GC_acquire_dirty_lock(); |
| 964 |
if (pthread_suspend_np(p -> id) != 0) |
| 965 |
ABORT("pthread_suspend_np failed"); |
| 966 |
GC_release_dirty_lock(); |
| 967 |
if (pthread_stackseg_np(p->id, &stack)) |
| 968 |
ABORT("pthread_stackseg_np failed"); |
| 969 |
p -> stop_info.stack_ptr = (ptr_t)stack.ss_sp - stack.ss_size; |
| 970 |
if (GC_on_thread_event) |
| 971 |
GC_on_thread_event(GC_EVENT_THREAD_SUSPENDED, |
| 972 |
(void *)p->id); |
| 973 |
} |
| 974 |
# else |
| 975 |
/* The synchronization between GC_dirty (based on */ |
| 976 |
/* test-and-set) and the signal-based thread suspension */ |
| 977 |
/* is performed in GC_stop_world because */ |
| 978 |
/* GC_release_dirty_lock cannot be called before */ |
| 979 |
/* acknowledging the thread is really suspended. */ |
| 980 |
result = raise_signal(p, GC_sig_suspend); |
| 981 |
switch(result) { |
| 982 |
case ESRCH: |
| 983 |
/* Not really there anymore. Possible? */ |
| 984 |
n_live_threads--; |
| 985 |
break; |
| 986 |
case 0: |
| 987 |
if (GC_on_thread_event) |
| 988 |
GC_on_thread_event(GC_EVENT_THREAD_SUSPENDED, |
| 989 |
(void *)(word)THREAD_SYSTEM_ID(p)); |
| 990 |
/* Note: thread id might be truncated. */ |
| 991 |
break; |
| 992 |
default: |
| 993 |
ABORT_ARG1("pthread_kill failed at suspend", |
| 994 |
": errcode= %d", result); |
| 995 |
} |
| 996 |
# endif |
| 997 |
} |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
# else /* NACL */ |
| 1002 |
# ifndef NACL_PARK_WAIT_USEC |
| 1003 |
# define NACL_PARK_WAIT_USEC 100 /* us */ |
| 1004 |
# endif |
| 1005 |
unsigned long num_sleeps = 0; |
| 1006 |
|
| 1007 |
# ifdef DEBUG_THREADS |
| 1008 |
GC_log_printf("pthread_stop_world: number of threads: %d\n", |
| 1009 |
GC_nacl_num_gc_threads - 1); |
| 1010 |
# endif |
| 1011 |
GC_nacl_thread_parker = pthread_self(); |
| 1012 |
GC_nacl_park_threads_now = 1; |
| 1013 |
|
| 1014 |
if (GC_manual_vdb) |
| 1015 |
GC_acquire_dirty_lock(); |
| 1016 |
for (;;) { |
| 1017 |
int num_threads_parked = 0; |
| 1018 |
int num_used = 0; |
| 1019 |
|
| 1020 |
/* Check the 'parked' flag for each thread the GC knows about. */ |
| 1021 |
for (i = 0; i < MAX_NACL_GC_THREADS |
| 1022 |
&& num_used < GC_nacl_num_gc_threads; i++) { |
| 1023 |
if (GC_nacl_thread_used[i] == 1) { |
| 1024 |
num_used++; |
| 1025 |
if (GC_nacl_thread_parked[i] == 1) { |
| 1026 |
num_threads_parked++; |
| 1027 |
if (GC_on_thread_event) |
| 1028 |
GC_on_thread_event(GC_EVENT_THREAD_SUSPENDED, (void *)(word)i); |
| 1029 |
} |
| 1030 |
} |
| 1031 |
} |
| 1032 |
/* -1 for the current thread. */ |
| 1033 |
if (num_threads_parked >= GC_nacl_num_gc_threads - 1) |
| 1034 |
break; |
| 1035 |
# ifdef DEBUG_THREADS |
| 1036 |
GC_log_printf("Sleep waiting for %d threads to park...\n", |
| 1037 |
GC_nacl_num_gc_threads - num_threads_parked - 1); |
| 1038 |
# endif |
| 1039 |
GC_usleep(NACL_PARK_WAIT_USEC); |
| 1040 |
if (++num_sleeps > (1000 * 1000) / NACL_PARK_WAIT_USEC) { |
| 1041 |
WARN("GC appears stalled waiting for %" WARN_PRIdPTR |
| 1042 |
" threads to park...\n", |
| 1043 |
GC_nacl_num_gc_threads - num_threads_parked - 1); |
| 1044 |
num_sleeps = 0; |
| 1045 |
} |
| 1046 |
} |
| 1047 |
if (GC_manual_vdb) |
| 1048 |
GC_release_dirty_lock(); |
| 1049 |
# endif /* NACL */ |
| 1050 |
return n_live_threads; |
| 1051 |
} |
| 1052 |
|
| 1053 |
GC_INNER void GC_stop_world(void) |
| 1054 |
{ |
| 1055 |
# if !defined(GC_OPENBSD_UTHREADS) && !defined(NACL) |
| 1056 |
int n_live_threads; |
| 1057 |
# endif |
| 1058 |
GC_ASSERT(I_HOLD_LOCK()); |
| 1059 |
# ifdef DEBUG_THREADS |
| 1060 |
GC_stopping_thread = pthread_self(); |
| 1061 |
GC_stopping_pid = getpid(); |
| 1062 |
GC_log_printf("Stopping the world from %p\n", (void *)GC_stopping_thread); |
| 1063 |
# endif |
| 1064 |
|
| 1065 |
/* Make sure all free list construction has stopped before we start. */ |
| 1066 |
/* No new construction can start, since free list construction is */ |
| 1067 |
/* required to acquire and release the GC lock before it starts, */ |
| 1068 |
/* and we have the lock. */ |
| 1069 |
# ifdef PARALLEL_MARK |
| 1070 |
if (GC_parallel) { |
| 1071 |
GC_acquire_mark_lock(); |
| 1072 |
GC_ASSERT(GC_fl_builder_count == 0); |
| 1073 |
/* We should have previously waited for it to become zero. */ |
| 1074 |
} |
| 1075 |
# endif /* PARALLEL_MARK */ |
| 1076 |
|
| 1077 |
# if defined(GC_OPENBSD_UTHREADS) || defined(NACL) |
| 1078 |
(void)GC_suspend_all(); |
| 1079 |
# else |
| 1080 |
AO_store(&GC_stop_count, |
| 1081 |
(AO_t)((word)GC_stop_count + (THREAD_RESTARTED+1))); |
| 1082 |
/* Only concurrent reads are possible. */ |
| 1083 |
if (GC_manual_vdb) { |
| 1084 |
GC_acquire_dirty_lock(); |
| 1085 |
/* The write fault handler cannot be called if GC_manual_vdb */ |
| 1086 |
/* (thus double-locking should not occur in */ |
| 1087 |
/* async_set_pht_entry_from_index based on test-and-set). */ |
| 1088 |
} |
| 1089 |
AO_store_release(&GC_world_is_stopped, TRUE); |
| 1090 |
n_live_threads = GC_suspend_all(); |
| 1091 |
if (GC_retry_signals) { |
| 1092 |
resend_lost_signals_retry(n_live_threads, GC_suspend_all); |
| 1093 |
} else { |
| 1094 |
suspend_restart_barrier(n_live_threads); |
| 1095 |
} |
| 1096 |
if (GC_manual_vdb) |
| 1097 |
GC_release_dirty_lock(); /* cannot be done in GC_suspend_all */ |
| 1098 |
# endif |
| 1099 |
|
| 1100 |
# ifdef PARALLEL_MARK |
| 1101 |
if (GC_parallel) |
| 1102 |
GC_release_mark_lock(); |
| 1103 |
# endif |
| 1104 |
# ifdef DEBUG_THREADS |
| 1105 |
GC_log_printf("World stopped from %p\n", (void *)pthread_self()); |
| 1106 |
GC_stopping_thread = 0; |
| 1107 |
# endif |
| 1108 |
} |
| 1109 |
|
| 1110 |
#ifdef NACL |
| 1111 |
# if defined(__x86_64__) |
| 1112 |
# define NACL_STORE_REGS() \ |
| 1113 |
do { \ |
| 1114 |
__asm__ __volatile__ ("push %rbx"); \ |
| 1115 |
__asm__ __volatile__ ("push %rbp"); \ |
| 1116 |
__asm__ __volatile__ ("push %r12"); \ |
| 1117 |
__asm__ __volatile__ ("push %r13"); \ |
| 1118 |
__asm__ __volatile__ ("push %r14"); \ |
| 1119 |
__asm__ __volatile__ ("push %r15"); \ |
| 1120 |
__asm__ __volatile__ ("mov %%esp, %0" \ |
| 1121 |
: "=m" (GC_nacl_gc_thread_self->stop_info.stack_ptr)); \ |
| 1122 |
BCOPY(GC_nacl_gc_thread_self->stop_info.stack_ptr, \ |
| 1123 |
GC_nacl_gc_thread_self->stop_info.reg_storage, \ |
| 1124 |
NACL_GC_REG_STORAGE_SIZE * sizeof(ptr_t)); \ |
| 1125 |
__asm__ __volatile__ ("naclasp $48, %r15"); \ |
| 1126 |
} while (0) |
| 1127 |
# elif defined(__i386__) |
| 1128 |
# define NACL_STORE_REGS() \ |
| 1129 |
do { \ |
| 1130 |
__asm__ __volatile__ ("push %ebx"); \ |
| 1131 |
__asm__ __volatile__ ("push %ebp"); \ |
| 1132 |
__asm__ __volatile__ ("push %esi"); \ |
| 1133 |
__asm__ __volatile__ ("push %edi"); \ |
| 1134 |
__asm__ __volatile__ ("mov %%esp, %0" \ |
| 1135 |
: "=m" (GC_nacl_gc_thread_self->stop_info.stack_ptr)); \ |
| 1136 |
BCOPY(GC_nacl_gc_thread_self->stop_info.stack_ptr, \ |
| 1137 |
GC_nacl_gc_thread_self->stop_info.reg_storage, \ |
| 1138 |
NACL_GC_REG_STORAGE_SIZE * sizeof(ptr_t));\ |
| 1139 |
__asm__ __volatile__ ("add $16, %esp"); \ |
| 1140 |
} while (0) |
| 1141 |
# elif defined(__arm__) |
| 1142 |
# define NACL_STORE_REGS() \ |
| 1143 |
do { \ |
| 1144 |
__asm__ __volatile__ ("push {r4-r8,r10-r12,lr}"); \ |
| 1145 |
__asm__ __volatile__ ("mov r0, %0" \ |
| 1146 |
: : "r" (&GC_nacl_gc_thread_self->stop_info.stack_ptr)); \ |
| 1147 |
__asm__ __volatile__ ("bic r0, r0, #0xc0000000"); \ |
| 1148 |
__asm__ __volatile__ ("str sp, [r0]"); \ |
| 1149 |
BCOPY(GC_nacl_gc_thread_self->stop_info.stack_ptr, \ |
| 1150 |
GC_nacl_gc_thread_self->stop_info.reg_storage, \ |
| 1151 |
NACL_GC_REG_STORAGE_SIZE * sizeof(ptr_t)); \ |
| 1152 |
__asm__ __volatile__ ("add sp, sp, #40"); \ |
| 1153 |
__asm__ __volatile__ ("bic sp, sp, #0xc0000000"); \ |
| 1154 |
} while (0) |
| 1155 |
# else |
| 1156 |
# error TODO Please port NACL_STORE_REGS |
| 1157 |
# endif |
| 1158 |
|
| 1159 |
GC_API_OSCALL void nacl_pre_syscall_hook(void) |
| 1160 |
{ |
| 1161 |
if (GC_nacl_thread_idx != -1) { |
| 1162 |
NACL_STORE_REGS(); |
| 1163 |
GC_nacl_gc_thread_self->stop_info.stack_ptr = GC_approx_sp(); |
| 1164 |
GC_nacl_thread_parked[GC_nacl_thread_idx] = 1; |
| 1165 |
} |
| 1166 |
} |
| 1167 |
|
| 1168 |
GC_API_OSCALL void __nacl_suspend_thread_if_needed(void) |
| 1169 |
{ |
| 1170 |
if (GC_nacl_park_threads_now) { |
| 1171 |
pthread_t self = pthread_self(); |
| 1172 |
|
| 1173 |
/* Don't try to park the thread parker. */ |
| 1174 |
if (GC_nacl_thread_parker == self) |
| 1175 |
return; |
| 1176 |
|
| 1177 |
/* This can happen when a thread is created outside of the GC */ |
| 1178 |
/* system (wthread mostly). */ |
| 1179 |
if (GC_nacl_thread_idx < 0) |
| 1180 |
return; |
| 1181 |
|
| 1182 |
/* If it was already 'parked', we're returning from a syscall, */ |
| 1183 |
/* so don't bother storing registers again, the GC has a set. */ |
| 1184 |
if (!GC_nacl_thread_parked[GC_nacl_thread_idx]) { |
| 1185 |
NACL_STORE_REGS(); |
| 1186 |
GC_nacl_gc_thread_self->stop_info.stack_ptr = GC_approx_sp(); |
| 1187 |
} |
| 1188 |
GC_nacl_thread_parked[GC_nacl_thread_idx] = 1; |
| 1189 |
while (GC_nacl_park_threads_now) { |
| 1190 |
/* Just spin. */ |
| 1191 |
} |
| 1192 |
GC_nacl_thread_parked[GC_nacl_thread_idx] = 0; |
| 1193 |
|
| 1194 |
/* Clear out the reg storage for next suspend. */ |
| 1195 |
BZERO(GC_nacl_gc_thread_self->stop_info.reg_storage, |
| 1196 |
NACL_GC_REG_STORAGE_SIZE * sizeof(ptr_t)); |
| 1197 |
} |
| 1198 |
} |
| 1199 |
|
| 1200 |
GC_API_OSCALL void nacl_post_syscall_hook(void) |
| 1201 |
{ |
| 1202 |
/* Calling __nacl_suspend_thread_if_needed right away should */ |
| 1203 |
/* guarantee we don't mutate the GC set. */ |
| 1204 |
__nacl_suspend_thread_if_needed(); |
| 1205 |
if (GC_nacl_thread_idx != -1) { |
| 1206 |
GC_nacl_thread_parked[GC_nacl_thread_idx] = 0; |
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
STATIC GC_bool GC_nacl_thread_parking_inited = FALSE; |
| 1211 |
STATIC pthread_mutex_t GC_nacl_thread_alloc_lock = PTHREAD_MUTEX_INITIALIZER; |
| 1212 |
|
| 1213 |
struct nacl_irt_blockhook { |
| 1214 |
int (*register_block_hooks)(void (*pre)(void), void (*post)(void)); |
| 1215 |
}; |
| 1216 |
|
| 1217 |
EXTERN_C_BEGIN |
| 1218 |
extern size_t nacl_interface_query(const char *interface_ident, |
| 1219 |
void *table, size_t tablesize); |
| 1220 |
EXTERN_C_END |
| 1221 |
|
| 1222 |
GC_INNER void GC_nacl_initialize_gc_thread(void) |
| 1223 |
{ |
| 1224 |
int i; |
| 1225 |
static struct nacl_irt_blockhook gc_hook; |
| 1226 |
|
| 1227 |
pthread_mutex_lock(&GC_nacl_thread_alloc_lock); |
| 1228 |
if (!EXPECT(GC_nacl_thread_parking_inited, TRUE)) { |
| 1229 |
BZERO(GC_nacl_thread_parked, sizeof(GC_nacl_thread_parked)); |
| 1230 |
BZERO(GC_nacl_thread_used, sizeof(GC_nacl_thread_used)); |
| 1231 |
/* TODO: replace with public 'register hook' function when */ |
| 1232 |
/* available from glibc. */ |
| 1233 |
nacl_interface_query("nacl-irt-blockhook-0.1", |
| 1234 |
&gc_hook, sizeof(gc_hook)); |
| 1235 |
gc_hook.register_block_hooks(nacl_pre_syscall_hook, |
| 1236 |
nacl_post_syscall_hook); |
| 1237 |
GC_nacl_thread_parking_inited = TRUE; |
| 1238 |
} |
| 1239 |
GC_ASSERT(GC_nacl_num_gc_threads <= MAX_NACL_GC_THREADS); |
| 1240 |
for (i = 0; i < MAX_NACL_GC_THREADS; i++) { |
| 1241 |
if (GC_nacl_thread_used[i] == 0) { |
| 1242 |
GC_nacl_thread_used[i] = 1; |
| 1243 |
GC_nacl_thread_idx = i; |
| 1244 |
GC_nacl_num_gc_threads++; |
| 1245 |
break; |
| 1246 |
} |
| 1247 |
} |
| 1248 |
pthread_mutex_unlock(&GC_nacl_thread_alloc_lock); |
| 1249 |
} |
| 1250 |
|
| 1251 |
GC_INNER void GC_nacl_shutdown_gc_thread(void) |
| 1252 |
{ |
| 1253 |
pthread_mutex_lock(&GC_nacl_thread_alloc_lock); |
| 1254 |
GC_ASSERT(GC_nacl_thread_idx >= 0); |
| 1255 |
GC_ASSERT(GC_nacl_thread_idx < MAX_NACL_GC_THREADS); |
| 1256 |
GC_ASSERT(GC_nacl_thread_used[GC_nacl_thread_idx] != 0); |
| 1257 |
GC_nacl_thread_used[GC_nacl_thread_idx] = 0; |
| 1258 |
GC_nacl_thread_idx = -1; |
| 1259 |
GC_nacl_num_gc_threads--; |
| 1260 |
pthread_mutex_unlock(&GC_nacl_thread_alloc_lock); |
| 1261 |
} |
| 1262 |
|
| 1263 |
#else /* !NACL */ |
| 1264 |
|
| 1265 |
# ifndef GC_OPENBSD_UTHREADS |
| 1266 |
static GC_bool in_resend_restart_signals; |
| 1267 |
# endif |
| 1268 |
|
| 1269 |
/* Restart all threads that were suspended by the collector. */ |
| 1270 |
/* Return the number of restart signals that were sent. */ |
| 1271 |
STATIC int GC_restart_all(void) |
| 1272 |
{ |
| 1273 |
int n_live_threads = 0; |
| 1274 |
int i; |
| 1275 |
pthread_t self = pthread_self(); |
| 1276 |
GC_thread p; |
| 1277 |
# ifndef GC_OPENBSD_UTHREADS |
| 1278 |
int result; |
| 1279 |
# endif |
| 1280 |
|
| 1281 |
for (i = 0; i < THREAD_TABLE_SZ; i++) { |
| 1282 |
for (p = GC_threads[i]; p != NULL; p = p -> next) { |
| 1283 |
if (!THREAD_EQUAL(p -> id, self)) { |
| 1284 |
if ((p -> flags & FINISHED) != 0) continue; |
| 1285 |
if (p -> thread_blocked) continue; |
| 1286 |
# ifndef GC_OPENBSD_UTHREADS |
| 1287 |
# ifdef GC_ENABLE_SUSPEND_THREAD |
| 1288 |
if ((p -> stop_info.ext_suspend_cnt & 1) != 0) continue; |
| 1289 |
# endif |
| 1290 |
if (GC_retry_signals |
| 1291 |
&& AO_load(&p->stop_info.last_stop_count) |
| 1292 |
== (AO_t)((word)GC_stop_count | THREAD_RESTARTED)) { |
| 1293 |
/* The thread has been restarted. */ |
| 1294 |
if (!in_resend_restart_signals) { |
| 1295 |
/* Some user signal (which we do not block, e.g. SIGQUIT) */ |
| 1296 |
/* has already restarted the thread, but nonetheless we */ |
| 1297 |
/* need to count the thread in n_live_threads, so that */ |
| 1298 |
/* to decrement the semaphore's value proper amount of */ |
| 1299 |
/* times. (We are also sending the restart signal to the */ |
| 1300 |
/* thread, it is not needed actually but does not hurt.) */ |
| 1301 |
} else { |
| 1302 |
continue; |
| 1303 |
/* FIXME: Still, an extremely low chance exists that the */ |
| 1304 |
/* user signal restarts the thread after the restart */ |
| 1305 |
/* signal has been lost (causing sem_timedwait() to fail) */ |
| 1306 |
/* while retrying, causing finally a mismatch between */ |
| 1307 |
/* GC_suspend_ack_sem and n_live_threads. */ |
| 1308 |
} |
| 1309 |
} |
| 1310 |
n_live_threads++; |
| 1311 |
# endif |
| 1312 |
# ifdef DEBUG_THREADS |
| 1313 |
GC_log_printf("Sending restart signal to %p\n", (void *)p->id); |
| 1314 |
# endif |
| 1315 |
# ifdef GC_OPENBSD_UTHREADS |
| 1316 |
if (pthread_resume_np(p -> id) != 0) |
| 1317 |
ABORT("pthread_resume_np failed"); |
| 1318 |
if (GC_on_thread_event) |
| 1319 |
GC_on_thread_event(GC_EVENT_THREAD_UNSUSPENDED, (void *)p->id); |
| 1320 |
# else |
| 1321 |
result = raise_signal(p, GC_sig_thr_restart); |
| 1322 |
switch(result) { |
| 1323 |
case ESRCH: |
| 1324 |
/* Not really there anymore. Possible? */ |
| 1325 |
n_live_threads--; |
| 1326 |
break; |
| 1327 |
case 0: |
| 1328 |
if (GC_on_thread_event) |
| 1329 |
GC_on_thread_event(GC_EVENT_THREAD_UNSUSPENDED, |
| 1330 |
(void *)(word)THREAD_SYSTEM_ID(p)); |
| 1331 |
break; |
| 1332 |
default: |
| 1333 |
ABORT_ARG1("pthread_kill failed at resume", |
| 1334 |
": errcode= %d", result); |
| 1335 |
} |
| 1336 |
# endif |
| 1337 |
} |
| 1338 |
} |
| 1339 |
} |
| 1340 |
return n_live_threads; |
| 1341 |
} |
| 1342 |
#endif /* !NACL */ |
| 1343 |
|
| 1344 |
/* Caller holds allocation lock, and has held it continuously since */ |
| 1345 |
/* the world stopped. */ |
| 1346 |
GC_INNER void GC_start_world(void) |
| 1347 |
{ |
| 1348 |
# ifndef NACL |
| 1349 |
int n_live_threads; |
| 1350 |
|
| 1351 |
GC_ASSERT(I_HOLD_LOCK()); |
| 1352 |
# ifdef DEBUG_THREADS |
| 1353 |
GC_log_printf("World starting\n"); |
| 1354 |
# endif |
| 1355 |
# ifndef GC_OPENBSD_UTHREADS |
| 1356 |
AO_store_release(&GC_world_is_stopped, FALSE); |
| 1357 |
/* The updated value should now be visible to the */ |
| 1358 |
/* signal handler (note that pthread_kill is not on */ |
| 1359 |
/* the list of functions which synchronize memory). */ |
| 1360 |
GC_ASSERT(!in_resend_restart_signals); |
| 1361 |
# endif |
| 1362 |
n_live_threads = GC_restart_all(); |
| 1363 |
# ifdef GC_OPENBSD_UTHREADS |
| 1364 |
(void)n_live_threads; |
| 1365 |
# else |
| 1366 |
if (GC_retry_signals) { |
| 1367 |
in_resend_restart_signals = TRUE; |
| 1368 |
resend_lost_signals_retry(n_live_threads, GC_restart_all); |
| 1369 |
in_resend_restart_signals = FALSE; |
| 1370 |
} /* else */ |
| 1371 |
# ifdef GC_NETBSD_THREADS_WORKAROUND |
| 1372 |
else { |
| 1373 |
suspend_restart_barrier(n_live_threads); |
| 1374 |
} |
| 1375 |
# endif |
| 1376 |
# endif |
| 1377 |
# ifdef DEBUG_THREADS |
| 1378 |
GC_log_printf("World started\n"); |
| 1379 |
# endif |
| 1380 |
# else /* NACL */ |
| 1381 |
# ifdef DEBUG_THREADS |
| 1382 |
GC_log_printf("World starting...\n"); |
| 1383 |
# endif |
| 1384 |
GC_nacl_park_threads_now = 0; |
| 1385 |
if (GC_on_thread_event) |
| 1386 |
GC_on_thread_event(GC_EVENT_THREAD_UNSUSPENDED, NULL); |
| 1387 |
/* TODO: Send event for every unsuspended thread. */ |
| 1388 |
# endif |
| 1389 |
} |
| 1390 |
|
| 1391 |
GC_INNER void GC_stop_init(void) |
| 1392 |
{ |
| 1393 |
# if !defined(GC_OPENBSD_UTHREADS) && !defined(NACL) |
| 1394 |
struct sigaction act; |
| 1395 |
char *str; |
| 1396 |
|
| 1397 |
if (SIGNAL_UNSET == GC_sig_suspend) |
| 1398 |
GC_sig_suspend = SIG_SUSPEND; |
| 1399 |
if (SIGNAL_UNSET == GC_sig_thr_restart) |
| 1400 |
GC_sig_thr_restart = SIG_THR_RESTART; |
| 1401 |
if (GC_sig_suspend == GC_sig_thr_restart) |
| 1402 |
ABORT("Cannot use same signal for thread suspend and resume"); |
| 1403 |
|
| 1404 |
if (sem_init(&GC_suspend_ack_sem, GC_SEM_INIT_PSHARED, 0) != 0) |
| 1405 |
ABORT("sem_init failed"); |
| 1406 |
|
| 1407 |
# ifdef SA_RESTART |
| 1408 |
act.sa_flags = SA_RESTART |
| 1409 |
# else |
| 1410 |
act.sa_flags = 0 |
| 1411 |
# endif |
| 1412 |
# ifndef NO_SA_SIGACTION |
| 1413 |
| SA_SIGINFO |
| 1414 |
# endif |
| 1415 |
; |
| 1416 |
if (sigfillset(&act.sa_mask) != 0) { |
| 1417 |
ABORT("sigfillset failed"); |
| 1418 |
} |
| 1419 |
# ifdef GC_RTEMS_PTHREADS |
| 1420 |
if(sigprocmask(SIG_UNBLOCK, &act.sa_mask, NULL) != 0) { |
| 1421 |
ABORT("sigprocmask failed"); |
| 1422 |
} |
| 1423 |
# endif |
| 1424 |
GC_remove_allowed_signals(&act.sa_mask); |
| 1425 |
/* GC_sig_thr_restart is set in the resulting mask. */ |
| 1426 |
/* It is unmasked by the handler when necessary. */ |
| 1427 |
# ifndef NO_SA_SIGACTION |
| 1428 |
act.sa_sigaction = GC_suspend_handler; |
| 1429 |
# else |
| 1430 |
act.sa_handler = GC_suspend_handler; |
| 1431 |
# endif |
| 1432 |
/* act.sa_restorer is deprecated and should not be initialized. */ |
| 1433 |
if (sigaction(GC_sig_suspend, &act, NULL) != 0) { |
| 1434 |
ABORT("Cannot set SIG_SUSPEND handler"); |
| 1435 |
} |
| 1436 |
|
| 1437 |
# ifndef NO_SA_SIGACTION |
| 1438 |
act.sa_flags &= ~SA_SIGINFO; |
| 1439 |
# endif |
| 1440 |
act.sa_handler = GC_restart_handler; |
| 1441 |
if (sigaction(GC_sig_thr_restart, &act, NULL) != 0) { |
| 1442 |
ABORT("Cannot set SIG_THR_RESTART handler"); |
| 1443 |
} |
| 1444 |
|
| 1445 |
/* Initialize suspend_handler_mask (excluding GC_sig_thr_restart). */ |
| 1446 |
if (sigfillset(&suspend_handler_mask) != 0) ABORT("sigfillset failed"); |
| 1447 |
GC_remove_allowed_signals(&suspend_handler_mask); |
| 1448 |
if (sigdelset(&suspend_handler_mask, GC_sig_thr_restart) != 0) |
| 1449 |
ABORT("sigdelset failed"); |
| 1450 |
|
| 1451 |
/* Override the default value of GC_retry_signals. */ |
| 1452 |
str = GETENV("GC_RETRY_SIGNALS"); |
| 1453 |
if (str != NULL) { |
| 1454 |
if (*str == '0' && *(str + 1) == '\0') { |
| 1455 |
/* Do not retry if the environment variable is set to "0". */ |
| 1456 |
GC_retry_signals = FALSE; |
| 1457 |
} else { |
| 1458 |
GC_retry_signals = TRUE; |
| 1459 |
} |
| 1460 |
} |
| 1461 |
if (GC_retry_signals) { |
| 1462 |
GC_COND_LOG_PRINTF( |
| 1463 |
"Will retry suspend and restart signals if necessary\n"); |
| 1464 |
} |
| 1465 |
# ifndef NO_SIGNALS_UNBLOCK_IN_MAIN |
| 1466 |
/* Explicitly unblock the signals once before new threads creation. */ |
| 1467 |
GC_unblock_gc_signals(); |
| 1468 |
# endif |
| 1469 |
# endif /* !GC_OPENBSD_UTHREADS && !NACL */ |
| 1470 |
} |
| 1471 |
|
| 1472 |
#endif /* GC_PTHREADS && !GC_DARWIN_THREADS && !GC_WIN32_THREADS */ |