blob: 8bc958f9cd91c22cd05e09fb95f9fecd808a8222 [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
Guido van Rossum1d5735e1994-08-30 08:27:36 +000014#include <stdlib.h>
Guido van Rossum1d5735e1994-08-30 08:27:36 +000015
Guido van Rossumd11bfdd1997-04-29 21:48:34 +000016#ifdef __sgi
17#ifndef HAVE_PTHREAD_H /* XXX Need to check in configure.in */
18#undef _POSIX_THREADS
19#endif
20#endif
21
Guido van Rossum49b56061998-10-01 20:42:43 +000022#include "pythread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000023
Guido van Rossum1d5735e1994-08-30 08:27:36 +000024#ifndef _POSIX_THREADS
25
Guido van Rossum1984f1e1992-08-04 12:41:02 +000026#ifdef __sgi
Guido van Rossum1d5735e1994-08-30 08:27:36 +000027#define SGI_THREADS
Guido van Rossum1984f1e1992-08-04 12:41:02 +000028#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +000029
Guido van Rossum1d5735e1994-08-30 08:27:36 +000030#ifdef HAVE_THREAD_H
31#define SOLARIS_THREADS
32#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +000033
Guido van Rossum1d5735e1994-08-30 08:27:36 +000034#if defined(sun) && !defined(SOLARIS_THREADS)
35#define SUN_LWP
36#endif
37
Sjoerd Mullender66bca321993-12-03 16:54:45 +000038#endif /* _POSIX_THREADS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000039
Guido van Rossum1984f1e1992-08-04 12:41:02 +000040
Guido van Rossum408027e1996-12-30 16:17:54 +000041#ifdef Py_DEBUG
Guido van Rossum1d5735e1994-08-30 08:27:36 +000042static int thread_debug = 0;
Jeremy Hyltonbd232892002-06-25 19:26:34 +000043#define dprintf(args) (void)((thread_debug & 1) && printf args)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000044#define d2printf(args) ((thread_debug & 8) && printf args)
45#else
46#define dprintf(args)
47#define d2printf(args)
48#endif
49
Guido van Rossum1984f1e1992-08-04 12:41:02 +000050static int initialized;
51
Thomas Wouters8ec68fd2000-07-24 14:39:50 +000052static void PyThread__init_thread(void); /* Forward */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000053
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000054void PyThread_init_thread(void)
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000055{
Guido van Rossum408027e1996-12-30 16:17:54 +000056#ifdef Py_DEBUG
Sjoerd Mullender66bca321993-12-03 16:54:45 +000057 char *p = getenv("THREADDEBUG");
58
59 if (p) {
60 if (*p)
61 thread_debug = atoi(p);
62 else
63 thread_debug = 1;
64 }
Guido van Rossum408027e1996-12-30 16:17:54 +000065#endif /* Py_DEBUG */
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000066 if (initialized)
67 return;
68 initialized = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +000069 dprintf(("PyThread_init_thread called\n"));
70 PyThread__init_thread();
Sjoerd Mullenderaee8bc11992-09-02 11:25:37 +000071}
72
Guido van Rossum1d5735e1994-08-30 08:27:36 +000073#ifdef SGI_THREADS
74#include "thread_sgi.h"
Sjoerd Mullender66bca321993-12-03 16:54:45 +000075#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +000076
77#ifdef SOLARIS_THREADS
78#include "thread_solaris.h"
79#endif
80
81#ifdef SUN_LWP
82#include "thread_lwp.h"
83#endif
84
Guido van Rossum9e8181b2000-09-19 00:46:46 +000085#ifdef HAVE_PTH
Guido van Rossum07bd90e2000-05-08 13:41:38 +000086#include "thread_pth.h"
Martin v. Löwis70849f82003-09-20 11:13:36 +000087#undef _POSIX_THREADS
Guido van Rossum9e8181b2000-09-19 00:46:46 +000088#endif
89
Sjoerd Mullender66bca321993-12-03 16:54:45 +000090#ifdef _POSIX_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +000091#include "thread_pthread.h"
Sjoerd Mullendere8934121993-01-13 12:08:48 +000092#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +000093
Guido van Rossum1984f1e1992-08-04 12:41:02 +000094#ifdef C_THREADS
Guido van Rossum1d5735e1994-08-30 08:27:36 +000095#include "thread_cthread.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000096#endif
Guido van Rossumf9f2e821992-08-17 08:59:08 +000097
Guido van Rossumc3f82b61995-01-17 16:29:31 +000098#ifdef NT_THREADS
99#include "thread_nt.h"
100#endif
101
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#ifdef OS2_THREADS
103#include "thread_os2.h"
104#endif
105
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000106#ifdef BEOS_THREADS
107#include "thread_beos.h"
108#endif
109
Guido van Rossum2571cc81999-04-07 16:07:23 +0000110#ifdef WINCE_THREADS
111#include "thread_wince.h"
112#endif
113
Martin v. Löwis7d1cd692002-03-09 12:10:54 +0000114#ifdef PLAN9_THREADS
115#include "thread_plan9.h"
116#endif
117
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000118#ifdef ATHEOS_THREADS
119#include "thread_atheos.h"
120#endif
121
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000122/*
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000123#ifdef FOOBAR_THREADS
124#include "thread_foobar.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000125#endif
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000126*/
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000127
128#ifndef Py_HAVE_NATIVE_TLS
129/* If the platform has not supplied a platform specific
130 TLS implementation, provide our own.
131
132 This code stolen from "thread_sgi.h", where it was the only
133 implementation of an existing Python TLS API.
134*/
135/*
136 * Per-thread data ("key") support.
137 */
138
139struct key {
140 struct key *next;
141 long id;
142 int key;
143 void *value;
144};
145
146static struct key *keyhead = NULL;
147static int nkeys = 0;
148static PyThread_type_lock keymutex = NULL;
149
150static struct key *find_key(int key, void *value)
151{
152 struct key *p;
153 long id = PyThread_get_thread_ident();
154 for (p = keyhead; p != NULL; p = p->next) {
155 if (p->id == id && p->key == key)
156 return p;
157 }
158 if (value == NULL)
159 return NULL;
160 p = (struct key *)malloc(sizeof(struct key));
161 if (p != NULL) {
162 p->id = id;
163 p->key = key;
164 p->value = value;
165 PyThread_acquire_lock(keymutex, 1);
166 p->next = keyhead;
167 keyhead = p;
168 PyThread_release_lock(keymutex);
169 }
170 return p;
171}
172
173int PyThread_create_key(void)
174{
175 if (keymutex == NULL)
176 keymutex = PyThread_allocate_lock();
177 return ++nkeys;
178}
179
180void PyThread_delete_key(int key)
181{
182 struct key *p, **q;
183 PyThread_acquire_lock(keymutex, 1);
184 q = &keyhead;
185 while ((p = *q) != NULL) {
186 if (p->key == key) {
187 *q = p->next;
188 free((void *)p);
189 /* NB This does *not* free p->value! */
190 }
191 else
192 q = &p->next;
193 }
194 PyThread_release_lock(keymutex);
195}
196
197int PyThread_set_key_value(int key, void *value)
198{
199 struct key *p = find_key(key, value);
200 if (p == NULL)
201 return -1;
202 else
203 return 0;
204}
205
206void *PyThread_get_key_value(int key)
207{
208 struct key *p = find_key(key, NULL);
209 if (p == NULL)
210 return NULL;
211 else
212 return p->value;
213}
214
215void PyThread_delete_key_value(int key)
216{
217 long id = PyThread_get_thread_ident();
218 struct key *p, **q;
219 PyThread_acquire_lock(keymutex, 1);
220 q = &keyhead;
221 while ((p = *q) != NULL) {
222 if (p->key == key && p->id == id) {
223 *q = p->next;
224 free((void *)p);
225 /* NB This does *not* free p->value! */
226 break;
227 }
228 else
229 q = &p->next;
230 }
231 PyThread_release_lock(keymutex);
232}
233
234#endif /* Py_HAVE_NATIVE_TLS */