blob: b4e3ad0915fb27bf7c9ecd04416cd73c6a6ae99f [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
27#ifndef HAVE_PTHREAD_H /* XXX Need to check in configure.in */
28#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
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000049 enough of the Posix threads package is implimented 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
53 a check of __ia64 to verify that we're running on a ia64 system instead
54 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
Guido van Rossum408027e1996-12-30 16:17:54 +000065#ifdef Py_DEBUG
Guido van Rossum1d5735e1994-08-30 08:27:36 +000066static int thread_debug = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000067#define dprintf(args) (void)((thread_debug & 1) && printf args)
68#define d2printf(args) ((thread_debug & 8) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000069#else
70#define dprintf(args)
71#define d2printf(args)
72#endif
73
Guido van Rossum1984f1e1992-08-04 12:41:02 +000074static int initialized;
75
Thomas Wouters8ec68fd2000-07-24 14:39:50 +000076static void PyThread__init_thread(void); /* Forward */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000077
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000078void
79PyThread_init_thread(void)
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000080{
Guido van Rossum408027e1996-12-30 16:17:54 +000081#ifdef Py_DEBUG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000082 char *p = Py_GETENV("PYTHONTHREADDEBUG");
Sjoerd Mullender66bca321993-12-03 16:54:45 +000083
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000084 if (p) {
85 if (*p)
86 thread_debug = atoi(p);
87 else
88 thread_debug = 1;
89 }
Guido van Rossum408027e1996-12-30 16:17:54 +000090#endif /* Py_DEBUG */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000091 if (initialized)
92 return;
93 initialized = 1;
94 dprintf(("PyThread_init_thread called\n"));
95 PyThread__init_thread();
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000096}
97
Thomas Wouters0e3f5912006-08-11 14:57:12 +000098/* Support for runtime thread stack size tuning.
99 A value of 0 means using the platform's default stack size
100 or the size specified by the THREAD_STACK_SIZE macro. */
101static size_t _pythread_stacksize = 0;
102
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000103#ifdef SGI_THREADS
104#include "thread_sgi.h"
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000105#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000106
107#ifdef SOLARIS_THREADS
108#include "thread_solaris.h"
109#endif
110
111#ifdef SUN_LWP
112#include "thread_lwp.h"
113#endif
114
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000115#ifdef HAVE_PTH
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000116#include "thread_pth.h"
Martin v. Löwis70849f82003-09-20 11:13:36 +0000117#undef _POSIX_THREADS
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000118#endif
119
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000120#ifdef _POSIX_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000121#include "thread_pthread.h"
Sjoerd Mullendere8934121993-01-13 12:08:48 +0000122#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000123
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000124#ifdef C_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000125#include "thread_cthread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000126#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000127
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000128#ifdef NT_THREADS
129#include "thread_nt.h"
130#endif
131
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000132#ifdef OS2_THREADS
133#include "thread_os2.h"
134#endif
135
Martin v. Löwis7d1cd692002-03-09 12:10:54 +0000136#ifdef PLAN9_THREADS
137#include "thread_plan9.h"
138#endif
139
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000140#ifdef ATHEOS_THREADS
141#include "thread_atheos.h"
142#endif
143
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000144/*
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000145#ifdef FOOBAR_THREADS
146#include "thread_foobar.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000147#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000148*/
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000149
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000150/* return the current thread stack size */
151size_t
152PyThread_get_stacksize(void)
153{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000154 return _pythread_stacksize;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000155}
156
157/* Only platforms defining a THREAD_SET_STACKSIZE() macro
158 in thread_<platform>.h support changing the stack size.
159 Return 0 if stack size is valid,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000160 -1 if stack size value is invalid,
161 -2 if setting stack size is not supported. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000162int
163PyThread_set_stacksize(size_t size)
164{
165#if defined(THREAD_SET_STACKSIZE)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000166 return THREAD_SET_STACKSIZE(size);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000167#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000168 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000169#endif
170}
171
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000172#ifndef Py_HAVE_NATIVE_TLS
173/* If the platform has not supplied a platform specific
174 TLS implementation, provide our own.
175
176 This code stolen from "thread_sgi.h", where it was the only
177 implementation of an existing Python TLS API.
178*/
Tim Petersfda787f2004-10-09 22:33:09 +0000179/* ------------------------------------------------------------------------
180Per-thread data ("key") support.
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000181
Tim Petersfda787f2004-10-09 22:33:09 +0000182Use PyThread_create_key() to create a new key. This is typically shared
183across threads.
184
185Use PyThread_set_key_value(thekey, value) to associate void* value with
186thekey in the current thread. Each thread has a distinct mapping of thekey
187to a void* value. Caution: if the current thread already has a mapping
188for thekey, value is ignored.
189
190Use PyThread_get_key_value(thekey) to retrieve the void* value associated
191with thekey in the current thread. This returns NULL if no value is
192associated with thekey in the current thread.
193
194Use PyThread_delete_key_value(thekey) to forget the current thread's associated
195value for thekey. PyThread_delete_key(thekey) forgets the values associated
196with thekey across *all* threads.
197
198While some of these functions have error-return values, none set any
199Python exception.
200
201None of the functions does memory management on behalf of the void* values.
202You need to allocate and deallocate them yourself. If the void* values
203happen to be PyObject*, these functions don't do refcount operations on
204them either.
205
206The GIL does not need to be held when calling these functions; they supply
207their own locking. This isn't true of PyThread_create_key(), though (see
208next paragraph).
209
210There's a hidden assumption that PyThread_create_key() will be called before
211any of the other functions are called. There's also a hidden assumption
212that calls to PyThread_create_key() are serialized externally.
213------------------------------------------------------------------------ */
214
215/* A singly-linked list of struct key objects remembers all the key->value
216 * associations. File static keyhead heads the list. keymutex is used
217 * to enforce exclusion internally.
218 */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000219struct key {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000220 /* Next record in the list, or NULL if this is the last record. */
221 struct key *next;
Tim Petersfda787f2004-10-09 22:33:09 +0000222
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000223 /* The thread id, according to PyThread_get_thread_ident(). */
224 long id;
Tim Petersfda787f2004-10-09 22:33:09 +0000225
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000226 /* The key and its associated value. */
227 int key;
228 void *value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000229};
230
231static struct key *keyhead = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000232static PyThread_type_lock keymutex = NULL;
Tim Petersfda787f2004-10-09 22:33:09 +0000233static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000234
Tim Petersfda787f2004-10-09 22:33:09 +0000235/* Internal helper.
236 * If the current thread has a mapping for key, the appropriate struct key*
237 * is returned. NB: value is ignored in this case!
238 * If there is no mapping for key in the current thread, then:
239 * If value is NULL, NULL is returned.
240 * Else a mapping of key to value is created for the current thread,
241 * and a pointer to a new struct key* is returned; except that if
242 * malloc() can't find room for a new struct key*, NULL is returned.
243 * So when value==NULL, this acts like a pure lookup routine, and when
244 * value!=NULL, this acts like dict.setdefault(), returning an existing
245 * mapping if one exists, else creating a new mapping.
Tim Peters263091e2004-10-10 01:58:44 +0000246 *
247 * Caution: this used to be too clever, trying to hold keymutex only
248 * around the "p->next = keyhead; keyhead = p" pair. That allowed
249 * another thread to mutate the list, via key deletion, concurrent with
250 * find_key() crawling over the list. Hilarity ensued. For example, when
251 * the for-loop here does "p = p->next", p could end up pointing at a
252 * record that PyThread_delete_key_value() was concurrently free()'ing.
253 * That could lead to anything, from failing to find a key that exists, to
254 * segfaults. Now we lock the whole routine.
Tim Petersfda787f2004-10-09 22:33:09 +0000255 */
Tim Peters19717fa2004-10-09 17:38:29 +0000256static struct key *
257find_key(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000258{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000259 struct key *p, *prev_p;
260 long id = PyThread_get_thread_ident();
Tim Petersfda787f2004-10-09 22:33:09 +0000261
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000262 if (!keymutex)
263 return NULL;
264 PyThread_acquire_lock(keymutex, 1);
265 prev_p = NULL;
266 for (p = keyhead; p != NULL; p = p->next) {
267 if (p->id == id && p->key == key)
268 goto Done;
269 /* Sanity check. These states should never happen but if
270 * they do we must abort. Otherwise we'll end up spinning in
271 * in a tight loop with the lock held. A similar check is done
272 * in pystate.c tstate_delete_common(). */
273 if (p == prev_p)
274 Py_FatalError("tls find_key: small circular list(!)");
275 prev_p = p;
276 if (p->next == keyhead)
277 Py_FatalError("tls find_key: circular list(!)");
278 }
279 if (value == NULL) {
280 assert(p == NULL);
281 goto Done;
282 }
283 p = (struct key *)malloc(sizeof(struct key));
284 if (p != NULL) {
285 p->id = id;
286 p->key = key;
287 p->value = value;
288 p->next = keyhead;
289 keyhead = p;
290 }
Tim Peters263091e2004-10-10 01:58:44 +0000291 Done:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000292 PyThread_release_lock(keymutex);
293 return p;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000294}
295
Tim Petersfda787f2004-10-09 22:33:09 +0000296/* Return a new key. This must be called before any other functions in
297 * this family, and callers must arrange to serialize calls to this
298 * function. No violations are detected.
299 */
Tim Peters19717fa2004-10-09 17:38:29 +0000300int
301PyThread_create_key(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000302{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000303 /* All parts of this function are wrong if it's called by multiple
304 * threads simultaneously.
305 */
306 if (keymutex == NULL)
307 keymutex = PyThread_allocate_lock();
308 return ++nkeys;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000309}
310
Tim Petersfda787f2004-10-09 22:33:09 +0000311/* Forget the associations for key across *all* threads. */
Tim Peters19717fa2004-10-09 17:38:29 +0000312void
313PyThread_delete_key(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000314{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000315 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000316
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000317 PyThread_acquire_lock(keymutex, 1);
318 q = &keyhead;
319 while ((p = *q) != NULL) {
320 if (p->key == key) {
321 *q = p->next;
322 free((void *)p);
323 /* NB This does *not* free p->value! */
324 }
325 else
326 q = &p->next;
327 }
328 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000329}
330
Tim Petersfda787f2004-10-09 22:33:09 +0000331/* Confusing: If the current thread has an association for key,
332 * value is ignored, and 0 is returned. Else an attempt is made to create
333 * an association of key to value for the current thread. 0 is returned
334 * if that succeeds, but -1 is returned if there's not enough memory
335 * to create the association. value must not be NULL.
336 */
Tim Peters19717fa2004-10-09 17:38:29 +0000337int
338PyThread_set_key_value(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000339{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000340 struct key *p;
Tim Petersfda787f2004-10-09 22:33:09 +0000341
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000342 assert(value != NULL);
343 p = find_key(key, value);
344 if (p == NULL)
345 return -1;
346 else
347 return 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000348}
349
Tim Petersfda787f2004-10-09 22:33:09 +0000350/* Retrieve the value associated with key in the current thread, or NULL
351 * if the current thread doesn't have an association for key.
352 */
Tim Peters19717fa2004-10-09 17:38:29 +0000353void *
354PyThread_get_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000355{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000356 struct key *p = find_key(key, NULL);
Tim Petersfda787f2004-10-09 22:33:09 +0000357
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000358 if (p == NULL)
359 return NULL;
360 else
361 return p->value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000362}
363
Tim Petersfda787f2004-10-09 22:33:09 +0000364/* Forget the current thread's association for key, if any. */
Tim Peters19717fa2004-10-09 17:38:29 +0000365void
366PyThread_delete_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000367{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000368 long id = PyThread_get_thread_ident();
369 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000370
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000371 PyThread_acquire_lock(keymutex, 1);
372 q = &keyhead;
373 while ((p = *q) != NULL) {
374 if (p->key == key && p->id == id) {
375 *q = p->next;
376 free((void *)p);
377 /* NB This does *not* free p->value! */
378 break;
379 }
380 else
381 q = &p->next;
382 }
383 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000384}
385
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000386/* Forget everything not associated with the current thread id.
387 * This function is called from PyOS_AfterFork(). It is necessary
388 * because other thread ids which were in use at the time of the fork
389 * may be reused for new threads created in the forked process.
390 */
391void
392PyThread_ReInitTLS(void)
393{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000394 long id = PyThread_get_thread_ident();
395 struct key *p, **q;
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000396
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000397 if (!keymutex)
398 return;
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000399
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000400 /* As with interpreter_lock in PyEval_ReInitThreads()
401 we just create a new lock without freeing the old one */
402 keymutex = PyThread_allocate_lock();
403
404 /* Delete all keys which do not match the current thread id */
405 q = &keyhead;
406 while ((p = *q) != NULL) {
407 if (p->id != id) {
408 *q = p->next;
409 free((void *)p);
410 /* NB This does *not* free p->value! */
411 }
412 else
413 q = &p->next;
414 }
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000415}
416
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000417#endif /* Py_HAVE_NATIVE_TLS */