blob: 8ac40f95d109d7ca062ae14ed025fdc3acac8ef7 [file] [log] [blame]
Guido van Rossum34679b71993-01-26 13:33:44 +00001
Guido van Rossum1d5735e1994-08-30 08:27:36 +00002/* Thread package.
3 This is intended to be usable independently from Python.
4 The implementation for system foobar is in a file thread_foobar.h
5 which is included by this file dependent on config settings.
6 Stuff shared by all thread_*.h files is collected here. */
7
Martin v. Löwiscdc44512002-01-12 11:05:12 +00008#include "Python.h"
Guido van Rossum1d5735e1994-08-30 08:27:36 +00009
Matthias Klosea2542be2004-08-16 11:35:51 +000010
11#ifndef _POSIX_THREADS
12/* This means pthreads are not implemented in libc headers, hence the macro
13 not present in unistd.h. But they still can be implemented as an external
14 library (e.g. gnu pth in pthread emulation) */
15# ifdef HAVE_PTHREAD_H
16# include <pthread.h> /* _POSIX_THREADS */
17# endif
18#endif
19
Guido van Rossum2571cc81999-04-07 16:07:23 +000020#ifndef DONT_HAVE_STDIO_H
Guido van Rossum1d5735e1994-08-30 08:27:36 +000021#include <stdio.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +000022#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +000023
Guido van Rossum1d5735e1994-08-30 08:27:36 +000024#include <stdlib.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +000025
Guido van Rossumd11bfdd1997-04-29 21:48:34 +000026#ifdef __sgi
Matthias Klose3cef2a92012-03-14 23:39:33 +010027#ifndef HAVE_PTHREAD_H /* XXX Need to check in configure.ac */
Guido van Rossumd11bfdd1997-04-29 21:48:34 +000028#undef _POSIX_THREADS
29#endif
30#endif
31
Guido van Rossum49b56061998-10-01 20:42:43 +000032#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000033
Guido van Rossum1d5735e1994-08-30 08:27:36 +000034#ifndef _POSIX_THREADS
35
Guido van Rossum1984f1e1992-08-04 12:41:02 +000036#ifdef __sgi
Guido van Rossum1d5735e1994-08-30 08:27:36 +000037#define SGI_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000038#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +000039
Guido van Rossum1d5735e1994-08-30 08:27:36 +000040#ifdef HAVE_THREAD_H
41#define SOLARIS_THREADS
42#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +000043
Guido van Rossum1d5735e1994-08-30 08:27:36 +000044#if defined(sun) && !defined(SOLARIS_THREADS)
45#define SUN_LWP
46#endif
47
Guido van Rossum539c6622005-09-14 17:49:54 +000048/* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then
Ezio Melottic2077b02011-03-16 12:34:31 +020049 enough of the Posix threads package is implemented to support python
Guido van Rossum539c6622005-09-14 17:49:54 +000050 threads.
51
52 This is valid for HP-UX 11.23 running on an ia64 system. If needed, add
Martin Panterb362f752015-11-02 03:37:02 +000053 a check of __ia64 to verify that we're running on an ia64 system instead
Guido van Rossum539c6622005-09-14 17:49:54 +000054 of a pa-risc system.
55*/
56#ifdef __hpux
57#ifdef _SC_THREADS
58#define _POSIX_THREADS
59#endif
60#endif
61
Sjoerd Mullender66bca321993-12-03 16:54:45 +000062#endif /* _POSIX_THREADS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000063
Guido van Rossum1984f1e1992-08-04 12:41:02 +000064
Dan Willemsen04e39102019-02-14 21:00:06 -080065#ifdef dprintf
66#undef dprintf
67#endif
Guido van Rossum408027e1996-12-30 16:17:54 +000068#ifdef Py_DEBUG
Guido van Rossum1d5735e1994-08-30 08:27:36 +000069static int thread_debug = 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +000070#define dprintf(args) (void)((thread_debug & 1) && printf args)
71#define d2printf(args) ((thread_debug & 8) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000072#else
73#define dprintf(args)
74#define d2printf(args)
75#endif
76
Guido van Rossum1984f1e1992-08-04 12:41:02 +000077static int initialized;
78
Thomas Wouters8ec68fd2000-07-24 14:39:50 +000079static void PyThread__init_thread(void); /* Forward */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000080
Andrew MacIntyre63f0db62006-06-04 12:59:59 +000081void
82PyThread_init_thread(void)
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000083{
Guido van Rossum408027e1996-12-30 16:17:54 +000084#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +000085 char *p = Py_GETENV("PYTHONTHREADDEBUG");
Sjoerd Mullender66bca321993-12-03 16:54:45 +000086
Antoine Pitrouc83ea132010-05-09 14:46:46 +000087 if (p) {
88 if (*p)
89 thread_debug = atoi(p);
90 else
91 thread_debug = 1;
92 }
Guido van Rossum408027e1996-12-30 16:17:54 +000093#endif /* Py_DEBUG */
Antoine Pitrouc83ea132010-05-09 14:46:46 +000094 if (initialized)
95 return;
96 initialized = 1;
97 dprintf(("PyThread_init_thread called\n"));
98 PyThread__init_thread();
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000099}
100
Andrew MacIntyre92913322006-06-13 15:04:24 +0000101/* Support for runtime thread stack size tuning.
102 A value of 0 means using the platform's default stack size
103 or the size specified by the THREAD_STACK_SIZE macro. */
104static size_t _pythread_stacksize = 0;
105
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000106#ifdef SGI_THREADS
107#include "thread_sgi.h"
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000108#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000109
110#ifdef SOLARIS_THREADS
111#include "thread_solaris.h"
112#endif
113
114#ifdef SUN_LWP
115#include "thread_lwp.h"
116#endif
117
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000118#ifdef HAVE_PTH
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000119#include "thread_pth.h"
Martin v. Löwis70849f82003-09-20 11:13:36 +0000120#undef _POSIX_THREADS
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000121#endif
122
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000123#ifdef _POSIX_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000124#include "thread_pthread.h"
Sjoerd Mullendere8934121993-01-13 12:08:48 +0000125#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000126
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000127#ifdef C_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000128#include "thread_cthread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000129#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000130
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000131#ifdef NT_THREADS
132#include "thread_nt.h"
133#endif
134
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000135#ifdef OS2_THREADS
136#include "thread_os2.h"
137#endif
138
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000139#ifdef BEOS_THREADS
140#include "thread_beos.h"
141#endif
142
Martin v. Löwis7d1cd692002-03-09 12:10:54 +0000143#ifdef PLAN9_THREADS
144#include "thread_plan9.h"
145#endif
146
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000147#ifdef ATHEOS_THREADS
148#include "thread_atheos.h"
149#endif
150
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000151/*
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000152#ifdef FOOBAR_THREADS
153#include "thread_foobar.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000154#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000155*/
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000156
Andrew MacIntyre92913322006-06-13 15:04:24 +0000157/* return the current thread stack size */
158size_t
159PyThread_get_stacksize(void)
160{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000161 return _pythread_stacksize;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000162}
163
164/* Only platforms defining a THREAD_SET_STACKSIZE() macro
165 in thread_<platform>.h support changing the stack size.
166 Return 0 if stack size is valid,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000167 -1 if stack size value is invalid,
168 -2 if setting stack size is not supported. */
Andrew MacIntyre92913322006-06-13 15:04:24 +0000169int
170PyThread_set_stacksize(size_t size)
171{
172#if defined(THREAD_SET_STACKSIZE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000173 return THREAD_SET_STACKSIZE(size);
Andrew MacIntyre92913322006-06-13 15:04:24 +0000174#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000175 return -2;
Andrew MacIntyre92913322006-06-13 15:04:24 +0000176#endif
177}
178
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000179#ifndef Py_HAVE_NATIVE_TLS
180/* If the platform has not supplied a platform specific
181 TLS implementation, provide our own.
182
183 This code stolen from "thread_sgi.h", where it was the only
184 implementation of an existing Python TLS API.
185*/
Tim Petersfda787f2004-10-09 22:33:09 +0000186/* ------------------------------------------------------------------------
187Per-thread data ("key") support.
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000188
Tim Petersfda787f2004-10-09 22:33:09 +0000189Use PyThread_create_key() to create a new key. This is typically shared
190across threads.
191
192Use PyThread_set_key_value(thekey, value) to associate void* value with
193thekey in the current thread. Each thread has a distinct mapping of thekey
194to a void* value. Caution: if the current thread already has a mapping
195for thekey, value is ignored.
196
197Use PyThread_get_key_value(thekey) to retrieve the void* value associated
198with thekey in the current thread. This returns NULL if no value is
199associated with thekey in the current thread.
200
201Use PyThread_delete_key_value(thekey) to forget the current thread's associated
202value for thekey. PyThread_delete_key(thekey) forgets the values associated
203with thekey across *all* threads.
204
205While some of these functions have error-return values, none set any
206Python exception.
207
208None of the functions does memory management on behalf of the void* values.
209You need to allocate and deallocate them yourself. If the void* values
210happen to be PyObject*, these functions don't do refcount operations on
211them either.
212
213The GIL does not need to be held when calling these functions; they supply
214their own locking. This isn't true of PyThread_create_key(), though (see
215next paragraph).
216
217There's a hidden assumption that PyThread_create_key() will be called before
218any of the other functions are called. There's also a hidden assumption
219that calls to PyThread_create_key() are serialized externally.
220------------------------------------------------------------------------ */
221
222/* A singly-linked list of struct key objects remembers all the key->value
223 * associations. File static keyhead heads the list. keymutex is used
224 * to enforce exclusion internally.
225 */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000226struct key {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000227 /* Next record in the list, or NULL if this is the last record. */
228 struct key *next;
Tim Petersfda787f2004-10-09 22:33:09 +0000229
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000230 /* The thread id, according to PyThread_get_thread_ident(). */
231 long id;
Tim Petersfda787f2004-10-09 22:33:09 +0000232
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000233 /* The key and its associated value. */
234 int key;
235 void *value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000236};
237
238static struct key *keyhead = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000239static PyThread_type_lock keymutex = NULL;
Tim Petersfda787f2004-10-09 22:33:09 +0000240static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000241
Tim Petersfda787f2004-10-09 22:33:09 +0000242/* Internal helper.
243 * If the current thread has a mapping for key, the appropriate struct key*
244 * is returned. NB: value is ignored in this case!
245 * If there is no mapping for key in the current thread, then:
246 * If value is NULL, NULL is returned.
247 * Else a mapping of key to value is created for the current thread,
248 * and a pointer to a new struct key* is returned; except that if
249 * malloc() can't find room for a new struct key*, NULL is returned.
250 * So when value==NULL, this acts like a pure lookup routine, and when
251 * value!=NULL, this acts like dict.setdefault(), returning an existing
252 * mapping if one exists, else creating a new mapping.
Tim Peters263091e2004-10-10 01:58:44 +0000253 *
254 * Caution: this used to be too clever, trying to hold keymutex only
255 * around the "p->next = keyhead; keyhead = p" pair. That allowed
256 * another thread to mutate the list, via key deletion, concurrent with
257 * find_key() crawling over the list. Hilarity ensued. For example, when
258 * the for-loop here does "p = p->next", p could end up pointing at a
259 * record that PyThread_delete_key_value() was concurrently free()'ing.
260 * That could lead to anything, from failing to find a key that exists, to
261 * segfaults. Now we lock the whole routine.
Tim Petersfda787f2004-10-09 22:33:09 +0000262 */
Tim Peters19717fa2004-10-09 17:38:29 +0000263static struct key *
264find_key(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000265{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000266 struct key *p, *prev_p;
267 long id = PyThread_get_thread_ident();
Tim Petersfda787f2004-10-09 22:33:09 +0000268
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000269 if (!keymutex)
270 return NULL;
271 PyThread_acquire_lock(keymutex, 1);
272 prev_p = NULL;
273 for (p = keyhead; p != NULL; p = p->next) {
274 if (p->id == id && p->key == key)
275 goto Done;
276 /* Sanity check. These states should never happen but if
Serhiy Storchaka0f8f7842014-12-01 18:16:30 +0200277 * they do we must abort. Otherwise we'll end up spinning
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000278 * in a tight loop with the lock held. A similar check is done
279 * in pystate.c tstate_delete_common(). */
280 if (p == prev_p)
281 Py_FatalError("tls find_key: small circular list(!)");
282 prev_p = p;
283 if (p->next == keyhead)
284 Py_FatalError("tls find_key: circular list(!)");
285 }
286 if (value == NULL) {
287 assert(p == NULL);
288 goto Done;
289 }
290 p = (struct key *)malloc(sizeof(struct key));
291 if (p != NULL) {
292 p->id = id;
293 p->key = key;
294 p->value = value;
295 p->next = keyhead;
296 keyhead = p;
297 }
Tim Peters263091e2004-10-10 01:58:44 +0000298 Done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000299 PyThread_release_lock(keymutex);
300 return p;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000301}
302
Tim Petersfda787f2004-10-09 22:33:09 +0000303/* Return a new key. This must be called before any other functions in
304 * this family, and callers must arrange to serialize calls to this
305 * function. No violations are detected.
306 */
Tim Peters19717fa2004-10-09 17:38:29 +0000307int
308PyThread_create_key(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000309{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000310 /* All parts of this function are wrong if it's called by multiple
311 * threads simultaneously.
312 */
313 if (keymutex == NULL)
314 keymutex = PyThread_allocate_lock();
315 return ++nkeys;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000316}
317
Tim Petersfda787f2004-10-09 22:33:09 +0000318/* Forget the associations for key across *all* threads. */
Tim Peters19717fa2004-10-09 17:38:29 +0000319void
320PyThread_delete_key(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000321{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000322 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000323
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000324 PyThread_acquire_lock(keymutex, 1);
325 q = &keyhead;
326 while ((p = *q) != NULL) {
327 if (p->key == key) {
328 *q = p->next;
329 free((void *)p);
330 /* NB This does *not* free p->value! */
331 }
332 else
333 q = &p->next;
334 }
335 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000336}
337
Tim Petersfda787f2004-10-09 22:33:09 +0000338/* Confusing: If the current thread has an association for key,
339 * value is ignored, and 0 is returned. Else an attempt is made to create
340 * an association of key to value for the current thread. 0 is returned
341 * if that succeeds, but -1 is returned if there's not enough memory
342 * to create the association. value must not be NULL.
343 */
Tim Peters19717fa2004-10-09 17:38:29 +0000344int
345PyThread_set_key_value(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000346{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000347 struct key *p;
Tim Petersfda787f2004-10-09 22:33:09 +0000348
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000349 assert(value != NULL);
350 p = find_key(key, value);
351 if (p == NULL)
352 return -1;
353 else
354 return 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000355}
356
Tim Petersfda787f2004-10-09 22:33:09 +0000357/* Retrieve the value associated with key in the current thread, or NULL
358 * if the current thread doesn't have an association for key.
359 */
Tim Peters19717fa2004-10-09 17:38:29 +0000360void *
361PyThread_get_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000362{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000363 struct key *p = find_key(key, NULL);
Tim Petersfda787f2004-10-09 22:33:09 +0000364
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000365 if (p == NULL)
366 return NULL;
367 else
368 return p->value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000369}
370
Tim Petersfda787f2004-10-09 22:33:09 +0000371/* Forget the current thread's association for key, if any. */
Tim Peters19717fa2004-10-09 17:38:29 +0000372void
373PyThread_delete_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000374{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000375 long id = PyThread_get_thread_ident();
376 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000377
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000378 PyThread_acquire_lock(keymutex, 1);
379 q = &keyhead;
380 while ((p = *q) != NULL) {
381 if (p->key == key && p->id == id) {
382 *q = p->next;
383 free((void *)p);
384 /* NB This does *not* free p->value! */
385 break;
386 }
387 else
388 q = &p->next;
389 }
390 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000391}
392
Benjamin Peterson114f7e52008-06-13 00:09:47 +0000393/* Forget everything not associated with the current thread id.
394 * This function is called from PyOS_AfterFork(). It is necessary
395 * because other thread ids which were in use at the time of the fork
396 * may be reused for new threads created in the forked process.
397 */
398void
399PyThread_ReInitTLS(void)
400{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000401 long id = PyThread_get_thread_ident();
402 struct key *p, **q;
Benjamin Peterson114f7e52008-06-13 00:09:47 +0000403
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000404 if (!keymutex)
405 return;
Benjamin Peterson114f7e52008-06-13 00:09:47 +0000406
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000407 /* As with interpreter_lock in PyEval_ReInitThreads()
408 we just create a new lock without freeing the old one */
409 keymutex = PyThread_allocate_lock();
410
411 /* Delete all keys which do not match the current thread id */
412 q = &keyhead;
413 while ((p = *q) != NULL) {
414 if (p->id != id) {
415 *q = p->next;
416 free((void *)p);
417 /* NB This does *not* free p->value! */
418 }
419 else
420 q = &p->next;
421 }
Benjamin Peterson114f7e52008-06-13 00:09:47 +0000422}
423
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000424#endif /* Py_HAVE_NATIVE_TLS */