Mercurial > cpython-lru_cache-weakref
comparison Python/thread.c @ 28694:a4154dd5939a legacy-trunk
New PyGILState_ API - implements pep 311, from patch 684256.
| author | Mark Hammond <mhammond@skippinet.com.au> |
|---|---|
| date | Sat, 19 Apr 2003 15:41:53 +0000 |
| parents | a8df0c0be242 |
| children | 1a817694ecbd |
comparison
equal
deleted
inserted
replaced
| 28693:7922d5f43854 | 28694:a4154dd5939a |
|---|---|
| 135 /* | 135 /* |
| 136 #ifdef FOOBAR_THREADS | 136 #ifdef FOOBAR_THREADS |
| 137 #include "thread_foobar.h" | 137 #include "thread_foobar.h" |
| 138 #endif | 138 #endif |
| 139 */ | 139 */ |
| 140 | |
| 141 #ifndef Py_HAVE_NATIVE_TLS | |
| 142 /* If the platform has not supplied a platform specific | |
| 143 TLS implementation, provide our own. | |
| 144 | |
| 145 This code stolen from "thread_sgi.h", where it was the only | |
| 146 implementation of an existing Python TLS API. | |
| 147 */ | |
| 148 /* | |
| 149 * Per-thread data ("key") support. | |
| 150 */ | |
| 151 | |
| 152 struct key { | |
| 153 struct key *next; | |
| 154 long id; | |
| 155 int key; | |
| 156 void *value; | |
| 157 }; | |
| 158 | |
| 159 static struct key *keyhead = NULL; | |
| 160 static int nkeys = 0; | |
| 161 static PyThread_type_lock keymutex = NULL; | |
| 162 | |
| 163 static struct key *find_key(int key, void *value) | |
| 164 { | |
| 165 struct key *p; | |
| 166 long id = PyThread_get_thread_ident(); | |
| 167 for (p = keyhead; p != NULL; p = p->next) { | |
| 168 if (p->id == id && p->key == key) | |
| 169 return p; | |
| 170 } | |
| 171 if (value == NULL) | |
| 172 return NULL; | |
| 173 p = (struct key *)malloc(sizeof(struct key)); | |
| 174 if (p != NULL) { | |
| 175 p->id = id; | |
| 176 p->key = key; | |
| 177 p->value = value; | |
| 178 PyThread_acquire_lock(keymutex, 1); | |
| 179 p->next = keyhead; | |
| 180 keyhead = p; | |
| 181 PyThread_release_lock(keymutex); | |
| 182 } | |
| 183 return p; | |
| 184 } | |
| 185 | |
| 186 int PyThread_create_key(void) | |
| 187 { | |
| 188 if (keymutex == NULL) | |
| 189 keymutex = PyThread_allocate_lock(); | |
| 190 return ++nkeys; | |
| 191 } | |
| 192 | |
| 193 void PyThread_delete_key(int key) | |
| 194 { | |
| 195 struct key *p, **q; | |
| 196 PyThread_acquire_lock(keymutex, 1); | |
| 197 q = &keyhead; | |
| 198 while ((p = *q) != NULL) { | |
| 199 if (p->key == key) { | |
| 200 *q = p->next; | |
| 201 free((void *)p); | |
| 202 /* NB This does *not* free p->value! */ | |
| 203 } | |
| 204 else | |
| 205 q = &p->next; | |
| 206 } | |
| 207 PyThread_release_lock(keymutex); | |
| 208 } | |
| 209 | |
| 210 int PyThread_set_key_value(int key, void *value) | |
| 211 { | |
| 212 struct key *p = find_key(key, value); | |
| 213 if (p == NULL) | |
| 214 return -1; | |
| 215 else | |
| 216 return 0; | |
| 217 } | |
| 218 | |
| 219 void *PyThread_get_key_value(int key) | |
| 220 { | |
| 221 struct key *p = find_key(key, NULL); | |
| 222 if (p == NULL) | |
| 223 return NULL; | |
| 224 else | |
| 225 return p->value; | |
| 226 } | |
| 227 | |
| 228 void PyThread_delete_key_value(int key) | |
| 229 { | |
| 230 long id = PyThread_get_thread_ident(); | |
| 231 struct key *p, **q; | |
| 232 PyThread_acquire_lock(keymutex, 1); | |
| 233 q = &keyhead; | |
| 234 while ((p = *q) != NULL) { | |
| 235 if (p->key == key && p->id == id) { | |
| 236 *q = p->next; | |
| 237 free((void *)p); | |
| 238 /* NB This does *not* free p->value! */ | |
| 239 break; | |
| 240 } | |
| 241 else | |
| 242 q = &p->next; | |
| 243 } | |
| 244 PyThread_release_lock(keymutex); | |
| 245 } | |
| 246 | |
| 247 #endif /* Py_HAVE_NATIVE_TLS */ |
