blob: e55d34244e0f77f92f41ff1a3809179225755093 [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#ifndef _POSIX_THREADS
11/* This means pthreads are not implemented in libc headers, hence the macro
12 not present in unistd.h. But they still can be implemented as an external
13 library (e.g. gnu pth in pthread emulation) */
14# ifdef HAVE_PTHREAD_H
15# include <pthread.h> /* _POSIX_THREADS */
16# endif
17#endif
18
Guido van Rossum2571cc81999-04-07 16:07:23 +000019#ifndef DONT_HAVE_STDIO_H
Guido van Rossum1d5735e1994-08-30 08:27:36 +000020#include <stdio.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +000021#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +000022
Guido van Rossum1d5735e1994-08-30 08:27:36 +000023#include <stdlib.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +000024
Guido van Rossum49b56061998-10-01 20:42:43 +000025#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000026
Guido van Rossum1d5735e1994-08-30 08:27:36 +000027#ifndef _POSIX_THREADS
28
Guido van Rossum539c6622005-09-14 17:49:54 +000029/* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then
Ezio Melotti13925002011-03-16 11:05:33 +020030 enough of the Posix threads package is implemented to support python
Guido van Rossum539c6622005-09-14 17:49:54 +000031 threads.
32
33 This is valid for HP-UX 11.23 running on an ia64 system. If needed, add
34 a check of __ia64 to verify that we're running on a ia64 system instead
35 of a pa-risc system.
36*/
37#ifdef __hpux
38#ifdef _SC_THREADS
39#define _POSIX_THREADS
40#endif
41#endif
42
Sjoerd Mullender66bca321993-12-03 16:54:45 +000043#endif /* _POSIX_THREADS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000044
Guido van Rossum1984f1e1992-08-04 12:41:02 +000045
Guido van Rossum408027e1996-12-30 16:17:54 +000046#ifdef Py_DEBUG
Guido van Rossum1d5735e1994-08-30 08:27:36 +000047static int thread_debug = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048#define dprintf(args) (void)((thread_debug & 1) && printf args)
49#define d2printf(args) ((thread_debug & 8) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000050#else
51#define dprintf(args)
52#define d2printf(args)
53#endif
54
Guido van Rossum1984f1e1992-08-04 12:41:02 +000055static int initialized;
56
Thomas Wouters8ec68fd2000-07-24 14:39:50 +000057static void PyThread__init_thread(void); /* Forward */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000058
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000059void
60PyThread_init_thread(void)
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000061{
Guido van Rossum408027e1996-12-30 16:17:54 +000062#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 char *p = Py_GETENV("PYTHONTHREADDEBUG");
Sjoerd Mullender66bca321993-12-03 16:54:45 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (p) {
66 if (*p)
67 thread_debug = atoi(p);
68 else
69 thread_debug = 1;
70 }
Guido van Rossum408027e1996-12-30 16:17:54 +000071#endif /* Py_DEBUG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 if (initialized)
73 return;
74 initialized = 1;
75 dprintf(("PyThread_init_thread called\n"));
76 PyThread__init_thread();
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000077}
78
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079/* Support for runtime thread stack size tuning.
80 A value of 0 means using the platform's default stack size
81 or the size specified by the THREAD_STACK_SIZE macro. */
82static size_t _pythread_stacksize = 0;
83
Sjoerd Mullender66bca321993-12-03 16:54:45 +000084#ifdef _POSIX_THREADS
Victor Stinner754851f2011-04-19 23:58:51 +020085#define PYTHREAD_NAME "pthread"
Guido van Rossum1d5735e1994-08-30 08:27:36 +000086#include "thread_pthread.h"
Sjoerd Mullendere8934121993-01-13 12:08:48 +000087#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +000088
Guido van Rossumc3f82b61995-01-17 16:29:31 +000089#ifdef NT_THREADS
Victor Stinner754851f2011-04-19 23:58:51 +020090#define PYTHREAD_NAME "nt"
Guido van Rossumc3f82b61995-01-17 16:29:31 +000091#include "thread_nt.h"
92#endif
93
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000094#ifdef OS2_THREADS
Victor Stinner754851f2011-04-19 23:58:51 +020095#define PYTHREAD_NAME "os2"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000096#include "thread_os2.h"
97#endif
98
Guido van Rossumf9f2e821992-08-17 08:59:08 +000099/*
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000100#ifdef FOOBAR_THREADS
101#include "thread_foobar.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000102#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000103*/
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000104
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000105/* return the current thread stack size */
106size_t
107PyThread_get_stacksize(void)
108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 return _pythread_stacksize;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000110}
111
112/* Only platforms defining a THREAD_SET_STACKSIZE() macro
113 in thread_<platform>.h support changing the stack size.
114 Return 0 if stack size is valid,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 -1 if stack size value is invalid,
116 -2 if setting stack size is not supported. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000117int
118PyThread_set_stacksize(size_t size)
119{
120#if defined(THREAD_SET_STACKSIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 return THREAD_SET_STACKSIZE(size);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000122#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000124#endif
125}
126
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000127#ifndef Py_HAVE_NATIVE_TLS
128/* If the platform has not supplied a platform specific
129 TLS implementation, provide our own.
130
131 This code stolen from "thread_sgi.h", where it was the only
132 implementation of an existing Python TLS API.
133*/
Tim Petersfda787f2004-10-09 22:33:09 +0000134/* ------------------------------------------------------------------------
135Per-thread data ("key") support.
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000136
Tim Petersfda787f2004-10-09 22:33:09 +0000137Use PyThread_create_key() to create a new key. This is typically shared
138across threads.
139
140Use PyThread_set_key_value(thekey, value) to associate void* value with
141thekey in the current thread. Each thread has a distinct mapping of thekey
142to a void* value. Caution: if the current thread already has a mapping
143for thekey, value is ignored.
144
145Use PyThread_get_key_value(thekey) to retrieve the void* value associated
146with thekey in the current thread. This returns NULL if no value is
147associated with thekey in the current thread.
148
149Use PyThread_delete_key_value(thekey) to forget the current thread's associated
150value for thekey. PyThread_delete_key(thekey) forgets the values associated
151with thekey across *all* threads.
152
153While some of these functions have error-return values, none set any
154Python exception.
155
156None of the functions does memory management on behalf of the void* values.
157You need to allocate and deallocate them yourself. If the void* values
158happen to be PyObject*, these functions don't do refcount operations on
159them either.
160
161The GIL does not need to be held when calling these functions; they supply
162their own locking. This isn't true of PyThread_create_key(), though (see
163next paragraph).
164
165There's a hidden assumption that PyThread_create_key() will be called before
166any of the other functions are called. There's also a hidden assumption
167that calls to PyThread_create_key() are serialized externally.
168------------------------------------------------------------------------ */
169
170/* A singly-linked list of struct key objects remembers all the key->value
171 * associations. File static keyhead heads the list. keymutex is used
172 * to enforce exclusion internally.
173 */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000174struct key {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 /* Next record in the list, or NULL if this is the last record. */
176 struct key *next;
Tim Petersfda787f2004-10-09 22:33:09 +0000177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 /* The thread id, according to PyThread_get_thread_ident(). */
179 long id;
Tim Petersfda787f2004-10-09 22:33:09 +0000180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 /* The key and its associated value. */
182 int key;
183 void *value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000184};
185
186static struct key *keyhead = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000187static PyThread_type_lock keymutex = NULL;
Tim Petersfda787f2004-10-09 22:33:09 +0000188static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000189
Tim Petersfda787f2004-10-09 22:33:09 +0000190/* Internal helper.
191 * If the current thread has a mapping for key, the appropriate struct key*
192 * is returned. NB: value is ignored in this case!
193 * If there is no mapping for key in the current thread, then:
194 * If value is NULL, NULL is returned.
195 * Else a mapping of key to value is created for the current thread,
196 * and a pointer to a new struct key* is returned; except that if
197 * malloc() can't find room for a new struct key*, NULL is returned.
198 * So when value==NULL, this acts like a pure lookup routine, and when
199 * value!=NULL, this acts like dict.setdefault(), returning an existing
200 * mapping if one exists, else creating a new mapping.
Tim Peters263091e2004-10-10 01:58:44 +0000201 *
202 * Caution: this used to be too clever, trying to hold keymutex only
203 * around the "p->next = keyhead; keyhead = p" pair. That allowed
204 * another thread to mutate the list, via key deletion, concurrent with
205 * find_key() crawling over the list. Hilarity ensued. For example, when
206 * the for-loop here does "p = p->next", p could end up pointing at a
207 * record that PyThread_delete_key_value() was concurrently free()'ing.
208 * That could lead to anything, from failing to find a key that exists, to
209 * segfaults. Now we lock the whole routine.
Tim Petersfda787f2004-10-09 22:33:09 +0000210 */
Tim Peters19717fa2004-10-09 17:38:29 +0000211static struct key *
212find_key(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 struct key *p, *prev_p;
215 long id = PyThread_get_thread_ident();
Tim Petersfda787f2004-10-09 22:33:09 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 if (!keymutex)
218 return NULL;
219 PyThread_acquire_lock(keymutex, 1);
220 prev_p = NULL;
221 for (p = keyhead; p != NULL; p = p->next) {
222 if (p->id == id && p->key == key)
223 goto Done;
224 /* Sanity check. These states should never happen but if
225 * they do we must abort. Otherwise we'll end up spinning in
226 * in a tight loop with the lock held. A similar check is done
227 * in pystate.c tstate_delete_common(). */
228 if (p == prev_p)
229 Py_FatalError("tls find_key: small circular list(!)");
230 prev_p = p;
231 if (p->next == keyhead)
232 Py_FatalError("tls find_key: circular list(!)");
233 }
234 if (value == NULL) {
235 assert(p == NULL);
236 goto Done;
237 }
238 p = (struct key *)malloc(sizeof(struct key));
239 if (p != NULL) {
240 p->id = id;
241 p->key = key;
242 p->value = value;
243 p->next = keyhead;
244 keyhead = p;
245 }
Tim Peters263091e2004-10-10 01:58:44 +0000246 Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 PyThread_release_lock(keymutex);
248 return p;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000249}
250
Tim Petersfda787f2004-10-09 22:33:09 +0000251/* Return a new key. This must be called before any other functions in
252 * this family, and callers must arrange to serialize calls to this
253 * function. No violations are detected.
254 */
Tim Peters19717fa2004-10-09 17:38:29 +0000255int
256PyThread_create_key(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 /* All parts of this function are wrong if it's called by multiple
259 * threads simultaneously.
260 */
261 if (keymutex == NULL)
262 keymutex = PyThread_allocate_lock();
263 return ++nkeys;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000264}
265
Tim Petersfda787f2004-10-09 22:33:09 +0000266/* Forget the associations for key across *all* threads. */
Tim Peters19717fa2004-10-09 17:38:29 +0000267void
268PyThread_delete_key(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 PyThread_acquire_lock(keymutex, 1);
273 q = &keyhead;
274 while ((p = *q) != NULL) {
275 if (p->key == key) {
276 *q = p->next;
277 free((void *)p);
278 /* NB This does *not* free p->value! */
279 }
280 else
281 q = &p->next;
282 }
283 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000284}
285
Tim Petersfda787f2004-10-09 22:33:09 +0000286/* Confusing: If the current thread has an association for key,
287 * value is ignored, and 0 is returned. Else an attempt is made to create
288 * an association of key to value for the current thread. 0 is returned
289 * if that succeeds, but -1 is returned if there's not enough memory
290 * to create the association. value must not be NULL.
291 */
Tim Peters19717fa2004-10-09 17:38:29 +0000292int
293PyThread_set_key_value(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 struct key *p;
Tim Petersfda787f2004-10-09 22:33:09 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 assert(value != NULL);
298 p = find_key(key, value);
299 if (p == NULL)
300 return -1;
301 else
302 return 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000303}
304
Tim Petersfda787f2004-10-09 22:33:09 +0000305/* Retrieve the value associated with key in the current thread, or NULL
306 * if the current thread doesn't have an association for key.
307 */
Tim Peters19717fa2004-10-09 17:38:29 +0000308void *
309PyThread_get_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 struct key *p = find_key(key, NULL);
Tim Petersfda787f2004-10-09 22:33:09 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 if (p == NULL)
314 return NULL;
315 else
316 return p->value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000317}
318
Tim Petersfda787f2004-10-09 22:33:09 +0000319/* Forget the current thread's association for key, if any. */
Tim Peters19717fa2004-10-09 17:38:29 +0000320void
321PyThread_delete_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 long id = PyThread_get_thread_ident();
324 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 PyThread_acquire_lock(keymutex, 1);
327 q = &keyhead;
328 while ((p = *q) != NULL) {
329 if (p->key == key && p->id == id) {
330 *q = p->next;
331 free((void *)p);
332 /* NB This does *not* free p->value! */
333 break;
334 }
335 else
336 q = &p->next;
337 }
338 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000339}
340
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000341/* Forget everything not associated with the current thread id.
342 * This function is called from PyOS_AfterFork(). It is necessary
343 * because other thread ids which were in use at the time of the fork
344 * may be reused for new threads created in the forked process.
345 */
346void
347PyThread_ReInitTLS(void)
348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 long id = PyThread_get_thread_ident();
350 struct key *p, **q;
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (!keymutex)
353 return;
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 /* As with interpreter_lock in PyEval_ReInitThreads()
356 we just create a new lock without freeing the old one */
357 keymutex = PyThread_allocate_lock();
358
359 /* Delete all keys which do not match the current thread id */
360 q = &keyhead;
361 while ((p = *q) != NULL) {
362 if (p->id != id) {
363 *q = p->next;
364 free((void *)p);
365 /* NB This does *not* free p->value! */
366 }
367 else
368 q = &p->next;
369 }
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000370}
371
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000372#endif /* Py_HAVE_NATIVE_TLS */
Victor Stinner754851f2011-04-19 23:58:51 +0200373
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200374PyDoc_STRVAR(threadinfo__doc__,
375"sys.thread_info\n\
376\n\
377A struct sequence holding information about the thread implementation.");
378
379static PyStructSequence_Field threadinfo_fields[] = {
380 {"name", "name of the thread implementation"},
381 {"lock", "name of the lock implementation"},
382 {"version", "name and version of the thread library"},
383 {0}
384};
385
386static PyStructSequence_Desc threadinfo_desc = {
387 "sys.thread_info", /* name */
388 threadinfo__doc__, /* doc */
389 threadinfo_fields, /* fields */
390 3
391};
392
393static PyTypeObject ThreadInfoType;
394
Victor Stinner754851f2011-04-19 23:58:51 +0200395PyObject*
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200396PyThread_GetInfo(void)
Victor Stinner754851f2011-04-19 23:58:51 +0200397{
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200398 PyObject *threadinfo, *value;
399 int pos = 0;
Victor Stinnere07f5222011-04-20 12:23:26 +0200400#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
401 && defined(_CS_GNU_LIBPTHREAD_VERSION))
Victor Stinner754851f2011-04-19 23:58:51 +0200402 char buffer[255];
403 int len;
Victor Stinnere07f5222011-04-20 12:23:26 +0200404#endif
Victor Stinner754851f2011-04-19 23:58:51 +0200405
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200406 if (ThreadInfoType.tp_name == 0)
407 PyStructSequence_InitType(&ThreadInfoType, &threadinfo_desc);
408
409 threadinfo = PyStructSequence_New(&ThreadInfoType);
410 if (threadinfo == NULL)
Victor Stinner754851f2011-04-19 23:58:51 +0200411 return NULL;
412
413 value = PyUnicode_FromString(PYTHREAD_NAME);
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200414 if (value == NULL) {
415 Py_DECREF(threadinfo);
416 return NULL;
417 }
418 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
Victor Stinner754851f2011-04-19 23:58:51 +0200419
420#ifdef _POSIX_THREADS
421#ifdef USE_SEMAPHORES
422 value = PyUnicode_FromString("semaphore");
423#else
424 value = PyUnicode_FromString("mutex+cond");
425#endif
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200426 if (value == NULL) {
427 Py_DECREF(threadinfo);
Victor Stinner754851f2011-04-19 23:58:51 +0200428 return NULL;
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200429 }
430#else
431 Py_INCREF(Py_None);
432 value = Py_None;
433#endif
434 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
Victor Stinner754851f2011-04-19 23:58:51 +0200435
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200436#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
437 && defined(_CS_GNU_LIBPTHREAD_VERSION))
438 value = NULL;
Victor Stinner754851f2011-04-19 23:58:51 +0200439 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, buffer, sizeof(buffer));
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200440 if (1 < len && len < sizeof(buffer)) {
Victor Stinner754851f2011-04-19 23:58:51 +0200441 value = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
442 if (value == NULL)
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200443 PyErr_Clear();
Victor Stinner754851f2011-04-19 23:58:51 +0200444 }
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200445 if (value == NULL)
Victor Stinner754851f2011-04-19 23:58:51 +0200446#endif
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200447 {
448 Py_INCREF(Py_None);
449 value = Py_None;
450 }
451 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
452 return threadinfo;
Victor Stinner754851f2011-04-19 23:58:51 +0200453}