• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* 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 
8 #include "Python.h"
9 
10 
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 
20 #ifndef DONT_HAVE_STDIO_H
21 #include <stdio.h>
22 #endif
23 
24 #include <stdlib.h>
25 
26 #ifdef __sgi
27 #ifndef HAVE_PTHREAD_H /* XXX Need to check in configure.ac */
28 #undef _POSIX_THREADS
29 #endif
30 #endif
31 
32 #include "pythread.h"
33 
34 #ifndef _POSIX_THREADS
35 
36 #ifdef __sgi
37 #define SGI_THREADS
38 #endif
39 
40 #ifdef HAVE_THREAD_H
41 #define SOLARIS_THREADS
42 #endif
43 
44 #if defined(sun) && !defined(SOLARIS_THREADS)
45 #define SUN_LWP
46 #endif
47 
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 implemented to support python
50    threads.
51 
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 an ia64 system instead
54    of a pa-risc system.
55 */
56 #ifdef __hpux
57 #ifdef _SC_THREADS
58 #define _POSIX_THREADS
59 #endif
60 #endif
61 
62 #endif /* _POSIX_THREADS */
63 
64 
65 #ifdef dprintf
66 #undef dprintf
67 #endif
68 #ifdef Py_DEBUG
69 static int thread_debug = 0;
70 #define dprintf(args)   (void)((thread_debug & 1) && printf args)
71 #define d2printf(args)  ((thread_debug & 8) && printf args)
72 #else
73 #define dprintf(args)
74 #define d2printf(args)
75 #endif
76 
77 static int initialized;
78 
79 static void PyThread__init_thread(void); /* Forward */
80 
81 void
PyThread_init_thread(void)82 PyThread_init_thread(void)
83 {
84 #ifdef Py_DEBUG
85     char *p = Py_GETENV("PYTHONTHREADDEBUG");
86 
87     if (p) {
88         if (*p)
89             thread_debug = atoi(p);
90         else
91             thread_debug = 1;
92     }
93 #endif /* Py_DEBUG */
94     if (initialized)
95         return;
96     initialized = 1;
97     dprintf(("PyThread_init_thread called\n"));
98     PyThread__init_thread();
99 }
100 
101 /* Support for runtime thread stack size tuning.
102    A value of 0 means using the platform's default stack size
103    or the size specified by the THREAD_STACK_SIZE macro. */
104 static size_t _pythread_stacksize = 0;
105 
106 #ifdef SGI_THREADS
107 #include "thread_sgi.h"
108 #endif
109 
110 #ifdef SOLARIS_THREADS
111 #include "thread_solaris.h"
112 #endif
113 
114 #ifdef SUN_LWP
115 #include "thread_lwp.h"
116 #endif
117 
118 #ifdef HAVE_PTH
119 #include "thread_pth.h"
120 #undef _POSIX_THREADS
121 #endif
122 
123 #ifdef _POSIX_THREADS
124 #include "thread_pthread.h"
125 #endif
126 
127 #ifdef C_THREADS
128 #include "thread_cthread.h"
129 #endif
130 
131 #ifdef NT_THREADS
132 #include "thread_nt.h"
133 #endif
134 
135 #ifdef OS2_THREADS
136 #include "thread_os2.h"
137 #endif
138 
139 #ifdef BEOS_THREADS
140 #include "thread_beos.h"
141 #endif
142 
143 #ifdef PLAN9_THREADS
144 #include "thread_plan9.h"
145 #endif
146 
147 #ifdef ATHEOS_THREADS
148 #include "thread_atheos.h"
149 #endif
150 
151 /*
152 #ifdef FOOBAR_THREADS
153 #include "thread_foobar.h"
154 #endif
155 */
156 
157 /* return the current thread stack size */
158 size_t
PyThread_get_stacksize(void)159 PyThread_get_stacksize(void)
160 {
161     return _pythread_stacksize;
162 }
163 
164 /* Only platforms defining a THREAD_SET_STACKSIZE() macro
165    in thread_<platform>.h support changing the stack size.
166    Return 0 if stack size is valid,
167       -1 if stack size value is invalid,
168       -2 if setting stack size is not supported. */
169 int
PyThread_set_stacksize(size_t size)170 PyThread_set_stacksize(size_t size)
171 {
172 #if defined(THREAD_SET_STACKSIZE)
173     return THREAD_SET_STACKSIZE(size);
174 #else
175     return -2;
176 #endif
177 }
178 
179 #ifndef Py_HAVE_NATIVE_TLS
180 /* If the platform has not supplied a platform specific
181    TLS implementation, provide our own.
182 
183    This code stolen from "thread_sgi.h", where it was the only
184    implementation of an existing Python TLS API.
185 */
186 /* ------------------------------------------------------------------------
187 Per-thread data ("key") support.
188 
189 Use PyThread_create_key() to create a new key.  This is typically shared
190 across threads.
191 
192 Use PyThread_set_key_value(thekey, value) to associate void* value with
193 thekey in the current thread.  Each thread has a distinct mapping of thekey
194 to a void* value.  Caution:  if the current thread already has a mapping
195 for thekey, value is ignored.
196 
197 Use PyThread_get_key_value(thekey) to retrieve the void* value associated
198 with thekey in the current thread.  This returns NULL if no value is
199 associated with thekey in the current thread.
200 
201 Use PyThread_delete_key_value(thekey) to forget the current thread's associated
202 value for thekey.  PyThread_delete_key(thekey) forgets the values associated
203 with thekey across *all* threads.
204 
205 While some of these functions have error-return values, none set any
206 Python exception.
207 
208 None of the functions does memory management on behalf of the void* values.
209 You need to allocate and deallocate them yourself.  If the void* values
210 happen to be PyObject*, these functions don't do refcount operations on
211 them either.
212 
213 The GIL does not need to be held when calling these functions; they supply
214 their own locking.  This isn't true of PyThread_create_key(), though (see
215 next paragraph).
216 
217 There's a hidden assumption that PyThread_create_key() will be called before
218 any of the other functions are called.  There's also a hidden assumption
219 that calls to PyThread_create_key() are serialized externally.
220 ------------------------------------------------------------------------ */
221 
222 /* A singly-linked list of struct key objects remembers all the key->value
223  * associations.  File static keyhead heads the list.  keymutex is used
224  * to enforce exclusion internally.
225  */
226 struct key {
227     /* Next record in the list, or NULL if this is the last record. */
228     struct key *next;
229 
230     /* The thread id, according to PyThread_get_thread_ident(). */
231     long id;
232 
233     /* The key and its associated value. */
234     int key;
235     void *value;
236 };
237 
238 static struct key *keyhead = NULL;
239 static PyThread_type_lock keymutex = NULL;
240 static int nkeys = 0;  /* PyThread_create_key() hands out nkeys+1 next */
241 
242 /* Internal helper.
243  * If the current thread has a mapping for key, the appropriate struct key*
244  * is returned.  NB:  value is ignored in this case!
245  * If there is no mapping for key in the current thread, then:
246  *     If value is NULL, NULL is returned.
247  *     Else a mapping of key to value is created for the current thread,
248  *     and a pointer to a new struct key* is returned; except that if
249  *     malloc() can't find room for a new struct key*, NULL is returned.
250  * So when value==NULL, this acts like a pure lookup routine, and when
251  * value!=NULL, this acts like dict.setdefault(), returning an existing
252  * mapping if one exists, else creating a new mapping.
253  *
254  * Caution:  this used to be too clever, trying to hold keymutex only
255  * around the "p->next = keyhead; keyhead = p" pair.  That allowed
256  * another thread to mutate the list, via key deletion, concurrent with
257  * find_key() crawling over the list.  Hilarity ensued.  For example, when
258  * the for-loop here does "p = p->next", p could end up pointing at a
259  * record that PyThread_delete_key_value() was concurrently free()'ing.
260  * That could lead to anything, from failing to find a key that exists, to
261  * segfaults.  Now we lock the whole routine.
262  */
263 static struct key *
find_key(int key,void * value)264 find_key(int key, void *value)
265 {
266     struct key *p, *prev_p;
267     long id = PyThread_get_thread_ident();
268 
269     if (!keymutex)
270         return NULL;
271     PyThread_acquire_lock(keymutex, 1);
272     prev_p = NULL;
273     for (p = keyhead; p != NULL; p = p->next) {
274         if (p->id == id && p->key == key)
275             goto Done;
276         /* Sanity check.  These states should never happen but if
277          * they do we must abort.  Otherwise we'll end up spinning
278          * in a tight loop with the lock held.  A similar check is done
279          * in pystate.c tstate_delete_common().  */
280         if (p == prev_p)
281             Py_FatalError("tls find_key: small circular list(!)");
282         prev_p = p;
283         if (p->next == keyhead)
284             Py_FatalError("tls find_key: circular list(!)");
285     }
286     if (value == NULL) {
287         assert(p == NULL);
288         goto Done;
289     }
290     p = (struct key *)malloc(sizeof(struct key));
291     if (p != NULL) {
292         p->id = id;
293         p->key = key;
294         p->value = value;
295         p->next = keyhead;
296         keyhead = p;
297     }
298  Done:
299     PyThread_release_lock(keymutex);
300     return p;
301 }
302 
303 /* Return a new key.  This must be called before any other functions in
304  * this family, and callers must arrange to serialize calls to this
305  * function.  No violations are detected.
306  */
307 int
PyThread_create_key(void)308 PyThread_create_key(void)
309 {
310     /* All parts of this function are wrong if it's called by multiple
311      * threads simultaneously.
312      */
313     if (keymutex == NULL)
314         keymutex = PyThread_allocate_lock();
315     return ++nkeys;
316 }
317 
318 /* Forget the associations for key across *all* threads. */
319 void
PyThread_delete_key(int key)320 PyThread_delete_key(int key)
321 {
322     struct key *p, **q;
323 
324     PyThread_acquire_lock(keymutex, 1);
325     q = &keyhead;
326     while ((p = *q) != NULL) {
327         if (p->key == key) {
328             *q = p->next;
329             free((void *)p);
330             /* NB This does *not* free p->value! */
331         }
332         else
333             q = &p->next;
334     }
335     PyThread_release_lock(keymutex);
336 }
337 
338 /* Confusing:  If the current thread has an association for key,
339  * value is ignored, and 0 is returned.  Else an attempt is made to create
340  * an association of key to value for the current thread.  0 is returned
341  * if that succeeds, but -1 is returned if there's not enough memory
342  * to create the association.  value must not be NULL.
343  */
344 int
PyThread_set_key_value(int key,void * value)345 PyThread_set_key_value(int key, void *value)
346 {
347     struct key *p;
348 
349     assert(value != NULL);
350     p = find_key(key, value);
351     if (p == NULL)
352         return -1;
353     else
354         return 0;
355 }
356 
357 /* Retrieve the value associated with key in the current thread, or NULL
358  * if the current thread doesn't have an association for key.
359  */
360 void *
PyThread_get_key_value(int key)361 PyThread_get_key_value(int key)
362 {
363     struct key *p = find_key(key, NULL);
364 
365     if (p == NULL)
366         return NULL;
367     else
368         return p->value;
369 }
370 
371 /* Forget the current thread's association for key, if any. */
372 void
PyThread_delete_key_value(int key)373 PyThread_delete_key_value(int key)
374 {
375     long id = PyThread_get_thread_ident();
376     struct key *p, **q;
377 
378     PyThread_acquire_lock(keymutex, 1);
379     q = &keyhead;
380     while ((p = *q) != NULL) {
381         if (p->key == key && p->id == id) {
382             *q = p->next;
383             free((void *)p);
384             /* NB This does *not* free p->value! */
385             break;
386         }
387         else
388             q = &p->next;
389     }
390     PyThread_release_lock(keymutex);
391 }
392 
393 /* Forget everything not associated with the current thread id.
394  * This function is called from PyOS_AfterFork().  It is necessary
395  * because other thread ids which were in use at the time of the fork
396  * may be reused for new threads created in the forked process.
397  */
398 void
PyThread_ReInitTLS(void)399 PyThread_ReInitTLS(void)
400 {
401     long id = PyThread_get_thread_ident();
402     struct key *p, **q;
403 
404     if (!keymutex)
405         return;
406 
407     /* As with interpreter_lock in PyEval_ReInitThreads()
408        we just create a new lock without freeing the old one */
409     keymutex = PyThread_allocate_lock();
410 
411     /* Delete all keys which do not match the current thread id */
412     q = &keyhead;
413     while ((p = *q) != NULL) {
414         if (p->id != id) {
415             *q = p->next;
416             free((void *)p);
417             /* NB This does *not* free p->value! */
418         }
419         else
420             q = &p->next;
421     }
422 }
423 
424 #endif /* Py_HAVE_NATIVE_TLS */
425