| 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 |
* |
| 8 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 9 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 10 |
* |
| 11 |
* Permission is hereby granted to use or copy this program |
| 12 |
* for any purpose, provided the above notices are retained on all copies. |
| 13 |
* Permission to modify the code and to distribute modified code is granted, |
| 14 |
* provided the above notices are retained, and a notice that the code was |
| 15 |
* modified is included with the above copyright notice. |
| 16 |
*/ |
| 17 |
|
| 18 |
#ifndef GC_PTHREAD_SUPPORT_H |
| 19 |
#define GC_PTHREAD_SUPPORT_H |
| 20 |
|
| 21 |
#include "private/gc_priv.h" |
| 22 |
|
| 23 |
#if defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS) |
| 24 |
|
| 25 |
#if defined(GC_DARWIN_THREADS) |
| 26 |
# include "private/darwin_stop_world.h" |
| 27 |
#else |
| 28 |
# include "private/pthread_stop_world.h" |
| 29 |
#endif |
| 30 |
|
| 31 |
#ifdef THREAD_LOCAL_ALLOC |
| 32 |
# include "thread_local_alloc.h" |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifdef THREAD_SANITIZER |
| 36 |
# include "dbg_mlc.h" /* for oh type */ |
| 37 |
#endif |
| 38 |
|
| 39 |
EXTERN_C_BEGIN |
| 40 |
|
| 41 |
/* We use the allocation lock to protect thread-related data structures. */ |
| 42 |
|
| 43 |
/* The set of all known threads. We intercept thread creation and */ |
| 44 |
/* joins. */ |
| 45 |
/* Protected by allocation/GC lock. */ |
| 46 |
/* Some of this should be declared volatile, but that's inconsistent */ |
| 47 |
/* with some library routine declarations. */ |
| 48 |
typedef struct GC_Thread_Rep { |
| 49 |
# ifdef THREAD_SANITIZER |
| 50 |
char dummy[sizeof(oh)]; /* A dummy field to avoid TSan false */ |
| 51 |
/* positive about the race between */ |
| 52 |
/* GC_has_other_debug_info and */ |
| 53 |
/* GC_suspend_handler_inner (which */ |
| 54 |
/* sets stop_info.stack_ptr). */ |
| 55 |
# endif |
| 56 |
|
| 57 |
struct GC_Thread_Rep * next; /* More recently allocated threads */ |
| 58 |
/* with a given pthread id come */ |
| 59 |
/* first. (All but the first are */ |
| 60 |
/* guaranteed to be dead, but we may */ |
| 61 |
/* not yet have registered the join.) */ |
| 62 |
pthread_t id; |
| 63 |
# ifdef USE_TKILL_ON_ANDROID |
| 64 |
pid_t kernel_id; |
| 65 |
# endif |
| 66 |
/* Extra bookkeeping information the stopping code uses */ |
| 67 |
struct thread_stop_info stop_info; |
| 68 |
|
| 69 |
unsigned char flags; /* Protected by GC lock. */ |
| 70 |
# define FINISHED 1 /* Thread has exited. */ |
| 71 |
# define DETACHED 2 /* Thread is treated as detached. */ |
| 72 |
/* Thread may really be detached, or */ |
| 73 |
/* it may have been explicitly */ |
| 74 |
/* registered, in which case we can */ |
| 75 |
/* deallocate its GC_Thread_Rep once */ |
| 76 |
/* it unregisters itself, since it */ |
| 77 |
/* may not return a GC pointer. */ |
| 78 |
# define MAIN_THREAD 4 /* True for the original thread only. */ |
| 79 |
# define DISABLED_GC 0x10 /* Collections are disabled while the */ |
| 80 |
/* thread is exiting. */ |
| 81 |
|
| 82 |
unsigned char thread_blocked; |
| 83 |
/* Protected by GC lock. */ |
| 84 |
/* Treated as a boolean value. If set, */ |
| 85 |
/* thread will acquire GC lock before */ |
| 86 |
/* doing any pointer manipulations, and */ |
| 87 |
/* has set its SP value. Thus it does */ |
| 88 |
/* not need to be sent a signal to stop */ |
| 89 |
/* it. */ |
| 90 |
|
| 91 |
# ifndef GC_NO_FINALIZATION |
| 92 |
unsigned short finalizer_skipped; |
| 93 |
unsigned char finalizer_nested; |
| 94 |
/* Used by GC_check_finalizer_nested() */ |
| 95 |
/* to minimize the level of recursion */ |
| 96 |
/* when a client finalizer allocates */ |
| 97 |
/* memory (initially both are 0). */ |
| 98 |
# endif |
| 99 |
|
| 100 |
ptr_t stack_end; /* Cold end of the stack (except for */ |
| 101 |
/* main thread). */ |
| 102 |
ptr_t altstack; /* The start of the alt-stack if there */ |
| 103 |
/* is one, NULL otherwise. */ |
| 104 |
word altstack_size; /* The size of the alt-stack if exists. */ |
| 105 |
ptr_t stack; /* The start and size of the normal */ |
| 106 |
/* stack (set by GC_register_altstack). */ |
| 107 |
word stack_size; |
| 108 |
# if defined(GC_DARWIN_THREADS) && !defined(DARWIN_DONT_PARSE_STACK) |
| 109 |
ptr_t topOfStack; /* Result of GC_FindTopOfStack(0); */ |
| 110 |
/* valid only if the thread is blocked; */ |
| 111 |
/* non-NULL value means already set. */ |
| 112 |
# endif |
| 113 |
# if defined(E2K) || defined(IA64) |
| 114 |
ptr_t backing_store_end; /* Note: may reference data in GC heap */ |
| 115 |
ptr_t backing_store_ptr; |
| 116 |
# endif |
| 117 |
|
| 118 |
struct GC_traced_stack_sect_s *traced_stack_sect; |
| 119 |
/* Points to the "frame" data held in stack by */ |
| 120 |
/* the innermost GC_call_with_gc_active() of */ |
| 121 |
/* this thread. May be NULL. */ |
| 122 |
|
| 123 |
void * status; /* The value returned from the thread. */ |
| 124 |
/* Used only to avoid premature */ |
| 125 |
/* reclamation of any data it might */ |
| 126 |
/* reference. */ |
| 127 |
/* This is unfortunately also the */ |
| 128 |
/* reason we need to intercept join */ |
| 129 |
/* and detach. */ |
| 130 |
|
| 131 |
# ifdef THREAD_LOCAL_ALLOC |
| 132 |
struct thread_local_freelists tlfs GC_ATTR_WORD_ALIGNED; |
| 133 |
# endif |
| 134 |
} * GC_thread; |
| 135 |
|
| 136 |
#ifndef THREAD_TABLE_SZ |
| 137 |
# define THREAD_TABLE_SZ 256 /* Power of 2 (for speed). */ |
| 138 |
#endif |
| 139 |
|
| 140 |
#if CPP_WORDSZ == 64 |
| 141 |
# define THREAD_TABLE_INDEX(id) \ |
| 142 |
(int)(((((NUMERIC_THREAD_ID(id) >> 8) ^ NUMERIC_THREAD_ID(id)) >> 16) \ |
| 143 |
^ ((NUMERIC_THREAD_ID(id) >> 8) ^ NUMERIC_THREAD_ID(id))) \ |
| 144 |
% THREAD_TABLE_SZ) |
| 145 |
#else |
| 146 |
# define THREAD_TABLE_INDEX(id) \ |
| 147 |
(int)(((NUMERIC_THREAD_ID(id) >> 16) \ |
| 148 |
^ (NUMERIC_THREAD_ID(id) >> 8) \ |
| 149 |
^ NUMERIC_THREAD_ID(id)) % THREAD_TABLE_SZ) |
| 150 |
#endif |
| 151 |
|
| 152 |
GC_EXTERN volatile GC_thread GC_threads[THREAD_TABLE_SZ]; |
| 153 |
|
| 154 |
GC_EXTERN GC_bool GC_thr_initialized; |
| 155 |
|
| 156 |
GC_INNER GC_thread GC_lookup_thread(pthread_t id); |
| 157 |
|
| 158 |
#ifdef NACL |
| 159 |
GC_EXTERN __thread GC_thread GC_nacl_gc_thread_self; |
| 160 |
GC_INNER void GC_nacl_initialize_gc_thread(void); |
| 161 |
GC_INNER void GC_nacl_shutdown_gc_thread(void); |
| 162 |
#endif |
| 163 |
|
| 164 |
#ifdef GC_EXPLICIT_SIGNALS_UNBLOCK |
| 165 |
GC_INNER void GC_unblock_gc_signals(void); |
| 166 |
#endif |
| 167 |
|
| 168 |
#if defined(GC_ENABLE_SUSPEND_THREAD) && !defined(GC_DARWIN_THREADS) \ |
| 169 |
&& !defined(GC_OPENBSD_UTHREADS) && !defined(NACL) \ |
| 170 |
&& !defined(PLATFORM_STOP_WORLD) && !defined(SN_TARGET_PSP2) |
| 171 |
GC_INNER void GC_suspend_self_inner(GC_thread me, word suspend_cnt); |
| 172 |
|
| 173 |
GC_INNER void GC_suspend_self_blocked(ptr_t thread_me, void *context); |
| 174 |
/* Wrapper over GC_suspend_self_inner. */ |
| 175 |
#endif |
| 176 |
|
| 177 |
#ifdef GC_PTHREAD_START_STANDALONE |
| 178 |
# define GC_INNER_PTHRSTART /* empty */ |
| 179 |
#else |
| 180 |
# define GC_INNER_PTHRSTART GC_INNER |
| 181 |
#endif |
| 182 |
|
| 183 |
GC_INNER_PTHRSTART void * GC_CALLBACK GC_inner_start_routine( |
| 184 |
struct GC_stack_base *sb, void *arg); |
| 185 |
|
| 186 |
GC_INNER_PTHRSTART GC_thread GC_start_rtn_prepare_thread( |
| 187 |
void *(**pstart)(void *), |
| 188 |
void **pstart_arg, |
| 189 |
struct GC_stack_base *sb, void *arg); |
| 190 |
GC_INNER_PTHRSTART void GC_thread_exit_proc(void *); |
| 191 |
|
| 192 |
EXTERN_C_END |
| 193 |
|
| 194 |
#endif /* GC_PTHREADS && !GC_WIN32_THREADS */ |
| 195 |
|
| 196 |
#endif /* GC_PTHREAD_SUPPORT_H */ |