Skip to content

Commit

Permalink
posix: Implement set and get inheritsched APIs for pthread attr
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
moonlight83340 committed Feb 6, 2024
1 parent a5ae2d7 commit ab5db34
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/zephyr/posix/pthread.h
Expand Up @@ -39,6 +39,12 @@ extern "C" {
#define PTHREAD_CANCEL_DEFERRED 0
#define PTHREAD_CANCEL_ASYNCHRONOUS 1

/* 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}

Expand Down Expand Up @@ -429,6 +435,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_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
Expand Down
1 change: 1 addition & 0 deletions lib/posix/options/posix_internal.h
Expand Up @@ -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 inheritsched: 1;
union {
bool caller_destroys: 1;
bool initialized: 1;
Expand Down
54 changes: 54 additions & 0 deletions lib/posix/options/pthread.c
Expand Up @@ -384,6 +384,45 @@ int pthread_attr_setstack(pthread_attr_t *_attr, void *stackaddr, size_t stacksi
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);
Expand Down Expand Up @@ -556,6 +595,20 @@ int pthread_create(pthread_t *th, const pthread_attr_t *_attr, void *(*threadrou
t->attr = *(struct posix_thread_attr *)_attr;
}

struct posix_thread *self = NULL;

K_SPINLOCK(&pthread_pool_lock) {
self = to_posix_thread(pthread_self());
if (self == NULL) {
K_SPINLOCK_BREAK;
}
if (self_attr.inheritsched == PTHREAD_INHERIT_SCHED) {
t->attr.priority = self->attr.priority;
t->attr.schedpolicy = self->attr.schedpolicy;
t->attr.inheritsched = self->attr.inheritsched;
}
}

/* spawn the thread */
k_thread_create(
&t->thread, t->attr.stack, __get_attr_stacksize(&t->attr) + t->attr.guardsize,
Expand Down Expand Up @@ -797,6 +850,7 @@ int pthread_attr_init(pthread_attr_t *_attr)

*attr = (struct posix_thread_attr){0};
attr->guardsize = CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_DEFAULT;
attr->inheritsched = PTHREAD_INHERIT_SCHED;

if (DYNAMIC_STACK_SIZE > 0) {
attr->stack = k_thread_stack_alloc(DYNAMIC_STACK_SIZE + attr->guardsize,
Expand Down

0 comments on commit ab5db34

Please sign in to comment.