blob: 016ccbab662845da555318f2078f7ff59637b949 [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 Rossumd11bfdd1997-04-29 21:48:34 +000022#ifdef __sgi
23#ifndef HAVE_PTHREAD_H /* XXX Need to check in configure.in */
24#undef _POSIX_THREADS
25#endif
26#endif
27
Guido van Rossum49b56061998-10-01 20:42:43 +000028#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000029
Guido van Rossum1d5735e1994-08-30 08:27:36 +000030#ifndef _POSIX_THREADS
31
Guido van Rossum1984f1e1992-08-04 12:41:02 +000032#ifdef __sgi
Guido van Rossum1d5735e1994-08-30 08:27:36 +000033#define SGI_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000034#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +000035
Guido van Rossum1d5735e1994-08-30 08:27:36 +000036#ifdef HAVE_THREAD_H
37#define SOLARIS_THREADS
38#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +000039
Guido van Rossum1d5735e1994-08-30 08:27:36 +000040#if defined(sun) && !defined(SOLARIS_THREADS)
41#define SUN_LWP
42#endif
43
Sjoerd Mullender66bca321993-12-03 16:54:45 +000044#endif /* _POSIX_THREADS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000045
Guido van Rossum1984f1e1992-08-04 12:41:02 +000046
Guido van Rossum408027e1996-12-30 16:17:54 +000047#ifdef Py_DEBUG
Guido van Rossum1d5735e1994-08-30 08:27:36 +000048static int thread_debug = 0;
Jeremy Hyltonbd232892002-06-25 19:26:34 +000049#define dprintf(args) (void)((thread_debug & 1) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000050#define d2printf(args) ((thread_debug & 8) && printf args)
51#else
52#define dprintf(args)
53#define d2printf(args)
54#endif
55
Guido van Rossum1984f1e1992-08-04 12:41:02 +000056static int initialized;
57
Thomas Wouters8ec68fd2000-07-24 14:39:50 +000058static void PyThread__init_thread(void); /* Forward */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000059
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000060void PyThread_init_thread(void)
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000061{
Guido van Rossum408027e1996-12-30 16:17:54 +000062#ifdef Py_DEBUG
Sjoerd Mullender66bca321993-12-03 16:54:45 +000063 char *p = getenv("THREADDEBUG");
64
65 if (p) {
66 if (*p)
67 thread_debug = atoi(p);
68 else
69 thread_debug = 1;
70 }
Guido van Rossum408027e1996-12-30 16:17:54 +000071#endif /* Py_DEBUG */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000072 if (initialized)
73 return;
74 initialized = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +000075 dprintf(("PyThread_init_thread called\n"));
76 PyThread__init_thread();
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000077}
78
Guido van Rossum1d5735e1994-08-30 08:27:36 +000079#ifdef SGI_THREADS
80#include "thread_sgi.h"
Sjoerd Mullender66bca321993-12-03 16:54:45 +000081#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +000082
83#ifdef SOLARIS_THREADS
84#include "thread_solaris.h"
85#endif
86
87#ifdef SUN_LWP
88#include "thread_lwp.h"
89#endif
90
Guido van Rossum9e8181b2000-09-19 00:46:46 +000091#ifdef HAVE_PTH
Guido van Rossum07bd90e2000-05-08 13:41:38 +000092#include "thread_pth.h"
Martin v. Löwis70849f82003-09-20 11:13:36 +000093#undef _POSIX_THREADS
Guido van Rossum9e8181b2000-09-19 00:46:46 +000094#endif
95
Sjoerd Mullender66bca321993-12-03 16:54:45 +000096#ifdef _POSIX_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +000097#include "thread_pthread.h"
Sjoerd Mullendere8934121993-01-13 12:08:48 +000098#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +000099
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000100#ifdef C_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000101#include "thread_cthread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000102#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000103
Guido van Rossumc3f82b61995-01-17 16:29:31 +0000104#ifdef NT_THREADS
105#include "thread_nt.h"
106#endif
107
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000108#ifdef OS2_THREADS
109#include "thread_os2.h"
110#endif
111
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000112#ifdef BEOS_THREADS
113#include "thread_beos.h"
114#endif
115
Guido van Rossum2571cc81999-04-07 16:07:23 +0000116#ifdef WINCE_THREADS
117#include "thread_wince.h"
118#endif
119
Martin v. Löwis7d1cd692002-03-09 12:10:54 +0000120#ifdef PLAN9_THREADS
121#include "thread_plan9.h"
122#endif
123
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000124#ifdef ATHEOS_THREADS
125#include "thread_atheos.h"
126#endif
127
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000128/*
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000129#ifdef FOOBAR_THREADS
130#include "thread_foobar.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000131#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000132*/
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000133
134#ifndef Py_HAVE_NATIVE_TLS
135/* If the platform has not supplied a platform specific
136 TLS implementation, provide our own.
137
138 This code stolen from "thread_sgi.h", where it was the only
139 implementation of an existing Python TLS API.
140*/
141/*
142 * Per-thread data ("key") support.
143 */
144
145struct key {
146 struct key *next;
147 long id;
148 int key;
149 void *value;
150};
151
152static struct key *keyhead = NULL;
153static int nkeys = 0;
154static PyThread_type_lock keymutex = NULL;
155
156static struct key *find_key(int key, void *value)
157{
158 struct key *p;
159 long id = PyThread_get_thread_ident();
160 for (p = keyhead; p != NULL; p = p->next) {
161 if (p->id == id && p->key == key)
162 return p;
163 }
164 if (value == NULL)
165 return NULL;
166 p = (struct key *)malloc(sizeof(struct key));
167 if (p != NULL) {
168 p->id = id;
169 p->key = key;
170 p->value = value;
171 PyThread_acquire_lock(keymutex, 1);
172 p->next = keyhead;
173 keyhead = p;
174 PyThread_release_lock(keymutex);
175 }
176 return p;
177}
178
179int PyThread_create_key(void)
180{
181 if (keymutex == NULL)
182 keymutex = PyThread_allocate_lock();
183 return ++nkeys;
184}
185
186void PyThread_delete_key(int key)
187{
188 struct key *p, **q;
189 PyThread_acquire_lock(keymutex, 1);
190 q = &keyhead;
191 while ((p = *q) != NULL) {
192 if (p->key == key) {
193 *q = p->next;
194 free((void *)p);
195 /* NB This does *not* free p->value! */
196 }
197 else
198 q = &p->next;
199 }
200 PyThread_release_lock(keymutex);
201}
202
203int PyThread_set_key_value(int key, void *value)
204{
205 struct key *p = find_key(key, value);
206 if (p == NULL)
207 return -1;
208 else
209 return 0;
210}
211
212void *PyThread_get_key_value(int key)
213{
214 struct key *p = find_key(key, NULL);
215 if (p == NULL)
216 return NULL;
217 else
218 return p->value;
219}
220
221void PyThread_delete_key_value(int key)
222{
223 long id = PyThread_get_thread_ident();
224 struct key *p, **q;
225 PyThread_acquire_lock(keymutex, 1);
226 q = &keyhead;
227 while ((p = *q) != NULL) {
228 if (p->key == key && p->id == id) {
229 *q = p->next;
230 free((void *)p);
231 /* NB This does *not* free p->value! */
232 break;
233 }
234 else
235 q = &p->next;
236 }
237 PyThread_release_lock(keymutex);
238}
239
240#endif /* Py_HAVE_NATIVE_TLS */