mirror of
https://github.com/python/cpython.git
synced 2025-08-30 05:35:08 +00:00
Massive changes for separate thread state management.
All per-thread globals are moved into a struct which is manipulated separately.
This commit is contained in:
parent
73237c54b4
commit
a027efa5bf
15 changed files with 865 additions and 262 deletions
|
@ -451,3 +451,83 @@ void up_sema _P1(sema, type_sema sema)
|
|||
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue