Skip to content

Commit

Permalink
posix: Implement set and get scope APIs for pthread attr
Browse files Browse the repository at this point in the history
Implement `pthread_attr_setscope()` and `pthread_attr_getscope()`
is required as part of _POSIX_THREAD_PRIORITY_SCHEDULING Option Group.

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
  • Loading branch information
moonlight83340 committed Feb 2, 2024
1 parent 24ebebd commit 6b4042b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/zephyr/posix/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ extern "C" {
#define PTHREAD_CANCEL_DEFERRED 0
#define PTHREAD_CANCEL_ASYNCHRONOUS 1

/* Pthread scope */
#define PTHREAD_SCOPE_PROCESS 0
#define PTHREAD_SCOPE_SYSTEM 1

/* Passed to pthread_once */
#define PTHREAD_ONCE_INIT {0}

Expand Down Expand Up @@ -429,6 +433,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
Expand Down
1 change: 1 addition & 0 deletions lib/posix/options/posix_internal.h
Original file line number Diff line number Diff line change
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;
uint8_t contentionscope: 2;
union {
bool caller_destroys: 1;
bool initialized: 1;
Expand Down
34 changes: 34 additions & 0 deletions lib/posix/options/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,40 @@ int pthread_attr_setstack(pthread_attr_t *_attr, void *stackaddr, size_t stacksi
return 0;
}

/**
* @brief Set 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) ||
(contentionscope != PTHREAD_SCOPE_SYSTEM && contentionscope != PTHREAD_SCOPE_PROCESS)) {
LOG_ERR("Invalid pthread_attr_t or contentionscope");
return EINVAL;
}
attr->contentionscope = contentionscope;
return 0;
}

static void posix_thread_recycle_work_handler(struct k_work *work)
{
ARG_UNUSED(work);
Expand Down

0 comments on commit 6b4042b

Please sign in to comment.