blob: db5ef33d40149c437e7b32fe6cbd4cc6c624874d [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
Andrew MacIntyre63f0db62006-06-04 12:59:59 +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
Sjoerd Mullender66bca321993-12-03 16:54:45 +000082 char *p = getenv("THREADDEBUG");
83
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
Andrew MacIntyre6539d2d2006-06-04 12:31:09 +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
103size_t
104PyThread_get_stacksize(void)
105{
106 return _pythread_stacksize;
107}
108
109static int
110_pythread_unsupported_set_stacksize(size_t size)
111{
112 return PyErr_Warn(PyExc_RuntimeWarning,
113 "setting thread stack size not supported on "
114 "this platform");
115}
116
117/* Only platforms with THREAD_SET_STACKSIZE() defined in
118 pthread_<platform>.h, overriding this default definition,
119 will support changing the stack size.
120 Return 1 if an exception is pending, 0 otherwise. */
121#define THREAD_SET_STACKSIZE(x) _pythread_unsupported_set_stacksize(x)
122
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000123#ifdef SGI_THREADS
124#include "thread_sgi.h"
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000125#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000126
127#ifdef SOLARIS_THREADS
128#include "thread_solaris.h"
129#endif
130
131#ifdef SUN_LWP
132#include "thread_lwp.h"
133#endif
134
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000135#ifdef HAVE_PTH
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000136#include "thread_pth.h"
Martin v. Löwis70849f82003-09-20 11:13:36 +0000137#undef _POSIX_THREADS
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000138#endif
139
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000140#ifdef _POSIX_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000141#include "thread_pthread.h"
Sjoerd Mullendere8934121993-01-13 12:08:48 +0000142#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000143
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000144#ifdef C_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000145#include "thread_cthread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000146#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000147
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000148#ifdef NT_THREADS
149#include "thread_nt.h"
150#endif
151
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000152#ifdef OS2_THREADS
153#include "thread_os2.h"
154#endif
155
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000156#ifdef BEOS_THREADS
157#include "thread_beos.h"
158#endif
159
Guido van Rossum2571cc81999-04-07 16:07:23 +0000160#ifdef WINCE_THREADS
161#include "thread_wince.h"
162#endif
163
Martin v. Löwis7d1cd692002-03-09 12:10:54 +0000164#ifdef PLAN9_THREADS
165#include "thread_plan9.h"
166#endif
167
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000168#ifdef ATHEOS_THREADS
169#include "thread_atheos.h"
170#endif
171
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000172/*
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000173#ifdef FOOBAR_THREADS
174#include "thread_foobar.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000175#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000176*/
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000177
Andrew MacIntyre6539d2d2006-06-04 12:31:09 +0000178/* use appropriate thread stack size setting routine.
179 Return 1 if an exception is pending, 0 otherwise. */
180int
181PyThread_set_stacksize(size_t size)
182{
183 return THREAD_SET_STACKSIZE(size);
184}
185
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000186#ifndef Py_HAVE_NATIVE_TLS
187/* If the platform has not supplied a platform specific
188 TLS implementation, provide our own.
189
190 This code stolen from "thread_sgi.h", where it was the only
191 implementation of an existing Python TLS API.
192*/
Tim Petersfda787f2004-10-09 22:33:09 +0000193/* ------------------------------------------------------------------------
194Per-thread data ("key") support.
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000195
Tim Petersfda787f2004-10-09 22:33:09 +0000196Use PyThread_create_key() to create a new key. This is typically shared
197across threads.
198
199Use PyThread_set_key_value(thekey, value) to associate void* value with
200thekey in the current thread. Each thread has a distinct mapping of thekey
201to a void* value. Caution: if the current thread already has a mapping
202for thekey, value is ignored.
203
204Use PyThread_get_key_value(thekey) to retrieve the void* value associated
205with thekey in the current thread. This returns NULL if no value is
206associated with thekey in the current thread.
207
208Use PyThread_delete_key_value(thekey) to forget the current thread's associated
209value for thekey. PyThread_delete_key(thekey) forgets the values associated
210with thekey across *all* threads.
211
212While some of these functions have error-return values, none set any
213Python exception.
214
215None of the functions does memory management on behalf of the void* values.
216You need to allocate and deallocate them yourself. If the void* values
217happen to be PyObject*, these functions don't do refcount operations on
218them either.
219
220The GIL does not need to be held when calling these functions; they supply
221their own locking. This isn't true of PyThread_create_key(), though (see
222next paragraph).
223
224There's a hidden assumption that PyThread_create_key() will be called before
225any of the other functions are called. There's also a hidden assumption
226that calls to PyThread_create_key() are serialized externally.
227------------------------------------------------------------------------ */
228
229/* A singly-linked list of struct key objects remembers all the key->value
230 * associations. File static keyhead heads the list. keymutex is used
231 * to enforce exclusion internally.
232 */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000233struct key {
Tim Petersfda787f2004-10-09 22:33:09 +0000234 /* Next record in the list, or NULL if this is the last record. */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000235 struct key *next;
Tim Petersfda787f2004-10-09 22:33:09 +0000236
237 /* The thread id, according to PyThread_get_thread_ident(). */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000238 long id;
Tim Petersfda787f2004-10-09 22:33:09 +0000239
240 /* The key and its associated value. */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000241 int key;
242 void *value;
243};
244
245static struct key *keyhead = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000246static PyThread_type_lock keymutex = NULL;
Tim Petersfda787f2004-10-09 22:33:09 +0000247static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000248
Tim Petersfda787f2004-10-09 22:33:09 +0000249/* Internal helper.
250 * If the current thread has a mapping for key, the appropriate struct key*
251 * is returned. NB: value is ignored in this case!
252 * If there is no mapping for key in the current thread, then:
253 * If value is NULL, NULL is returned.
254 * Else a mapping of key to value is created for the current thread,
255 * and a pointer to a new struct key* is returned; except that if
256 * malloc() can't find room for a new struct key*, NULL is returned.
257 * So when value==NULL, this acts like a pure lookup routine, and when
258 * value!=NULL, this acts like dict.setdefault(), returning an existing
259 * mapping if one exists, else creating a new mapping.
Tim Peters263091e2004-10-10 01:58:44 +0000260 *
261 * Caution: this used to be too clever, trying to hold keymutex only
262 * around the "p->next = keyhead; keyhead = p" pair. That allowed
263 * another thread to mutate the list, via key deletion, concurrent with
264 * find_key() crawling over the list. Hilarity ensued. For example, when
265 * the for-loop here does "p = p->next", p could end up pointing at a
266 * record that PyThread_delete_key_value() was concurrently free()'ing.
267 * That could lead to anything, from failing to find a key that exists, to
268 * segfaults. Now we lock the whole routine.
Tim Petersfda787f2004-10-09 22:33:09 +0000269 */
Tim Peters19717fa2004-10-09 17:38:29 +0000270static struct key *
271find_key(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000272{
273 struct key *p;
274 long id = PyThread_get_thread_ident();
Tim Petersfda787f2004-10-09 22:33:09 +0000275
Tim Peters263091e2004-10-10 01:58:44 +0000276 PyThread_acquire_lock(keymutex, 1);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000277 for (p = keyhead; p != NULL; p = p->next) {
278 if (p->id == id && p->key == key)
Tim Peters263091e2004-10-10 01:58:44 +0000279 goto Done;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000280 }
Tim Peters263091e2004-10-10 01:58:44 +0000281 if (value == NULL) {
282 assert(p == NULL);
283 goto Done;
284 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000285 p = (struct key *)malloc(sizeof(struct key));
286 if (p != NULL) {
287 p->id = id;
288 p->key = key;
289 p->value = value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000290 p->next = keyhead;
291 keyhead = p;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000292 }
Tim Peters263091e2004-10-10 01:58:44 +0000293 Done:
294 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000295 return p;
296}
297
Tim Petersfda787f2004-10-09 22:33:09 +0000298/* Return a new key. This must be called before any other functions in
299 * this family, and callers must arrange to serialize calls to this
300 * function. No violations are detected.
301 */
Tim Peters19717fa2004-10-09 17:38:29 +0000302int
303PyThread_create_key(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000304{
Tim Petersfda787f2004-10-09 22:33:09 +0000305 /* All parts of this function are wrong if it's called by multiple
306 * threads simultaneously.
307 */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000308 if (keymutex == NULL)
309 keymutex = PyThread_allocate_lock();
310 return ++nkeys;
311}
312
Tim Petersfda787f2004-10-09 22:33:09 +0000313/* Forget the associations for key across *all* threads. */
Tim Peters19717fa2004-10-09 17:38:29 +0000314void
315PyThread_delete_key(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000316{
317 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000318
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000319 PyThread_acquire_lock(keymutex, 1);
320 q = &keyhead;
321 while ((p = *q) != NULL) {
322 if (p->key == key) {
323 *q = p->next;
324 free((void *)p);
325 /* NB This does *not* free p->value! */
326 }
327 else
328 q = &p->next;
329 }
330 PyThread_release_lock(keymutex);
331}
332
Tim Petersfda787f2004-10-09 22:33:09 +0000333/* Confusing: If the current thread has an association for key,
334 * value is ignored, and 0 is returned. Else an attempt is made to create
335 * an association of key to value for the current thread. 0 is returned
336 * if that succeeds, but -1 is returned if there's not enough memory
337 * to create the association. value must not be NULL.
338 */
Tim Peters19717fa2004-10-09 17:38:29 +0000339int
340PyThread_set_key_value(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000341{
Tim Petersfda787f2004-10-09 22:33:09 +0000342 struct key *p;
343
344 assert(value != NULL);
345 p = find_key(key, value);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000346 if (p == NULL)
347 return -1;
348 else
349 return 0;
350}
351
Tim Petersfda787f2004-10-09 22:33:09 +0000352/* Retrieve the value associated with key in the current thread, or NULL
353 * if the current thread doesn't have an association for key.
354 */
Tim Peters19717fa2004-10-09 17:38:29 +0000355void *
356PyThread_get_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000357{
358 struct key *p = find_key(key, NULL);
Tim Petersfda787f2004-10-09 22:33:09 +0000359
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000360 if (p == NULL)
361 return NULL;
362 else
363 return p->value;
364}
365
Tim Petersfda787f2004-10-09 22:33:09 +0000366/* Forget the current thread's association for key, if any. */
Tim Peters19717fa2004-10-09 17:38:29 +0000367void
368PyThread_delete_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000369{
370 long id = PyThread_get_thread_ident();
371 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000372
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000373 PyThread_acquire_lock(keymutex, 1);
374 q = &keyhead;
375 while ((p = *q) != NULL) {
376 if (p->key == key && p->id == id) {
377 *q = p->next;
378 free((void *)p);
379 /* NB This does *not* free p->value! */
380 break;
381 }
382 else
383 q = &p->next;
384 }
385 PyThread_release_lock(keymutex);
386}
387
388#endif /* Py_HAVE_NATIVE_TLS */