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 | |
Matthias Klose | a2542be | 2004-08-16 11:35:51 +0000 | [diff] [blame] | 10 | |
| 11 | #ifndef _POSIX_THREADS |
| 12 | /* This means pthreads are not implemented in libc headers, hence the macro |
| 13 | not present in unistd.h. But they still can be implemented as an external |
| 14 | library (e.g. gnu pth in pthread emulation) */ |
| 15 | # ifdef HAVE_PTHREAD_H |
| 16 | # include <pthread.h> /* _POSIX_THREADS */ |
| 17 | # endif |
| 18 | #endif |
| 19 | |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 20 | #ifndef DONT_HAVE_STDIO_H |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 22 | #endif |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 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 | */ |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 145 | /* ------------------------------------------------------------------------ |
| 146 | Per-thread data ("key") support. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 147 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 148 | Use PyThread_create_key() to create a new key. This is typically shared |
| 149 | across threads. |
| 150 | |
| 151 | Use PyThread_set_key_value(thekey, value) to associate void* value with |
| 152 | thekey in the current thread. Each thread has a distinct mapping of thekey |
| 153 | to a void* value. Caution: if the current thread already has a mapping |
| 154 | for thekey, value is ignored. |
| 155 | |
| 156 | Use PyThread_get_key_value(thekey) to retrieve the void* value associated |
| 157 | with thekey in the current thread. This returns NULL if no value is |
| 158 | associated with thekey in the current thread. |
| 159 | |
| 160 | Use PyThread_delete_key_value(thekey) to forget the current thread's associated |
| 161 | value for thekey. PyThread_delete_key(thekey) forgets the values associated |
| 162 | with thekey across *all* threads. |
| 163 | |
| 164 | While some of these functions have error-return values, none set any |
| 165 | Python exception. |
| 166 | |
| 167 | None of the functions does memory management on behalf of the void* values. |
| 168 | You need to allocate and deallocate them yourself. If the void* values |
| 169 | happen to be PyObject*, these functions don't do refcount operations on |
| 170 | them either. |
| 171 | |
| 172 | The GIL does not need to be held when calling these functions; they supply |
| 173 | their own locking. This isn't true of PyThread_create_key(), though (see |
| 174 | next paragraph). |
| 175 | |
| 176 | There's a hidden assumption that PyThread_create_key() will be called before |
| 177 | any of the other functions are called. There's also a hidden assumption |
| 178 | that calls to PyThread_create_key() are serialized externally. |
| 179 | ------------------------------------------------------------------------ */ |
| 180 | |
| 181 | /* A singly-linked list of struct key objects remembers all the key->value |
| 182 | * associations. File static keyhead heads the list. keymutex is used |
| 183 | * to enforce exclusion internally. |
| 184 | */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 185 | struct key { |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 186 | /* Next record in the list, or NULL if this is the last record. */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 187 | struct key *next; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 188 | |
| 189 | /* The thread id, according to PyThread_get_thread_ident(). */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 190 | long id; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 191 | |
| 192 | /* The key and its associated value. */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 193 | int key; |
| 194 | void *value; |
| 195 | }; |
| 196 | |
| 197 | static struct key *keyhead = NULL; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 198 | static PyThread_type_lock keymutex = NULL; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 199 | static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 200 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 201 | /* Internal helper. |
| 202 | * If the current thread has a mapping for key, the appropriate struct key* |
| 203 | * is returned. NB: value is ignored in this case! |
| 204 | * If there is no mapping for key in the current thread, then: |
| 205 | * If value is NULL, NULL is returned. |
| 206 | * Else a mapping of key to value is created for the current thread, |
| 207 | * and a pointer to a new struct key* is returned; except that if |
| 208 | * malloc() can't find room for a new struct key*, NULL is returned. |
| 209 | * So when value==NULL, this acts like a pure lookup routine, and when |
| 210 | * value!=NULL, this acts like dict.setdefault(), returning an existing |
| 211 | * mapping if one exists, else creating a new mapping. |
| 212 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 213 | static struct key * |
| 214 | find_key(int key, void *value) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 215 | { |
| 216 | struct key *p; |
| 217 | long id = PyThread_get_thread_ident(); |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 218 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 219 | for (p = keyhead; p != NULL; p = p->next) { |
| 220 | if (p->id == id && p->key == key) |
| 221 | return p; |
| 222 | } |
| 223 | if (value == NULL) |
| 224 | return NULL; |
| 225 | p = (struct key *)malloc(sizeof(struct key)); |
| 226 | if (p != NULL) { |
| 227 | p->id = id; |
| 228 | p->key = key; |
| 229 | p->value = value; |
| 230 | PyThread_acquire_lock(keymutex, 1); |
| 231 | p->next = keyhead; |
| 232 | keyhead = p; |
| 233 | PyThread_release_lock(keymutex); |
| 234 | } |
| 235 | return p; |
| 236 | } |
| 237 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 238 | /* Return a new key. This must be called before any other functions in |
| 239 | * this family, and callers must arrange to serialize calls to this |
| 240 | * function. No violations are detected. |
| 241 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 242 | int |
| 243 | PyThread_create_key(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 244 | { |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 245 | /* All parts of this function are wrong if it's called by multiple |
| 246 | * threads simultaneously. |
| 247 | */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 248 | if (keymutex == NULL) |
| 249 | keymutex = PyThread_allocate_lock(); |
| 250 | return ++nkeys; |
| 251 | } |
| 252 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 253 | /* Forget the associations for key across *all* threads. */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 254 | void |
| 255 | PyThread_delete_key(int key) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 256 | { |
| 257 | struct key *p, **q; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 258 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 259 | PyThread_acquire_lock(keymutex, 1); |
| 260 | q = &keyhead; |
| 261 | while ((p = *q) != NULL) { |
| 262 | if (p->key == key) { |
| 263 | *q = p->next; |
| 264 | free((void *)p); |
| 265 | /* NB This does *not* free p->value! */ |
| 266 | } |
| 267 | else |
| 268 | q = &p->next; |
| 269 | } |
| 270 | PyThread_release_lock(keymutex); |
| 271 | } |
| 272 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 273 | /* Confusing: If the current thread has an association for key, |
| 274 | * value is ignored, and 0 is returned. Else an attempt is made to create |
| 275 | * an association of key to value for the current thread. 0 is returned |
| 276 | * if that succeeds, but -1 is returned if there's not enough memory |
| 277 | * to create the association. value must not be NULL. |
| 278 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 279 | int |
| 280 | PyThread_set_key_value(int key, void *value) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 281 | { |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 282 | struct key *p; |
| 283 | |
| 284 | assert(value != NULL); |
| 285 | p = find_key(key, value); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 286 | if (p == NULL) |
| 287 | return -1; |
| 288 | else |
| 289 | return 0; |
| 290 | } |
| 291 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 292 | /* Retrieve the value associated with key in the current thread, or NULL |
| 293 | * if the current thread doesn't have an association for key. |
| 294 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 295 | void * |
| 296 | PyThread_get_key_value(int key) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 297 | { |
| 298 | struct key *p = find_key(key, NULL); |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 299 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 300 | if (p == NULL) |
| 301 | return NULL; |
| 302 | else |
| 303 | return p->value; |
| 304 | } |
| 305 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 306 | /* Forget the current thread's association for key, if any. */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 307 | void |
| 308 | PyThread_delete_key_value(int key) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 309 | { |
| 310 | long id = PyThread_get_thread_ident(); |
| 311 | struct key *p, **q; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 312 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 313 | PyThread_acquire_lock(keymutex, 1); |
| 314 | q = &keyhead; |
| 315 | while ((p = *q) != NULL) { |
| 316 | if (p->key == key && p->id == id) { |
| 317 | *q = p->next; |
| 318 | free((void *)p); |
| 319 | /* NB This does *not* free p->value! */ |
| 320 | break; |
| 321 | } |
| 322 | else |
| 323 | q = &p->next; |
| 324 | } |
| 325 | PyThread_release_lock(keymutex); |
| 326 | } |
| 327 | |
| 328 | #endif /* Py_HAVE_NATIVE_TLS */ |