blob: d224046e6471d1ab58f12b24626b2171726e8537 [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 Rossum49b56061998-10-01 20:42:43 +000026#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000027
Guido van Rossum1d5735e1994-08-30 08:27:36 +000028#ifndef _POSIX_THREADS
29
Guido van Rossum1984f1e1992-08-04 12:41:02 +000030#ifdef __sgi
Guido van Rossum1d5735e1994-08-30 08:27:36 +000031#define SGI_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000032#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +000033
Guido van Rossum1d5735e1994-08-30 08:27:36 +000034#ifdef HAVE_THREAD_H
35#define SOLARIS_THREADS
36#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +000037
Guido van Rossum1d5735e1994-08-30 08:27:36 +000038#if defined(sun) && !defined(SOLARIS_THREADS)
39#define SUN_LWP
40#endif
41
Guido van Rossum539c6622005-09-14 17:49:54 +000042/* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then
Ezio Melotti13925002011-03-16 11:05:33 +020043 enough of the Posix threads package is implemented to support python
Guido van Rossum539c6622005-09-14 17:49:54 +000044 threads.
45
46 This is valid for HP-UX 11.23 running on an ia64 system. If needed, add
47 a check of __ia64 to verify that we're running on a ia64 system instead
48 of a pa-risc system.
49*/
50#ifdef __hpux
51#ifdef _SC_THREADS
52#define _POSIX_THREADS
53#endif
54#endif
55
Sjoerd Mullender66bca321993-12-03 16:54:45 +000056#endif /* _POSIX_THREADS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000057
Guido van Rossum1984f1e1992-08-04 12:41:02 +000058
Guido van Rossum408027e1996-12-30 16:17:54 +000059#ifdef Py_DEBUG
Guido van Rossum1d5735e1994-08-30 08:27:36 +000060static int thread_debug = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061#define dprintf(args) (void)((thread_debug & 1) && printf args)
62#define d2printf(args) ((thread_debug & 8) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000063#else
64#define dprintf(args)
65#define d2printf(args)
66#endif
67
Guido van Rossum1984f1e1992-08-04 12:41:02 +000068static int initialized;
69
Thomas Wouters8ec68fd2000-07-24 14:39:50 +000070static void PyThread__init_thread(void); /* Forward */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000071
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000072void
73PyThread_init_thread(void)
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000074{
Guido van Rossum408027e1996-12-30 16:17:54 +000075#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 char *p = Py_GETENV("PYTHONTHREADDEBUG");
Sjoerd Mullender66bca321993-12-03 16:54:45 +000077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (p) {
79 if (*p)
80 thread_debug = atoi(p);
81 else
82 thread_debug = 1;
83 }
Guido van Rossum408027e1996-12-30 16:17:54 +000084#endif /* Py_DEBUG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 if (initialized)
86 return;
87 initialized = 1;
88 dprintf(("PyThread_init_thread called\n"));
89 PyThread__init_thread();
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000090}
91
Thomas Wouters0e3f5912006-08-11 14:57:12 +000092/* Support for runtime thread stack size tuning.
93 A value of 0 means using the platform's default stack size
94 or the size specified by the THREAD_STACK_SIZE macro. */
95static size_t _pythread_stacksize = 0;
96
Guido van Rossum1d5735e1994-08-30 08:27:36 +000097#ifdef SGI_THREADS
Antoine Pitrou2a9c2bb2009-10-24 20:43:49 +000098#error SGI Irix threads are now unsupported, and code will be removed in 3.3.
Guido van Rossum1d5735e1994-08-30 08:27:36 +000099#include "thread_sgi.h"
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000100#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000101
102#ifdef SOLARIS_THREADS
103#include "thread_solaris.h"
104#endif
105
106#ifdef SUN_LWP
Antoine Pitrou1b8c7832009-10-24 20:30:34 +0000107#error SunOS lightweight processes are now unsupported, and code will be removed in 3.3.
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000108#include "thread_lwp.h"
109#endif
110
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000111#ifdef HAVE_PTH
Antoine Pitroudb6c5672009-10-24 20:35:52 +0000112#error GNU pth threads are now unsupported, and code will be removed in 3.3.
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000113#include "thread_pth.h"
Martin v. Löwis70849f82003-09-20 11:13:36 +0000114#undef _POSIX_THREADS
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000115#endif
116
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000117#ifdef _POSIX_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000118#include "thread_pthread.h"
Sjoerd Mullendere8934121993-01-13 12:08:48 +0000119#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000120
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000121#ifdef C_THREADS
Antoine Pitrou86b21c12009-10-24 20:24:16 +0000122#error Mach C Threads are now unsupported, and code will be removed in 3.3.
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000123#include "thread_cthread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000124#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000125
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000126#ifdef NT_THREADS
127#include "thread_nt.h"
128#endif
129
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000130#ifdef OS2_THREADS
131#include "thread_os2.h"
132#endif
133
Martin v. Löwis7d1cd692002-03-09 12:10:54 +0000134#ifdef PLAN9_THREADS
135#include "thread_plan9.h"
136#endif
137
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000138/*
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000139#ifdef FOOBAR_THREADS
140#include "thread_foobar.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000141#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000142*/
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000143
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000144/* return the current thread stack size */
145size_t
146PyThread_get_stacksize(void)
147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 return _pythread_stacksize;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000149}
150
151/* Only platforms defining a THREAD_SET_STACKSIZE() macro
152 in thread_<platform>.h support changing the stack size.
153 Return 0 if stack size is valid,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 -1 if stack size value is invalid,
155 -2 if setting stack size is not supported. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000156int
157PyThread_set_stacksize(size_t size)
158{
159#if defined(THREAD_SET_STACKSIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 return THREAD_SET_STACKSIZE(size);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000161#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000163#endif
164}
165
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000166#ifndef Py_HAVE_NATIVE_TLS
167/* If the platform has not supplied a platform specific
168 TLS implementation, provide our own.
169
170 This code stolen from "thread_sgi.h", where it was the only
171 implementation of an existing Python TLS API.
172*/
Tim Petersfda787f2004-10-09 22:33:09 +0000173/* ------------------------------------------------------------------------
174Per-thread data ("key") support.
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000175
Tim Petersfda787f2004-10-09 22:33:09 +0000176Use PyThread_create_key() to create a new key. This is typically shared
177across threads.
178
179Use PyThread_set_key_value(thekey, value) to associate void* value with
180thekey in the current thread. Each thread has a distinct mapping of thekey
181to a void* value. Caution: if the current thread already has a mapping
182for thekey, value is ignored.
183
184Use PyThread_get_key_value(thekey) to retrieve the void* value associated
185with thekey in the current thread. This returns NULL if no value is
186associated with thekey in the current thread.
187
188Use PyThread_delete_key_value(thekey) to forget the current thread's associated
189value for thekey. PyThread_delete_key(thekey) forgets the values associated
190with thekey across *all* threads.
191
192While some of these functions have error-return values, none set any
193Python exception.
194
195None of the functions does memory management on behalf of the void* values.
196You need to allocate and deallocate them yourself. If the void* values
197happen to be PyObject*, these functions don't do refcount operations on
198them either.
199
200The GIL does not need to be held when calling these functions; they supply
201their own locking. This isn't true of PyThread_create_key(), though (see
202next paragraph).
203
204There's a hidden assumption that PyThread_create_key() will be called before
205any of the other functions are called. There's also a hidden assumption
206that calls to PyThread_create_key() are serialized externally.
207------------------------------------------------------------------------ */
208
209/* A singly-linked list of struct key objects remembers all the key->value
210 * associations. File static keyhead heads the list. keymutex is used
211 * to enforce exclusion internally.
212 */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000213struct key {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 /* Next record in the list, or NULL if this is the last record. */
215 struct key *next;
Tim Petersfda787f2004-10-09 22:33:09 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 /* The thread id, according to PyThread_get_thread_ident(). */
218 long id;
Tim Petersfda787f2004-10-09 22:33:09 +0000219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 /* The key and its associated value. */
221 int key;
222 void *value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000223};
224
225static struct key *keyhead = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000226static PyThread_type_lock keymutex = NULL;
Tim Petersfda787f2004-10-09 22:33:09 +0000227static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000228
Tim Petersfda787f2004-10-09 22:33:09 +0000229/* Internal helper.
230 * If the current thread has a mapping for key, the appropriate struct key*
231 * is returned. NB: value is ignored in this case!
232 * If there is no mapping for key in the current thread, then:
233 * If value is NULL, NULL is returned.
234 * Else a mapping of key to value is created for the current thread,
235 * and a pointer to a new struct key* is returned; except that if
236 * malloc() can't find room for a new struct key*, NULL is returned.
237 * So when value==NULL, this acts like a pure lookup routine, and when
238 * value!=NULL, this acts like dict.setdefault(), returning an existing
239 * mapping if one exists, else creating a new mapping.
Tim Peters263091e2004-10-10 01:58:44 +0000240 *
241 * Caution: this used to be too clever, trying to hold keymutex only
242 * around the "p->next = keyhead; keyhead = p" pair. That allowed
243 * another thread to mutate the list, via key deletion, concurrent with
244 * find_key() crawling over the list. Hilarity ensued. For example, when
245 * the for-loop here does "p = p->next", p could end up pointing at a
246 * record that PyThread_delete_key_value() was concurrently free()'ing.
247 * That could lead to anything, from failing to find a key that exists, to
248 * segfaults. Now we lock the whole routine.
Tim Petersfda787f2004-10-09 22:33:09 +0000249 */
Tim Peters19717fa2004-10-09 17:38:29 +0000250static struct key *
251find_key(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 struct key *p, *prev_p;
254 long id = PyThread_get_thread_ident();
Tim Petersfda787f2004-10-09 22:33:09 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 if (!keymutex)
257 return NULL;
258 PyThread_acquire_lock(keymutex, 1);
259 prev_p = NULL;
260 for (p = keyhead; p != NULL; p = p->next) {
261 if (p->id == id && p->key == key)
262 goto Done;
263 /* Sanity check. These states should never happen but if
264 * they do we must abort. Otherwise we'll end up spinning in
265 * in a tight loop with the lock held. A similar check is done
266 * in pystate.c tstate_delete_common(). */
267 if (p == prev_p)
268 Py_FatalError("tls find_key: small circular list(!)");
269 prev_p = p;
270 if (p->next == keyhead)
271 Py_FatalError("tls find_key: circular list(!)");
272 }
273 if (value == NULL) {
274 assert(p == NULL);
275 goto Done;
276 }
277 p = (struct key *)malloc(sizeof(struct key));
278 if (p != NULL) {
279 p->id = id;
280 p->key = key;
281 p->value = value;
282 p->next = keyhead;
283 keyhead = p;
284 }
Tim Peters263091e2004-10-10 01:58:44 +0000285 Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 PyThread_release_lock(keymutex);
287 return p;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000288}
289
Tim Petersfda787f2004-10-09 22:33:09 +0000290/* Return a new key. This must be called before any other functions in
291 * this family, and callers must arrange to serialize calls to this
292 * function. No violations are detected.
293 */
Tim Peters19717fa2004-10-09 17:38:29 +0000294int
295PyThread_create_key(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* All parts of this function are wrong if it's called by multiple
298 * threads simultaneously.
299 */
300 if (keymutex == NULL)
301 keymutex = PyThread_allocate_lock();
302 return ++nkeys;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000303}
304
Tim Petersfda787f2004-10-09 22:33:09 +0000305/* Forget the associations for key across *all* threads. */
Tim Peters19717fa2004-10-09 17:38:29 +0000306void
307PyThread_delete_key(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 PyThread_acquire_lock(keymutex, 1);
312 q = &keyhead;
313 while ((p = *q) != NULL) {
314 if (p->key == key) {
315 *q = p->next;
316 free((void *)p);
317 /* NB This does *not* free p->value! */
318 }
319 else
320 q = &p->next;
321 }
322 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000323}
324
Tim Petersfda787f2004-10-09 22:33:09 +0000325/* Confusing: If the current thread has an association for key,
326 * value is ignored, and 0 is returned. Else an attempt is made to create
327 * an association of key to value for the current thread. 0 is returned
328 * if that succeeds, but -1 is returned if there's not enough memory
329 * to create the association. value must not be NULL.
330 */
Tim Peters19717fa2004-10-09 17:38:29 +0000331int
332PyThread_set_key_value(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 struct key *p;
Tim Petersfda787f2004-10-09 22:33:09 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 assert(value != NULL);
337 p = find_key(key, value);
338 if (p == NULL)
339 return -1;
340 else
341 return 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000342}
343
Tim Petersfda787f2004-10-09 22:33:09 +0000344/* Retrieve the value associated with key in the current thread, or NULL
345 * if the current thread doesn't have an association for key.
346 */
Tim Peters19717fa2004-10-09 17:38:29 +0000347void *
348PyThread_get_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 struct key *p = find_key(key, NULL);
Tim Petersfda787f2004-10-09 22:33:09 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (p == NULL)
353 return NULL;
354 else
355 return p->value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000356}
357
Tim Petersfda787f2004-10-09 22:33:09 +0000358/* Forget the current thread's association for key, if any. */
Tim Peters19717fa2004-10-09 17:38:29 +0000359void
360PyThread_delete_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 long id = PyThread_get_thread_ident();
363 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 PyThread_acquire_lock(keymutex, 1);
366 q = &keyhead;
367 while ((p = *q) != NULL) {
368 if (p->key == key && p->id == id) {
369 *q = p->next;
370 free((void *)p);
371 /* NB This does *not* free p->value! */
372 break;
373 }
374 else
375 q = &p->next;
376 }
377 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000378}
379
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000380/* Forget everything not associated with the current thread id.
381 * This function is called from PyOS_AfterFork(). It is necessary
382 * because other thread ids which were in use at the time of the fork
383 * may be reused for new threads created in the forked process.
384 */
385void
386PyThread_ReInitTLS(void)
387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 long id = PyThread_get_thread_ident();
389 struct key *p, **q;
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (!keymutex)
392 return;
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 /* As with interpreter_lock in PyEval_ReInitThreads()
395 we just create a new lock without freeing the old one */
396 keymutex = PyThread_allocate_lock();
397
398 /* Delete all keys which do not match the current thread id */
399 q = &keyhead;
400 while ((p = *q) != NULL) {
401 if (p->id != id) {
402 *q = p->next;
403 free((void *)p);
404 /* NB This does *not* free p->value! */
405 }
406 else
407 q = &p->next;
408 }
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000409}
410
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000411#endif /* Py_HAVE_NATIVE_TLS */