posix: pthread_create: use spinlock for pthread_pool_lock

The `pthread_create()` function is not a cancellation point and
iterating over / mutating `posix_thread_pool` is not a blocking
operation, so use a spinlock for the internal `pthread_pool_lock`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
This commit is contained in:
Chris Friedt 2022-11-06 08:10:39 -05:00 committed by Christopher Friedt
parent 9b5bc03f86
commit 39b8b3ac8a

View file

@ -37,7 +37,7 @@ static const pthread_attr_t init_pthread_attrs = {
};
static struct posix_thread posix_thread_pool[CONFIG_MAX_PTHREAD_COUNT];
PTHREAD_MUTEX_DEFINE(pthread_pool_lock);
static struct k_spinlock pthread_pool_lock;
pthread_t pthread_self(void)
{
@ -150,6 +150,7 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
void *(*threadroutine)(void *), void *arg)
{
int32_t prio;
k_spinlock_key_t key;
uint32_t pthread_num;
k_spinlock_key_t cancel_key;
pthread_condattr_t cond_attr;
@ -165,7 +166,7 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
return EINVAL;
}
pthread_mutex_lock(&pthread_pool_lock);
key = k_spin_lock(&pthread_pool_lock);
for (pthread_num = 0;
pthread_num < CONFIG_MAX_PTHREAD_COUNT; pthread_num++) {
thread = &posix_thread_pool[pthread_num];
@ -174,7 +175,7 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
break;
}
}
pthread_mutex_unlock(&pthread_pool_lock);
k_spin_unlock(&pthread_pool_lock, key);
if (pthread_num >= CONFIG_MAX_PTHREAD_COUNT) {
return EAGAIN;