blob: 4c0edfb93674d5e1024173ef007772f2801725c2 [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
Sjoerd Mullender66bca321993-12-03 16:54:45 +000048#endif /* _POSIX_THREADS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000049
Guido van Rossum1984f1e1992-08-04 12:41:02 +000050
Guido van Rossum408027e1996-12-30 16:17:54 +000051#ifdef Py_DEBUG
Guido van Rossum1d5735e1994-08-30 08:27:36 +000052static int thread_debug = 0;
Jeremy Hyltonbd232892002-06-25 19:26:34 +000053#define dprintf(args) (void)((thread_debug & 1) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000054#define d2printf(args) ((thread_debug & 8) && printf args)
55#else
56#define dprintf(args)
57#define d2printf(args)
58#endif
59
Guido van Rossum1984f1e1992-08-04 12:41:02 +000060static int initialized;
61
Thomas Wouters8ec68fd2000-07-24 14:39:50 +000062static void PyThread__init_thread(void); /* Forward */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000063
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000064void PyThread_init_thread(void)
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000065{
Guido van Rossum408027e1996-12-30 16:17:54 +000066#ifdef Py_DEBUG
Sjoerd Mullender66bca321993-12-03 16:54:45 +000067 char *p = getenv("THREADDEBUG");
68
69 if (p) {
70 if (*p)
71 thread_debug = atoi(p);
72 else
73 thread_debug = 1;
74 }
Guido van Rossum408027e1996-12-30 16:17:54 +000075#endif /* Py_DEBUG */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000076 if (initialized)
77 return;
78 initialized = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +000079 dprintf(("PyThread_init_thread called\n"));
80 PyThread__init_thread();
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000081}
82
Guido van Rossum1d5735e1994-08-30 08:27:36 +000083#ifdef SGI_THREADS
84#include "thread_sgi.h"
Sjoerd Mullender66bca321993-12-03 16:54:45 +000085#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +000086
87#ifdef SOLARIS_THREADS
88#include "thread_solaris.h"
89#endif
90
91#ifdef SUN_LWP
92#include "thread_lwp.h"
93#endif
94
Guido van Rossum9e8181b2000-09-19 00:46:46 +000095#ifdef HAVE_PTH
Guido van Rossum07bd90e2000-05-08 13:41:38 +000096#include "thread_pth.h"
Martin v. Löwis70849f82003-09-20 11:13:36 +000097#undef _POSIX_THREADS
Guido van Rossum9e8181b2000-09-19 00:46:46 +000098#endif
99
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000100#ifdef _POSIX_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000101#include "thread_pthread.h"
Sjoerd Mullendere8934121993-01-13 12:08:48 +0000102#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000103
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000104#ifdef C_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000105#include "thread_cthread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000106#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000107
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000108#ifdef NT_THREADS
109#include "thread_nt.h"
110#endif
111
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000112#ifdef OS2_THREADS
113#include "thread_os2.h"
114#endif
115
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000116#ifdef BEOS_THREADS
117#include "thread_beos.h"
118#endif
119
Guido van Rossum2571cc81999-04-07 16:07:23 +0000120#ifdef WINCE_THREADS
121#include "thread_wince.h"
122#endif
123
Martin v. Löwis7d1cd692002-03-09 12:10:54 +0000124#ifdef PLAN9_THREADS
125#include "thread_plan9.h"
126#endif
127
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000128#ifdef ATHEOS_THREADS
129#include "thread_atheos.h"
130#endif
131
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000132/*
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000133#ifdef FOOBAR_THREADS
134#include "thread_foobar.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000135#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000136*/
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000137
138#ifndef Py_HAVE_NATIVE_TLS
139/* If the platform has not supplied a platform specific
140 TLS implementation, provide our own.
141
142 This code stolen from "thread_sgi.h", where it was the only
143 implementation of an existing Python TLS API.
144*/
Tim Petersfda787f2004-10-09 22:33:09 +0000145/* ------------------------------------------------------------------------
146Per-thread data ("key") support.
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000147
Tim Petersfda787f2004-10-09 22:33:09 +0000148Use PyThread_create_key() to create a new key. This is typically shared
149across threads.
150
151Use PyThread_set_key_value(thekey, value) to associate void* value with
152thekey in the current thread. Each thread has a distinct mapping of thekey
153to a void* value. Caution: if the current thread already has a mapping
154for thekey, value is ignored.
155
156Use PyThread_get_key_value(thekey) to retrieve the void* value associated
157with thekey in the current thread. This returns NULL if no value is
158associated with thekey in the current thread.
159
160Use PyThread_delete_key_value(thekey) to forget the current thread's associated
161value for thekey. PyThread_delete_key(thekey) forgets the values associated
162with thekey across *all* threads.
163
164While some of these functions have error-return values, none set any
165Python exception.
166
167None of the functions does memory management on behalf of the void* values.
168You need to allocate and deallocate them yourself. If the void* values
169happen to be PyObject*, these functions don't do refcount operations on
170them either.
171
172The GIL does not need to be held when calling these functions; they supply
173their own locking. This isn't true of PyThread_create_key(), though (see
174next paragraph).
175
176There's a hidden assumption that PyThread_create_key() will be called before
177any of the other functions are called. There's also a hidden assumption
178that calls to PyThread_create_key() are serialized externally.
179------------------------------------------------------------------------ */
180
181/* A singly-linked list of struct key objects remembers all the key->value
182 * associations. File static keyhead heads the list. keymutex is used
183 * to enforce exclusion internally.
184 */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000185struct key {
Tim Petersfda787f2004-10-09 22:33:09 +0000186 /* Next record in the list, or NULL if this is the last record. */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000187 struct key *next;
Tim Petersfda787f2004-10-09 22:33:09 +0000188
189 /* The thread id, according to PyThread_get_thread_ident(). */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000190 long id;
Tim Petersfda787f2004-10-09 22:33:09 +0000191
192 /* The key and its associated value. */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000193 int key;
194 void *value;
195};
196
197static struct key *keyhead = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000198static PyThread_type_lock keymutex = NULL;
Tim Petersfda787f2004-10-09 22:33:09 +0000199static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000200
Tim Petersfda787f2004-10-09 22:33:09 +0000201/* Internal helper.
202 * If the current thread has a mapping for key, the appropriate struct key*
203 * is returned. NB: value is ignored in this case!
204 * If there is no mapping for key in the current thread, then:
205 * If value is NULL, NULL is returned.
206 * Else a mapping of key to value is created for the current thread,
207 * and a pointer to a new struct key* is returned; except that if
208 * malloc() can't find room for a new struct key*, NULL is returned.
209 * So when value==NULL, this acts like a pure lookup routine, and when
210 * value!=NULL, this acts like dict.setdefault(), returning an existing
211 * mapping if one exists, else creating a new mapping.
Tim Peters263091e2004-10-10 01:58:44 +0000212 *
213 * Caution: this used to be too clever, trying to hold keymutex only
214 * around the "p->next = keyhead; keyhead = p" pair. That allowed
215 * another thread to mutate the list, via key deletion, concurrent with
216 * find_key() crawling over the list. Hilarity ensued. For example, when
217 * the for-loop here does "p = p->next", p could end up pointing at a
218 * record that PyThread_delete_key_value() was concurrently free()'ing.
219 * That could lead to anything, from failing to find a key that exists, to
220 * segfaults. Now we lock the whole routine.
Tim Petersfda787f2004-10-09 22:33:09 +0000221 */
Tim Peters19717fa2004-10-09 17:38:29 +0000222static struct key *
223find_key(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000224{
225 struct key *p;
226 long id = PyThread_get_thread_ident();
Tim Petersfda787f2004-10-09 22:33:09 +0000227
Tim Peters263091e2004-10-10 01:58:44 +0000228 PyThread_acquire_lock(keymutex, 1);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000229 for (p = keyhead; p != NULL; p = p->next) {
230 if (p->id == id && p->key == key)
Tim Peters263091e2004-10-10 01:58:44 +0000231 goto Done;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000232 }
Tim Peters263091e2004-10-10 01:58:44 +0000233 if (value == NULL) {
234 assert(p == NULL);
235 goto Done;
236 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000237 p = (struct key *)malloc(sizeof(struct key));
238 if (p != NULL) {
239 p->id = id;
240 p->key = key;
241 p->value = value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000242 p->next = keyhead;
243 keyhead = p;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000244 }
Tim Peters263091e2004-10-10 01:58:44 +0000245 Done:
246 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000247 return p;
248}
249
Tim Petersfda787f2004-10-09 22:33:09 +0000250/* Return a new key. This must be called before any other functions in
251 * this family, and callers must arrange to serialize calls to this
252 * function. No violations are detected.
253 */
Tim Peters19717fa2004-10-09 17:38:29 +0000254int
255PyThread_create_key(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000256{
Tim Petersfda787f2004-10-09 22:33:09 +0000257 /* All parts of this function are wrong if it's called by multiple
258 * threads simultaneously.
259 */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000260 if (keymutex == NULL)
261 keymutex = PyThread_allocate_lock();
262 return ++nkeys;
263}
264
Tim Petersfda787f2004-10-09 22:33:09 +0000265/* Forget the associations for key across *all* threads. */
Tim Peters19717fa2004-10-09 17:38:29 +0000266void
267PyThread_delete_key(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000268{
269 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000270
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000271 PyThread_acquire_lock(keymutex, 1);
272 q = &keyhead;
273 while ((p = *q) != NULL) {
274 if (p->key == key) {
275 *q = p->next;
276 free((void *)p);
277 /* NB This does *not* free p->value! */
278 }
279 else
280 q = &p->next;
281 }
282 PyThread_release_lock(keymutex);
283}
284
Tim Petersfda787f2004-10-09 22:33:09 +0000285/* Confusing: If the current thread has an association for key,
286 * value is ignored, and 0 is returned. Else an attempt is made to create
287 * an association of key to value for the current thread. 0 is returned
288 * if that succeeds, but -1 is returned if there's not enough memory
289 * to create the association. value must not be NULL.
290 */
Tim Peters19717fa2004-10-09 17:38:29 +0000291int
292PyThread_set_key_value(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000293{
Tim Petersfda787f2004-10-09 22:33:09 +0000294 struct key *p;
295
296 assert(value != NULL);
297 p = find_key(key, value);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000298 if (p == NULL)
299 return -1;
300 else
301 return 0;
302}
303
Tim Petersfda787f2004-10-09 22:33:09 +0000304/* Retrieve the value associated with key in the current thread, or NULL
305 * if the current thread doesn't have an association for key.
306 */
Tim Peters19717fa2004-10-09 17:38:29 +0000307void *
308PyThread_get_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000309{
310 struct key *p = find_key(key, NULL);
Tim Petersfda787f2004-10-09 22:33:09 +0000311
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000312 if (p == NULL)
313 return NULL;
314 else
315 return p->value;
316}
317
Tim Petersfda787f2004-10-09 22:33:09 +0000318/* Forget the current thread's association for key, if any. */
Tim Peters19717fa2004-10-09 17:38:29 +0000319void
320PyThread_delete_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000321{
322 long id = PyThread_get_thread_ident();
323 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000324
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000325 PyThread_acquire_lock(keymutex, 1);
326 q = &keyhead;
327 while ((p = *q) != NULL) {
328 if (p->key == key && p->id == id) {
329 *q = p->next;
330 free((void *)p);
331 /* NB This does *not* free p->value! */
332 break;
333 }
334 else
335 q = &p->next;
336 }
337 PyThread_release_lock(keymutex);
338}
339
340#endif /* Py_HAVE_NATIVE_TLS */