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 | #ifndef _POSIX_THREADS |
| 11 | /* This means pthreads are not implemented in libc headers, hence the macro |
| 12 | not present in unistd.h. But they still can be implemented as an external |
| 13 | library (e.g. gnu pth in pthread emulation) */ |
| 14 | # ifdef HAVE_PTHREAD_H |
| 15 | # include <pthread.h> /* _POSIX_THREADS */ |
| 16 | # endif |
| 17 | #endif |
| 18 | |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 19 | #ifndef DONT_HAVE_STDIO_H |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 21 | #endif |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 25 | #include "pythread.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 27 | #ifndef _POSIX_THREADS |
| 28 | |
Guido van Rossum | 539c662 | 2005-09-14 17:49:54 +0000 | [diff] [blame] | 29 | /* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 30 | enough of the Posix threads package is implemented to support python |
Guido van Rossum | 539c662 | 2005-09-14 17:49:54 +0000 | [diff] [blame] | 31 | threads. |
| 32 | |
| 33 | This is valid for HP-UX 11.23 running on an ia64 system. If needed, add |
| 34 | a check of __ia64 to verify that we're running on a ia64 system instead |
| 35 | of a pa-risc system. |
| 36 | */ |
| 37 | #ifdef __hpux |
| 38 | #ifdef _SC_THREADS |
| 39 | #define _POSIX_THREADS |
| 40 | #endif |
| 41 | #endif |
| 42 | |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 43 | #endif /* _POSIX_THREADS */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 46 | #ifdef Py_DEBUG |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 47 | static int thread_debug = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | #define dprintf(args) (void)((thread_debug & 1) && printf args) |
| 49 | #define d2printf(args) ((thread_debug & 8) && printf args) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 50 | #else |
| 51 | #define dprintf(args) |
| 52 | #define d2printf(args) |
| 53 | #endif |
| 54 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 55 | static int initialized; |
| 56 | |
Thomas Wouters | 8ec68fd | 2000-07-24 14:39:50 +0000 | [diff] [blame] | 57 | static void PyThread__init_thread(void); /* Forward */ |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 58 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 59 | void |
| 60 | PyThread_init_thread(void) |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 61 | { |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 62 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | char *p = Py_GETENV("PYTHONTHREADDEBUG"); |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 64 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | if (p) { |
| 66 | if (*p) |
| 67 | thread_debug = atoi(p); |
| 68 | else |
| 69 | thread_debug = 1; |
| 70 | } |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 71 | #endif /* Py_DEBUG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | if (initialized) |
| 73 | return; |
| 74 | initialized = 1; |
| 75 | dprintf(("PyThread_init_thread called\n")); |
| 76 | PyThread__init_thread(); |
Sjoerd Mullender | aee8bc1 | 1992-09-02 11:25:37 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 79 | /* Support for runtime thread stack size tuning. |
| 80 | A value of 0 means using the platform's default stack size |
| 81 | or the size specified by the THREAD_STACK_SIZE macro. */ |
| 82 | static size_t _pythread_stacksize = 0; |
| 83 | |
Sjoerd Mullender | 66bca32 | 1993-12-03 16:54:45 +0000 | [diff] [blame] | 84 | #ifdef _POSIX_THREADS |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 85 | #define PYTHREAD_NAME "pthread" |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 86 | #include "thread_pthread.h" |
Sjoerd Mullender | e893412 | 1993-01-13 12:08:48 +0000 | [diff] [blame] | 87 | #endif |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 89 | #ifdef NT_THREADS |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 90 | #define PYTHREAD_NAME "nt" |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 91 | #include "thread_nt.h" |
| 92 | #endif |
| 93 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 95 | /* |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 96 | #ifdef FOOBAR_THREADS |
| 97 | #include "thread_foobar.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 98 | #endif |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 99 | */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 100 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 101 | /* return the current thread stack size */ |
| 102 | size_t |
| 103 | PyThread_get_stacksize(void) |
| 104 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | return _pythread_stacksize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* Only platforms defining a THREAD_SET_STACKSIZE() macro |
| 109 | in thread_<platform>.h support changing the stack size. |
| 110 | Return 0 if stack size is valid, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | -1 if stack size value is invalid, |
| 112 | -2 if setting stack size is not supported. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 113 | int |
| 114 | PyThread_set_stacksize(size_t size) |
| 115 | { |
| 116 | #if defined(THREAD_SET_STACKSIZE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | return THREAD_SET_STACKSIZE(size); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 118 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 120 | #endif |
| 121 | } |
| 122 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 123 | #ifndef Py_HAVE_NATIVE_TLS |
| 124 | /* If the platform has not supplied a platform specific |
| 125 | TLS implementation, provide our own. |
| 126 | |
| 127 | This code stolen from "thread_sgi.h", where it was the only |
| 128 | implementation of an existing Python TLS API. |
| 129 | */ |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 130 | /* ------------------------------------------------------------------------ |
| 131 | Per-thread data ("key") support. |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 132 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 133 | Use PyThread_create_key() to create a new key. This is typically shared |
| 134 | across threads. |
| 135 | |
| 136 | Use PyThread_set_key_value(thekey, value) to associate void* value with |
| 137 | thekey in the current thread. Each thread has a distinct mapping of thekey |
| 138 | to a void* value. Caution: if the current thread already has a mapping |
| 139 | for thekey, value is ignored. |
| 140 | |
| 141 | Use PyThread_get_key_value(thekey) to retrieve the void* value associated |
| 142 | with thekey in the current thread. This returns NULL if no value is |
| 143 | associated with thekey in the current thread. |
| 144 | |
| 145 | Use PyThread_delete_key_value(thekey) to forget the current thread's associated |
| 146 | value for thekey. PyThread_delete_key(thekey) forgets the values associated |
| 147 | with thekey across *all* threads. |
| 148 | |
| 149 | While some of these functions have error-return values, none set any |
| 150 | Python exception. |
| 151 | |
| 152 | None of the functions does memory management on behalf of the void* values. |
| 153 | You need to allocate and deallocate them yourself. If the void* values |
| 154 | happen to be PyObject*, these functions don't do refcount operations on |
| 155 | them either. |
| 156 | |
| 157 | The GIL does not need to be held when calling these functions; they supply |
| 158 | their own locking. This isn't true of PyThread_create_key(), though (see |
| 159 | next paragraph). |
| 160 | |
| 161 | There's a hidden assumption that PyThread_create_key() will be called before |
| 162 | any of the other functions are called. There's also a hidden assumption |
| 163 | that calls to PyThread_create_key() are serialized externally. |
| 164 | ------------------------------------------------------------------------ */ |
| 165 | |
| 166 | /* A singly-linked list of struct key objects remembers all the key->value |
| 167 | * associations. File static keyhead heads the list. keymutex is used |
| 168 | * to enforce exclusion internally. |
| 169 | */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 170 | struct key { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | /* Next record in the list, or NULL if this is the last record. */ |
| 172 | struct key *next; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | /* The thread id, according to PyThread_get_thread_ident(). */ |
| 175 | long id; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | /* The key and its associated value. */ |
| 178 | int key; |
| 179 | void *value; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | static struct key *keyhead = NULL; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 183 | static PyThread_type_lock keymutex = NULL; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 184 | static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 185 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 186 | /* Internal helper. |
| 187 | * If the current thread has a mapping for key, the appropriate struct key* |
| 188 | * is returned. NB: value is ignored in this case! |
| 189 | * If there is no mapping for key in the current thread, then: |
| 190 | * If value is NULL, NULL is returned. |
| 191 | * Else a mapping of key to value is created for the current thread, |
| 192 | * and a pointer to a new struct key* is returned; except that if |
| 193 | * malloc() can't find room for a new struct key*, NULL is returned. |
| 194 | * So when value==NULL, this acts like a pure lookup routine, and when |
| 195 | * value!=NULL, this acts like dict.setdefault(), returning an existing |
| 196 | * mapping if one exists, else creating a new mapping. |
Tim Peters | 263091e | 2004-10-10 01:58:44 +0000 | [diff] [blame] | 197 | * |
| 198 | * Caution: this used to be too clever, trying to hold keymutex only |
| 199 | * around the "p->next = keyhead; keyhead = p" pair. That allowed |
| 200 | * another thread to mutate the list, via key deletion, concurrent with |
| 201 | * find_key() crawling over the list. Hilarity ensued. For example, when |
| 202 | * the for-loop here does "p = p->next", p could end up pointing at a |
| 203 | * record that PyThread_delete_key_value() was concurrently free()'ing. |
| 204 | * That could lead to anything, from failing to find a key that exists, to |
| 205 | * segfaults. Now we lock the whole routine. |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 206 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 207 | static struct key * |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 208 | find_key(int set_value, int key, void *value) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 209 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | struct key *p, *prev_p; |
| 211 | long id = PyThread_get_thread_ident(); |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | if (!keymutex) |
| 214 | return NULL; |
| 215 | PyThread_acquire_lock(keymutex, 1); |
| 216 | prev_p = NULL; |
| 217 | for (p = keyhead; p != NULL; p = p->next) { |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 218 | if (p->id == id && p->key == key) { |
| 219 | if (set_value) |
| 220 | p->value = value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | goto Done; |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 222 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | /* Sanity check. These states should never happen but if |
| 224 | * they do we must abort. Otherwise we'll end up spinning in |
| 225 | * in a tight loop with the lock held. A similar check is done |
| 226 | * in pystate.c tstate_delete_common(). */ |
| 227 | if (p == prev_p) |
| 228 | Py_FatalError("tls find_key: small circular list(!)"); |
| 229 | prev_p = p; |
| 230 | if (p->next == keyhead) |
| 231 | Py_FatalError("tls find_key: circular list(!)"); |
| 232 | } |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 233 | if (!set_value && value == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | assert(p == NULL); |
| 235 | goto Done; |
| 236 | } |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 237 | p = (struct key *)PyMem_RawMalloc(sizeof(struct key)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | if (p != NULL) { |
| 239 | p->id = id; |
| 240 | p->key = key; |
| 241 | p->value = value; |
| 242 | p->next = keyhead; |
| 243 | keyhead = p; |
| 244 | } |
Tim Peters | 263091e | 2004-10-10 01:58:44 +0000 | [diff] [blame] | 245 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | PyThread_release_lock(keymutex); |
| 247 | return p; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 250 | /* Return a new key. This must be called before any other functions in |
| 251 | * this family, and callers must arrange to serialize calls to this |
| 252 | * function. No violations are detected. |
| 253 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 254 | int |
| 255 | PyThread_create_key(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 256 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | /* All parts of this function are wrong if it's called by multiple |
| 258 | * threads simultaneously. |
| 259 | */ |
| 260 | if (keymutex == NULL) |
| 261 | keymutex = PyThread_allocate_lock(); |
| 262 | return ++nkeys; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 265 | /* Forget the associations for key across *all* threads. */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 266 | void |
| 267 | PyThread_delete_key(int key) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | struct key *p, **q; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | PyThread_acquire_lock(keymutex, 1); |
| 272 | q = &keyhead; |
| 273 | while ((p = *q) != NULL) { |
| 274 | if (p->key == key) { |
| 275 | *q = p->next; |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 276 | PyMem_RawFree((void *)p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | /* NB This does *not* free p->value! */ |
| 278 | } |
| 279 | else |
| 280 | q = &p->next; |
| 281 | } |
| 282 | PyThread_release_lock(keymutex); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 285 | int |
| 286 | PyThread_set_key_value(int key, void *value) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | struct key *p; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 289 | |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 290 | p = find_key(1, key, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | if (p == NULL) |
| 292 | return -1; |
| 293 | else |
| 294 | return 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 297 | /* Retrieve the value associated with key in the current thread, or NULL |
| 298 | * if the current thread doesn't have an association for key. |
| 299 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 300 | void * |
| 301 | PyThread_get_key_value(int key) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 302 | { |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 303 | struct key *p = find_key(0, key, NULL); |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | if (p == NULL) |
| 306 | return NULL; |
| 307 | else |
| 308 | return p->value; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 311 | /* Forget the current thread's association for key, if any. */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 312 | void |
| 313 | PyThread_delete_key_value(int key) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 314 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | long id = PyThread_get_thread_ident(); |
| 316 | struct key *p, **q; |
Tim Peters | fda787f | 2004-10-09 22:33:09 +0000 | [diff] [blame] | 317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | PyThread_acquire_lock(keymutex, 1); |
| 319 | q = &keyhead; |
| 320 | while ((p = *q) != NULL) { |
| 321 | if (p->key == key && p->id == id) { |
| 322 | *q = p->next; |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 323 | PyMem_RawFree((void *)p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | /* NB This does *not* free p->value! */ |
| 325 | break; |
| 326 | } |
| 327 | else |
| 328 | q = &p->next; |
| 329 | } |
| 330 | PyThread_release_lock(keymutex); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Benjamin Peterson | e68df0f | 2008-06-13 00:26:50 +0000 | [diff] [blame] | 333 | /* Forget everything not associated with the current thread id. |
| 334 | * This function is called from PyOS_AfterFork(). It is necessary |
| 335 | * because other thread ids which were in use at the time of the fork |
| 336 | * may be reused for new threads created in the forked process. |
| 337 | */ |
| 338 | void |
| 339 | PyThread_ReInitTLS(void) |
| 340 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | long id = PyThread_get_thread_ident(); |
| 342 | struct key *p, **q; |
Benjamin Peterson | e68df0f | 2008-06-13 00:26:50 +0000 | [diff] [blame] | 343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | if (!keymutex) |
| 345 | return; |
Benjamin Peterson | e68df0f | 2008-06-13 00:26:50 +0000 | [diff] [blame] | 346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | /* As with interpreter_lock in PyEval_ReInitThreads() |
| 348 | we just create a new lock without freeing the old one */ |
| 349 | keymutex = PyThread_allocate_lock(); |
| 350 | |
| 351 | /* Delete all keys which do not match the current thread id */ |
| 352 | q = &keyhead; |
| 353 | while ((p = *q) != NULL) { |
| 354 | if (p->id != id) { |
| 355 | *q = p->next; |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 356 | PyMem_RawFree((void *)p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | /* NB This does *not* free p->value! */ |
| 358 | } |
| 359 | else |
| 360 | q = &p->next; |
| 361 | } |
Benjamin Peterson | e68df0f | 2008-06-13 00:26:50 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 364 | #endif /* Py_HAVE_NATIVE_TLS */ |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 365 | |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 366 | PyDoc_STRVAR(threadinfo__doc__, |
| 367 | "sys.thread_info\n\ |
| 368 | \n\ |
| 369 | A struct sequence holding information about the thread implementation."); |
| 370 | |
| 371 | static PyStructSequence_Field threadinfo_fields[] = { |
| 372 | {"name", "name of the thread implementation"}, |
| 373 | {"lock", "name of the lock implementation"}, |
| 374 | {"version", "name and version of the thread library"}, |
| 375 | {0} |
| 376 | }; |
| 377 | |
| 378 | static PyStructSequence_Desc threadinfo_desc = { |
| 379 | "sys.thread_info", /* name */ |
| 380 | threadinfo__doc__, /* doc */ |
| 381 | threadinfo_fields, /* fields */ |
| 382 | 3 |
| 383 | }; |
| 384 | |
| 385 | static PyTypeObject ThreadInfoType; |
| 386 | |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 387 | PyObject* |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 388 | PyThread_GetInfo(void) |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 389 | { |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 390 | PyObject *threadinfo, *value; |
| 391 | int pos = 0; |
Victor Stinner | e07f522 | 2011-04-20 12:23:26 +0200 | [diff] [blame] | 392 | #if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \ |
| 393 | && defined(_CS_GNU_LIBPTHREAD_VERSION)) |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 394 | char buffer[255]; |
| 395 | int len; |
Victor Stinner | e07f522 | 2011-04-20 12:23:26 +0200 | [diff] [blame] | 396 | #endif |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 397 | |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 398 | if (ThreadInfoType.tp_name == 0) { |
| 399 | if (PyStructSequence_InitType2(&ThreadInfoType, &threadinfo_desc) < 0) |
| 400 | return NULL; |
| 401 | } |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 402 | |
| 403 | threadinfo = PyStructSequence_New(&ThreadInfoType); |
| 404 | if (threadinfo == NULL) |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 405 | return NULL; |
| 406 | |
| 407 | value = PyUnicode_FromString(PYTHREAD_NAME); |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 408 | if (value == NULL) { |
| 409 | Py_DECREF(threadinfo); |
| 410 | return NULL; |
| 411 | } |
| 412 | PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 413 | |
| 414 | #ifdef _POSIX_THREADS |
| 415 | #ifdef USE_SEMAPHORES |
| 416 | value = PyUnicode_FromString("semaphore"); |
| 417 | #else |
| 418 | value = PyUnicode_FromString("mutex+cond"); |
| 419 | #endif |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 420 | if (value == NULL) { |
| 421 | Py_DECREF(threadinfo); |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 422 | return NULL; |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 423 | } |
| 424 | #else |
| 425 | Py_INCREF(Py_None); |
| 426 | value = Py_None; |
| 427 | #endif |
| 428 | PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 429 | |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 430 | #if (defined(_POSIX_THREADS) && defined(HAVE_CONFSTR) \ |
| 431 | && defined(_CS_GNU_LIBPTHREAD_VERSION)) |
| 432 | value = NULL; |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 433 | len = confstr(_CS_GNU_LIBPTHREAD_VERSION, buffer, sizeof(buffer)); |
Victor Stinner | 98ea54c | 2014-08-15 23:30:40 +0200 | [diff] [blame] | 434 | if (1 < len && (size_t)len < sizeof(buffer)) { |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 435 | value = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
| 436 | if (value == NULL) |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 437 | PyErr_Clear(); |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 438 | } |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 439 | if (value == NULL) |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 440 | #endif |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 441 | { |
| 442 | Py_INCREF(Py_None); |
| 443 | value = Py_None; |
| 444 | } |
| 445 | PyStructSequence_SET_ITEM(threadinfo, pos++, value); |
| 446 | return threadinfo; |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 447 | } |