posix: Implement set and get inheritsched APIs for pthread attr

Implement `pthread_attr_setinheritsched()` and
`pthread_attr_getinheritsched()`are required
as part of _POSIX_THREAD_PRIORITY_SCHEDULING Option Group.

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
This commit is contained in:
Gaetan Perrot 2024-02-09 14:49:37 +09:00 committed by Fabio Baltieri
parent f7f5c4470a
commit d61a7b2777
3 changed files with 58 additions and 1 deletions

View file

@ -45,6 +45,12 @@ extern "C" {
#undef PTHREAD_SCOPE_SYSTEM #undef PTHREAD_SCOPE_SYSTEM
#define PTHREAD_SCOPE_SYSTEM 0 #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 */ /* Passed to pthread_once */
#define PTHREAD_ONCE_INIT {0} #define PTHREAD_ONCE_INIT {0}
@ -423,6 +429,8 @@ int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
size_t stacksize); size_t stacksize);
int pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope); int pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope);
int pthread_attr_setscope(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 #ifdef CONFIG_PTHREAD_IPC
int pthread_once(pthread_once_t *once, void (*initFunc)(void)); int pthread_once(pthread_once_t *once, void (*initFunc)(void));
#endif #endif

View file

@ -30,6 +30,7 @@ struct posix_thread_attr {
int8_t priority; int8_t priority;
uint8_t schedpolicy: 2; uint8_t schedpolicy: 2;
bool contentionscope: 1; bool contentionscope: 1;
bool inheritsched: 1;
union { union {
bool caller_destroys: 1; bool caller_destroys: 1;
bool initialized: 1; bool initialized: 1;

View file

@ -414,7 +414,7 @@ int pthread_attr_setscope(pthread_attr_t *_attr, int contentionscope)
return EINVAL; return EINVAL;
} }
if (!(contentionscope == PTHREAD_SCOPE_PROCESS || if (!(contentionscope == PTHREAD_SCOPE_PROCESS ||
contentionscope == PTHREAD_SCOPE_SYSTEM)) { contentionscope == PTHREAD_SCOPE_SYSTEM)) {
LOG_DBG("%s contentionscope %d", "Invalid", contentionscope); LOG_DBG("%s contentionscope %d", "Invalid", contentionscope);
return EINVAL; return EINVAL;
} }
@ -427,6 +427,45 @@ int pthread_attr_setscope(pthread_attr_t *_attr, int contentionscope)
return 0; 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) static void posix_thread_recycle_work_handler(struct k_work *work)
{ {
ARG_UNUSED(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; 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 */ /* spawn the thread */
k_thread_create( k_thread_create(
&t->thread, t->attr.stack, __get_attr_stacksize(&t->attr) + t->attr.guardsize, &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 = (struct posix_thread_attr){0};
attr->guardsize = CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_DEFAULT; attr->guardsize = CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_DEFAULT;
attr->contentionscope = PTHREAD_SCOPE_SYSTEM; attr->contentionscope = PTHREAD_SCOPE_SYSTEM;
attr->inheritsched = PTHREAD_INHERIT_SCHED;
if (DYNAMIC_STACK_SIZE > 0) { if (DYNAMIC_STACK_SIZE > 0) {
attr->stack = k_thread_stack_alloc(DYNAMIC_STACK_SIZE + attr->guardsize, attr->stack = k_thread_stack_alloc(DYNAMIC_STACK_SIZE + attr->guardsize,