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.
8 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
11 * Permission is hereby granted to use or copy this program
12 * for any purpose, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
18 #ifndef GC_PTHREAD_SUPPORT_H
19 #define GC_PTHREAD_SUPPORT_H
21 #include "private/gc_priv.h"
23 #if defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS)
25 #if defined(GC_DARWIN_THREADS)
26 # include "private/darwin_stop_world.h"
28 # include "private/pthread_stop_world.h"
31 #ifdef THREAD_LOCAL_ALLOC
32 # include "thread_local_alloc.h"
35 /* We use the allocation lock to protect thread-related data structures. */
37 /* The set of all known threads. We intercept thread creation and */
39 /* Protected by allocation/GC lock. */
40 /* Some of this should be declared volatile, but that's inconsistent */
41 /* with some library routine declarations. */
42 typedef struct GC_Thread_Rep {
43 struct GC_Thread_Rep * next; /* More recently allocated threads */
44 /* with a given pthread id come */
45 /* first. (All but the first are */
46 /* guaranteed to be dead, but we may */
47 /* not yet have registered the join.) */
49 # ifdef PLATFORM_ANDROID
52 /* Extra bookkeeping information the stopping code uses */
53 struct thread_stop_info stop_info;
56 # define FINISHED 1 /* Thread has exited. */
57 # define DETACHED 2 /* Thread is treated as detached. */
58 /* Thread may really be detached, or */
59 /* it may have been explicitly */
60 /* registered, in which case we can */
61 /* deallocate its GC_Thread_Rep once */
62 /* it unregisters itself, since it */
63 /* may not return a GC pointer. */
64 # define MAIN_THREAD 4 /* True for the original thread only. */
65 # define SUSPENDED_EXT 8 /* Thread was suspended externally */
66 /* (this is not used by the unmodified */
67 /* GC itself at present). */
68 # define DISABLED_GC 0x10 /* Collections are disabled while the */
69 /* thread is exiting. */
71 unsigned char thread_blocked;
72 /* Protected by GC lock. */
73 /* Treated as a boolean value. If set, */
74 /* thread will acquire GC lock before */
75 /* doing any pointer manipulations, and */
76 /* has set its SP value. Thus it does */
77 /* not need to be sent a signal to stop */
80 unsigned short finalizer_skipped;
81 unsigned char finalizer_nested;
82 /* Used by GC_check_finalizer_nested() */
83 /* to minimize the level of recursion */
84 /* when a client finalizer allocates */
85 /* memory (initially both are 0). */
87 ptr_t stack_end; /* Cold end of the stack (except for */
89 # if defined(GC_DARWIN_THREADS) && !defined(DARWIN_DONT_PARSE_STACK)
90 ptr_t topOfStack; /* Result of GC_FindTopOfStack(0); */
91 /* valid only if the thread is blocked; */
92 /* non-NULL value means already set. */
95 ptr_t backing_store_end;
96 ptr_t backing_store_ptr;
99 struct GC_traced_stack_sect_s *traced_stack_sect;
100 /* Points to the "frame" data held in stack by */
101 /* the innermost GC_call_with_gc_active() of */
102 /* this thread. May be NULL. */
104 void * status; /* The value returned from the thread. */
105 /* Used only to avoid premature */
106 /* reclamation of any data it might */
108 /* This is unfortunately also the */
109 /* reason we need to intercept join */
112 # ifdef THREAD_LOCAL_ALLOC
113 struct thread_local_freelists tlfs;
117 # define THREAD_TABLE_SZ 256 /* Must be power of 2 */
118 GC_EXTERN volatile GC_thread GC_threads[THREAD_TABLE_SZ];
120 GC_EXTERN GC_bool GC_thr_initialized;
122 GC_INNER GC_thread GC_lookup_thread(pthread_t id);
124 GC_EXTERN GC_bool GC_in_thread_creation;
125 /* We may currently be in thread creation or destruction. */
126 /* Only set to TRUE while allocation lock is held. */
127 /* When set, it is OK to run GC from unknown thread. */
130 GC_EXTERN __thread GC_thread GC_nacl_gc_thread_self;
131 GC_INNER void GC_nacl_initialize_gc_thread(void);
132 GC_INNER void GC_nacl_shutdown_gc_thread(void);
135 #ifdef GC_EXPLICIT_SIGNALS_UNBLOCK
136 GC_INNER void GC_unblock_gc_signals(void);
139 GC_INNER GC_thread GC_start_rtn_prepare_thread(void *(**pstart)(void *),
141 struct GC_stack_base *sb, void *arg);
142 GC_INNER void GC_thread_exit_proc(void *);
144 #endif /* GC_PTHREADS && !GC_WIN32_THREADS */
146 #endif /* GC_PTHREAD_SUPPORT_H */