Skip to content

Commit 1e7eb7a

Browse files
committed
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 <[email protected]>
1 parent 20979f8 commit 1e7eb7a

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

include/zephyr/posix/pthread.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ extern "C" {
3333
#define PTHREAD_CANCELED ((void *)-1)
3434
#define PTHREAD_CANCEL_ENABLE 0
3535
#define PTHREAD_CANCEL_DISABLE 1
36+
#define PTHREAD_CANCEL_DEFERRED 0
37+
#define PTHREAD_CANCEL_ASYNCHRONOUS 1
3638

3739
/* Passed to pthread_once */
3840
#define PTHREAD_ONCE_INIT \
@@ -452,6 +454,7 @@ int pthread_detach(pthread_t thread);
452454
int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
453455
void *(*threadroutine)(void *), void *arg);
454456
int pthread_setcancelstate(int state, int *oldstate);
457+
int pthread_setcanceltype(int type, int *oldtype);
455458
int pthread_attr_setschedparam(pthread_attr_t *attr,
456459
const struct sched_param *schedparam);
457460
int pthread_setschedparam(pthread_t pthread, int policy,

lib/posix/posix_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct posix_thread {
3838

3939
/* Pthread cancellation */
4040
uint8_t cancel_state;
41+
uint8_t cancel_type;
4142
bool cancel_pending;
4243

4344
/* Detach state */

lib/posix/pthread.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,34 @@ int pthread_setcancelstate(int state, int *oldstate)
500500
return 0;
501501
}
502502

503+
/**
504+
* @brief Set cancelability Type.
505+
*
506+
* See IEEE 1003.1
507+
*/
508+
int pthread_setcanceltype(int type, int *oldtype)
509+
{
510+
k_spinlock_key_t key;
511+
struct posix_thread *t;
512+
513+
if (type != PTHREAD_CANCEL_DEFERRED && type != PTHREAD_CANCEL_ASYNCHRONOUS) {
514+
LOG_ERR("Invalid pthread cancel type %d", type);
515+
return EINVAL;
516+
}
517+
518+
t = to_posix_thread(pthread_self());
519+
if (t == NULL) {
520+
return EINVAL;
521+
}
522+
523+
key = k_spin_lock(&pthread_pool_lock);
524+
*oldtype = t->cancel_type;
525+
t->cancel_type = type;
526+
k_spin_unlock(&pthread_pool_lock, key);
527+
528+
return 0;
529+
}
530+
503531
/**
504532
* @brief Cancel execution of a thread.
505533
*

0 commit comments

Comments
 (0)