Debian Codegraph @

external/bdwgc/include/private/thread_local_alloc.h

raw
1 /*
2 * Copyright (c) 2000-2005 by Hewlett-Packard Company. All rights reserved.
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
12 */
13
14 /* Included indirectly from a thread-library-specific file. */
15 /* This is the interface for thread-local allocation, whose */
16 /* implementation is mostly thread-library-independent. */
17 /* Here we describe only the interface that needs to be known */
18 /* and invoked from the thread support layer; the actual */
19 /* implementation also exports GC_malloc and friends, which */
20 /* are declared in gc.h. */
21
22 #ifndef GC_THREAD_LOCAL_ALLOC_H
23 #define GC_THREAD_LOCAL_ALLOC_H
24
25 #include "private/gc_priv.h"
26
27 #ifdef THREAD_LOCAL_ALLOC
28
29 #include "gc_inline.h"
30
31 #if defined(USE_HPUX_TLS)
32 # error USE_HPUX_TLS macro was replaced by USE_COMPILER_TLS
33 #endif
34
35 #include <stdlib.h>
36
37 EXTERN_C_BEGIN
38
39 #if !defined(USE_PTHREAD_SPECIFIC) && !defined(USE_WIN32_SPECIFIC) \
40 && !defined(USE_WIN32_COMPILER_TLS) && !defined(USE_COMPILER_TLS) \
41 && !defined(USE_CUSTOM_SPECIFIC)
42 # if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
43 # if defined(CYGWIN32) && GC_GNUC_PREREQ(4, 0)
44 # if defined(__clang__)
45 /* As of Cygwin clang3.5.2, thread-local storage is unsupported. */
46 # define USE_PTHREAD_SPECIFIC
47 # else
48 # define USE_COMPILER_TLS
49 # endif
50 # elif defined(__GNUC__) || defined(MSWINCE)
51 # define USE_WIN32_SPECIFIC
52 # else
53 # define USE_WIN32_COMPILER_TLS
54 # endif /* !GNU */
55 # elif (defined(LINUX) && !defined(ARM32) && !defined(AVR32) \
56 && GC_GNUC_PREREQ(3, 3) \
57 && !(defined(__clang__) && defined(HOST_ANDROID))) \
58 || (defined(FREEBSD) && defined(__GLIBC__) /* kFreeBSD */ \
59 && GC_GNUC_PREREQ(4, 4)) \
60 || (defined(HOST_ANDROID) && defined(ARM32) \
61 && (GC_GNUC_PREREQ(4, 6) || GC_CLANG_PREREQ_FULL(3, 8, 256229)))
62 # define USE_COMPILER_TLS
63 # elif defined(GC_DGUX386_THREADS) || defined(GC_OSF1_THREADS) \
64 || defined(GC_AIX_THREADS) || defined(GC_DARWIN_THREADS) \
65 || defined(GC_FREEBSD_THREADS) || defined(GC_NETBSD_THREADS) \
66 || defined(GC_LINUX_THREADS) || defined(GC_HAIKU_THREADS) \
67 || defined(GC_RTEMS_PTHREADS)
68 # define USE_PTHREAD_SPECIFIC
69 # elif defined(GC_HPUX_THREADS)
70 # ifdef __GNUC__
71 # define USE_PTHREAD_SPECIFIC
72 /* Empirically, as of gcc 3.3, USE_COMPILER_TLS doesn't work. */
73 # else
74 # define USE_COMPILER_TLS
75 # endif
76 # else
77 # define USE_CUSTOM_SPECIFIC /* Use our own. */
78 # endif
79 #endif
80
81 #ifndef THREAD_FREELISTS_KINDS
82 # ifdef ENABLE_DISCLAIM
83 # define THREAD_FREELISTS_KINDS (NORMAL+2)
84 # else
85 # define THREAD_FREELISTS_KINDS (NORMAL+1)
86 # endif
87 #endif /* !THREAD_FREELISTS_KINDS */
88
89 /* One of these should be declared as the tlfs field in the */
90 /* structure pointed to by a GC_thread. */
91 typedef struct thread_local_freelists {
92 void * _freelists[THREAD_FREELISTS_KINDS][TINY_FREELISTS];
93 # define ptrfree_freelists _freelists[PTRFREE]
94 # define normal_freelists _freelists[NORMAL]
95 /* Note: Preserve *_freelists names for some clients. */
96 # ifdef GC_GCJ_SUPPORT
97 void * gcj_freelists[TINY_FREELISTS];
98 # define ERROR_FL ((void *)(word)-1)
99 /* Value used for gcj_freelists[-1]; allocation is */
100 /* erroneous. */
101 # endif
102 /* Free lists contain either a pointer or a small count */
103 /* reflecting the number of granules allocated at that */
104 /* size. */
105 /* 0 ==> thread-local allocation in use, free list */
106 /* empty. */
107 /* > 0, <= DIRECT_GRANULES ==> Using global allocation, */
108 /* too few objects of this size have been */
109 /* allocated by this thread. */
110 /* >= HBLKSIZE => pointer to nonempty free list. */
111 /* > DIRECT_GRANULES, < HBLKSIZE ==> transition to */
112 /* local alloc, equivalent to 0. */
113 # define DIRECT_GRANULES (HBLKSIZE/GRANULE_BYTES)
114 /* Don't use local free lists for up to this much */
115 /* allocation. */
116 } *GC_tlfs;
117
118 #if defined(USE_PTHREAD_SPECIFIC)
119 # define GC_getspecific pthread_getspecific
120 # define GC_setspecific pthread_setspecific
121 # define GC_key_create pthread_key_create
122 # define GC_remove_specific(key) pthread_setspecific(key, NULL)
123 /* Explicitly delete the value to stop the TLS */
124 /* destructor from being called repeatedly. */
125 # define GC_remove_specific_after_fork(key, t) (void)0
126 /* Should not need any action. */
127 typedef pthread_key_t GC_key_t;
128 #elif defined(USE_COMPILER_TLS) || defined(USE_WIN32_COMPILER_TLS)
129 # define GC_getspecific(x) (x)
130 # define GC_setspecific(key, v) ((key) = (v), 0)
131 # define GC_key_create(key, d) 0
132 # define GC_remove_specific(key) /* No need for cleanup on exit. */
133 # define GC_remove_specific_after_fork(key, t) (void)0
134 typedef void * GC_key_t;
135 #elif defined(USE_WIN32_SPECIFIC)
136 # ifndef WIN32_LEAN_AND_MEAN
137 # define WIN32_LEAN_AND_MEAN 1
138 # endif
139 # define NOSERVICE
140 EXTERN_C_END
141 # include <windows.h>
142 EXTERN_C_BEGIN
143 # define GC_getspecific TlsGetValue
144 # define GC_setspecific(key, v) !TlsSetValue(key, v)
145 /* We assume 0 == success, msft does the opposite. */
146 # ifndef TLS_OUT_OF_INDEXES
147 /* this is currently missing in WinCE */
148 # define TLS_OUT_OF_INDEXES (DWORD)0xFFFFFFFF
149 # endif
150 # define GC_key_create(key, d) \
151 ((d) != 0 || (*(key) = TlsAlloc()) == TLS_OUT_OF_INDEXES ? -1 : 0)
152 # define GC_remove_specific(key) /* No need for cleanup on exit. */
153 /* Need TlsFree on process exit/detach? */
154 # define GC_remove_specific_after_fork(key, t) (void)0
155 typedef DWORD GC_key_t;
156 #elif defined(USE_CUSTOM_SPECIFIC)
157 EXTERN_C_END
158 # include "private/specific.h"
159 EXTERN_C_BEGIN
160 #else
161 # error implement me
162 #endif
163
164 /* Each thread structure must be initialized. */
165 /* This call must be made from the new thread. */
166 /* Caller holds allocation lock. */
167 GC_INNER void GC_init_thread_local(GC_tlfs p);
168
169 /* Called when a thread is unregistered, or exits. */
170 /* We hold the allocator lock. */
171 GC_INNER void GC_destroy_thread_local(GC_tlfs p);
172
173 /* The thread support layer must arrange to mark thread-local */
174 /* free lists explicitly, since the link field is often */
175 /* invisible to the marker. It knows how to find all threads; */
176 /* we take care of an individual thread freelist structure. */
177 GC_INNER void GC_mark_thread_local_fls_for(GC_tlfs p);
178
179 #ifdef GC_ASSERTIONS
180 GC_bool GC_is_thread_tsd_valid(void *tsd);
181 void GC_check_tls_for(GC_tlfs p);
182 # if defined(USE_CUSTOM_SPECIFIC)
183 void GC_check_tsd_marks(tsd *key);
184 # endif
185 #endif /* GC_ASSERTIONS */
186
187 #ifndef GC_ATTR_TLS_FAST
188 # define GC_ATTR_TLS_FAST /* empty */
189 #endif
190
191 extern
192 #if defined(USE_COMPILER_TLS)
193 __thread GC_ATTR_TLS_FAST
194 #elif defined(USE_WIN32_COMPILER_TLS)
195 __declspec(thread) GC_ATTR_TLS_FAST
196 #endif
197 GC_key_t GC_thread_key;
198 /* This is set up by the thread_local_alloc implementation. No need */
199 /* for cleanup on thread exit. But the thread support layer makes sure */
200 /* that GC_thread_key is traced, if necessary. */
201
202 EXTERN_C_END
203
204 #endif /* THREAD_LOCAL_ALLOC */
205
206 #endif /* GC_THREAD_LOCAL_ALLOC_H */