From 8de18b15e99af0b808bceaa3b43f76143c701173 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Wed, 16 Nov 2022 17:40:49 -0500 Subject: [PATCH] posix: newlib compatible PTHREAD_CREATE_DETACHED and JOINABLE Define `PTHREAD_CREATE_DETACHED` and `PTHREAD_CREATE_JOINABLE` to be compatible with the Newlib definitions. This is a temporary workaround for #51211 until Newlib headers are pulled in. Signed-off-by: Chris Friedt --- include/zephyr/posix/pthread.h | 4 ++-- lib/posix/posix_internal.h | 8 ++++---- lib/posix/pthread.c | 16 ++++++++++++++++ tests/posix/common/src/pthread.c | 4 ++-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/include/zephyr/posix/pthread.h b/include/zephyr/posix/pthread.h index 4186aefba555d40..6797442eaef60d8 100644 --- a/include/zephyr/posix/pthread.h +++ b/include/zephyr/posix/pthread.h @@ -22,8 +22,8 @@ extern "C" { #endif /* Pthread detach/joinable */ -#define PTHREAD_CREATE_JOINABLE 1 -#define PTHREAD_CREATE_DETACHED 2 +#define PTHREAD_CREATE_DETACHED 0 +#define PTHREAD_CREATE_JOINABLE 1 /* Pthread cancellation */ #define _PTHREAD_CANCEL_POS 0 diff --git a/lib/posix/posix_internal.h b/lib/posix/posix_internal.h index 2351d94a994416a..cdabf5bae0c856e 100644 --- a/lib/posix/posix_internal.h +++ b/lib/posix/posix_internal.h @@ -25,12 +25,12 @@ struct posix_cond { }; enum pthread_state { - /* The thread structure is unallocated and available for reuse. */ - PTHREAD_TERMINATED = 0, - /* The thread is running and joinable. */ - PTHREAD_JOINABLE = PTHREAD_CREATE_JOINABLE, /* The thread is running and detached. */ PTHREAD_DETACHED = PTHREAD_CREATE_DETACHED, + /* The thread is running and joinable. */ + PTHREAD_JOINABLE = PTHREAD_CREATE_JOINABLE, + /* The thread structure is unallocated and available for reuse. */ + PTHREAD_TERMINATED, /* A joinable thread exited and its return code is available. */ PTHREAD_EXITED }; diff --git a/lib/posix/pthread.c b/lib/posix/pthread.c index 4a803bbfa82ea04..11d3f3d8fad23e3 100644 --- a/lib/posix/pthread.c +++ b/lib/posix/pthread.c @@ -4,6 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include #include #include @@ -690,3 +691,18 @@ int pthread_getname_np(pthread_t thread, char *name, size_t len) return 0; #endif } + +static int posix_thread_pool_init(const struct device *dev) +{ + size_t i; + + ARG_UNUSED(dev); + + for (i = 0; i < CONFIG_MAX_PTHREAD_COUNT; ++i) { + posix_thread_pool[i].state = PTHREAD_EXITED; + } + + return 0; +} + +SYS_INIT(posix_thread_pool_init, PRE_KERNEL_1, 0); diff --git a/tests/posix/common/src/pthread.c b/tests/posix/common/src/pthread.c index facc4a1629a713e..21fc71bb5127ae6 100644 --- a/tests/posix/common/src/pthread.c +++ b/tests/posix/common/src/pthread.c @@ -459,9 +459,9 @@ ZTEST(posix_apis, test_posix_pthread_error_condition) "set scheduling policy error"); zassert_equal(pthread_attr_setschedpolicy(&attr, 2), EINVAL, "set scheduling policy error"); - zassert_false(pthread_attr_setdetachstate(&attr, 1), + zassert_false(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE), "set detach state error"); - zassert_false(pthread_attr_setdetachstate(&attr, 2), + zassert_false(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED), "set detach state error"); zassert_equal(pthread_attr_setdetachstate(&attr, 3), EINVAL, "set detach state error");