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. */
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 */
20 #ifndef DONT_HAVE_STDIO_H
27 #ifndef HAVE_PTHREAD_H /* XXX Need to check in configure.in */
34 #ifndef _POSIX_THREADS
41 #define SOLARIS_THREADS
44 #if defined(sun) && !defined(SOLARIS_THREADS)
48 /* 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
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
58 #define _POSIX_THREADS
62 #endif /* _POSIX_THREADS */
66 static int thread_debug
= 0;
67 #define dprintf(args) (void)((thread_debug & 1) && printf args)
68 #define d2printf(args) ((thread_debug & 8) && printf args)
71 #define d2printf(args)
74 static int initialized
;
76 static void PyThread__init_thread(void); /* Forward */
79 PyThread_init_thread(void)
82 char *p
= Py_GETENV("PYTHONTHREADDEBUG");
86 thread_debug
= atoi(p
);
94 dprintf(("PyThread_init_thread called\n"));
95 PyThread__init_thread();
98 /* 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. */
101 static size_t _pythread_stacksize
= 0;
104 #include "thread_sgi.h"
107 #ifdef SOLARIS_THREADS
108 #include "thread_solaris.h"
112 #include "thread_lwp.h"
116 #include "thread_pth.h"
117 #undef _POSIX_THREADS
120 #ifdef _POSIX_THREADS
121 #include "thread_pthread.h"
125 #include "thread_cthread.h"
129 #include "thread_nt.h"
133 #include "thread_os2.h"
137 #include "thread_beos.h"
141 #include "thread_plan9.h"
144 #ifdef ATHEOS_THREADS
145 #include "thread_atheos.h"
149 #ifdef FOOBAR_THREADS
150 #include "thread_foobar.h"
154 /* return the current thread stack size */
156 PyThread_get_stacksize(void)
158 return _pythread_stacksize
;
161 /* Only platforms defining a THREAD_SET_STACKSIZE() macro
162 in thread_<platform>.h support changing the stack size.
163 Return 0 if stack size is valid,
164 -1 if stack size value is invalid,
165 -2 if setting stack size is not supported. */
167 PyThread_set_stacksize(size_t size
)
169 #if defined(THREAD_SET_STACKSIZE)
170 return THREAD_SET_STACKSIZE(size
);
176 #ifndef Py_HAVE_NATIVE_TLS
177 /* If the platform has not supplied a platform specific
178 TLS implementation, provide our own.
180 This code stolen from "thread_sgi.h", where it was the only
181 implementation of an existing Python TLS API.
183 /* ------------------------------------------------------------------------
184 Per-thread data ("key") support.
186 Use PyThread_create_key() to create a new key. This is typically shared
189 Use PyThread_set_key_value(thekey, value) to associate void* value with
190 thekey in the current thread. Each thread has a distinct mapping of thekey
191 to a void* value. Caution: if the current thread already has a mapping
192 for thekey, value is ignored.
194 Use PyThread_get_key_value(thekey) to retrieve the void* value associated
195 with thekey in the current thread. This returns NULL if no value is
196 associated with thekey in the current thread.
198 Use PyThread_delete_key_value(thekey) to forget the current thread's associated
199 value for thekey. PyThread_delete_key(thekey) forgets the values associated
200 with thekey across *all* threads.
202 While some of these functions have error-return values, none set any
205 None of the functions does memory management on behalf of the void* values.
206 You need to allocate and deallocate them yourself. If the void* values
207 happen to be PyObject*, these functions don't do refcount operations on
210 The GIL does not need to be held when calling these functions; they supply
211 their own locking. This isn't true of PyThread_create_key(), though (see
214 There's a hidden assumption that PyThread_create_key() will be called before
215 any of the other functions are called. There's also a hidden assumption
216 that calls to PyThread_create_key() are serialized externally.
217 ------------------------------------------------------------------------ */
219 /* A singly-linked list of struct key objects remembers all the key->value
220 * associations. File static keyhead heads the list. keymutex is used
221 * to enforce exclusion internally.
224 /* Next record in the list, or NULL if this is the last record. */
227 /* The thread id, according to PyThread_get_thread_ident(). */
230 /* The key and its associated value. */
235 static struct key
*keyhead
= NULL
;
236 static PyThread_type_lock keymutex
= NULL
;
237 static int nkeys
= 0; /* PyThread_create_key() hands out nkeys+1 next */
240 * If the current thread has a mapping for key, the appropriate struct key*
241 * is returned. NB: value is ignored in this case!
242 * If there is no mapping for key in the current thread, then:
243 * If value is NULL, NULL is returned.
244 * Else a mapping of key to value is created for the current thread,
245 * and a pointer to a new struct key* is returned; except that if
246 * malloc() can't find room for a new struct key*, NULL is returned.
247 * So when value==NULL, this acts like a pure lookup routine, and when
248 * value!=NULL, this acts like dict.setdefault(), returning an existing
249 * mapping if one exists, else creating a new mapping.
251 * Caution: this used to be too clever, trying to hold keymutex only
252 * around the "p->next = keyhead; keyhead = p" pair. That allowed
253 * another thread to mutate the list, via key deletion, concurrent with
254 * find_key() crawling over the list. Hilarity ensued. For example, when
255 * the for-loop here does "p = p->next", p could end up pointing at a
256 * record that PyThread_delete_key_value() was concurrently free()'ing.
257 * That could lead to anything, from failing to find a key that exists, to
258 * segfaults. Now we lock the whole routine.
261 find_key(int key
, void *value
)
263 struct key
*p
, *prev_p
;
264 long id
= PyThread_get_thread_ident();
268 PyThread_acquire_lock(keymutex
, 1);
270 for (p
= keyhead
; p
!= NULL
; p
= p
->next
) {
271 if (p
->id
== id
&& p
->key
== key
)
273 /* Sanity check. These states should never happen but if
274 * they do we must abort. Otherwise we'll end up spinning in
275 * in a tight loop with the lock held. A similar check is done
276 * in pystate.c tstate_delete_common(). */
278 Py_FatalError("tls find_key: small circular list(!)");
280 if (p
->next
== keyhead
)
281 Py_FatalError("tls find_key: circular list(!)");
287 p
= (struct key
*)malloc(sizeof(struct key
));
296 PyThread_release_lock(keymutex
);
300 /* Return a new key. This must be called before any other functions in
301 * this family, and callers must arrange to serialize calls to this
302 * function. No violations are detected.
305 PyThread_create_key(void)
307 /* All parts of this function are wrong if it's called by multiple
308 * threads simultaneously.
310 if (keymutex
== NULL
)
311 keymutex
= PyThread_allocate_lock();
315 /* Forget the associations for key across *all* threads. */
317 PyThread_delete_key(int key
)
321 PyThread_acquire_lock(keymutex
, 1);
323 while ((p
= *q
) != NULL
) {
327 /* NB This does *not* free p->value! */
332 PyThread_release_lock(keymutex
);
335 /* Confusing: If the current thread has an association for key,
336 * value is ignored, and 0 is returned. Else an attempt is made to create
337 * an association of key to value for the current thread. 0 is returned
338 * if that succeeds, but -1 is returned if there's not enough memory
339 * to create the association. value must not be NULL.
342 PyThread_set_key_value(int key
, void *value
)
346 assert(value
!= NULL
);
347 p
= find_key(key
, value
);
354 /* Retrieve the value associated with key in the current thread, or NULL
355 * if the current thread doesn't have an association for key.
358 PyThread_get_key_value(int key
)
360 struct key
*p
= find_key(key
, NULL
);
368 /* Forget the current thread's association for key, if any. */
370 PyThread_delete_key_value(int key
)
372 long id
= PyThread_get_thread_ident();
375 PyThread_acquire_lock(keymutex
, 1);
377 while ((p
= *q
) != NULL
) {
378 if (p
->key
== key
&& p
->id
== id
) {
381 /* NB This does *not* free p->value! */
387 PyThread_release_lock(keymutex
);
390 /* Forget everything not associated with the current thread id.
391 * This function is called from PyOS_AfterFork(). It is necessary
392 * because other thread ids which were in use at the time of the fork
393 * may be reused for new threads created in the forked process.
396 PyThread_ReInitTLS(void)
398 long id
= PyThread_get_thread_ident();
404 /* As with interpreter_lock in PyEval_ReInitThreads()
405 we just create a new lock without freeing the old one */
406 keymutex
= PyThread_allocate_lock();
408 /* Delete all keys which do not match the current thread id */
410 while ((p
= *q
) != NULL
) {
414 /* NB This does *not* free p->value! */
421 #endif /* Py_HAVE_NATIVE_TLS */