diff --git a/include/zephyr/posix/pthread.h b/include/zephyr/posix/pthread.h index c7909d40ae..a7feef8bc7 100644 --- a/include/zephyr/posix/pthread.h +++ b/include/zephyr/posix/pthread.h @@ -33,6 +33,8 @@ extern "C" { #define PTHREAD_CANCELED ((void *)-1) #define PTHREAD_CANCEL_ENABLE 0 #define PTHREAD_CANCEL_DISABLE 1 +#define PTHREAD_CANCEL_DEFERRED 0 +#define PTHREAD_CANCEL_ASYNCHRONOUS 1 /* Passed to pthread_once */ #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, void *(*threadroutine)(void *), void *arg); int pthread_setcancelstate(int state, int *oldstate); +int pthread_setcanceltype(int type, int *oldtype); int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *schedparam); int pthread_setschedparam(pthread_t pthread, int policy, diff --git a/lib/posix/posix_internal.h b/lib/posix/posix_internal.h index 39c64c5333..4ce000e514 100644 --- a/lib/posix/posix_internal.h +++ b/lib/posix/posix_internal.h @@ -38,6 +38,7 @@ struct posix_thread { /* Pthread cancellation */ uint8_t cancel_state; + uint8_t cancel_type; bool cancel_pending; /* Detach state */ diff --git a/lib/posix/pthread.c b/lib/posix/pthread.c index e96cfc287e..d00d5564c7 100644 --- a/lib/posix/pthread.c +++ b/lib/posix/pthread.c @@ -500,6 +500,34 @@ int pthread_setcancelstate(int state, int *oldstate) 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. *