Debian Codegraph @

rpython/translator/c/src/threadlocal.c

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