Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 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. */ |
| 7 | |
Martin v. Löwis | cdc4451 | 2002-01-12 11:05:12 +0000 | [diff] [blame] | 8 | #include "Python.h" |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 10 | #ifndef DONT_HAVE_STDIO_H |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 12 | #endif |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 13 | |
| 14 | #ifdef HAVE_STDLIB_H |
| 15 | #include <stdlib.h> |
| 16 | #else |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 17 | #ifdef Py_DEBUG |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 18 | extern char *getenv(const char *); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 19 | #endif |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 20 | #endif |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | 64f9105 | 1997-05-22 20:41:59 +0000 | [diff] [blame] | 22 | #ifdef __DGUX |
| 23 | #define _USING_POSIX4A_DRAFT6 |
| 24 | #endif |
| 25 | |
Guido van Rossum | d11bfdd | 1997-04-29 21:48:34 +0000 | [diff] [blame] | 26 | #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 Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 32 | #include "pythread.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 34 | #ifndef _POSIX_THREADS |
| 35 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 36 | #ifdef __sgi |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 37 | #define SGI_THREADS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 38 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 40 | #ifdef HAVE_THREAD_H |
| 41 | #define SOLARIS_THREADS |
| 42 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 44 | #if defined(sun) && !defined(SOLARIS_THREADS) |
| 45 | #define SUN_LWP |
| 46 | #endif |
| 47 | |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 48 | #endif /* _POSIX_THREADS */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 51 | #ifdef Py_DEBUG |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 52 | static int thread_debug = 0; |
Jeremy Hylton | bd23289 | 2002-06-25 19:26:34 +0000 | [diff] [blame] | 53 | #define dprintf(args) (void)((thread_debug & 1) && printf args) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 54 | #define d2printf(args) ((thread_debug & 8) && printf args) |
| 55 | #else |
| 56 | #define dprintf(args) |
| 57 | #define d2printf(args) |
| 58 | #endif |
| 59 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 60 | static int initialized; |
| 61 | |
Thomas Wouters | 8ec68fd | 2000-07-24 14:39:50 +0000 | [diff] [blame] | 62 | static void PyThread__init_thread(void); /* Forward */ |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 63 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 64 | void PyThread_init_thread(void) |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 65 | { |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 66 | #ifdef Py_DEBUG |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 67 | char *p = getenv("THREADDEBUG"); |
| 68 | |
| 69 | if (p) { |
| 70 | if (*p) |
| 71 | thread_debug = atoi(p); |
| 72 | else |
| 73 | thread_debug = 1; |
| 74 | } |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 75 | #endif /* Py_DEBUG */ |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 76 | if (initialized) |
| 77 | return; |
| 78 | initialized = 1; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 79 | dprintf(("PyThread_init_thread called\n")); |
| 80 | PyThread__init_thread(); |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 83 | #ifdef SGI_THREADS |
| 84 | #include "thread_sgi.h" |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 85 | #endif |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 86 | |
| 87 | #ifdef SOLARIS_THREADS |
| 88 | #include "thread_solaris.h" |
| 89 | #endif |
| 90 | |
| 91 | #ifdef SUN_LWP |
| 92 | #include "thread_lwp.h" |
| 93 | #endif |
| 94 | |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 95 | #ifdef HAVE_PTH |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 96 | #include "thread_pth.h" |
Martin v. Löwis | 70849f8 | 2003-09-20 11:13:36 +0000 | [diff] [blame] | 97 | #undef _POSIX_THREADS |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 98 | #endif |
| 99 | |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 100 | #ifdef _POSIX_THREADS |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 101 | #include "thread_pthread.h" |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 102 | #endif |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 103 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 104 | #ifdef C_THREADS |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 105 | #include "thread_cthread.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 106 | #endif |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 108 | #ifdef NT_THREADS |
| 109 | #include "thread_nt.h" |
| 110 | #endif |
| 111 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 112 | #ifdef OS2_THREADS |
| 113 | #include "thread_os2.h" |
| 114 | #endif |
| 115 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 116 | #ifdef BEOS_THREADS |
| 117 | #include "thread_beos.h" |
| 118 | #endif |
| 119 | |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 120 | #ifdef WINCE_THREADS |
| 121 | #include "thread_wince.h" |
| 122 | #endif |
| 123 | |
Martin v. Löwis | 7d1cd69 | 2002-03-09 12:10:54 +0000 | [diff] [blame] | 124 | #ifdef PLAN9_THREADS |
| 125 | #include "thread_plan9.h" |
| 126 | #endif |
| 127 | |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 128 | #ifdef ATHEOS_THREADS |
| 129 | #include "thread_atheos.h" |
| 130 | #endif |
| 131 | |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 132 | /* |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 133 | #ifdef FOOBAR_THREADS |
| 134 | #include "thread_foobar.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 135 | #endif |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 136 | */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 137 | |
| 138 | #ifndef Py_HAVE_NATIVE_TLS |
| 139 | /* If the platform has not supplied a platform specific |
| 140 | TLS implementation, provide our own. |
| 141 | |
| 142 | This code stolen from "thread_sgi.h", where it was the only |
| 143 | implementation of an existing Python TLS API. |
| 144 | */ |
| 145 | /* |
| 146 | * Per-thread data ("key") support. |
| 147 | */ |
| 148 | |
| 149 | struct key { |
| 150 | struct key *next; |
| 151 | long id; |
| 152 | int key; |
| 153 | void *value; |
| 154 | }; |
| 155 | |
| 156 | static struct key *keyhead = NULL; |
| 157 | static int nkeys = 0; |
| 158 | static PyThread_type_lock keymutex = NULL; |
| 159 | |
| 160 | static struct key *find_key(int key, void *value) |
| 161 | { |
| 162 | struct key *p; |
| 163 | long id = PyThread_get_thread_ident(); |
| 164 | for (p = keyhead; p != NULL; p = p->next) { |
| 165 | if (p->id == id && p->key == key) |
| 166 | return p; |
| 167 | } |
| 168 | if (value == NULL) |
| 169 | return NULL; |
| 170 | p = (struct key *)malloc(sizeof(struct key)); |
| 171 | if (p != NULL) { |
| 172 | p->id = id; |
| 173 | p->key = key; |
| 174 | p->value = value; |
| 175 | PyThread_acquire_lock(keymutex, 1); |
| 176 | p->next = keyhead; |
| 177 | keyhead = p; |
| 178 | PyThread_release_lock(keymutex); |
| 179 | } |
| 180 | return p; |
| 181 | } |
| 182 | |
| 183 | int PyThread_create_key(void) |
| 184 | { |
| 185 | if (keymutex == NULL) |
| 186 | keymutex = PyThread_allocate_lock(); |
| 187 | return ++nkeys; |
| 188 | } |
| 189 | |
| 190 | void PyThread_delete_key(int key) |
| 191 | { |
| 192 | struct key *p, **q; |
| 193 | PyThread_acquire_lock(keymutex, 1); |
| 194 | q = &keyhead; |
| 195 | while ((p = *q) != NULL) { |
| 196 | if (p->key == key) { |
| 197 | *q = p->next; |
| 198 | free((void *)p); |
| 199 | /* NB This does *not* free p->value! */ |
| 200 | } |
| 201 | else |
| 202 | q = &p->next; |
| 203 | } |
| 204 | PyThread_release_lock(keymutex); |
| 205 | } |
| 206 | |
| 207 | int PyThread_set_key_value(int key, void *value) |
| 208 | { |
| 209 | struct key *p = find_key(key, value); |
| 210 | if (p == NULL) |
| 211 | return -1; |
| 212 | else |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | void *PyThread_get_key_value(int key) |
| 217 | { |
| 218 | struct key *p = find_key(key, NULL); |
| 219 | if (p == NULL) |
| 220 | return NULL; |
| 221 | else |
| 222 | return p->value; |
| 223 | } |
| 224 | |
| 225 | void PyThread_delete_key_value(int key) |
| 226 | { |
| 227 | long id = PyThread_get_thread_ident(); |
| 228 | struct key *p, **q; |
| 229 | PyThread_acquire_lock(keymutex, 1); |
| 230 | q = &keyhead; |
| 231 | while ((p = *q) != NULL) { |
| 232 | if (p->key == key && p->id == id) { |
| 233 | *q = p->next; |
| 234 | free((void *)p); |
| 235 | /* NB This does *not* free p->value! */ |
| 236 | break; |
| 237 | } |
| 238 | else |
| 239 | q = &p->next; |
| 240 | } |
| 241 | PyThread_release_lock(keymutex); |
| 242 | } |
| 243 | |
| 244 | #endif /* Py_HAVE_NATIVE_TLS */ |