| 1 | #include <Python.h> |
| 2 | #include "pythread.h" |
| 3 | #include "src/thread.h" |
| 4 | # include <pthread.h> |
| 5 | |
| 6 | /* ------------------------------------------------------------------------ |
| 7 | Per-thread data ("key") support. |
| 8 | |
| 9 | Use PyThread_create_key() to create a new key. This is typically shared |
| 10 | across threads. |
| 11 | |
| 12 | Use PyThread_set_key_value(thekey, value) to associate void* value with |
| 13 | thekey in the current thread. Each thread has a distinct mapping of thekey |
| 14 | to a void* value. Caution: if the current thread already has a mapping |
| 15 | for thekey, value is ignored. |
| 16 | |
| 17 | Use PyThread_get_key_value(thekey) to retrieve the void* value associated |
| 18 | with thekey in the current thread. This returns NULL if no value is |
| 19 | associated with thekey in the current thread. |
| 20 | |
| 21 | Use PyThread_delete_key_value(thekey) to forget the current thread's associated |
| 22 | value for thekey. PyThread_delete_key(thekey) forgets the values associated |
| 23 | with thekey across *all* threads. |
| 24 | |
| 25 | While some of these functions have error-return values, none set any |
| 26 | Python exception. |
| 27 | |
| 28 | None of the functions does memory management on behalf of the void* values. |
| 29 | You need to allocate and deallocate them yourself. If the void* values |
| 30 | happen to be PyObject*, these functions don't do refcount operations on |
| 31 | them either. |
| 32 | |
| 33 | The GIL does not need to be held when calling these functions; they supply |
| 34 | their own locking. This isn't true of PyThread_create_key(), though (see |
| 35 | next paragraph). |
| 36 | |
| 37 | There's a hidden assumption that PyThread_create_key() will be called before |
| 38 | any of the other functions are called. There's also a hidden assumption |
| 39 | that calls to PyThread_create_key() are serialized externally. |
| 40 | ------------------------------------------------------------------------ */ |
| 41 | |
| 42 | /* A singly-linked list of struct key objects remembers all the key->value |
| 43 | * associations. File static keyhead heads the list. keymutex is used |
| 44 | * to enforce exclusion internally. |
| 45 | */ |
| 46 | struct key { |
| 47 | /* Next record in the list, or NULL if this is the last record. */ |
| 48 | struct key *next; |
| 49 | |
| 50 | /* The thread id, according to PyThread_get_thread_ident(). */ |
| 51 | long id; |
| 52 | |
| 53 | /* The key and its associated value. */ |
| 54 | int key; |
| 55 | void *value; |
| 56 | }; |
| 57 | |
| 58 | static struct key *keyhead = NULL; |
| 59 | static PyThread_type_lock keymutex = NULL; |
| 60 | static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */ |
| 61 | |
| 62 | /* Internal helper. |
| 63 | * If the current thread has a mapping for key, the appropriate struct key* |
| 64 | * is returned. NB: value is ignored in this case! |
| 65 | * If there is no mapping for key in the current thread, then: |
| 66 | * If value is NULL, NULL is returned. |
| 67 | * Else a mapping of key to value is created for the current thread, |
| 68 | * and a pointer to a new struct key* is returned; except that if |
| 69 | * malloc() can't find room for a new struct key*, NULL is returned. |
| 70 | * So when value==NULL, this acts like a pure lookup routine, and when |
| 71 | * value!=NULL, this acts like dict.setdefault(), returning an existing |
| 72 | * mapping if one exists, else creating a new mapping. |
| 73 | * |
| 74 | * Caution: this used to be too clever, trying to hold keymutex only |
| 75 | * around the "p->next = keyhead; keyhead = p" pair. That allowed |
| 76 | * another thread to mutate the list, via key deletion, concurrent with |
| 77 | * find_key() crawling over the list. Hilarity ensued. For example, when |
| 78 | * the for-loop here does "p = p->next", p could end up pointing at a |
| 79 | * record that PyThread_delete_key_value() was concurrently free()'ing. |
| 80 | * That could lead to anything, from failing to find a key that exists, to |
| 81 | * segfaults. Now we lock the whole routine. |
| 82 | */ |
| 83 | static struct key * |
| 84 | find_key(int key, void *value) |
| 85 | { |
| 86 | struct key *p, *prev_p; |
| 87 | long id = PyThread_get_thread_ident(); |
| 88 | |
| 89 | if (!keymutex) |
| 90 | return NULL; |
| 91 | PyThread_acquire_lock(keymutex, 1); |
| 92 | prev_p = NULL; |
| 93 | for (p = keyhead; p != NULL; p = p->next) { |
| 94 | if (p->id == id && p->key == key) |
| 95 | goto Done; |
| 96 | /* Sanity check. These states should never happen but if |
| 97 | * they do we must abort. Otherwise we'll end up spinning in |
| 98 | * in a tight loop with the lock held. A similar check is done |
| 99 | * in pystate.c tstate_delete_common(). */ |
| 100 | if (p == prev_p) |
| 101 | Py_FatalError("tls find_key: small circular list(!)"); |
| 102 | prev_p = p; |
| 103 | if (p->next == keyhead) |
| 104 | Py_FatalError("tls find_key: circular list(!)"); |
| 105 | } |
| 106 | if (value == NULL) { |
| 107 | assert(p == NULL); |
| 108 | goto Done; |
| 109 | } |
| 110 | p = (struct key *)malloc(sizeof(struct key)); |
| 111 | if (p != NULL) { |
| 112 | p->id = id; |
| 113 | p->key = key; |
| 114 | p->value = value; |
| 115 | p->next = keyhead; |
| 116 | keyhead = p; |
| 117 | } |
| 118 | Done: |
| 119 | PyThread_release_lock(keymutex); |
| 120 | return p; |
| 121 | } |
| 122 | |
| 123 | /* Return a new key. This must be called before any other functions in |
| 124 | * this family, and callers must arrange to serialize calls to this |
| 125 | * function. No violations are detected. |
| 126 | */ |
| 127 | int |
| 128 | PyThread_create_key(void) |
| 129 | { |
| 130 | /* All parts of this function are wrong if it's called by multiple |
| 131 | * threads simultaneously. |
| 132 | */ |
| 133 | if (keymutex == NULL) |
| 134 | keymutex = PyThread_allocate_lock(); |
| 135 | return ++nkeys; |
| 136 | } |
| 137 | |
| 138 | /* Forget the associations for key across *all* threads. */ |
| 139 | void |
| 140 | PyThread_delete_key(int key) |
| 141 | { |
| 142 | struct key *p, **q; |
| 143 | |
| 144 | PyThread_acquire_lock(keymutex, 1); |
| 145 | q = &keyhead; |
| 146 | while ((p = *q) != NULL) { |
| 147 | if (p->key == key) { |
| 148 | *q = p->next; |
| 149 | free((void *)p); |
| 150 | /* NB This does *not* free p->value! */ |
| 151 | } |
| 152 | else |
| 153 | q = &p->next; |
| 154 | } |
| 155 | PyThread_release_lock(keymutex); |
| 156 | } |
| 157 | |
| 158 | /* Confusing: If the current thread has an association for key, |
| 159 | * value is ignored, and 0 is returned. Else an attempt is made to create |
| 160 | * an association of key to value for the current thread. 0 is returned |
| 161 | * if that succeeds, but -1 is returned if there's not enough memory |
| 162 | * to create the association. value must not be NULL. |
| 163 | */ |
| 164 | int |
| 165 | PyThread_set_key_value(int key, void *value) |
| 166 | { |
| 167 | struct key *p; |
| 168 | |
| 169 | assert(value != NULL); |
| 170 | p = find_key(key, value); |
| 171 | if (p == NULL) |
| 172 | return -1; |
| 173 | else |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | /* Retrieve the value associated with key in the current thread, or NULL |
| 178 | * if the current thread doesn't have an association for key. |
| 179 | */ |
| 180 | void * |
| 181 | PyThread_get_key_value(int key) |
| 182 | { |
| 183 | struct key *p = find_key(key, NULL); |
| 184 | |
| 185 | if (p == NULL) |
| 186 | return NULL; |
| 187 | else |
| 188 | return p->value; |
| 189 | } |
| 190 | |
| 191 | /* Forget the current thread's association for key, if any. */ |
| 192 | void |
| 193 | PyThread_delete_key_value(int key) |
| 194 | { |
| 195 | long id = PyThread_get_thread_ident(); |
| 196 | struct key *p, **q; |
| 197 | |
| 198 | PyThread_acquire_lock(keymutex, 1); |
| 199 | q = &keyhead; |
| 200 | while ((p = *q) != NULL) { |
| 201 | if (p->key == key && p->id == id) { |
| 202 | *q = p->next; |
| 203 | free((void *)p); |
| 204 | /* NB This does *not* free p->value! */ |
| 205 | break; |
| 206 | } |
| 207 | else |
| 208 | q = &p->next; |
| 209 | } |
| 210 | PyThread_release_lock(keymutex); |
| 211 | } |
| 212 | |
| 213 | /* Forget everything not associated with the current thread id. |
| 214 | * This function is called from PyOS_AfterFork(). It is necessary |
| 215 | * because other thread ids which were in use at the time of the fork |
| 216 | * may be reused for new threads created in the forked process. |
| 217 | */ |
| 218 | void |
| 219 | PyThread_ReInitTLS(void) |
| 220 | { |
| 221 | long id = PyThread_get_thread_ident(); |
| 222 | struct key *p, **q; |
| 223 | |
| 224 | if (!keymutex) |
| 225 | return; |
| 226 | |
| 227 | /* As with interpreter_lock in PyEval_ReInitThreads() |
| 228 | we just create a new lock without freeing the old one */ |
| 229 | keymutex = PyThread_allocate_lock(); |
| 230 | |
| 231 | /* Delete all keys which do not match the current thread id */ |
| 232 | q = &keyhead; |
| 233 | while ((p = *q) != NULL) { |
| 234 | if (p->id != id) { |
| 235 | *q = p->next; |
| 236 | free((void *)p); |
| 237 | /* NB This does *not* free p->value! */ |
| 238 | } |
| 239 | else |
| 240 | q = &p->next; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* Platform-specific components of TSS API implementation. */ |
| 245 | |
| 246 | int |
| 247 | PyThread_tss_create(Py_tss_t *key) |
| 248 | { |
| 249 | assert(key != NULL); |
| 250 | /* If the key has been created, function is silently skipped. */ |
| 251 | if (key->_is_initialized) { |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | int fail = pthread_key_create(&(key->_key), NULL); |
| 256 | if (fail) { |
| 257 | return -1; |
| 258 | } |
| 259 | key->_is_initialized = 1; |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | void |
| 264 | PyThread_tss_delete(Py_tss_t *key) |
| 265 | { |
| 266 | assert(key != NULL); |
| 267 | /* If the key has not been created, function is silently skipped. */ |
| 268 | if (!key->_is_initialized) { |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | pthread_key_delete(key->_key); |
| 273 | /* pthread has not provided the defined invalid value for the key. */ |
| 274 | key->_is_initialized = 0; |
| 275 | } |
| 276 | |
| 277 | int |
| 278 | PyThread_tss_set(Py_tss_t *key, void *value) |
| 279 | { |
| 280 | assert(key != NULL); |
| 281 | int fail = pthread_setspecific(key->_key, value); |
| 282 | return fail ? -1 : 0; |
| 283 | } |
| 284 | |
| 285 | void * |
| 286 | PyThread_tss_get(Py_tss_t *key) |
| 287 | { |
| 288 | assert(key != NULL); |
| 289 | return pthread_getspecific(key->_key); |
| 290 | } |
| 291 |