blob: f5722a7f13229ff307ebfd8d83890c6c3636a7a6 [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
49 enough of the Posix threads package is implimented to support python
50 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;
Jeremy Hyltonbd232892002-06-25 19:26:34 +000067#define dprintf(args) (void)((thread_debug & 1) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000068#define d2printf(args) ((thread_debug & 8) && printf args)
69#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
Christian Heimes790c8232008-01-07 21:14:23 +000082 char *p = Py_GETENV("PYTHONTHREADDEBUG");
Sjoerd Mullender66bca321993-12-03 16:54:45 +000083
84 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 */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000091 if (initialized)
92 return;
93 initialized = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +000094 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
Antoine Pitrou1b8c7832009-10-24 20:30:34 +0000112#error SunOS lightweight processes are now unsupported, and code will be removed in 3.3.
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000113#include "thread_lwp.h"
114#endif
115
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000116#ifdef HAVE_PTH
Antoine Pitroudb6c5672009-10-24 20:35:52 +0000117#error GNU pth threads are now unsupported, and code will be removed in 3.3.
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000118#include "thread_pth.h"
Martin v. Löwis70849f82003-09-20 11:13:36 +0000119#undef _POSIX_THREADS
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000120#endif
121
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000122#ifdef _POSIX_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000123#include "thread_pthread.h"
Sjoerd Mullendere8934121993-01-13 12:08:48 +0000124#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000125
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000126#ifdef C_THREADS
Antoine Pitrou86b21c12009-10-24 20:24:16 +0000127#error Mach C Threads are now unsupported, and code will be removed in 3.3.
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
Martin v. Löwis7d1cd692002-03-09 12:10:54 +0000139#ifdef PLAN9_THREADS
140#include "thread_plan9.h"
141#endif
142
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000143/*
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000144#ifdef FOOBAR_THREADS
145#include "thread_foobar.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000146#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000147*/
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000148
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000149/* return the current thread stack size */
150size_t
151PyThread_get_stacksize(void)
152{
153 return _pythread_stacksize;
154}
155
156/* Only platforms defining a THREAD_SET_STACKSIZE() macro
157 in thread_<platform>.h support changing the stack size.
158 Return 0 if stack size is valid,
159 -1 if stack size value is invalid,
160 -2 if setting stack size is not supported. */
161int
162PyThread_set_stacksize(size_t size)
163{
164#if defined(THREAD_SET_STACKSIZE)
165 return THREAD_SET_STACKSIZE(size);
166#else
167 return -2;
168#endif
169}
170
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000171#ifndef Py_HAVE_NATIVE_TLS
172/* If the platform has not supplied a platform specific
173 TLS implementation, provide our own.
174
175 This code stolen from "thread_sgi.h", where it was the only
176 implementation of an existing Python TLS API.
177*/
Tim Petersfda787f2004-10-09 22:33:09 +0000178/* ------------------------------------------------------------------------
179Per-thread data ("key") support.
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000180
Tim Petersfda787f2004-10-09 22:33:09 +0000181Use PyThread_create_key() to create a new key. This is typically shared
182across threads.
183
184Use PyThread_set_key_value(thekey, value) to associate void* value with
185thekey in the current thread. Each thread has a distinct mapping of thekey
186to a void* value. Caution: if the current thread already has a mapping
187for thekey, value is ignored.
188
189Use PyThread_get_key_value(thekey) to retrieve the void* value associated
190with thekey in the current thread. This returns NULL if no value is
191associated with thekey in the current thread.
192
193Use PyThread_delete_key_value(thekey) to forget the current thread's associated
194value for thekey. PyThread_delete_key(thekey) forgets the values associated
195with thekey across *all* threads.
196
197While some of these functions have error-return values, none set any
198Python exception.
199
200None of the functions does memory management on behalf of the void* values.
201You need to allocate and deallocate them yourself. If the void* values
202happen to be PyObject*, these functions don't do refcount operations on
203them either.
204
205The GIL does not need to be held when calling these functions; they supply
206their own locking. This isn't true of PyThread_create_key(), though (see
207next paragraph).
208
209There's a hidden assumption that PyThread_create_key() will be called before
210any of the other functions are called. There's also a hidden assumption
211that calls to PyThread_create_key() are serialized externally.
212------------------------------------------------------------------------ */
213
214/* A singly-linked list of struct key objects remembers all the key->value
215 * associations. File static keyhead heads the list. keymutex is used
216 * to enforce exclusion internally.
217 */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000218struct key {
Tim Petersfda787f2004-10-09 22:33:09 +0000219 /* Next record in the list, or NULL if this is the last record. */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000220 struct key *next;
Tim Petersfda787f2004-10-09 22:33:09 +0000221
222 /* The thread id, according to PyThread_get_thread_ident(). */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000223 long id;
Tim Petersfda787f2004-10-09 22:33:09 +0000224
225 /* The key and its associated value. */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000226 int key;
227 void *value;
228};
229
230static struct key *keyhead = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000231static PyThread_type_lock keymutex = NULL;
Tim Petersfda787f2004-10-09 22:33:09 +0000232static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000233
Tim Petersfda787f2004-10-09 22:33:09 +0000234/* Internal helper.
235 * If the current thread has a mapping for key, the appropriate struct key*
236 * is returned. NB: value is ignored in this case!
237 * If there is no mapping for key in the current thread, then:
238 * If value is NULL, NULL is returned.
239 * Else a mapping of key to value is created for the current thread,
240 * and a pointer to a new struct key* is returned; except that if
241 * malloc() can't find room for a new struct key*, NULL is returned.
242 * So when value==NULL, this acts like a pure lookup routine, and when
243 * value!=NULL, this acts like dict.setdefault(), returning an existing
244 * mapping if one exists, else creating a new mapping.
Tim Peters263091e2004-10-10 01:58:44 +0000245 *
246 * Caution: this used to be too clever, trying to hold keymutex only
247 * around the "p->next = keyhead; keyhead = p" pair. That allowed
248 * another thread to mutate the list, via key deletion, concurrent with
249 * find_key() crawling over the list. Hilarity ensued. For example, when
250 * the for-loop here does "p = p->next", p could end up pointing at a
251 * record that PyThread_delete_key_value() was concurrently free()'ing.
252 * That could lead to anything, from failing to find a key that exists, to
253 * segfaults. Now we lock the whole routine.
Tim Petersfda787f2004-10-09 22:33:09 +0000254 */
Tim Peters19717fa2004-10-09 17:38:29 +0000255static struct key *
256find_key(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000257{
Georg Brandl6aa2d1f2008-08-12 08:35:52 +0000258 struct key *p, *prev_p;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000259 long id = PyThread_get_thread_ident();
Tim Petersfda787f2004-10-09 22:33:09 +0000260
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000261 if (!keymutex)
262 return NULL;
Tim Peters263091e2004-10-10 01:58:44 +0000263 PyThread_acquire_lock(keymutex, 1);
Georg Brandl6aa2d1f2008-08-12 08:35:52 +0000264 prev_p = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000265 for (p = keyhead; p != NULL; p = p->next) {
266 if (p->id == id && p->key == key)
Tim Peters263091e2004-10-10 01:58:44 +0000267 goto Done;
Georg Brandl6aa2d1f2008-08-12 08:35:52 +0000268 /* Sanity check. These states should never happen but if
269 * they do we must abort. Otherwise we'll end up spinning in
270 * in a tight loop with the lock held. A similar check is done
271 * in pystate.c tstate_delete_common(). */
272 if (p == prev_p)
273 Py_FatalError("tls find_key: small circular list(!)");
274 prev_p = p;
275 if (p->next == keyhead)
276 Py_FatalError("tls find_key: circular list(!)");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000277 }
Tim Peters263091e2004-10-10 01:58:44 +0000278 if (value == NULL) {
279 assert(p == NULL);
280 goto Done;
281 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000282 p = (struct key *)malloc(sizeof(struct key));
283 if (p != NULL) {
284 p->id = id;
285 p->key = key;
286 p->value = value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000287 p->next = keyhead;
288 keyhead = p;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000289 }
Tim Peters263091e2004-10-10 01:58:44 +0000290 Done:
291 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000292 return p;
293}
294
Tim Petersfda787f2004-10-09 22:33:09 +0000295/* Return a new key. This must be called before any other functions in
296 * this family, and callers must arrange to serialize calls to this
297 * function. No violations are detected.
298 */
Tim Peters19717fa2004-10-09 17:38:29 +0000299int
300PyThread_create_key(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000301{
Tim Petersfda787f2004-10-09 22:33:09 +0000302 /* All parts of this function are wrong if it's called by multiple
303 * threads simultaneously.
304 */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000305 if (keymutex == NULL)
306 keymutex = PyThread_allocate_lock();
307 return ++nkeys;
308}
309
Tim Petersfda787f2004-10-09 22:33:09 +0000310/* Forget the associations for key across *all* threads. */
Tim Peters19717fa2004-10-09 17:38:29 +0000311void
312PyThread_delete_key(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000313{
314 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000315
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000316 PyThread_acquire_lock(keymutex, 1);
317 q = &keyhead;
318 while ((p = *q) != NULL) {
319 if (p->key == key) {
320 *q = p->next;
321 free((void *)p);
322 /* NB This does *not* free p->value! */
323 }
324 else
325 q = &p->next;
326 }
327 PyThread_release_lock(keymutex);
328}
329
Tim Petersfda787f2004-10-09 22:33:09 +0000330/* Confusing: If the current thread has an association for key,
331 * value is ignored, and 0 is returned. Else an attempt is made to create
332 * an association of key to value for the current thread. 0 is returned
333 * if that succeeds, but -1 is returned if there's not enough memory
334 * to create the association. value must not be NULL.
335 */
Tim Peters19717fa2004-10-09 17:38:29 +0000336int
337PyThread_set_key_value(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000338{
Tim Petersfda787f2004-10-09 22:33:09 +0000339 struct key *p;
340
341 assert(value != NULL);
342 p = find_key(key, value);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000343 if (p == NULL)
344 return -1;
345 else
346 return 0;
347}
348
Tim Petersfda787f2004-10-09 22:33:09 +0000349/* Retrieve the value associated with key in the current thread, or NULL
350 * if the current thread doesn't have an association for key.
351 */
Tim Peters19717fa2004-10-09 17:38:29 +0000352void *
353PyThread_get_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000354{
355 struct key *p = find_key(key, NULL);
Tim Petersfda787f2004-10-09 22:33:09 +0000356
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000357 if (p == NULL)
358 return NULL;
359 else
360 return p->value;
361}
362
Tim Petersfda787f2004-10-09 22:33:09 +0000363/* Forget the current thread's association for key, if any. */
Tim Peters19717fa2004-10-09 17:38:29 +0000364void
365PyThread_delete_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000366{
367 long id = PyThread_get_thread_ident();
368 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000369
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000370 PyThread_acquire_lock(keymutex, 1);
371 q = &keyhead;
372 while ((p = *q) != NULL) {
373 if (p->key == key && p->id == id) {
374 *q = p->next;
375 free((void *)p);
376 /* NB This does *not* free p->value! */
377 break;
378 }
379 else
380 q = &p->next;
381 }
382 PyThread_release_lock(keymutex);
383}
384
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000385/* Forget everything not associated with the current thread id.
386 * This function is called from PyOS_AfterFork(). It is necessary
387 * because other thread ids which were in use at the time of the fork
388 * may be reused for new threads created in the forked process.
389 */
390void
391PyThread_ReInitTLS(void)
392{
393 long id = PyThread_get_thread_ident();
394 struct key *p, **q;
395
396 if (!keymutex)
397 return;
398
399 /* As with interpreter_lock in PyEval_ReInitThreads()
400 we just create a new lock without freeing the old one */
401 keymutex = PyThread_allocate_lock();
402
403 /* Delete all keys which do not match the current thread id */
404 q = &keyhead;
405 while ((p = *q) != NULL) {
406 if (p->id != id) {
407 *q = p->next;
408 free((void *)p);
409 /* NB This does *not* free p->value! */
410 }
411 else
412 q = &p->next;
413 }
414}
415
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000416#endif /* Py_HAVE_NATIVE_TLS */