From 19651c934429da4ee1e0fcd1e157c8f8769e0748 Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Wed, 22 Nov 2023 21:23:53 -0500 Subject: [PATCH 1/6] posix: pthread: move pthread_equal() closer to pthread_self() * "identity" functions grouped more closely * posix_thread_pool_init() should be adjacent to the SYS_INIT() Signed-off-by: Christopher Friedt --- lib/posix/pthread.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/posix/pthread.c b/lib/posix/pthread.c index f2bbc07ae9a4..070806ed758c 100644 --- a/lib/posix/pthread.c +++ b/lib/posix/pthread.c @@ -135,6 +135,11 @@ pthread_t pthread_self(void) return mark_pthread_obj_initialized(bit); } +int pthread_equal(pthread_t pt1, pthread_t pt2) +{ + return (pt1 == pt2); +} + static bool is_posix_policy_prio_valid(uint32_t priority, int policy) { if (priority >= sched_get_priority_min(policy) && @@ -964,10 +969,4 @@ static int posix_thread_pool_init(void) return 0; } - -int pthread_equal(pthread_t pt1, pthread_t pt2) -{ - return (pt1 == pt2); -} - SYS_INIT(posix_thread_pool_init, PRE_KERNEL_1, 0); From 1d5d2c78dd909ce58498ed5ac8a3c145b57c4361 Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Wed, 22 Nov 2023 22:02:21 -0500 Subject: [PATCH 2/6] tests: posix: headers: check pthread_spin_lock() et al exist The simple header test was not updated to verify that the following functions were implemented even though they were implemented some time ago. * pthread_spin_destroy() * pthread_spin_init() * pthread_spin_lock() * pthread_spin_trylock() * pthread_spin_unlock() Signed-off-by: Christopher Friedt --- tests/posix/headers/src/pthread_h.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/posix/headers/src/pthread_h.c b/tests/posix/headers/src/pthread_h.c index d535a134693b..80795ba9c280 100644 --- a/tests/posix/headers/src/pthread_h.c +++ b/tests/posix/headers/src/pthread_h.c @@ -152,10 +152,11 @@ ZTEST(posix_headers, test_pthread_h) zassert_not_null(pthread_setschedparam); /* zassert_not_null(pthread_setschedprio); */ /* not implemented */ zassert_not_null(pthread_setspecific); - /* zassert_not_null(pthread_spin_destroy); */ /* not implemented */ - /* zassert_not_null(pthread_spin_init); */ /* not implemented */ - /* zassert_not_null(pthread_spin_lock); */ /* not implemented */ - /* zassert_not_null(pthread_spin_unlock); */ /* not implemented */ + zassert_not_null(pthread_spin_destroy); + zassert_not_null(pthread_spin_init); + zassert_not_null(pthread_spin_lock); + zassert_not_null(pthread_spin_trylock); + zassert_not_null(pthread_spin_unlock); /* zassert_not_null(pthread_testcancel); */ /* not implemented */ } } From e5c6776a068a17ea14b83270f4e4b949555155e4 Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Wed, 22 Nov 2023 22:07:42 -0500 Subject: [PATCH 3/6] tests: posix: headers: ensure pthread_condattr_getclock() exist These calls were added some time ago, so ensure they are checked for existence. * pthread_condattr_getclock() * pthread_condattr_setclock() Signed-off-by: Christopher Friedt --- tests/posix/headers/src/pthread_h.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/posix/headers/src/pthread_h.c b/tests/posix/headers/src/pthread_h.c index 80795ba9c280..8e8844fbbcef 100644 --- a/tests/posix/headers/src/pthread_h.c +++ b/tests/posix/headers/src/pthread_h.c @@ -94,10 +94,10 @@ ZTEST(posix_headers, test_pthread_h) zassert_not_null(pthread_cond_timedwait); zassert_not_null(pthread_cond_wait); zassert_not_null(pthread_condattr_destroy); - /* zassert_not_null(pthread_condattr_getclock); */ /* not implemented */ + zassert_not_null(pthread_condattr_getclock); /* zassert_not_null(pthread_condattr_getpshared); */ /* not implemented */ zassert_not_null(pthread_condattr_init); - /* zassert_not_null(pthread_condattr_setclock); */ /* not implemented */ + zassert_not_null(pthread_condattr_setclock); /* zassert_not_null(pthread_condattr_setpshared); */ /* not implemented */ zassert_not_null(pthread_create); zassert_not_null(pthread_detach); From 5af73fefdee4ef8459245f2e39dc38cff4896057 Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Wed, 22 Nov 2023 22:12:54 -0500 Subject: [PATCH 4/6] posix: define PTHREAD_CANCELED globally Move the definition of PTHREAD_CANCELED from pthread.c to pthread.h. Signed-off-by: Christopher Friedt --- include/zephyr/posix/pthread.h | 1 + lib/posix/pthread.c | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/zephyr/posix/pthread.h b/include/zephyr/posix/pthread.h index d978e914a5e7..e9b2e70593f9 100644 --- a/include/zephyr/posix/pthread.h +++ b/include/zephyr/posix/pthread.h @@ -30,6 +30,7 @@ extern "C" { #define PTHREAD_PROCESS_SHARED 1 /* Pthread cancellation */ +#define PTHREAD_CANCELED ((void *)-1) #define _PTHREAD_CANCEL_POS 0 #define PTHREAD_CANCEL_ENABLE (0U << _PTHREAD_CANCEL_POS) #define PTHREAD_CANCEL_DISABLE BIT(_PTHREAD_CANCEL_POS) diff --git a/lib/posix/pthread.c b/lib/posix/pthread.c index 070806ed758c..33a4b9265077 100644 --- a/lib/posix/pthread.c +++ b/lib/posix/pthread.c @@ -25,8 +25,7 @@ LOG_MODULE_REGISTER(pthread, CONFIG_PTHREAD_LOG_LEVEL); #define DYNAMIC_STACK_SIZE 0 #endif -#define PTHREAD_INIT_FLAGS PTHREAD_CANCEL_ENABLE -#define PTHREAD_CANCELED ((void *) -1) +#define PTHREAD_INIT_FLAGS PTHREAD_CANCEL_ENABLE enum posix_thread_qid { /* ready to be started via pthread_create() */ From 4c745e1257c5c12ba101554b89ddb87c4a2dd150 Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Wed, 22 Nov 2023 22:13:49 -0500 Subject: [PATCH 5/6] tests: posix: headers: add verification tests for more constants Ensure that the "headers" test checks that the following constants are defined: * PTHREAD_PROCESS_SHARED * PTHREAD_PROCESS_PRIVATE * PTHREAD_COND_INITIALIZER * PTHREAD_MUTEX_INITIALIZER * PTHREAD_CANCELED They were already defined by previous commits, but the test had not been updated. Signed-off-by: Christopher Friedt --- tests/posix/headers/src/pthread_h.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/posix/headers/src/pthread_h.c b/tests/posix/headers/src/pthread_h.c index 8e8844fbbcef..32929a8ce87d 100644 --- a/tests/posix/headers/src/pthread_h.c +++ b/tests/posix/headers/src/pthread_h.c @@ -28,7 +28,7 @@ ZTEST(posix_headers, test_pthread_h) /* zassert_not_equal(-1, PTHREAD_CANCEL_DEFERRED); */ /* not implemented */ zassert_not_equal(-1, PTHREAD_CANCEL_DISABLE); - /* zassert_not_equal(-1, PTHREAD_CANCELED); */ /* not implemented */ + zassert_not_equal((void *)-42, PTHREAD_CANCELED); zassert_not_equal(-1, PTHREAD_CREATE_DETACHED); zassert_not_equal(-1, PTHREAD_CREATE_JOINABLE); @@ -49,14 +49,14 @@ ZTEST(posix_headers, test_pthread_h) zassert_not_equal(-1, PTHREAD_PRIO_NONE); /* zassert_not_equal(-1, PTHREAD_PRIO_PROTECT); */ /* not implemented */ - /* zassert_not_equal(-1, PTHREAD_PROCESS_SHARED); */ /* not implemented */ - /* zassert_not_equal(-1, PTHREAD_PROCESS_PRIVATE); */ /* not implemented */ + zassert_not_equal(-1, PTHREAD_PROCESS_SHARED); + zassert_not_equal(-1, PTHREAD_PROCESS_PRIVATE); /* zassert_not_equal(-1, PTHREAD_SCOPE_PROCESS); */ /* not implemented */ /* zassert_not_equal(-1, PTHREAD_SCOPE_SYSTEM); */ /* not implemented */ - /* pthread_cond_t cond = PTHREAD_COND_INITIALIZER; */ /* not implemented */ - /* pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER; */ /* not implemented */ + pthread_cond_t cond = PTHREAD_COND_INITIALIZER; + pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER; /* pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER; */ /* not implemented */ if (IS_ENABLED(CONFIG_POSIX_API)) { From 9b233d5adccf8626462dacf86d03c51d341e232d Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Wed, 22 Nov 2023 22:23:44 -0500 Subject: [PATCH 6/6] posix: do not define _PTHREAD_CANCEL_POS _PTHREAD_CANCEL_POS is an implementation detail and should not be defined in the global scope. Furthermore, _PTHREAD_CANCEL_POS uses a reserved identifier (underscore followed by capital letter). Adjust definitions so that the implementation detail is only used in the implementation and not in the interface. Additionally, modify naming so that the non-standard macro does not use a reserved identifier. Signed-off-by: Christopher Friedt --- include/zephyr/posix/pthread.h | 7 +++---- lib/posix/pthread.c | 5 ++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/include/zephyr/posix/pthread.h b/include/zephyr/posix/pthread.h index e9b2e70593f9..39c2cda46cf2 100644 --- a/include/zephyr/posix/pthread.h +++ b/include/zephyr/posix/pthread.h @@ -30,10 +30,9 @@ extern "C" { #define PTHREAD_PROCESS_SHARED 1 /* Pthread cancellation */ -#define PTHREAD_CANCELED ((void *)-1) -#define _PTHREAD_CANCEL_POS 0 -#define PTHREAD_CANCEL_ENABLE (0U << _PTHREAD_CANCEL_POS) -#define PTHREAD_CANCEL_DISABLE BIT(_PTHREAD_CANCEL_POS) +#define PTHREAD_CANCELED ((void *)-1) +#define PTHREAD_CANCEL_ENABLE 0 +#define PTHREAD_CANCEL_DISABLE 1 /* Passed to pthread_once */ #define PTHREAD_ONCE_INIT \ diff --git a/lib/posix/pthread.c b/lib/posix/pthread.c index 33a4b9265077..f0808694500e 100644 --- a/lib/posix/pthread.c +++ b/lib/posix/pthread.c @@ -16,6 +16,7 @@ #include #include #include +#include LOG_MODULE_REGISTER(pthread, CONFIG_PTHREAD_LOG_LEVEL); @@ -25,6 +26,8 @@ LOG_MODULE_REGISTER(pthread, CONFIG_PTHREAD_LOG_LEVEL); #define DYNAMIC_STACK_SIZE 0 #endif +#define _pthread_cancel_pos LOG2(PTHREAD_CANCEL_DISABLE) + #define PTHREAD_INIT_FLAGS PTHREAD_CANCEL_ENABLE enum posix_thread_qid { @@ -406,7 +409,7 @@ int pthread_create(pthread_t *th, const pthread_attr_t *_attr, void *(*threadrou sys_dlist_append(&run_q, &t->q_node); t->qid = POSIX_THREAD_RUN_Q; t->detachstate = attr->detachstate; - if ((BIT(_PTHREAD_CANCEL_POS) & attr->flags) != 0) { + if ((BIT(_pthread_cancel_pos) & attr->flags) != 0) { t->cancel_state = PTHREAD_CANCEL_ENABLE; } t->cancel_pending = false;