This commit was manufactured by cvs2svn to create tag 'r234c1'.
[python/dscho.git] / Python / thread.c
blob2298b383e79e69c48701bda2fdae7de5ec8e3eb0
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. */
8 #include "Python.h"
10 #ifndef DONT_HAVE_STDIO_H
11 #include <stdio.h>
12 #endif
14 #ifdef HAVE_STDLIB_H
15 #include <stdlib.h>
16 #else
17 #ifdef Py_DEBUG
18 extern char *getenv(const char *);
19 #endif
20 #endif
22 #ifdef __DGUX
23 #define _USING_POSIX4A_DRAFT6
24 #endif
26 #ifdef __sgi
27 #ifndef HAVE_PTHREAD_H /* XXX Need to check in configure.in */
28 #undef _POSIX_THREADS
29 #endif
30 #endif
32 #include "pythread.h"
34 #ifndef _POSIX_THREADS
36 #ifdef __sgi
37 #define SGI_THREADS
38 #endif
40 #ifdef HAVE_THREAD_H
41 #define SOLARIS_THREADS
42 #endif
44 #if defined(sun) && !defined(SOLARIS_THREADS)
45 #define SUN_LWP
46 #endif
48 #if defined(__MWERKS__) && !defined(__BEOS__)
49 #define _POSIX_THREADS
50 #endif
52 #endif /* _POSIX_THREADS */
55 #ifdef Py_DEBUG
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)
59 #else
60 #define dprintf(args)
61 #define d2printf(args)
62 #endif
64 static int initialized;
66 static void PyThread__init_thread(void); /* Forward */
68 void PyThread_init_thread(void)
70 #ifdef Py_DEBUG
71 char *p = getenv("THREADDEBUG");
73 if (p) {
74 if (*p)
75 thread_debug = atoi(p);
76 else
77 thread_debug = 1;
79 #endif /* Py_DEBUG */
80 if (initialized)
81 return;
82 initialized = 1;
83 dprintf(("PyThread_init_thread called\n"));
84 PyThread__init_thread();
87 #ifdef SGI_THREADS
88 #include "thread_sgi.h"
89 #endif
91 #ifdef SOLARIS_THREADS
92 #include "thread_solaris.h"
93 #endif
95 #ifdef SUN_LWP
96 #include "thread_lwp.h"
97 #endif
99 #ifdef HAVE_PTH
100 #include "thread_pth.h"
101 #undef _POSIX_THREADS
102 #endif
104 #ifdef _POSIX_THREADS
105 #include "thread_pthread.h"
106 #endif
108 #ifdef C_THREADS
109 #include "thread_cthread.h"
110 #endif
112 #ifdef NT_THREADS
113 #include "thread_nt.h"
114 #endif
116 #ifdef OS2_THREADS
117 #include "thread_os2.h"
118 #endif
120 #ifdef BEOS_THREADS
121 #include "thread_beos.h"
122 #endif
124 #ifdef WINCE_THREADS
125 #include "thread_wince.h"
126 #endif
128 #ifdef PLAN9_THREADS
129 #include "thread_plan9.h"
130 #endif
132 #ifdef ATHEOS_THREADS
133 #include "thread_atheos.h"
134 #endif
137 #ifdef FOOBAR_THREADS
138 #include "thread_foobar.h"
139 #endif
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.
153 struct key {
154 struct key *next;
155 long id;
156 int key;
157 void *value;
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)
166 struct key *p;
167 long id = PyThread_get_thread_ident();
168 for (p = keyhead; p != NULL; p = p->next) {
169 if (p->id == id && p->key == key)
170 return p;
172 if (value == NULL)
173 return NULL;
174 p = (struct key *)malloc(sizeof(struct key));
175 if (p != NULL) {
176 p->id = id;
177 p->key = key;
178 p->value = value;
179 PyThread_acquire_lock(keymutex, 1);
180 p->next = keyhead;
181 keyhead = p;
182 PyThread_release_lock(keymutex);
184 return p;
187 int PyThread_create_key(void)
189 if (keymutex == NULL)
190 keymutex = PyThread_allocate_lock();
191 return ++nkeys;
194 void PyThread_delete_key(int key)
196 struct key *p, **q;
197 PyThread_acquire_lock(keymutex, 1);
198 q = &keyhead;
199 while ((p = *q) != NULL) {
200 if (p->key == key) {
201 *q = p->next;
202 free((void *)p);
203 /* NB This does *not* free p->value! */
205 else
206 q = &p->next;
208 PyThread_release_lock(keymutex);
211 int PyThread_set_key_value(int key, void *value)
213 struct key *p = find_key(key, value);
214 if (p == NULL)
215 return -1;
216 else
217 return 0;
220 void *PyThread_get_key_value(int key)
222 struct key *p = find_key(key, NULL);
223 if (p == NULL)
224 return NULL;
225 else
226 return p->value;
229 void PyThread_delete_key_value(int key)
231 long id = PyThread_get_thread_ident();
232 struct key *p, **q;
233 PyThread_acquire_lock(keymutex, 1);
234 q = &keyhead;
235 while ((p = *q) != NULL) {
236 if (p->key == key && p->id == id) {
237 *q = p->next;
238 free((void *)p);
239 /* NB This does *not* free p->value! */
240 break;
242 else
243 q = &p->next;
245 PyThread_release_lock(keymutex);
248 #endif /* Py_HAVE_NATIVE_TLS */