1 #ifndef GC_PTHREAD_SUPPORT_H
2 #define GC_PTHREAD_SUPPORT_H
3 
4 # include "private/gc_priv.h"
5 
6 # if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS) \
7      && !defined(GC_WIN32_THREADS)
8 
9 #if defined(GC_DARWIN_THREADS)
10 # include "private/darwin_stop_world.h"
11 #else
12 # include "private/pthread_stop_world.h"
13 #endif
14 
15 /* We use the allocation lock to protect thread-related data structures. */
16 
17 /* The set of all known threads.  We intercept thread creation and 	*/
18 /* joins.								*/
19 /* Protected by allocation/GC lock.					*/
20 /* Some of this should be declared volatile, but that's inconsistent	*/
21 /* with some library routine declarations.  		 		*/
22 typedef struct GC_Thread_Rep {
23     struct GC_Thread_Rep * next;  /* More recently allocated threads	*/
24 				  /* with a given pthread id come 	*/
25 				  /* first.  (All but the first are	*/
26 				  /* guaranteed to be dead, but we may  */
27 				  /* not yet have registered the join.) */
28     pthread_t id;
29     /* Extra bookkeeping information the stopping code uses */
30     struct thread_stop_info stop_info;
31 
32     short flags;
33 #	define FINISHED 1   	/* Thread has exited.	*/
34 #	define DETACHED 2	/* Thread is intended to be detached.	*/
35 #	define MAIN_THREAD 4	/* True for the original thread only.	*/
36 #       define SUSPENDED 8      /* True if thread was suspended externally */
37     short thread_blocked;	/* Protected by GC lock.		*/
38     				/* Treated as a boolean value.  If set,	*/
39     				/* thread will acquire GC lock before	*/
40     				/* doing any pointer manipulations, and	*/
41     				/* has set its sp value.  Thus it does	*/
42     				/* not need to be sent a signal to stop	*/
43     				/* it.					*/
44     ptr_t stack_end;		/* Cold end of the stack.		*/
45 #   ifdef IA64
46 	ptr_t backing_store_end;
47 	ptr_t backing_store_ptr;
48 #   endif
49     void * status;		/* The value returned from the thread.  */
50     				/* Used only to avoid premature 	*/
51 				/* reclamation of any data it might 	*/
52 				/* reference.				*/
53 #   ifdef THREAD_LOCAL_ALLOC
54 #	if CPP_WORDSZ == 64 && defined(ALIGN_DOUBLE)
55 #	    define GRANULARITY 16
56 #	    define NFREELISTS 49
57 #	else
58 #	    define GRANULARITY 8
59 #	    define NFREELISTS 65
60 #	endif
61 	/* The ith free list corresponds to size i*GRANULARITY */
62 #	define INDEX_FROM_BYTES(n) ((ADD_SLOP(n) + GRANULARITY - 1)/GRANULARITY)
63 #	define BYTES_FROM_INDEX(i) ((i) * GRANULARITY - EXTRA_BYTES)
64 #	define SMALL_ENOUGH(bytes) (ADD_SLOP(bytes) <= \
65 				    (NFREELISTS-1)*GRANULARITY)
66 	ptr_t ptrfree_freelists[NFREELISTS];
67 	ptr_t normal_freelists[NFREELISTS];
68 #	ifdef GC_GCJ_SUPPORT
69 	  ptr_t gcj_freelists[NFREELISTS];
70 #	endif
71 		/* Free lists contain either a pointer or a small count */
72 		/* reflecting the number of granules allocated at that	*/
73 		/* size.						*/
74 		/* 0 ==> thread-local allocation in use, free list	*/
75 		/*       empty.						*/
76 		/* > 0, <= DIRECT_GRANULES ==> Using global allocation,	*/
77 		/*       too few objects of this size have been		*/
78 		/* 	 allocated by this thread.			*/
79 		/* >= HBLKSIZE  => pointer to nonempty free list.	*/
80 		/* > DIRECT_GRANULES, < HBLKSIZE ==> transition to	*/
81 		/*    local alloc, equivalent to 0.			*/
82 #	define DIRECT_GRANULES (HBLKSIZE/GRANULARITY)
83 		/* Don't use local free lists for up to this much 	*/
84 		/* allocation.						*/
85 #   endif
86 } * GC_thread;
87 
88 # define THREAD_TABLE_SZ 128	/* Must be power of 2	*/
89 extern volatile GC_thread GC_threads[THREAD_TABLE_SZ];
90 
91 extern GC_bool GC_thr_initialized;
92 
93 GC_thread GC_lookup_thread(pthread_t id);
94 
95 void GC_stop_init();
96 
97 extern GC_bool GC_in_thread_creation;
98 	/* We may currently be in thread creation or destruction.	*/
99 	/* Only set to TRUE while allocation lock is held.		*/
100 	/* When set, it is OK to run GC from unknown thread.		*/
101 
102 #endif /* GC_PTHREADS && !GC_SOLARIS_THREADS.... etc */
103 #endif /* GC_PTHREAD_SUPPORT_H */
104