blob: c7d17d60e98c1d53c7614ab2dbe9bf4e02d433d4 [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 Rossum1984f1e1992-08-04 12:41:02 +000029#ifdef __sgi
Guido van Rossum1d5735e1994-08-30 08:27:36 +000030#define SGI_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000031#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +000032
Guido van Rossum1d5735e1994-08-30 08:27:36 +000033#ifdef HAVE_THREAD_H
34#define SOLARIS_THREADS
35#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +000036
Guido van Rossum1d5735e1994-08-30 08:27:36 +000037#if defined(sun) && !defined(SOLARIS_THREADS)
38#define SUN_LWP
39#endif
40
Guido van Rossum539c6622005-09-14 17:49:54 +000041/* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then
Ezio Melotti13925002011-03-16 11:05:33 +020042 enough of the Posix threads package is implemented to support python
Guido van Rossum539c6622005-09-14 17:49:54 +000043 threads.
44
45 This is valid for HP-UX 11.23 running on an ia64 system. If needed, add
46 a check of __ia64 to verify that we're running on a ia64 system instead
47 of a pa-risc system.
48*/
49#ifdef __hpux
50#ifdef _SC_THREADS
51#define _POSIX_THREADS
52#endif
53#endif
54
Sjoerd Mullender66bca321993-12-03 16:54:45 +000055#endif /* _POSIX_THREADS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000056
Guido van Rossum1984f1e1992-08-04 12:41:02 +000057
Guido van Rossum408027e1996-12-30 16:17:54 +000058#ifdef Py_DEBUG
Guido van Rossum1d5735e1994-08-30 08:27:36 +000059static int thread_debug = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060#define dprintf(args) (void)((thread_debug & 1) && printf args)
61#define d2printf(args) ((thread_debug & 8) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000062#else
63#define dprintf(args)
64#define d2printf(args)
65#endif
66
Guido van Rossum1984f1e1992-08-04 12:41:02 +000067static int initialized;
68
Thomas Wouters8ec68fd2000-07-24 14:39:50 +000069static void PyThread__init_thread(void); /* Forward */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000070
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000071void
72PyThread_init_thread(void)
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000073{
Guido van Rossum408027e1996-12-30 16:17:54 +000074#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 char *p = Py_GETENV("PYTHONTHREADDEBUG");
Sjoerd Mullender66bca321993-12-03 16:54:45 +000076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 if (p) {
78 if (*p)
79 thread_debug = atoi(p);
80 else
81 thread_debug = 1;
82 }
Guido van Rossum408027e1996-12-30 16:17:54 +000083#endif /* Py_DEBUG */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 if (initialized)
85 return;
86 initialized = 1;
87 dprintf(("PyThread_init_thread called\n"));
88 PyThread__init_thread();
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000089}
90
Thomas Wouters0e3f5912006-08-11 14:57:12 +000091/* Support for runtime thread stack size tuning.
92 A value of 0 means using the platform's default stack size
93 or the size specified by the THREAD_STACK_SIZE macro. */
94static size_t _pythread_stacksize = 0;
95
Guido van Rossum1d5735e1994-08-30 08:27:36 +000096#ifdef SGI_THREADS
Antoine Pitrou2a9c2bb2009-10-24 20:43:49 +000097#error SGI Irix threads are now unsupported, and code will be removed in 3.3.
Guido van Rossum1d5735e1994-08-30 08:27:36 +000098#include "thread_sgi.h"
Sjoerd Mullender66bca321993-12-03 16:54:45 +000099#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000100
101#ifdef SOLARIS_THREADS
Victor Stinner754851f2011-04-19 23:58:51 +0200102#define PYTHREAD_NAME "solaris"
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000103#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
Victor Stinner754851f2011-04-19 23:58:51 +0200118#define PYTHREAD_NAME "pthread"
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000119#include "thread_pthread.h"
Sjoerd Mullendere8934121993-01-13 12:08:48 +0000120#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000121
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000122#ifdef C_THREADS
Antoine Pitrou86b21c12009-10-24 20:24:16 +0000123#error Mach C Threads are now unsupported, and code will be removed in 3.3.
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000124#include "thread_cthread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000125#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000126
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000127#ifdef NT_THREADS
Victor Stinner754851f2011-04-19 23:58:51 +0200128#define PYTHREAD_NAME "nt"
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000129#include "thread_nt.h"
130#endif
131
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000132#ifdef OS2_THREADS
Victor Stinner754851f2011-04-19 23:58:51 +0200133#define PYTHREAD_NAME "os2"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000134#include "thread_os2.h"
135#endif
136
Martin v. Löwis7d1cd692002-03-09 12:10:54 +0000137#ifdef PLAN9_THREADS
Victor Stinner754851f2011-04-19 23:58:51 +0200138#define PYTHREAD_NAME "plan9"
Martin v. Löwis7d1cd692002-03-09 12:10:54 +0000139#include "thread_plan9.h"
140#endif
141
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000142/*
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000143#ifdef FOOBAR_THREADS
144#include "thread_foobar.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000145#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000146*/
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000147
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000148/* return the current thread stack size */
149size_t
150PyThread_get_stacksize(void)
151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 return _pythread_stacksize;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000153}
154
155/* Only platforms defining a THREAD_SET_STACKSIZE() macro
156 in thread_<platform>.h support changing the stack size.
157 Return 0 if stack size is valid,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 -1 if stack size value is invalid,
159 -2 if setting stack size is not supported. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000160int
161PyThread_set_stacksize(size_t size)
162{
163#if defined(THREAD_SET_STACKSIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 return THREAD_SET_STACKSIZE(size);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000165#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return -2;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000167#endif
168}
169
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000170#ifndef Py_HAVE_NATIVE_TLS
171/* If the platform has not supplied a platform specific
172 TLS implementation, provide our own.
173
174 This code stolen from "thread_sgi.h", where it was the only
175 implementation of an existing Python TLS API.
176*/
Tim Petersfda787f2004-10-09 22:33:09 +0000177/* ------------------------------------------------------------------------
178Per-thread data ("key") support.
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000179
Tim Petersfda787f2004-10-09 22:33:09 +0000180Use PyThread_create_key() to create a new key. This is typically shared
181across threads.
182
183Use PyThread_set_key_value(thekey, value) to associate void* value with
184thekey in the current thread. Each thread has a distinct mapping of thekey
185to a void* value. Caution: if the current thread already has a mapping
186for thekey, value is ignored.
187
188Use PyThread_get_key_value(thekey) to retrieve the void* value associated
189with thekey in the current thread. This returns NULL if no value is
190associated with thekey in the current thread.
191
192Use PyThread_delete_key_value(thekey) to forget the current thread's associated
193value for thekey. PyThread_delete_key(thekey) forgets the values associated
194with thekey across *all* threads.
195
196While some of these functions have error-return values, none set any
197Python exception.
198
199None of the functions does memory management on behalf of the void* values.
200You need to allocate and deallocate them yourself. If the void* values
201happen to be PyObject*, these functions don't do refcount operations on
202them either.
203
204The GIL does not need to be held when calling these functions; they supply
205their own locking. This isn't true of PyThread_create_key(), though (see
206next paragraph).
207
208There's a hidden assumption that PyThread_create_key() will be called before
209any of the other functions are called. There's also a hidden assumption
210that calls to PyThread_create_key() are serialized externally.
211------------------------------------------------------------------------ */
212
213/* A singly-linked list of struct key objects remembers all the key->value
214 * associations. File static keyhead heads the list. keymutex is used
215 * to enforce exclusion internally.
216 */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000217struct key {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 /* Next record in the list, or NULL if this is the last record. */
219 struct key *next;
Tim Petersfda787f2004-10-09 22:33:09 +0000220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 /* The thread id, according to PyThread_get_thread_ident(). */
222 long id;
Tim Petersfda787f2004-10-09 22:33:09 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 /* The key and its associated value. */
225 int key;
226 void *value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000227};
228
229static struct key *keyhead = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000230static PyThread_type_lock keymutex = NULL;
Tim Petersfda787f2004-10-09 22:33:09 +0000231static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000232
Tim Petersfda787f2004-10-09 22:33:09 +0000233/* Internal helper.
234 * If the current thread has a mapping for key, the appropriate struct key*
235 * is returned. NB: value is ignored in this case!
236 * If there is no mapping for key in the current thread, then:
237 * If value is NULL, NULL is returned.
238 * Else a mapping of key to value is created for the current thread,
239 * and a pointer to a new struct key* is returned; except that if
240 * malloc() can't find room for a new struct key*, NULL is returned.
241 * So when value==NULL, this acts like a pure lookup routine, and when
242 * value!=NULL, this acts like dict.setdefault(), returning an existing
243 * mapping if one exists, else creating a new mapping.
Tim Peters263091e2004-10-10 01:58:44 +0000244 *
245 * Caution: this used to be too clever, trying to hold keymutex only
246 * around the "p->next = keyhead; keyhead = p" pair. That allowed
247 * another thread to mutate the list, via key deletion, concurrent with
248 * find_key() crawling over the list. Hilarity ensued. For example, when
249 * the for-loop here does "p = p->next", p could end up pointing at a
250 * record that PyThread_delete_key_value() was concurrently free()'ing.
251 * That could lead to anything, from failing to find a key that exists, to
252 * segfaults. Now we lock the whole routine.
Tim Petersfda787f2004-10-09 22:33:09 +0000253 */
Tim Peters19717fa2004-10-09 17:38:29 +0000254static struct key *
255find_key(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 struct key *p, *prev_p;
258 long id = PyThread_get_thread_ident();
Tim Petersfda787f2004-10-09 22:33:09 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 if (!keymutex)
261 return NULL;
262 PyThread_acquire_lock(keymutex, 1);
263 prev_p = NULL;
264 for (p = keyhead; p != NULL; p = p->next) {
265 if (p->id == id && p->key == key)
266 goto Done;
267 /* Sanity check. These states should never happen but if
268 * they do we must abort. Otherwise we'll end up spinning in
269 * in a tight loop with the lock held. A similar check is done
270 * in pystate.c tstate_delete_common(). */
271 if (p == prev_p)
272 Py_FatalError("tls find_key: small circular list(!)");
273 prev_p = p;
274 if (p->next == keyhead)
275 Py_FatalError("tls find_key: circular list(!)");
276 }
277 if (value == NULL) {
278 assert(p == NULL);
279 goto Done;
280 }
281 p = (struct key *)malloc(sizeof(struct key));
282 if (p != NULL) {
283 p->id = id;
284 p->key = key;
285 p->value = value;
286 p->next = keyhead;
287 keyhead = p;
288 }
Tim Peters263091e2004-10-10 01:58:44 +0000289 Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 PyThread_release_lock(keymutex);
291 return p;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000292}
293
Tim Petersfda787f2004-10-09 22:33:09 +0000294/* Return a new key. This must be called before any other functions in
295 * this family, and callers must arrange to serialize calls to this
296 * function. No violations are detected.
297 */
Tim Peters19717fa2004-10-09 17:38:29 +0000298int
299PyThread_create_key(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 /* All parts of this function are wrong if it's called by multiple
302 * threads simultaneously.
303 */
304 if (keymutex == NULL)
305 keymutex = PyThread_allocate_lock();
306 return ++nkeys;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000307}
308
Tim Petersfda787f2004-10-09 22:33:09 +0000309/* Forget the associations for key across *all* threads. */
Tim Peters19717fa2004-10-09 17:38:29 +0000310void
311PyThread_delete_key(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 PyThread_acquire_lock(keymutex, 1);
316 q = &keyhead;
317 while ((p = *q) != NULL) {
318 if (p->key == key) {
319 *q = p->next;
320 free((void *)p);
321 /* NB This does *not* free p->value! */
322 }
323 else
324 q = &p->next;
325 }
326 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000327}
328
Tim Petersfda787f2004-10-09 22:33:09 +0000329/* Confusing: If the current thread has an association for key,
330 * value is ignored, and 0 is returned. Else an attempt is made to create
331 * an association of key to value for the current thread. 0 is returned
332 * if that succeeds, but -1 is returned if there's not enough memory
333 * to create the association. value must not be NULL.
334 */
Tim Peters19717fa2004-10-09 17:38:29 +0000335int
336PyThread_set_key_value(int key, void *value)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 struct key *p;
Tim Petersfda787f2004-10-09 22:33:09 +0000339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 assert(value != NULL);
341 p = find_key(key, value);
342 if (p == NULL)
343 return -1;
344 else
345 return 0;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000346}
347
Tim Petersfda787f2004-10-09 22:33:09 +0000348/* Retrieve the value associated with key in the current thread, or NULL
349 * if the current thread doesn't have an association for key.
350 */
Tim Peters19717fa2004-10-09 17:38:29 +0000351void *
352PyThread_get_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 struct key *p = find_key(key, NULL);
Tim Petersfda787f2004-10-09 22:33:09 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (p == NULL)
357 return NULL;
358 else
359 return p->value;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000360}
361
Tim Petersfda787f2004-10-09 22:33:09 +0000362/* Forget the current thread's association for key, if any. */
Tim Peters19717fa2004-10-09 17:38:29 +0000363void
364PyThread_delete_key_value(int key)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 long id = PyThread_get_thread_ident();
367 struct key *p, **q;
Tim Petersfda787f2004-10-09 22:33:09 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 PyThread_acquire_lock(keymutex, 1);
370 q = &keyhead;
371 while ((p = *q) != NULL) {
372 if (p->key == key && p->id == id) {
373 *q = p->next;
374 free((void *)p);
375 /* NB This does *not* free p->value! */
376 break;
377 }
378 else
379 q = &p->next;
380 }
381 PyThread_release_lock(keymutex);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000382}
383
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000384/* Forget everything not associated with the current thread id.
385 * This function is called from PyOS_AfterFork(). It is necessary
386 * because other thread ids which were in use at the time of the fork
387 * may be reused for new threads created in the forked process.
388 */
389void
390PyThread_ReInitTLS(void)
391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 long id = PyThread_get_thread_ident();
393 struct key *p, **q;
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 if (!keymutex)
396 return;
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 /* As with interpreter_lock in PyEval_ReInitThreads()
399 we just create a new lock without freeing the old one */
400 keymutex = PyThread_allocate_lock();
401
402 /* Delete all keys which do not match the current thread id */
403 q = &keyhead;
404 while ((p = *q) != NULL) {
405 if (p->id != id) {
406 *q = p->next;
407 free((void *)p);
408 /* NB This does *not* free p->value! */
409 }
410 else
411 q = &p->next;
412 }
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000413}
414
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000415#endif /* Py_HAVE_NATIVE_TLS */
Victor Stinner754851f2011-04-19 23:58:51 +0200416
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200417PyDoc_STRVAR(threadinfo__doc__,
418"sys.thread_info\n\
419\n\
420A struct sequence holding information about the thread implementation.");
421
422static PyStructSequence_Field threadinfo_fields[] = {
423 {"name", "name of the thread implementation"},
424 {"lock", "name of the lock implementation"},
425 {"version", "name and version of the thread library"},
426 {0}
427};
428
429static PyStructSequence_Desc threadinfo_desc = {
430 "sys.thread_info", /* name */
431 threadinfo__doc__, /* doc */
432 threadinfo_fields, /* fields */
433 3
434};
435
436static PyTypeObject ThreadInfoType;
437
Victor Stinner754851f2011-04-19 23:58:51 +0200438PyObject*
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200439PyThread_GetInfo(void)
Victor Stinner754851f2011-04-19 23:58:51 +0200440{
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200441 PyObject *threadinfo, *value;
442 int pos = 0;
Victor Stinnere07f5222011-04-20 12:23:26 +0200443#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
444 && defined(_CS_GNU_LIBPTHREAD_VERSION))
Victor Stinner754851f2011-04-19 23:58:51 +0200445 char buffer[255];
446 int len;
Victor Stinnere07f5222011-04-20 12:23:26 +0200447#endif
Victor Stinner754851f2011-04-19 23:58:51 +0200448
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200449 if (ThreadInfoType.tp_name == 0)
450 PyStructSequence_InitType(&ThreadInfoType, &threadinfo_desc);
451
452 threadinfo = PyStructSequence_New(&ThreadInfoType);
453 if (threadinfo == NULL)
Victor Stinner754851f2011-04-19 23:58:51 +0200454 return NULL;
455
456 value = PyUnicode_FromString(PYTHREAD_NAME);
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200457 if (value == NULL) {
458 Py_DECREF(threadinfo);
459 return NULL;
460 }
461 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
Victor Stinner754851f2011-04-19 23:58:51 +0200462
463#ifdef _POSIX_THREADS
464#ifdef USE_SEMAPHORES
465 value = PyUnicode_FromString("semaphore");
466#else
467 value = PyUnicode_FromString("mutex+cond");
468#endif
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200469 if (value == NULL) {
470 Py_DECREF(threadinfo);
Victor Stinner754851f2011-04-19 23:58:51 +0200471 return NULL;
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200472 }
473#else
474 Py_INCREF(Py_None);
475 value = Py_None;
476#endif
477 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
Victor Stinner754851f2011-04-19 23:58:51 +0200478
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200479#if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \
480 && defined(_CS_GNU_LIBPTHREAD_VERSION))
481 value = NULL;
Victor Stinner754851f2011-04-19 23:58:51 +0200482 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, buffer, sizeof(buffer));
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200483 if (1 < len && len < sizeof(buffer)) {
Victor Stinner754851f2011-04-19 23:58:51 +0200484 value = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
485 if (value == NULL)
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200486 PyErr_Clear();
Victor Stinner754851f2011-04-19 23:58:51 +0200487 }
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200488 if (value == NULL)
Victor Stinner754851f2011-04-19 23:58:51 +0200489#endif
Victor Stinnerd5c355c2011-04-30 14:53:09 +0200490 {
491 Py_INCREF(Py_None);
492 value = Py_None;
493 }
494 PyStructSequence_SET_ITEM(threadinfo, pos++, value);
495 return threadinfo;
Victor Stinner754851f2011-04-19 23:58:51 +0200496}