diff --git a/include/zephyr/posix/pthread.h b/include/zephyr/posix/pthread.h index d950c9ea7f..7d272fda5e 100644 --- a/include/zephyr/posix/pthread.h +++ b/include/zephyr/posix/pthread.h @@ -45,6 +45,12 @@ extern "C" { #undef PTHREAD_SCOPE_SYSTEM #define PTHREAD_SCOPE_SYSTEM 0 +/* Pthread inherit scheduler */ +#undef PTHREAD_INHERIT_SCHED +#define PTHREAD_INHERIT_SCHED 0 +#undef PTHREAD_EXPLICIT_SCHED +#define PTHREAD_EXPLICIT_SCHED 1 + /* Passed to pthread_once */ #define PTHREAD_ONCE_INIT {0} @@ -423,6 +429,8 @@ 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); +int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched); +int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched); #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 a723da17f9..06cc8d05a1 100644 --- a/lib/posix/options/posix_internal.h +++ b/lib/posix/options/posix_internal.h @@ -30,6 +30,7 @@ struct posix_thread_attr { int8_t priority; uint8_t schedpolicy: 2; bool contentionscope: 1; + bool inheritsched: 1; union { bool caller_destroys: 1; bool initialized: 1; diff --git a/lib/posix/options/pthread.c b/lib/posix/options/pthread.c index 086d6a940a..e7fe47fb03 100644 --- a/lib/posix/options/pthread.c +++ b/lib/posix/options/pthread.c @@ -414,7 +414,7 @@ int pthread_attr_setscope(pthread_attr_t *_attr, int contentionscope) return EINVAL; } if (!(contentionscope == PTHREAD_SCOPE_PROCESS || - contentionscope == PTHREAD_SCOPE_SYSTEM)) { + contentionscope == PTHREAD_SCOPE_SYSTEM)) { LOG_DBG("%s contentionscope %d", "Invalid", contentionscope); return EINVAL; } @@ -427,6 +427,45 @@ int pthread_attr_setscope(pthread_attr_t *_attr, int contentionscope) return 0; } +/** + * @brief Get inherit scheduler attributes in thread attributes object. + * + * See IEEE 1003.1 + */ +int pthread_attr_getinheritsched(const pthread_attr_t *_attr, int *inheritsched) +{ + struct posix_thread_attr *attr = (struct posix_thread_attr *)_attr; + + if (!__attr_is_initialized(attr) || inheritsched == NULL) { + return EINVAL; + } + *inheritsched = attr->inheritsched; + return 0; +} + +/** + * @brief Set inherit scheduler attributes in thread attributes object. + * + * See IEEE 1003.1 + */ +int pthread_attr_setinheritsched(pthread_attr_t *_attr, int inheritsched) +{ + 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 (inheritsched != PTHREAD_INHERIT_SCHED && inheritsched != PTHREAD_EXPLICIT_SCHED) { + LOG_DBG("Invalid inheritsched %d", inheritsched); + return EINVAL; + } + + attr->inheritsched = inheritsched; + return 0; +} + static void posix_thread_recycle_work_handler(struct k_work *work) { ARG_UNUSED(work); @@ -599,6 +638,14 @@ int pthread_create(pthread_t *th, const pthread_attr_t *_attr, void *(*threadrou t->attr = *(struct posix_thread_attr *)_attr; } + if (t->attr.inheritsched == PTHREAD_INHERIT_SCHED) { + int pol; + + t->attr.priority = + zephyr_to_posix_priority(k_thread_priority_get(k_current_get()), &pol); + t->attr.schedpolicy = pol; + } + /* spawn the thread */ k_thread_create( &t->thread, t->attr.stack, __get_attr_stacksize(&t->attr) + t->attr.guardsize, @@ -841,6 +888,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; + attr->inheritsched = PTHREAD_INHERIT_SCHED; if (DYNAMIC_STACK_SIZE > 0) { attr->stack = k_thread_stack_alloc(DYNAMIC_STACK_SIZE + attr->guardsize,