| 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(AARCH64) && !GC_CLANG_PREREQ(8, 0))))) \ |
| 59 |
|| ((defined(NETBSD) && __NetBSD_Version__ >= 600000000 /* 6.0 */ \ |
| 60 |
|| defined(FREEBSD)) \ |
| 61 |
&& (GC_GNUC_PREREQ(4, 4) || GC_CLANG_PREREQ(3, 9))) \ |
| 62 |
|| (defined(HOST_ANDROID) && defined(ARM32) \ |
| 63 |
&& (GC_GNUC_PREREQ(4, 6) || GC_CLANG_PREREQ_FULL(3, 8, 256229))) |
| 64 |
# define USE_COMPILER_TLS |
| 65 |
# elif defined(GC_DGUX386_THREADS) || defined(GC_OSF1_THREADS) \ |
| 66 |
|| defined(GC_AIX_THREADS) || defined(GC_DARWIN_THREADS) \ |
| 67 |
|| defined(GC_FREEBSD_THREADS) || defined(GC_NETBSD_THREADS) \ |
| 68 |
|| defined(GC_LINUX_THREADS) || defined(GC_HAIKU_THREADS) \ |
| 69 |
|| defined(GC_RTEMS_PTHREADS) |
| 70 |
# define USE_PTHREAD_SPECIFIC |
| 71 |
# elif defined(GC_HPUX_THREADS) |
| 72 |
# ifdef __GNUC__ |
| 73 |
# define USE_PTHREAD_SPECIFIC |
| 74 |
/* Empirically, as of gcc 3.3, USE_COMPILER_TLS doesn't work. */ |
| 75 |
# else |
| 76 |
# define USE_COMPILER_TLS |
| 77 |
# endif |
| 78 |
# else |
| 79 |
# define USE_CUSTOM_SPECIFIC /* Use our own. */ |
| 80 |
# endif |
| 81 |
#endif |
| 82 |
|
| 83 |
#ifndef THREAD_FREELISTS_KINDS |
| 84 |
# ifdef ENABLE_DISCLAIM |
| 85 |
# define THREAD_FREELISTS_KINDS (NORMAL+2) |
| 86 |
# else |
| 87 |
# define THREAD_FREELISTS_KINDS (NORMAL+1) |
| 88 |
# endif |
| 89 |
#endif /* !THREAD_FREELISTS_KINDS */ |
| 90 |
|
| 91 |
/* One of these should be declared as the tlfs field in the */ |
| 92 |
/* structure pointed to by a GC_thread. */ |
| 93 |
typedef struct thread_local_freelists { |
| 94 |
void * _freelists[THREAD_FREELISTS_KINDS][TINY_FREELISTS]; |
| 95 |
# define ptrfree_freelists _freelists[PTRFREE] |
| 96 |
# define normal_freelists _freelists[NORMAL] |
| 97 |
/* Note: Preserve *_freelists names for some clients. */ |
| 98 |
# ifdef GC_GCJ_SUPPORT |
| 99 |
void * gcj_freelists[TINY_FREELISTS]; |
| 100 |
# define ERROR_FL ((void *)GC_WORD_MAX) |
| 101 |
/* Value used for gcj_freelists[-1]; allocation is */ |
| 102 |
/* erroneous. */ |
| 103 |
# endif |
| 104 |
/* Free lists contain either a pointer or a small count */ |
| 105 |
/* reflecting the number of granules allocated at that */ |
| 106 |
/* size. */ |
| 107 |
/* 0 ==> thread-local allocation in use, free list */ |
| 108 |
/* empty. */ |
| 109 |
/* > 0, <= DIRECT_GRANULES ==> Using global allocation, */ |
| 110 |
/* too few objects of this size have been */ |
| 111 |
/* allocated by this thread. */ |
| 112 |
/* >= HBLKSIZE => pointer to nonempty free list. */ |
| 113 |
/* > DIRECT_GRANULES, < HBLKSIZE ==> transition to */ |
| 114 |
/* local alloc, equivalent to 0. */ |
| 115 |
# define DIRECT_GRANULES (HBLKSIZE/GRANULE_BYTES) |
| 116 |
/* Don't use local free lists for up to this much */ |
| 117 |
/* allocation. */ |
| 118 |
} *GC_tlfs; |
| 119 |
|
| 120 |
#if defined(USE_PTHREAD_SPECIFIC) |
| 121 |
# define GC_getspecific pthread_getspecific |
| 122 |
# define GC_setspecific pthread_setspecific |
| 123 |
# define GC_key_create pthread_key_create |
| 124 |
# define GC_remove_specific(key) pthread_setspecific(key, NULL) |
| 125 |
/* Explicitly delete the value to stop the TLS */ |
| 126 |
/* destructor from being called repeatedly. */ |
| 127 |
# define GC_remove_specific_after_fork(key, t) (void)0 |
| 128 |
/* Should not need any action. */ |
| 129 |
typedef pthread_key_t GC_key_t; |
| 130 |
#elif defined(USE_COMPILER_TLS) || defined(USE_WIN32_COMPILER_TLS) |
| 131 |
# define GC_getspecific(x) (x) |
| 132 |
# define GC_setspecific(key, v) ((key) = (v), 0) |
| 133 |
# define GC_key_create(key, d) 0 |
| 134 |
# define GC_remove_specific(key) /* No need for cleanup on exit. */ |
| 135 |
# define GC_remove_specific_after_fork(key, t) (void)0 |
| 136 |
typedef void * GC_key_t; |
| 137 |
#elif defined(USE_WIN32_SPECIFIC) |
| 138 |
# define GC_getspecific TlsGetValue |
| 139 |
# define GC_setspecific(key, v) !TlsSetValue(key, v) |
| 140 |
/* We assume 0 == success, msft does the opposite. */ |
| 141 |
# ifndef TLS_OUT_OF_INDEXES |
| 142 |
/* this is currently missing in WinCE */ |
| 143 |
# define TLS_OUT_OF_INDEXES (DWORD)0xFFFFFFFF |
| 144 |
# endif |
| 145 |
# define GC_key_create(key, d) \ |
| 146 |
((d) != 0 || (*(key) = TlsAlloc()) == TLS_OUT_OF_INDEXES ? -1 : 0) |
| 147 |
# define GC_remove_specific(key) /* No need for cleanup on exit. */ |
| 148 |
/* Need TlsFree on process exit/detach? */ |
| 149 |
# define GC_remove_specific_after_fork(key, t) (void)0 |
| 150 |
typedef DWORD GC_key_t; |
| 151 |
#elif defined(USE_CUSTOM_SPECIFIC) |
| 152 |
EXTERN_C_END |
| 153 |
# include "private/specific.h" |
| 154 |
EXTERN_C_BEGIN |
| 155 |
#else |
| 156 |
# error implement me |
| 157 |
#endif |
| 158 |
|
| 159 |
/* Each thread structure must be initialized. */ |
| 160 |
/* This call must be made from the new thread. */ |
| 161 |
/* Caller holds allocation lock. */ |
| 162 |
GC_INNER void GC_init_thread_local(GC_tlfs p); |
| 163 |
|
| 164 |
/* Called when a thread is unregistered, or exits. */ |
| 165 |
/* We hold the allocator lock. */ |
| 166 |
GC_INNER void GC_destroy_thread_local(GC_tlfs p); |
| 167 |
|
| 168 |
/* The thread support layer must arrange to mark thread-local */ |
| 169 |
/* free lists explicitly, since the link field is often */ |
| 170 |
/* invisible to the marker. It knows how to find all threads; */ |
| 171 |
/* we take care of an individual thread freelist structure. */ |
| 172 |
GC_INNER void GC_mark_thread_local_fls_for(GC_tlfs p); |
| 173 |
|
| 174 |
#ifdef GC_ASSERTIONS |
| 175 |
GC_bool GC_is_thread_tsd_valid(void *tsd); |
| 176 |
void GC_check_tls_for(GC_tlfs p); |
| 177 |
# if defined(USE_CUSTOM_SPECIFIC) |
| 178 |
void GC_check_tsd_marks(tsd *key); |
| 179 |
# endif |
| 180 |
#endif /* GC_ASSERTIONS */ |
| 181 |
|
| 182 |
#ifndef GC_ATTR_TLS_FAST |
| 183 |
# define GC_ATTR_TLS_FAST /* empty */ |
| 184 |
#endif |
| 185 |
|
| 186 |
extern |
| 187 |
#if defined(USE_COMPILER_TLS) |
| 188 |
__thread GC_ATTR_TLS_FAST |
| 189 |
#elif defined(USE_WIN32_COMPILER_TLS) |
| 190 |
__declspec(thread) GC_ATTR_TLS_FAST |
| 191 |
#endif |
| 192 |
GC_key_t GC_thread_key; |
| 193 |
/* This is set up by the thread_local_alloc implementation. No need */ |
| 194 |
/* for cleanup on thread exit. But the thread support layer makes sure */ |
| 195 |
/* that GC_thread_key is traced, if necessary. */ |
| 196 |
|
| 197 |
EXTERN_C_END |
| 198 |
|
| 199 |
#endif /* THREAD_LOCAL_ALLOC */ |
| 200 |
|
| 201 |
#endif /* GC_THREAD_LOCAL_ALLOC_H */ |