1 | n/a | |
---|
2 | n/a | /* Thread package. |
---|
3 | n/a | This is intended to be usable independently from Python. |
---|
4 | n/a | The implementation for system foobar is in a file thread_foobar.h |
---|
5 | n/a | which is included by this file dependent on config settings. |
---|
6 | n/a | Stuff shared by all thread_*.h files is collected here. */ |
---|
7 | n/a | |
---|
8 | n/a | #include "Python.h" |
---|
9 | n/a | |
---|
10 | n/a | #ifndef _POSIX_THREADS |
---|
11 | n/a | /* This means pthreads are not implemented in libc headers, hence the macro |
---|
12 | n/a | not present in unistd.h. But they still can be implemented as an external |
---|
13 | n/a | library (e.g. gnu pth in pthread emulation) */ |
---|
14 | n/a | # ifdef HAVE_PTHREAD_H |
---|
15 | n/a | # include <pthread.h> /* _POSIX_THREADS */ |
---|
16 | n/a | # endif |
---|
17 | n/a | #endif |
---|
18 | n/a | |
---|
19 | n/a | #ifndef DONT_HAVE_STDIO_H |
---|
20 | n/a | #include <stdio.h> |
---|
21 | n/a | #endif |
---|
22 | n/a | |
---|
23 | n/a | #include <stdlib.h> |
---|
24 | n/a | |
---|
25 | n/a | #include "pythread.h" |
---|
26 | n/a | |
---|
27 | n/a | #ifndef _POSIX_THREADS |
---|
28 | n/a | |
---|
29 | n/a | /* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then |
---|
30 | n/a | enough of the Posix threads package is implemented to support python |
---|
31 | n/a | threads. |
---|
32 | n/a | |
---|
33 | n/a | This is valid for HP-UX 11.23 running on an ia64 system. If needed, add |
---|
34 | n/a | a check of __ia64 to verify that we're running on an ia64 system instead |
---|
35 | n/a | of a pa-risc system. |
---|
36 | n/a | */ |
---|
37 | n/a | #ifdef __hpux |
---|
38 | n/a | #ifdef _SC_THREADS |
---|
39 | n/a | #define _POSIX_THREADS |
---|
40 | n/a | #endif |
---|
41 | n/a | #endif |
---|
42 | n/a | |
---|
43 | n/a | #endif /* _POSIX_THREADS */ |
---|
44 | n/a | |
---|
45 | n/a | |
---|
46 | n/a | #ifdef Py_DEBUG |
---|
47 | n/a | static int thread_debug = 0; |
---|
48 | n/a | #define dprintf(args) (void)((thread_debug & 1) && printf args) |
---|
49 | n/a | #define d2printf(args) ((thread_debug & 8) && printf args) |
---|
50 | n/a | #else |
---|
51 | n/a | #define dprintf(args) |
---|
52 | n/a | #define d2printf(args) |
---|
53 | n/a | #endif |
---|
54 | n/a | |
---|
55 | n/a | static int initialized; |
---|
56 | n/a | |
---|
57 | n/a | static void PyThread__init_thread(void); /* Forward */ |
---|
58 | n/a | |
---|
59 | n/a | void |
---|
60 | n/a | PyThread_init_thread(void) |
---|
61 | n/a | { |
---|
62 | n/a | #ifdef Py_DEBUG |
---|
63 | n/a | char *p = Py_GETENV("PYTHONTHREADDEBUG"); |
---|
64 | n/a | |
---|
65 | n/a | if (p) { |
---|
66 | n/a | if (*p) |
---|
67 | n/a | thread_debug = atoi(p); |
---|
68 | n/a | else |
---|
69 | n/a | thread_debug = 1; |
---|
70 | n/a | } |
---|
71 | n/a | #endif /* Py_DEBUG */ |
---|
72 | n/a | if (initialized) |
---|
73 | n/a | return; |
---|
74 | n/a | initialized = 1; |
---|
75 | n/a | dprintf(("PyThread_init_thread called\n")); |
---|
76 | n/a | PyThread__init_thread(); |
---|
77 | n/a | } |
---|
78 | n/a | |
---|
79 | n/a | /* Support for runtime thread stack size tuning. |
---|
80 | n/a | A value of 0 means using the platform's default stack size |
---|
81 | n/a | or the size specified by the THREAD_STACK_SIZE macro. */ |
---|
82 | n/a | static size_t _pythread_stacksize = 0; |
---|
83 | n/a | |
---|
84 | n/a | #ifdef _POSIX_THREADS |
---|
85 | n/a | #define PYTHREAD_NAME "pthread" |
---|
86 | n/a | #include "thread_pthread.h" |
---|
87 | n/a | #endif |
---|
88 | n/a | |
---|
89 | n/a | #ifdef NT_THREADS |
---|
90 | n/a | #define PYTHREAD_NAME "nt" |
---|
91 | n/a | #include "thread_nt.h" |
---|
92 | n/a | #endif |
---|
93 | n/a | |
---|
94 | n/a | |
---|
95 | n/a | /* |
---|
96 | n/a | #ifdef FOOBAR_THREADS |
---|
97 | n/a | #include "thread_foobar.h" |
---|
98 | n/a | #endif |
---|
99 | n/a | */ |
---|
100 | n/a | |
---|
101 | n/a | /* return the current thread stack size */ |
---|
102 | n/a | size_t |
---|
103 | n/a | PyThread_get_stacksize(void) |
---|
104 | n/a | { |
---|
105 | n/a | return _pythread_stacksize; |
---|
106 | n/a | } |
---|
107 | n/a | |
---|
108 | n/a | /* Only platforms defining a THREAD_SET_STACKSIZE() macro |
---|
109 | n/a | in thread_<platform>.h support changing the stack size. |
---|
110 | n/a | Return 0 if stack size is valid, |
---|
111 | n/a | -1 if stack size value is invalid, |
---|
112 | n/a | -2 if setting stack size is not supported. */ |
---|
113 | n/a | int |
---|
114 | n/a | PyThread_set_stacksize(size_t size) |
---|
115 | n/a | { |
---|
116 | n/a | #if defined(THREAD_SET_STACKSIZE) |
---|
117 | n/a | return THREAD_SET_STACKSIZE(size); |
---|
118 | n/a | #else |
---|
119 | n/a | return -2; |
---|
120 | n/a | #endif |
---|
121 | n/a | } |
---|
122 | n/a | |
---|
123 | n/a | #ifndef Py_HAVE_NATIVE_TLS |
---|
124 | n/a | /* If the platform has not supplied a platform specific |
---|
125 | n/a | TLS implementation, provide our own. |
---|
126 | n/a | |
---|
127 | n/a | This code stolen from "thread_sgi.h", where it was the only |
---|
128 | n/a | implementation of an existing Python TLS API. |
---|
129 | n/a | */ |
---|
130 | n/a | /* ------------------------------------------------------------------------ |
---|
131 | n/a | Per-thread data ("key") support. |
---|
132 | n/a | |
---|
133 | n/a | Use PyThread_create_key() to create a new key. This is typically shared |
---|
134 | n/a | across threads. |
---|
135 | n/a | |
---|
136 | n/a | Use PyThread_set_key_value(thekey, value) to associate void* value with |
---|
137 | n/a | thekey in the current thread. Each thread has a distinct mapping of thekey |
---|
138 | n/a | to a void* value. Caution: if the current thread already has a mapping |
---|
139 | n/a | for thekey, value is ignored. |
---|
140 | n/a | |
---|
141 | n/a | Use PyThread_get_key_value(thekey) to retrieve the void* value associated |
---|
142 | n/a | with thekey in the current thread. This returns NULL if no value is |
---|
143 | n/a | associated with thekey in the current thread. |
---|
144 | n/a | |
---|
145 | n/a | Use PyThread_delete_key_value(thekey) to forget the current thread's associated |
---|
146 | n/a | value for thekey. PyThread_delete_key(thekey) forgets the values associated |
---|
147 | n/a | with thekey across *all* threads. |
---|
148 | n/a | |
---|
149 | n/a | While some of these functions have error-return values, none set any |
---|
150 | n/a | Python exception. |
---|
151 | n/a | |
---|
152 | n/a | None of the functions does memory management on behalf of the void* values. |
---|
153 | n/a | You need to allocate and deallocate them yourself. If the void* values |
---|
154 | n/a | happen to be PyObject*, these functions don't do refcount operations on |
---|
155 | n/a | them either. |
---|
156 | n/a | |
---|
157 | n/a | The GIL does not need to be held when calling these functions; they supply |
---|
158 | n/a | their own locking. This isn't true of PyThread_create_key(), though (see |
---|
159 | n/a | next paragraph). |
---|
160 | n/a | |
---|
161 | n/a | There's a hidden assumption that PyThread_create_key() will be called before |
---|
162 | n/a | any of the other functions are called. There's also a hidden assumption |
---|
163 | n/a | that calls to PyThread_create_key() are serialized externally. |
---|
164 | n/a | ------------------------------------------------------------------------ */ |
---|
165 | n/a | |
---|
166 | n/a | /* A singly-linked list of struct key objects remembers all the key->value |
---|
167 | n/a | * associations. File static keyhead heads the list. keymutex is used |
---|
168 | n/a | * to enforce exclusion internally. |
---|
169 | n/a | */ |
---|
170 | n/a | struct key { |
---|
171 | n/a | /* Next record in the list, or NULL if this is the last record. */ |
---|
172 | n/a | struct key *next; |
---|
173 | n/a | |
---|
174 | n/a | /* The thread id, according to PyThread_get_thread_ident(). */ |
---|
175 | n/a | long id; |
---|
176 | n/a | |
---|
177 | n/a | /* The key and its associated value. */ |
---|
178 | n/a | int key; |
---|
179 | n/a | void *value; |
---|
180 | n/a | }; |
---|
181 | n/a | |
---|
182 | n/a | static struct key *keyhead = NULL; |
---|
183 | n/a | static PyThread_type_lock keymutex = NULL; |
---|
184 | n/a | static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */ |
---|
185 | n/a | |
---|
186 | n/a | /* Internal helper. |
---|
187 | n/a | * If the current thread has a mapping for key, the appropriate struct key* |
---|
188 | n/a | * is returned. NB: value is ignored in this case! |
---|
189 | n/a | * If there is no mapping for key in the current thread, then: |
---|
190 | n/a | * If value is NULL, NULL is returned. |
---|
191 | n/a | * Else a mapping of key to value is created for the current thread, |
---|
192 | n/a | * and a pointer to a new struct key* is returned; except that if |
---|
193 | n/a | * malloc() can't find room for a new struct key*, NULL is returned. |
---|
194 | n/a | * So when value==NULL, this acts like a pure lookup routine, and when |
---|
195 | n/a | * value!=NULL, this acts like dict.setdefault(), returning an existing |
---|
196 | n/a | * mapping if one exists, else creating a new mapping. |
---|
197 | n/a | * |
---|
198 | n/a | * Caution: this used to be too clever, trying to hold keymutex only |
---|
199 | n/a | * around the "p->next = keyhead; keyhead = p" pair. That allowed |
---|
200 | n/a | * another thread to mutate the list, via key deletion, concurrent with |
---|
201 | n/a | * find_key() crawling over the list. Hilarity ensued. For example, when |
---|
202 | n/a | * the for-loop here does "p = p->next", p could end up pointing at a |
---|
203 | n/a | * record that PyThread_delete_key_value() was concurrently free()'ing. |
---|
204 | n/a | * That could lead to anything, from failing to find a key that exists, to |
---|
205 | n/a | * segfaults. Now we lock the whole routine. |
---|
206 | n/a | */ |
---|
207 | n/a | static struct key * |
---|
208 | n/a | find_key(int set_value, int key, void *value) |
---|
209 | n/a | { |
---|
210 | n/a | struct key *p, *prev_p; |
---|
211 | n/a | long id = PyThread_get_thread_ident(); |
---|
212 | n/a | |
---|
213 | n/a | if (!keymutex) |
---|
214 | n/a | return NULL; |
---|
215 | n/a | PyThread_acquire_lock(keymutex, 1); |
---|
216 | n/a | prev_p = NULL; |
---|
217 | n/a | for (p = keyhead; p != NULL; p = p->next) { |
---|
218 | n/a | if (p->id == id && p->key == key) { |
---|
219 | n/a | if (set_value) |
---|
220 | n/a | p->value = value; |
---|
221 | n/a | goto Done; |
---|
222 | n/a | } |
---|
223 | n/a | /* Sanity check. These states should never happen but if |
---|
224 | n/a | * they do we must abort. Otherwise we'll end up spinning |
---|
225 | n/a | * in a tight loop with the lock held. A similar check is done |
---|
226 | n/a | * in pystate.c tstate_delete_common(). */ |
---|
227 | n/a | if (p == prev_p) |
---|
228 | n/a | Py_FatalError("tls find_key: small circular list(!)"); |
---|
229 | n/a | prev_p = p; |
---|
230 | n/a | if (p->next == keyhead) |
---|
231 | n/a | Py_FatalError("tls find_key: circular list(!)"); |
---|
232 | n/a | } |
---|
233 | n/a | if (!set_value && value == NULL) { |
---|
234 | n/a | assert(p == NULL); |
---|
235 | n/a | goto Done; |
---|
236 | n/a | } |
---|
237 | n/a | p = (struct key *)PyMem_RawMalloc(sizeof(struct key)); |
---|
238 | n/a | if (p != NULL) { |
---|
239 | n/a | p->id = id; |
---|
240 | n/a | p->key = key; |
---|
241 | n/a | p->value = value; |
---|
242 | n/a | p->next = keyhead; |
---|
243 | n/a | keyhead = p; |
---|
244 | n/a | } |
---|
245 | n/a | Done: |
---|
246 | n/a | PyThread_release_lock(keymutex); |
---|
247 | n/a | return p; |
---|
248 | n/a | } |
---|
249 | n/a | |
---|
250 | n/a | /* Return a new key. This must be called before any other functions in |
---|
251 | n/a | * this family, and callers must arrange to serialize calls to this |
---|
252 | n/a | * function. No violations are detected. |
---|
253 | n/a | */ |
---|
254 | n/a | int |
---|
255 | n/a | PyThread_create_key(void) |
---|
256 | n/a | { |
---|
257 | n/a | /* All parts of this function are wrong if it's called by multiple |
---|
258 | n/a | * threads simultaneously. |
---|
259 | n/a | */ |
---|
260 | n/a | if (keymutex == NULL) |
---|
261 | n/a | keymutex = PyThread_allocate_lock(); |
---|
262 | n/a | return ++nkeys; |
---|
263 | n/a | } |
---|
264 | n/a | |
---|
265 | n/a | /* Forget the associations for key across *all* threads. */ |
---|
266 | n/a | void |
---|
267 | n/a | PyThread_delete_key(int key) |
---|
268 | n/a | { |
---|
269 | n/a | struct key *p, **q; |
---|
270 | n/a | |
---|
271 | n/a | PyThread_acquire_lock(keymutex, 1); |
---|
272 | n/a | q = &keyhead; |
---|
273 | n/a | while ((p = *q) != NULL) { |
---|
274 | n/a | if (p->key == key) { |
---|
275 | n/a | *q = p->next; |
---|
276 | n/a | PyMem_RawFree((void *)p); |
---|
277 | n/a | /* NB This does *not* free p->value! */ |
---|
278 | n/a | } |
---|
279 | n/a | else |
---|
280 | n/a | q = &p->next; |
---|
281 | n/a | } |
---|
282 | n/a | PyThread_release_lock(keymutex); |
---|
283 | n/a | } |
---|
284 | n/a | |
---|
285 | n/a | int |
---|
286 | n/a | PyThread_set_key_value(int key, void *value) |
---|
287 | n/a | { |
---|
288 | n/a | struct key *p; |
---|
289 | n/a | |
---|
290 | n/a | p = find_key(1, key, value); |
---|
291 | n/a | if (p == NULL) |
---|
292 | n/a | return -1; |
---|
293 | n/a | else |
---|
294 | n/a | return 0; |
---|
295 | n/a | } |
---|
296 | n/a | |
---|
297 | n/a | /* Retrieve the value associated with key in the current thread, or NULL |
---|
298 | n/a | * if the current thread doesn't have an association for key. |
---|
299 | n/a | */ |
---|
300 | n/a | void * |
---|
301 | n/a | PyThread_get_key_value(int key) |
---|
302 | n/a | { |
---|
303 | n/a | struct key *p = find_key(0, key, NULL); |
---|
304 | n/a | |
---|
305 | n/a | if (p == NULL) |
---|
306 | n/a | return NULL; |
---|
307 | n/a | else |
---|
308 | n/a | return p->value; |
---|
309 | n/a | } |
---|
310 | n/a | |
---|
311 | n/a | /* Forget the current thread's association for key, if any. */ |
---|
312 | n/a | void |
---|
313 | n/a | PyThread_delete_key_value(int key) |
---|
314 | n/a | { |
---|
315 | n/a | long id = PyThread_get_thread_ident(); |
---|
316 | n/a | struct key *p, **q; |
---|
317 | n/a | |
---|
318 | n/a | PyThread_acquire_lock(keymutex, 1); |
---|
319 | n/a | q = &keyhead; |
---|
320 | n/a | while ((p = *q) != NULL) { |
---|
321 | n/a | if (p->key == key && p->id == id) { |
---|
322 | n/a | *q = p->next; |
---|
323 | n/a | PyMem_RawFree((void *)p); |
---|
324 | n/a | /* NB This does *not* free p->value! */ |
---|
325 | n/a | break; |
---|
326 | n/a | } |
---|
327 | n/a | else |
---|
328 | n/a | q = &p->next; |
---|
329 | n/a | } |
---|
330 | n/a | PyThread_release_lock(keymutex); |
---|
331 | n/a | } |
---|
332 | n/a | |
---|
333 | n/a | /* Forget everything not associated with the current thread id. |
---|
334 | n/a | * This function is called from PyOS_AfterFork(). It is necessary |
---|
335 | n/a | * because other thread ids which were in use at the time of the fork |
---|
336 | n/a | * may be reused for new threads created in the forked process. |
---|
337 | n/a | */ |
---|
338 | n/a | void |
---|
339 | n/a | PyThread_ReInitTLS(void) |
---|
340 | n/a | { |
---|
341 | n/a | long id = PyThread_get_thread_ident(); |
---|
342 | n/a | struct key *p, **q; |
---|
343 | n/a | |
---|
344 | n/a | if (!keymutex) |
---|
345 | n/a | return; |
---|
346 | n/a | |
---|
347 | n/a | /* As with interpreter_lock in PyEval_ReInitThreads() |
---|
348 | n/a | we just create a new lock without freeing the old one */ |
---|
349 | n/a | keymutex = PyThread_allocate_lock(); |
---|
350 | n/a | |
---|
351 | n/a | /* Delete all keys which do not match the current thread id */ |
---|
352 | n/a | q = &keyhead; |
---|
353 | n/a | while ((p = *q) != NULL) { |
---|
354 | n/a | if (p->id != id) { |
---|
355 | n/a | *q = p->next; |
---|
356 | n/a | PyMem_RawFree((void *)p); |
---|
357 | n/a | /* NB This does *not* free p->value! */ |
---|
358 | n/a | } |
---|
359 | n/a | else |
---|
360 | n/a | q = &p->next; |
---|
361 | n/a | } |
---|
362 | n/a | } |
---|
363 | n/a | |
---|
364 | n/a | #endif /* Py_HAVE_NATIVE_TLS */ |
---|
365 | n/a | |
---|
366 | n/a | PyDoc_STRVAR(threadinfo__doc__, |
---|
367 | n/a | "sys.thread_info\n\ |
---|
368 | n/a | \n\ |
---|
369 | n/a | A struct sequence holding information about the thread implementation."); |
---|
370 | n/a | |
---|
371 | n/a | static PyStructSequence_Field threadinfo_fields[] = { |
---|
372 | n/a | {"name", "name of the thread implementation"}, |
---|
373 | n/a | {"lock", "name of the lock implementation"}, |
---|
374 | n/a | {"version", "name and version of the thread library"}, |
---|
375 | n/a | {0} |
---|
376 | n/a | }; |
---|
377 | n/a | |
---|
378 | n/a | static PyStructSequence_Desc threadinfo_desc = { |
---|
379 | n/a | "sys.thread_info", /* name */ |
---|
380 | n/a | threadinfo__doc__, /* doc */ |
---|
381 | n/a | threadinfo_fields, /* fields */ |
---|
382 | n/a | 3 |
---|
383 | n/a | }; |
---|
384 | n/a | |
---|
385 | n/a | static PyTypeObject ThreadInfoType; |
---|
386 | n/a | |
---|
387 | n/a | PyObject* |
---|
388 | n/a | PyThread_GetInfo(void) |
---|
389 | n/a | { |
---|
390 | n/a | PyObject *threadinfo, *value; |
---|
391 | n/a | int pos = 0; |
---|
392 | n/a | #if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \ |
---|
393 | n/a | && defined(_CS_GNU_LIBPTHREAD_VERSION)) |
---|
394 | n/a | char buffer[255]; |
---|
395 | n/a | int len; |
---|
396 | n/a | #endif |
---|
397 | n/a | |
---|
398 | n/a | if (ThreadInfoType.tp_name == 0) { |
---|
399 | n/a | if (PyStructSequence_InitType2(&ThreadInfoType, &threadinfo_desc) < 0) |
---|
400 | n/a | return NULL; |
---|
401 | n/a | } |
---|
402 | n/a | |
---|
403 | n/a | threadinfo = PyStructSequence_New(&ThreadInfoType); |
---|
404 | n/a | if (threadinfo == NULL) |
---|
405 | n/a | return NULL; |
---|
406 | n/a | |
---|
407 | n/a | value = PyUnicode_FromString(PYTHREAD_NAME); |
---|
408 | n/a | if (value == NULL) { |
---|
409 | n/a | Py_DECREF(threadinfo); |
---|
410 | n/a | return NULL; |
---|
411 | n/a | } |
---|
412 | n/a | PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
---|
413 | n/a | |
---|
414 | n/a | #ifdef _POSIX_THREADS |
---|
415 | n/a | #ifdef USE_SEMAPHORES |
---|
416 | n/a | value = PyUnicode_FromString("semaphore"); |
---|
417 | n/a | #else |
---|
418 | n/a | value = PyUnicode_FromString("mutex+cond"); |
---|
419 | n/a | #endif |
---|
420 | n/a | if (value == NULL) { |
---|
421 | n/a | Py_DECREF(threadinfo); |
---|
422 | n/a | return NULL; |
---|
423 | n/a | } |
---|
424 | n/a | #else |
---|
425 | n/a | Py_INCREF(Py_None); |
---|
426 | n/a | value = Py_None; |
---|
427 | n/a | #endif |
---|
428 | n/a | PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
---|
429 | n/a | |
---|
430 | n/a | #if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \ |
---|
431 | n/a | && defined(_CS_GNU_LIBPTHREAD_VERSION)) |
---|
432 | n/a | value = NULL; |
---|
433 | n/a | len = confstr(_CS_GNU_LIBPTHREAD_VERSION, buffer, sizeof(buffer)); |
---|
434 | n/a | if (1 < len && (size_t)len < sizeof(buffer)) { |
---|
435 | n/a | value = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
---|
436 | n/a | if (value == NULL) |
---|
437 | n/a | PyErr_Clear(); |
---|
438 | n/a | } |
---|
439 | n/a | if (value == NULL) |
---|
440 | n/a | #endif |
---|
441 | n/a | { |
---|
442 | n/a | Py_INCREF(Py_None); |
---|
443 | n/a | value = Py_None; |
---|
444 | n/a | } |
---|
445 | n/a | PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
---|
446 | n/a | return threadinfo; |
---|
447 | n/a | } |
---|