Mercurial > cpython-lru_cache-weakref
diff Python/thread_sgi.h @ 5405:b7871ca930ad legacy-trunk
Massive changes for separate thread state management.
All per-thread globals are moved into a struct which is manipulated
separately.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 05 May 1997 20:56:21 +0000 |
| parents | 4567e158d20c |
| children | 877d23bd5bdc |
line wrap: on
line diff
--- a/Python/thread_sgi.h Mon May 05 20:53:25 1997 +0000 +++ b/Python/thread_sgi.h Mon May 05 20:56:21 1997 +0000 @@ -451,3 +451,83 @@ if (usvsema((usema_t *) sema) < 0) perror("usvsema"); } + +/* + * Per-thread data ("key") support. + */ + +struct key { + struct key *next; + long id; + int key; + void *value; +}; + +static struct key *keyhead = NULL; +static int nkeys = 0; +static type_lock keymutex = NULL; + +static struct key *find_key _P2(key, int key, value, void *value) +{ + struct key *p; + long id = get_thread_ident(); + for (p = keyhead; p != NULL; p = p->next) { + if (p->id == id && p->key == key) + return p; + } + if (value == NULL) + return NULL; + p = (struct key *)malloc(sizeof(struct key)); + if (p != NULL) { + p->id = id; + p->key = key; + p->value = value; + acquire_lock(keymutex, 1); + p->next = keyhead; + keyhead = p; + release_lock(keymutex); + } + return p; +} + +int create_key _P0() +{ + if (keymutex == NULL) + keymutex = allocate_lock(); + return ++nkeys; +} + +void delete_key _P1(key, int key) +{ + struct key *p, **q; + acquire_lock(keymutex, 1); + q = &keyhead; + while ((p = *q) != NULL) { + if (p->key == key) { + *q = p->next; + free((void *)p); + /* NB This does *not* free p->value! */ + } + else + q = &p->next; + } + release_lock(keymutex); +} + +int set_key_value _P2(key, int key, value, void *value) +{ + struct key *p = find_key(key, value); + if (p == NULL) + return -1; + else + return 0; +} + +void *get_key_value _P1(key, int key) +{ + struct key *p = find_key(key, NULL); + if (p == NULL) + return NULL; + else + return p->value; +}
