From 39b8b3ac8af9758ef3bd6778d5ebf5d903a504a3 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sun, 6 Nov 2022 08:10:39 -0500 Subject: [PATCH] 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 --- lib/posix/pthread.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/posix/pthread.c b/lib/posix/pthread.c index 5f75c1ea1f..213812d0d8 100644 --- a/lib/posix/pthread.c +++ b/lib/posix/pthread.c @@ -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;