| 1 |
#include "common_header.h" |
| 2 |
#include "structdef.h" /* for struct pypy_threadlocal_s */ |
| 3 |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
#include <assert.h> |
| 7 |
#include "src/threadlocal.h" |
| 8 |
#include "src/thread.h" |
| 9 |
|
| 10 |
|
| 11 |
/* this is a spin-lock that must be acquired around each doubly-linked-list |
| 12 |
manipulation (because such manipulations can occur without the GIL) */ |
| 13 |
static Signed pypy_threadlocal_lock = 0; |
| 14 |
|
| 15 |
static int check_valid(void); |
| 16 |
|
| 17 |
int _RPython_ThreadLocals_AcquireTimeout(int max_wait_iterations) { |
| 18 |
while (1) { |
| 19 |
Signed old_value = pypy_lock_test_and_set(&pypy_threadlocal_lock, 1); |
| 20 |
if (old_value == 0) |
| 21 |
break; |
| 22 |
/* busy loop */ |
| 23 |
if (max_wait_iterations == 0) |
| 24 |
return -1; |
| 25 |
if (max_wait_iterations > 0) |
| 26 |
--max_wait_iterations; |
| 27 |
} |
| 28 |
assert(check_valid()); |
| 29 |
return 0; |
| 30 |
} |
| 31 |
void _RPython_ThreadLocals_Acquire(void) { |
| 32 |
_RPython_ThreadLocals_AcquireTimeout(-1); |
| 33 |
} |
| 34 |
void _RPython_ThreadLocals_Release(void) { |
| 35 |
assert(check_valid()); |
| 36 |
pypy_lock_release(&pypy_threadlocal_lock); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
pthread_key_t pypy_threadlocal_key |
| 41 |
#ifdef _WIN32 |
| 42 |
= TLS_OUT_OF_INDEXES |
| 43 |
#endif |
| 44 |
; |
| 45 |
|
| 46 |
static struct pypy_threadlocal_s linkedlist_head = { |
| 47 |
-1, /* ready */ |
| 48 |
NULL, /* stack_end */ |
| 49 |
&linkedlist_head, /* prev */ |
| 50 |
&linkedlist_head }; /* next */ |
| 51 |
|
| 52 |
static int check_valid(void) |
| 53 |
{ |
| 54 |
struct pypy_threadlocal_s *prev, *cur; |
| 55 |
prev = &linkedlist_head; |
| 56 |
while (1) { |
| 57 |
cur = prev->next; |
| 58 |
assert(cur->prev == prev); |
| 59 |
if (cur == &linkedlist_head) |
| 60 |
break; |
| 61 |
assert(cur->ready == 42); |
| 62 |
assert(cur->next != cur); |
| 63 |
prev = cur; |
| 64 |
} |
| 65 |
assert(cur->ready == -1); |
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
static void cleanup_after_fork(void) |
| 70 |
{ |
| 71 |
/* assume that at most one pypy_threadlocal_s survived, the current one */ |
| 72 |
struct pypy_threadlocal_s *cur; |
| 73 |
cur = (struct pypy_threadlocal_s *)_RPy_ThreadLocals_Get(); |
| 74 |
if (cur && cur->ready == 42) { |
| 75 |
cur->next = cur->prev = &linkedlist_head; |
| 76 |
linkedlist_head.next = linkedlist_head.prev = cur; |
| 77 |
} |
| 78 |
else { |
| 79 |
linkedlist_head.next = linkedlist_head.prev = &linkedlist_head; |
| 80 |
} |
| 81 |
_RPython_ThreadLocals_Release(); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
struct pypy_threadlocal_s * |
| 86 |
_RPython_ThreadLocals_Enum(struct pypy_threadlocal_s *prev) |
| 87 |
{ |
| 88 |
if (prev == NULL) |
| 89 |
prev = &linkedlist_head; |
| 90 |
if (prev->next == &linkedlist_head) |
| 91 |
return NULL; |
| 92 |
return prev->next; |
| 93 |
} |
| 94 |
|
| 95 |
struct pypy_threadlocal_s *_RPython_ThreadLocals_Head(void) |
| 96 |
{ |
| 97 |
return &linkedlist_head; |
| 98 |
} |
| 99 |
|
| 100 |
static void _RPy_ThreadLocals_Init(void *p) |
| 101 |
{ |
| 102 |
struct pypy_threadlocal_s *tls = (struct pypy_threadlocal_s *)p; |
| 103 |
struct pypy_threadlocal_s *oldnext; |
| 104 |
memset(p, 0, sizeof(struct pypy_threadlocal_s)); |
| 105 |
|
| 106 |
#ifdef RPY_TLOFS_p_errno |
| 107 |
tls->p_errno = &errno; |
| 108 |
#endif |
| 109 |
#ifdef RPY_TLOFS_thread_ident |
| 110 |
tls->thread_ident = |
| 111 |
# ifdef _WIN32 |
| 112 |
(Signed)GetCurrentThreadId(); |
| 113 |
# else |
| 114 |
(Signed)pthread_self(); /* xxx This abuses pthread_self() by |
| 115 |
assuming it just returns a integer. According to |
| 116 |
comments in CPython's source code, the platforms |
| 117 |
where it is not the case are rather old nowadays. */ |
| 118 |
# endif |
| 119 |
#endif |
| 120 |
_RPython_ThreadLocals_Acquire(); |
| 121 |
oldnext = linkedlist_head.next; |
| 122 |
tls->prev = &linkedlist_head; |
| 123 |
tls->next = oldnext; |
| 124 |
linkedlist_head.next = tls; |
| 125 |
oldnext->prev = tls; |
| 126 |
tls->ready = 42; |
| 127 |
_RPython_ThreadLocals_Release(); |
| 128 |
} |
| 129 |
|
| 130 |
static void threadloc_unlink(void *p) |
| 131 |
{ |
| 132 |
/* warning: this can be called at completely random times without |
| 133 |
the GIL. */ |
| 134 |
struct pypy_threadlocal_s *tls = (struct pypy_threadlocal_s *)p; |
| 135 |
_RPython_ThreadLocals_Acquire(); |
| 136 |
if (tls->ready == 42) { |
| 137 |
tls->next->prev = tls->prev; |
| 138 |
tls->prev->next = tls->next; |
| 139 |
memset(tls, 0xDD, sizeof(struct pypy_threadlocal_s)); /* debug */ |
| 140 |
tls->ready = 0; |
| 141 |
} |
| 142 |
_RPython_ThreadLocals_Release(); |
| 143 |
#ifndef USE___THREAD |
| 144 |
free(p); |
| 145 |
#endif |
| 146 |
} |
| 147 |
|
| 148 |
#ifdef _WIN32 |
| 149 |
/* xxx Defines a DllMain() function. It's horrible imho: it only |
| 150 |
works if we happen to compile a DLL (not a EXE); and of course you |
| 151 |
get link-time errors if two files in the same DLL do the same. |
| 152 |
There are some alternatives known, but they are horrible in other |
| 153 |
ways (e.g. using undocumented behavior). This seems to be the |
| 154 |
simplest, but feel free to fix if you need that. |
| 155 |
|
| 156 |
For this reason we have the line 'not _win32 or config.translation.shared' |
| 157 |
in rpython.rlib.rthread. |
| 158 |
*/ |
| 159 |
BOOL WINAPI DllMain(HINSTANCE hinstDLL, |
| 160 |
DWORD reason_for_call, |
| 161 |
LPVOID reserved) |
| 162 |
{ |
| 163 |
LPVOID p; |
| 164 |
switch (reason_for_call) { |
| 165 |
case DLL_THREAD_DETACH: |
| 166 |
if (pypy_threadlocal_key != TLS_OUT_OF_INDEXES) { |
| 167 |
p = TlsGetValue(pypy_threadlocal_key); |
| 168 |
if (p != NULL) { |
| 169 |
TlsSetValue(pypy_threadlocal_key, NULL); |
| 170 |
threadloc_unlink(p); |
| 171 |
} |
| 172 |
} |
| 173 |
break; |
| 174 |
case DLL_PROCESS_ATTACH: |
| 175 |
case DLL_THREAD_ATTACH: |
| 176 |
#ifdef RPY_WITH_GIL |
| 177 |
RPython_ThreadLocals_ProgramInit(); |
| 178 |
#endif |
| 179 |
break; |
| 180 |
default: |
| 181 |
break; |
| 182 |
} |
| 183 |
return TRUE; |
| 184 |
} |
| 185 |
#endif |
| 186 |
|
| 187 |
void RPython_ThreadLocals_ProgramInit(void) |
| 188 |
{ |
| 189 |
/* Initialize the pypy_threadlocal_key, together with a destructor |
| 190 |
that will be called every time a thread shuts down (if there is |
| 191 |
a non-null thread-local value). This is needed even in the |
| 192 |
case where we use '__thread' below, for the destructor. |
| 193 |
*/ |
| 194 |
static int threadlocals_initialized = 0; |
| 195 |
if (threadlocals_initialized) |
| 196 |
return; |
| 197 |
|
| 198 |
assert(pypy_threadlocal_lock == 0); |
| 199 |
#ifdef _WIN32 |
| 200 |
pypy_threadlocal_key = TlsAlloc(); |
| 201 |
if (pypy_threadlocal_key == TLS_OUT_OF_INDEXES) |
| 202 |
#else |
| 203 |
if (pthread_key_create(&pypy_threadlocal_key, threadloc_unlink) != 0) |
| 204 |
#endif |
| 205 |
{ |
| 206 |
fprintf(stderr, "Internal RPython error: " |
| 207 |
"out of thread-local storage indexes"); |
| 208 |
abort(); |
| 209 |
} |
| 210 |
RPY_THREADLOCALREF_ENSURE(); |
| 211 |
|
| 212 |
#ifndef _WIN32 |
| 213 |
pthread_atfork(_RPython_ThreadLocals_Acquire, |
| 214 |
_RPython_ThreadLocals_Release, |
| 215 |
cleanup_after_fork); |
| 216 |
#endif |
| 217 |
threadlocals_initialized = 1; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
/* ------------------------------------------------------------ */ |
| 222 |
#ifdef USE___THREAD |
| 223 |
/* ------------------------------------------------------------ */ |
| 224 |
|
| 225 |
|
| 226 |
/* in this situation, we always have one full 'struct pypy_threadlocal_s' |
| 227 |
available, managed by gcc. */ |
| 228 |
__thread struct pypy_threadlocal_s pypy_threadlocal; |
| 229 |
|
| 230 |
char *_RPython_ThreadLocals_Build(void) |
| 231 |
{ |
| 232 |
RPyAssert(pypy_threadlocal.ready == 0, "unclean thread-local"); |
| 233 |
_RPy_ThreadLocals_Init(&pypy_threadlocal); |
| 234 |
|
| 235 |
/* we also set up &pypy_threadlocal as a POSIX thread-local variable, |
| 236 |
because we need the destructor behavior. */ |
| 237 |
pthread_setspecific(pypy_threadlocal_key, (void *)&pypy_threadlocal); |
| 238 |
|
| 239 |
return (char *)&pypy_threadlocal; |
| 240 |
} |
| 241 |
|
| 242 |
void RPython_ThreadLocals_ThreadDie(void) |
| 243 |
{ |
| 244 |
pthread_setspecific(pypy_threadlocal_key, NULL); |
| 245 |
threadloc_unlink(&pypy_threadlocal); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
/* ------------------------------------------------------------ */ |
| 250 |
#else |
| 251 |
/* ------------------------------------------------------------ */ |
| 252 |
|
| 253 |
|
| 254 |
/* this is the case where the 'struct pypy_threadlocal_s' is allocated |
| 255 |
explicitly, with malloc()/free(), and attached to (a single) thread- |
| 256 |
local key using the API of Windows or pthread. */ |
| 257 |
|
| 258 |
|
| 259 |
char *_RPython_ThreadLocals_Build(void) |
| 260 |
{ |
| 261 |
void *p = malloc(sizeof(struct pypy_threadlocal_s)); |
| 262 |
if (!p) { |
| 263 |
fprintf(stderr, "Internal RPython error: " |
| 264 |
"out of memory for the thread-local storage"); |
| 265 |
abort(); |
| 266 |
} |
| 267 |
_RPy_ThreadLocals_Init(p); |
| 268 |
_RPy_ThreadLocals_Set(p); |
| 269 |
return (char *)p; |
| 270 |
} |
| 271 |
|
| 272 |
void RPython_ThreadLocals_ThreadDie(void) |
| 273 |
{ |
| 274 |
void *p = _RPy_ThreadLocals_Get(); |
| 275 |
if (p != NULL) { |
| 276 |
_RPy_ThreadLocals_Set(NULL); |
| 277 |
threadloc_unlink(p); /* includes free(p) */ |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
/* ------------------------------------------------------------ */ |
| 283 |
#endif |
| 284 |
/* ------------------------------------------------------------ */ |