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. */
10 #ifndef DONT_HAVE_STDIO_H
18 extern char *getenv(const char *);
23 #define _USING_POSIX4A_DRAFT6
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 #if defined(__MWERKS__) && !defined(__BEOS__)
49 #define _POSIX_THREADS
52 #endif /* _POSIX_THREADS */
56 static int thread_debug
= 0;
57 #define dprintf(args) (void)((thread_debug & 1) && printf args)
58 #define d2printf(args) ((thread_debug & 8) && printf args)
61 #define d2printf(args)
64 static int initialized
;
66 static void PyThread__init_thread(void); /* Forward */
68 void PyThread_init_thread(void)
71 char *p
= getenv("THREADDEBUG");
75 thread_debug
= atoi(p
);
83 dprintf(("PyThread_init_thread called\n"));
84 PyThread__init_thread();
88 #include "thread_sgi.h"
91 #ifdef SOLARIS_THREADS
92 #include "thread_solaris.h"
96 #include "thread_lwp.h"
100 #include "thread_pth.h"
101 #undef _POSIX_THREADS
104 #ifdef _POSIX_THREADS
105 #include "thread_pthread.h"
109 #include "thread_cthread.h"
113 #include "thread_nt.h"
117 #include "thread_os2.h"
121 #include "thread_beos.h"
125 #include "thread_wince.h"
129 #include "thread_plan9.h"
132 #ifdef ATHEOS_THREADS
133 #include "thread_atheos.h"
137 #ifdef FOOBAR_THREADS
138 #include "thread_foobar.h"
142 #ifndef Py_HAVE_NATIVE_TLS
143 /* If the platform has not supplied a platform specific
144 TLS implementation, provide our own.
146 This code stolen from "thread_sgi.h", where it was the only
147 implementation of an existing Python TLS API.
150 * Per-thread data ("key") support.
160 static struct key
*keyhead
= NULL
;
161 static int nkeys
= 0;
162 static PyThread_type_lock keymutex
= NULL
;
164 static struct key
*find_key(int key
, void *value
)
167 long id
= PyThread_get_thread_ident();
168 for (p
= keyhead
; p
!= NULL
; p
= p
->next
) {
169 if (p
->id
== id
&& p
->key
== key
)
174 p
= (struct key
*)malloc(sizeof(struct key
));
179 PyThread_acquire_lock(keymutex
, 1);
182 PyThread_release_lock(keymutex
);
187 int PyThread_create_key(void)
189 if (keymutex
== NULL
)
190 keymutex
= PyThread_allocate_lock();
194 void PyThread_delete_key(int key
)
197 PyThread_acquire_lock(keymutex
, 1);
199 while ((p
= *q
) != NULL
) {
203 /* NB This does *not* free p->value! */
208 PyThread_release_lock(keymutex
);
211 int PyThread_set_key_value(int key
, void *value
)
213 struct key
*p
= find_key(key
, value
);
220 void *PyThread_get_key_value(int key
)
222 struct key
*p
= find_key(key
, NULL
);
229 void PyThread_delete_key_value(int key
)
231 long id
= PyThread_get_thread_ident();
233 PyThread_acquire_lock(keymutex
, 1);
235 while ((p
= *q
) != NULL
) {
236 if (p
->key
== key
&& p
->id
== id
) {
239 /* NB This does *not* free p->value! */
245 PyThread_release_lock(keymutex
);
248 #endif /* Py_HAVE_NATIVE_TLS */