blob: 2298b383e79e69c48701bda2fdae7de5ec8e3eb0 [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
Guido van Rossum2571cc81999-04-07 16:07:23 +000010#ifndef DONT_HAVE_STDIO_H
Guido van Rossum1d5735e1994-08-30 08:27:36 +000011#include <stdio.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +000012#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +000013
14#ifdef HAVE_STDLIB_H
15#include <stdlib.h>
16#else
Guido van Rossum2571cc81999-04-07 16:07:23 +000017#ifdef Py_DEBUG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000018extern char *getenv(const char *);
Guido van Rossum1d5735e1994-08-30 08:27:36 +000019#endif
Guido van Rossum2571cc81999-04-07 16:07:23 +000020#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +000021
Guido van Rossum64f91051997-05-22 20:41:59 +000022#ifdef __DGUX
23#define _USING_POSIX4A_DRAFT6
24#endif
25
Guido van Rossumd11bfdd1997-04-29 21:48:34 +000026#ifdef __sgi
27#ifndef HAVE_PTHREAD_H /* XXX Need to check in configure.in */
28#undef _POSIX_THREADS
29#endif
30#endif
31
Guido van Rossum49b56061998-10-01 20:42:43 +000032#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000033
Guido van Rossum1d5735e1994-08-30 08:27:36 +000034#ifndef _POSIX_THREADS
35
Guido van Rossum1984f1e1992-08-04 12:41:02 +000036#ifdef __sgi
Guido van Rossum1d5735e1994-08-30 08:27:36 +000037#define SGI_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000038#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +000039
Guido van Rossum1d5735e1994-08-30 08:27:36 +000040#ifdef HAVE_THREAD_H
41#define SOLARIS_THREADS
42#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +000043
Guido van Rossum1d5735e1994-08-30 08:27:36 +000044#if defined(sun) && !defined(SOLARIS_THREADS)
45#define SUN_LWP
46#endif
47
Fred Drakea6c2eb52000-10-06 15:48:38 +000048#if defined(__MWERKS__) && !defined(__BEOS__)
Guido van Rossum095249f2000-04-24 15:06:51 +000049#define _POSIX_THREADS
50#endif
51
Sjoerd Mullender66bca321993-12-03 16:54:45 +000052#endif /* _POSIX_THREADS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000053
Guido van Rossum1984f1e1992-08-04 12:41:02 +000054
Guido van Rossum408027e1996-12-30 16:17:54 +000055#ifdef Py_DEBUG
Guido van Rossum1d5735e1994-08-30 08:27:36 +000056static int thread_debug = 0;
Jeremy Hyltonbd232892002-06-25 19:26:34 +000057#define dprintf(args) (void)((thread_debug & 1) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000058#define d2printf(args) ((thread_debug & 8) && printf args)
59#else
60#define dprintf(args)
61#define d2printf(args)
62#endif
63
Guido van Rossum1984f1e1992-08-04 12:41:02 +000064static int initialized;
65
Thomas Wouters8ec68fd2000-07-24 14:39:50 +000066static void PyThread__init_thread(void); /* Forward */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000067
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000068void PyThread_init_thread(void)
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000069{
Guido van Rossum408027e1996-12-30 16:17:54 +000070#ifdef Py_DEBUG
Sjoerd Mullender66bca321993-12-03 16:54:45 +000071 char *p = getenv("THREADDEBUG");
72
73 if (p) {
74 if (*p)
75 thread_debug = atoi(p);
76 else
77 thread_debug = 1;
78 }
Guido van Rossum408027e1996-12-30 16:17:54 +000079#endif /* Py_DEBUG */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000080 if (initialized)
81 return;
82 initialized = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +000083 dprintf(("PyThread_init_thread called\n"));
84 PyThread__init_thread();
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000085}
86
Guido van Rossum1d5735e1994-08-30 08:27:36 +000087#ifdef SGI_THREADS
88#include "thread_sgi.h"
Sjoerd Mullender66bca321993-12-03 16:54:45 +000089#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +000090
91#ifdef SOLARIS_THREADS
92#include "thread_solaris.h"
93#endif
94
95#ifdef SUN_LWP
96#include "thread_lwp.h"
97#endif
98
Guido van Rossum9e8181b2000-09-19 00:46:46 +000099#ifdef HAVE_PTH
Guido van Rossum07bd90e2000-05-08 13:41:38 +0000100#include "thread_pth.h"
Martin v. Löwis70849f82003-09-20 11:13:36 +0000101#undef _POSIX_THREADS
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000102#endif
103
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000104#ifdef _POSIX_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000105#include "thread_pthread.h"
Sjoerd Mullendere8934121993-01-13 12:08:48 +0000106#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000107
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000108#ifdef C_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000109#include "thread_cthread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000110#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000111
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000112#ifdef NT_THREADS
113#include "thread_nt.h"
114#endif
115
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000116#ifdef OS2_THREADS
117#include "thread_os2.h"
118#endif
119
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000120#ifdef BEOS_THREADS
121#include "thread_beos.h"
122#endif
123
Guido van Rossum2571cc81999-04-07 16:07:23 +0000124#ifdef WINCE_THREADS
125#include "thread_wince.h"
126#endif
127
Martin v. Löwis7d1cd692002-03-09 12:10:54 +0000128#ifdef PLAN9_THREADS
129#include "thread_plan9.h"
130#endif
131
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000132#ifdef ATHEOS_THREADS
133#include "thread_atheos.h"
134#endif
135
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000136/*
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000137#ifdef FOOBAR_THREADS
138#include "thread_foobar.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000139#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000140*/
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000141
142#ifndef Py_HAVE_NATIVE_TLS
143/* If the platform has not supplied a platform specific
144 TLS implementation, provide our own.
145
146 This code stolen from "thread_sgi.h", where it was the only
147 implementation of an existing Python TLS API.
148*/
149/*
150 * Per-thread data ("key") support.
151 */
152
153struct key {
154 struct key *next;
155 long id;
156 int key;
157 void *value;
158};
159
160static struct key *keyhead = NULL;
161static int nkeys = 0;
162static PyThread_type_lock keymutex = NULL;
163
164static struct key *find_key(int key, void *value)
165{
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;
171 }
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);
183 }
184 return p;
185}
186
187int PyThread_create_key(void)
188{
189 if (keymutex == NULL)
190 keymutex = PyThread_allocate_lock();
191 return ++nkeys;
192}
193
194void PyThread_delete_key(int key)
195{
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! */
204 }
205 else
206 q = &p->next;
207 }
208 PyThread_release_lock(keymutex);
209}
210
211int PyThread_set_key_value(int key, void *value)
212{
213 struct key *p = find_key(key, value);
214 if (p == NULL)
215 return -1;
216 else
217 return 0;
218}
219
220void *PyThread_get_key_value(int key)
221{
222 struct key *p = find_key(key, NULL);
223 if (p == NULL)
224 return NULL;
225 else
226 return p->value;
227}
228
229void PyThread_delete_key_value(int key)
230{
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;
241 }
242 else
243 q = &p->next;
244 }
245 PyThread_release_lock(keymutex);
246}
247
248#endif /* Py_HAVE_NATIVE_TLS */