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 | |
Guido van Rossum | 539c662 | 2005-09-14 17:49:54 +0000 | [diff] [blame] | 48 | /* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then |
| 49 | enough of the Posix threads package is implimented to support python |
| 50 | threads. |
| 51 | |
| 52 | This is valid for HP-UX 11.23 running on an ia64 system. If needed, add |
| 53 | a check of __ia64 to verify that we're running on a ia64 system instead |
| 54 | of a pa-risc system. |
| 55 | */ |
| 56 | #ifdef __hpux |
| 57 | #ifdef _SC_THREADS |
| 58 | #define _POSIX_THREADS |
| 59 | #endif |
| 60 | #endif |
| 61 | |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 62 | #endif /* _POSIX_THREADS */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 65 | #ifdef Py_DEBUG |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 66 | static int thread_debug = 0; |
Jeremy Hylton | bd23289 | 2002-06-25 19:26:34 +0000 | [diff] [blame] | 67 | #define dprintf(args) (void)((thread_debug & 1) && printf args) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 68 | #define d2printf(args) ((thread_debug & 8) && printf args) |
| 69 | #else |
| 70 | #define dprintf(args) |
| 71 | #define d2printf(args) |
| 72 | #endif |
| 73 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 74 | static int initialized; |
| 75 | |
Thomas Wouters | 8ec68fd | 2000-07-24 14:39:50 +0000 | [diff] [blame] | 76 | static void PyThread__init_thread(void); /* Forward */ |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 77 | |
Andrew MacIntyre | 63f0db6 | 2006-06-04 12:59:59 +0000 | [diff] [blame] | 78 | void |
| 79 | PyThread_init_thread(void) |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 80 | { |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 81 | #ifdef Py_DEBUG |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 82 | char *p = getenv("THREADDEBUG"); |
| 83 | |
| 84 | if (p) { |
| 85 | if (*p) |
| 86 | thread_debug = atoi(p); |
| 87 | else |
| 88 | thread_debug = 1; |
| 89 | } |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 90 | #endif /* Py_DEBUG */ |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 91 | if (initialized) |
| 92 | return; |
| 93 | initialized = 1; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 94 | dprintf(("PyThread_init_thread called\n")); |
| 95 | PyThread__init_thread(); |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 98 | #ifdef SGI_THREADS |
| 99 | #include "thread_sgi.h" |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 100 | #endif |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 101 | |
| 102 | #ifdef SOLARIS_THREADS |
| 103 | #include "thread_solaris.h" |
| 104 | #endif |
| 105 | |
| 106 | #ifdef SUN_LWP |
| 107 | #include "thread_lwp.h" |
| 108 | #endif |
| 109 | |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 110 | #ifdef HAVE_PTH |
Guido van Rossum | 07bd90e | 2000-05-08 13:41:38 +0000 | [diff] [blame] | 111 | #include "thread_pth.h" |
Martin v. Löwis | 70849f8 | 2003-09-20 11:13:36 +0000 | [diff] [blame] | 112 | #undef _POSIX_THREADS |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 113 | #endif |
| 114 | |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 115 | #ifdef _POSIX_THREADS |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 116 | #include "thread_pthread.h" |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 117 | #endif |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 119 | #ifdef C_THREADS |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 120 | #include "thread_cthread.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 121 | #endif |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 123 | #ifdef NT_THREADS |
| 124 | #include "thread_nt.h" |
| 125 | #endif |
| 126 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 127 | #ifdef OS2_THREADS |
| 128 | #include "thread_os2.h" |
| 129 | #endif |
| 130 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 131 | #ifdef BEOS_THREADS |
| 132 | #include "thread_beos.h" |
| 133 | #endif |
| 134 | |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 135 | #ifdef WINCE_THREADS |
| 136 | #include "thread_wince.h" |
| 137 | #endif |
| 138 | |
Martin v. Löwis | 7d1cd69 | 2002-03-09 12:10:54 +0000 | [diff] [blame] | 139 | #ifdef PLAN9_THREADS |
| 140 | #include "thread_plan9.h" |
| 141 | #endif |
| 142 | |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 143 | #ifdef ATHEOS_THREADS |
| 144 | #include "thread_atheos.h" |
| 145 | #endif |
| 146 | |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 147 | /* |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 148 | #ifdef FOOBAR_THREADS |
| 149 | #include "thread_foobar.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 150 | #endif |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 151 | */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 152 | |
| 153 | #ifndef Py_HAVE_NATIVE_TLS |
| 154 | /* If the platform has not supplied a platform specific |
| 155 | TLS implementation, provide our own. |
| 156 | |
| 157 | This code stolen from "thread_sgi.h", where it was the only |
| 158 | implementation of an existing Python TLS API. |
| 159 | */ |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 160 | /* ------------------------------------------------------------------------ |
| 161 | Per-thread data ("key") support. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 162 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 163 | Use PyThread_create_key() to create a new key. This is typically shared |
| 164 | across threads. |
| 165 | |
| 166 | Use PyThread_set_key_value(thekey, value) to associate void* value with |
| 167 | thekey in the current thread. Each thread has a distinct mapping of thekey |
| 168 | to a void* value. Caution: if the current thread already has a mapping |
| 169 | for thekey, value is ignored. |
| 170 | |
| 171 | Use PyThread_get_key_value(thekey) to retrieve the void* value associated |
| 172 | with thekey in the current thread. This returns NULL if no value is |
| 173 | associated with thekey in the current thread. |
| 174 | |
| 175 | Use PyThread_delete_key_value(thekey) to forget the current thread's associated |
| 176 | value for thekey. PyThread_delete_key(thekey) forgets the values associated |
| 177 | with thekey across *all* threads. |
| 178 | |
| 179 | While some of these functions have error-return values, none set any |
| 180 | Python exception. |
| 181 | |
| 182 | None of the functions does memory management on behalf of the void* values. |
| 183 | You need to allocate and deallocate them yourself. If the void* values |
| 184 | happen to be PyObject*, these functions don't do refcount operations on |
| 185 | them either. |
| 186 | |
| 187 | The GIL does not need to be held when calling these functions; they supply |
| 188 | their own locking. This isn't true of PyThread_create_key(), though (see |
| 189 | next paragraph). |
| 190 | |
| 191 | There's a hidden assumption that PyThread_create_key() will be called before |
| 192 | any of the other functions are called. There's also a hidden assumption |
| 193 | that calls to PyThread_create_key() are serialized externally. |
| 194 | ------------------------------------------------------------------------ */ |
| 195 | |
| 196 | /* A singly-linked list of struct key objects remembers all the key->value |
| 197 | * associations. File static keyhead heads the list. keymutex is used |
| 198 | * to enforce exclusion internally. |
| 199 | */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 200 | struct key { |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 201 | /* 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] | 202 | struct key *next; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 203 | |
| 204 | /* The thread id, according to PyThread_get_thread_ident(). */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 205 | long id; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 206 | |
| 207 | /* The key and its associated value. */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 208 | int key; |
| 209 | void *value; |
| 210 | }; |
| 211 | |
| 212 | static struct key *keyhead = NULL; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 213 | static PyThread_type_lock keymutex = NULL; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 214 | static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 215 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 216 | /* Internal helper. |
| 217 | * If the current thread has a mapping for key, the appropriate struct key* |
| 218 | * is returned. NB: value is ignored in this case! |
| 219 | * If there is no mapping for key in the current thread, then: |
| 220 | * If value is NULL, NULL is returned. |
| 221 | * Else a mapping of key to value is created for the current thread, |
| 222 | * and a pointer to a new struct key* is returned; except that if |
| 223 | * malloc() can't find room for a new struct key*, NULL is returned. |
| 224 | * So when value==NULL, this acts like a pure lookup routine, and when |
| 225 | * value!=NULL, this acts like dict.setdefault(), returning an existing |
| 226 | * mapping if one exists, else creating a new mapping. |
Tim Peters | 263091e | 2004-10-10 01:58:44 +0000 | [diff] [blame] | 227 | * |
| 228 | * Caution: this used to be too clever, trying to hold keymutex only |
| 229 | * around the "p->next = keyhead; keyhead = p" pair. That allowed |
| 230 | * another thread to mutate the list, via key deletion, concurrent with |
| 231 | * find_key() crawling over the list. Hilarity ensued. For example, when |
| 232 | * the for-loop here does "p = p->next", p could end up pointing at a |
| 233 | * record that PyThread_delete_key_value() was concurrently free()'ing. |
| 234 | * That could lead to anything, from failing to find a key that exists, to |
| 235 | * segfaults. Now we lock the whole routine. |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 236 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 237 | static struct key * |
| 238 | find_key(int key, void *value) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 239 | { |
| 240 | struct key *p; |
| 241 | long id = PyThread_get_thread_ident(); |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 242 | |
Tim Peters | 263091e | 2004-10-10 01:58:44 +0000 | [diff] [blame] | 243 | PyThread_acquire_lock(keymutex, 1); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 244 | for (p = keyhead; p != NULL; p = p->next) { |
| 245 | if (p->id == id && p->key == key) |
Tim Peters | 263091e | 2004-10-10 01:58:44 +0000 | [diff] [blame] | 246 | goto Done; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 247 | } |
Tim Peters | 263091e | 2004-10-10 01:58:44 +0000 | [diff] [blame] | 248 | if (value == NULL) { |
| 249 | assert(p == NULL); |
| 250 | goto Done; |
| 251 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 252 | p = (struct key *)malloc(sizeof(struct key)); |
| 253 | if (p != NULL) { |
| 254 | p->id = id; |
| 255 | p->key = key; |
| 256 | p->value = value; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 257 | p->next = keyhead; |
| 258 | keyhead = p; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 259 | } |
Tim Peters | 263091e | 2004-10-10 01:58:44 +0000 | [diff] [blame] | 260 | Done: |
| 261 | PyThread_release_lock(keymutex); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 262 | return p; |
| 263 | } |
| 264 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 265 | /* Return a new key. This must be called before any other functions in |
| 266 | * this family, and callers must arrange to serialize calls to this |
| 267 | * function. No violations are detected. |
| 268 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 269 | int |
| 270 | PyThread_create_key(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 271 | { |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 272 | /* All parts of this function are wrong if it's called by multiple |
| 273 | * threads simultaneously. |
| 274 | */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 275 | if (keymutex == NULL) |
| 276 | keymutex = PyThread_allocate_lock(); |
| 277 | return ++nkeys; |
| 278 | } |
| 279 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 280 | /* Forget the associations for key across *all* threads. */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 281 | void |
| 282 | PyThread_delete_key(int key) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 283 | { |
| 284 | struct key *p, **q; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 285 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 286 | PyThread_acquire_lock(keymutex, 1); |
| 287 | q = &keyhead; |
| 288 | while ((p = *q) != NULL) { |
| 289 | if (p->key == key) { |
| 290 | *q = p->next; |
| 291 | free((void *)p); |
| 292 | /* NB This does *not* free p->value! */ |
| 293 | } |
| 294 | else |
| 295 | q = &p->next; |
| 296 | } |
| 297 | PyThread_release_lock(keymutex); |
| 298 | } |
| 299 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 300 | /* Confusing: If the current thread has an association for key, |
| 301 | * value is ignored, and 0 is returned. Else an attempt is made to create |
| 302 | * an association of key to value for the current thread. 0 is returned |
| 303 | * if that succeeds, but -1 is returned if there's not enough memory |
| 304 | * to create the association. value must not be NULL. |
| 305 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 306 | int |
| 307 | PyThread_set_key_value(int key, void *value) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 308 | { |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 309 | struct key *p; |
| 310 | |
| 311 | assert(value != NULL); |
| 312 | p = find_key(key, value); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 313 | if (p == NULL) |
| 314 | return -1; |
| 315 | else |
| 316 | return 0; |
| 317 | } |
| 318 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 319 | /* Retrieve the value associated with key in the current thread, or NULL |
| 320 | * if the current thread doesn't have an association for key. |
| 321 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 322 | void * |
| 323 | PyThread_get_key_value(int key) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 324 | { |
| 325 | struct key *p = find_key(key, NULL); |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 326 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 327 | if (p == NULL) |
| 328 | return NULL; |
| 329 | else |
| 330 | return p->value; |
| 331 | } |
| 332 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 333 | /* Forget the current thread's association for key, if any. */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 334 | void |
| 335 | PyThread_delete_key_value(int key) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 336 | { |
| 337 | long id = PyThread_get_thread_ident(); |
| 338 | struct key *p, **q; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 339 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 340 | PyThread_acquire_lock(keymutex, 1); |
| 341 | q = &keyhead; |
| 342 | while ((p = *q) != NULL) { |
| 343 | if (p->key == key && p->id == id) { |
| 344 | *q = p->next; |
| 345 | free((void *)p); |
| 346 | /* NB This does *not* free p->value! */ |
| 347 | break; |
| 348 | } |
| 349 | else |
| 350 | q = &p->next; |
| 351 | } |
| 352 | PyThread_release_lock(keymutex); |
| 353 | } |
| 354 | |
| 355 | #endif /* Py_HAVE_NATIVE_TLS */ |