blob: 87230e02e0ae9be09d1656d416cce1fac2e6bd36 [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"
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000101#endif
102
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000103#ifdef _POSIX_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000104#include "thread_pthread.h"
Sjoerd Mullendere8934121993-01-13 12:08:48 +0000105#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000106
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000107#ifdef C_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000108#include "thread_cthread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000109#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000110
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000111#ifdef NT_THREADS
112#include "thread_nt.h"
113#endif
114
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000115#ifdef OS2_THREADS
116#include "thread_os2.h"
117#endif
118
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000119#ifdef BEOS_THREADS
120#include "thread_beos.h"
121#endif
122
Guido van Rossum2571cc81999-04-07 16:07:23 +0000123#ifdef WINCE_THREADS
124#include "thread_wince.h"
125#endif
126
Martin v. Löwis7d1cd692002-03-09 12:10:54 +0000127#ifdef PLAN9_THREADS
128#include "thread_plan9.h"
129#endif
130
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000131#ifdef ATHEOS_THREADS
132#include "thread_atheos.h"
133#endif
134
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000135/*
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000136#ifdef FOOBAR_THREADS
137#include "thread_foobar.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000138#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000139*/
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000140
141#ifndef Py_HAVE_NATIVE_TLS
142/* If the platform has not supplied a platform specific
143 TLS implementation, provide our own.
144
145 This code stolen from "thread_sgi.h", where it was the only
146 implementation of an existing Python TLS API.
147*/
148/*
149 * Per-thread data ("key") support.
150 */
151
152struct key {
153 struct key *next;
154 long id;
155 int key;
156 void *value;
157};
158
159static struct key *keyhead = NULL;
160static int nkeys = 0;
161static PyThread_type_lock keymutex = NULL;
162
163static struct key *find_key(int key, void *value)
164{
165 struct key *p;
166 long id = PyThread_get_thread_ident();
167 for (p = keyhead; p != NULL; p = p->next) {
168 if (p->id == id && p->key == key)
169 return p;
170 }
171 if (value == NULL)
172 return NULL;
173 p = (struct key *)malloc(sizeof(struct key));
174 if (p != NULL) {
175 p->id = id;
176 p->key = key;
177 p->value = value;
178 PyThread_acquire_lock(keymutex, 1);
179 p->next = keyhead;
180 keyhead = p;
181 PyThread_release_lock(keymutex);
182 }
183 return p;
184}
185
186int PyThread_create_key(void)
187{
188 if (keymutex == NULL)
189 keymutex = PyThread_allocate_lock();
190 return ++nkeys;
191}
192
193void PyThread_delete_key(int key)
194{
195 struct key *p, **q;
196 PyThread_acquire_lock(keymutex, 1);
197 q = &keyhead;
198 while ((p = *q) != NULL) {
199 if (p->key == key) {
200 *q = p->next;
201 free((void *)p);
202 /* NB This does *not* free p->value! */
203 }
204 else
205 q = &p->next;
206 }
207 PyThread_release_lock(keymutex);
208}
209
210int PyThread_set_key_value(int key, void *value)
211{
212 struct key *p = find_key(key, value);
213 if (p == NULL)
214 return -1;
215 else
216 return 0;
217}
218
219void *PyThread_get_key_value(int key)
220{
221 struct key *p = find_key(key, NULL);
222 if (p == NULL)
223 return NULL;
224 else
225 return p->value;
226}
227
228void PyThread_delete_key_value(int key)
229{
230 long id = PyThread_get_thread_ident();
231 struct key *p, **q;
232 PyThread_acquire_lock(keymutex, 1);
233 q = &keyhead;
234 while ((p = *q) != NULL) {
235 if (p->key == key && p->id == id) {
236 *q = p->next;
237 free((void *)p);
238 /* NB This does *not* free p->value! */
239 break;
240 }
241 else
242 q = &p->next;
243 }
244 PyThread_release_lock(keymutex);
245}
246
247#endif /* Py_HAVE_NATIVE_TLS */