mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-06 02:55:49 +03:00
LibPthread: Implement simple thread-specific keys
This patch adds pthread_key_create() and pthread_{get,set}specific(). There's a maximum of 64 thread-specific keys for simplicity. Key destructors are not invoked on thread exit.
This commit is contained in:
parent
2b45b7a45c
commit
615553be5f
Notes:
sideshowbarker
2024-07-19 10:56:24 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/615553be5fe
@ -61,7 +61,7 @@ struct utimbuf {
|
||||
};
|
||||
|
||||
typedef int pthread_t;
|
||||
typedef void* pthread_key_t;
|
||||
typedef int pthread_key_t;
|
||||
typedef void* pthread_once_t;
|
||||
typedef uint32_t pthread_mutex_t;
|
||||
typedef void* pthread_attr_t;
|
||||
|
@ -465,4 +465,58 @@ int pthread_cond_broadcast(pthread_cond_t* cond)
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const int max_keys = 64;
|
||||
|
||||
typedef void (*KeyDestructor)(void*);
|
||||
|
||||
struct KeyTable {
|
||||
// FIXME: Invoke key destructors on thread exit!
|
||||
KeyDestructor destructors[64] { nullptr };
|
||||
int next { 0 };
|
||||
pthread_mutex_t mutex { PTHREAD_MUTEX_INITIALIZER };
|
||||
};
|
||||
|
||||
struct SpecificTable {
|
||||
void* values[64] { nullptr };
|
||||
};
|
||||
|
||||
static KeyTable s_keys;
|
||||
|
||||
__thread SpecificTable t_specifics;
|
||||
|
||||
int pthread_key_create(pthread_key_t* key, KeyDestructor destructor)
|
||||
{
|
||||
int ret = 0;
|
||||
pthread_mutex_lock(&s_keys.mutex);
|
||||
if (s_keys.next >= max_keys) {
|
||||
ret = ENOMEM;
|
||||
} else {
|
||||
*key = s_keys.next++;
|
||||
s_keys.destructors[*key] = destructor;
|
||||
ret = 0;
|
||||
}
|
||||
pthread_mutex_unlock(&s_keys.mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void* pthread_getspecific(pthread_key_t key)
|
||||
{
|
||||
if (key < 0)
|
||||
return nullptr;
|
||||
if (key >= max_keys)
|
||||
return nullptr;
|
||||
return t_specifics.values[key];
|
||||
}
|
||||
|
||||
int pthread_setspecific(pthread_key_t key, const void* value)
|
||||
{
|
||||
if (key < 0)
|
||||
return EINVAL;
|
||||
if (key >= max_keys)
|
||||
return EINVAL;
|
||||
|
||||
t_specifics.values[key] = const_cast<void*>(value);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user