From 669edc0d5e250d0f9e3fe151380c8b913de62ed8 Mon Sep 17 00:00:00 2001 From: Gaetan Perrot Date: Fri, 2 Feb 2024 17:45:25 +0900 Subject: [PATCH] posix: Implement set and get scope APIs for pthread attr Implement `pthread_attr_setscope()` and `pthread_attr_getscope()` are required as part of _POSIX_THREAD_PRIORITY_SCHEDULING Option Group. signed-off-by: Gaetan Perrot --- include/zephyr/posix/pthread.h | 8 ++++++ lib/posix/options/posix_internal.h | 1 + lib/posix/options/pthread.c | 44 ++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/include/zephyr/posix/pthread.h b/include/zephyr/posix/pthread.h index 023473cdb1..7ffdb1318d 100644 --- a/include/zephyr/posix/pthread.h +++ b/include/zephyr/posix/pthread.h @@ -39,6 +39,12 @@ extern "C" { #define PTHREAD_CANCEL_DEFERRED 0 #define PTHREAD_CANCEL_ASYNCHRONOUS 1 +/* Pthread scope */ +#undef PTHREAD_SCOPE_PROCESS +#define PTHREAD_SCOPE_PROCESS 1 +#undef PTHREAD_SCOPE_SYSTEM +#define PTHREAD_SCOPE_SYSTEM 0 + /* Passed to pthread_once */ #define PTHREAD_ONCE_INIT {0} @@ -419,6 +425,8 @@ int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t *stacksize); int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize); +int pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope); +int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope); #ifdef CONFIG_PTHREAD_IPC int pthread_once(pthread_once_t *once, void (*initFunc)(void)); #endif diff --git a/lib/posix/options/posix_internal.h b/lib/posix/options/posix_internal.h index 17d8c29d43..a723da17f9 100644 --- a/lib/posix/options/posix_internal.h +++ b/lib/posix/options/posix_internal.h @@ -29,6 +29,7 @@ struct posix_thread_attr { uint16_t guardsize : CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_BITS; int8_t priority; uint8_t schedpolicy: 2; + bool contentionscope: 1; union { bool caller_destroys: 1; bool initialized: 1; diff --git a/lib/posix/options/pthread.c b/lib/posix/options/pthread.c index da771fef9b..086d6a940a 100644 --- a/lib/posix/options/pthread.c +++ b/lib/posix/options/pthread.c @@ -384,6 +384,49 @@ int pthread_attr_setstack(pthread_attr_t *_attr, void *stackaddr, size_t stacksi return 0; } +/** + * @brief Get scope attributes in thread attributes object. + * + * See IEEE 1003.1 + */ +int pthread_attr_getscope(const pthread_attr_t *_attr, int *contentionscope) +{ + struct posix_thread_attr *attr = (struct posix_thread_attr *)_attr; + + if (!__attr_is_initialized(attr) || contentionscope == NULL) { + return EINVAL; + } + *contentionscope = attr->contentionscope; + return 0; +} + +/** + * @brief Set scope attributes in thread attributes object. + * + * See IEEE 1003.1 + */ +int pthread_attr_setscope(pthread_attr_t *_attr, int contentionscope) +{ + struct posix_thread_attr *attr = (struct posix_thread_attr *)_attr; + + if (!__attr_is_initialized(attr)) { + LOG_DBG("attr %p is not initialized", attr); + return EINVAL; + } + if (!(contentionscope == PTHREAD_SCOPE_PROCESS || + contentionscope == PTHREAD_SCOPE_SYSTEM)) { + LOG_DBG("%s contentionscope %d", "Invalid", contentionscope); + return EINVAL; + } + if (contentionscope == PTHREAD_SCOPE_PROCESS) { + /* Zephyr does not yet support processes or process scheduling */ + LOG_DBG("%s contentionscope %d", "Unsupported", contentionscope); + return ENOTSUP; + } + attr->contentionscope = contentionscope; + return 0; +} + static void posix_thread_recycle_work_handler(struct k_work *work) { ARG_UNUSED(work); @@ -797,6 +840,7 @@ int pthread_attr_init(pthread_attr_t *_attr) *attr = (struct posix_thread_attr){0}; attr->guardsize = CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_DEFAULT; + attr->contentionscope = PTHREAD_SCOPE_SYSTEM; if (DYNAMIC_STACK_SIZE > 0) { attr->stack = k_thread_stack_alloc(DYNAMIC_STACK_SIZE + attr->guardsize,