posix: pthread: support for pthread_setcanceltype()

pthread_setcanceltype() is required by the POSIX_THREADS_BASE
Option Group as detailed in Section E.1 of IEEE-1003.1-2017.

The POSIX_THREADS_BASE Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
This commit is contained in:
Christopher Friedt 2023-11-22 23:09:57 -05:00 committed by Chris Friedt
parent 20979f80a6
commit 1e7eb7a6da
3 changed files with 32 additions and 0 deletions

View file

@ -33,6 +33,8 @@ extern "C" {
#define PTHREAD_CANCELED ((void *)-1) #define PTHREAD_CANCELED ((void *)-1)
#define PTHREAD_CANCEL_ENABLE 0 #define PTHREAD_CANCEL_ENABLE 0
#define PTHREAD_CANCEL_DISABLE 1 #define PTHREAD_CANCEL_DISABLE 1
#define PTHREAD_CANCEL_DEFERRED 0
#define PTHREAD_CANCEL_ASYNCHRONOUS 1
/* Passed to pthread_once */ /* Passed to pthread_once */
#define PTHREAD_ONCE_INIT \ #define PTHREAD_ONCE_INIT \
@ -452,6 +454,7 @@ int pthread_detach(pthread_t thread);
int pthread_create(pthread_t *newthread, const pthread_attr_t *attr, int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
void *(*threadroutine)(void *), void *arg); void *(*threadroutine)(void *), void *arg);
int pthread_setcancelstate(int state, int *oldstate); int pthread_setcancelstate(int state, int *oldstate);
int pthread_setcanceltype(int type, int *oldtype);
int pthread_attr_setschedparam(pthread_attr_t *attr, int pthread_attr_setschedparam(pthread_attr_t *attr,
const struct sched_param *schedparam); const struct sched_param *schedparam);
int pthread_setschedparam(pthread_t pthread, int policy, int pthread_setschedparam(pthread_t pthread, int policy,

View file

@ -38,6 +38,7 @@ struct posix_thread {
/* Pthread cancellation */ /* Pthread cancellation */
uint8_t cancel_state; uint8_t cancel_state;
uint8_t cancel_type;
bool cancel_pending; bool cancel_pending;
/* Detach state */ /* Detach state */

View file

@ -500,6 +500,34 @@ int pthread_setcancelstate(int state, int *oldstate)
return 0; return 0;
} }
/**
* @brief Set cancelability Type.
*
* See IEEE 1003.1
*/
int pthread_setcanceltype(int type, int *oldtype)
{
k_spinlock_key_t key;
struct posix_thread *t;
if (type != PTHREAD_CANCEL_DEFERRED && type != PTHREAD_CANCEL_ASYNCHRONOUS) {
LOG_ERR("Invalid pthread cancel type %d", type);
return EINVAL;
}
t = to_posix_thread(pthread_self());
if (t == NULL) {
return EINVAL;
}
key = k_spin_lock(&pthread_pool_lock);
*oldtype = t->cancel_type;
t->cancel_type = type;
k_spin_unlock(&pthread_pool_lock, key);
return 0;
}
/** /**
* @brief Cancel execution of a thread. * @brief Cancel execution of a thread.
* *