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 <gaetanperrotpro@gmail.com>
This commit is contained in:
Gaetan Perrot 2024-02-02 17:45:25 +09:00 committed by Alberto Escolar
parent 76cd676050
commit 669edc0d5e
3 changed files with 53 additions and 0 deletions

View file

@ -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

View file

@ -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;

View file

@ -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,