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