Skip to content

Commit 9cd7564

Browse files
moonlight83340carlescufi
authored andcommitted
posix: pthread_testcancel #59946
Implement posix pthread_testcancel() signed-off-by: Gaetan Perrot <[email protected]>
1 parent f00f846 commit 9cd7564

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

include/zephyr/posix/pthread.h

+1
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
440440
void *(*threadroutine)(void *), void *arg);
441441
int pthread_setcancelstate(int state, int *oldstate);
442442
int pthread_setcanceltype(int type, int *oldtype);
443+
void pthread_testcancel(void);
443444
int pthread_attr_setschedparam(pthread_attr_t *attr,
444445
const struct sched_param *schedparam);
445446
int pthread_setschedparam(pthread_t pthread, int policy,

lib/posix/options/pthread.c

+29
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,35 @@ int pthread_setcanceltype(int type, int *oldtype)
679679
return ret;
680680
}
681681

682+
/**
683+
* @brief Create a cancellation point in the calling thread.
684+
*
685+
* See IEEE 1003.1
686+
*/
687+
void pthread_testcancel(void)
688+
{
689+
struct posix_thread *t;
690+
bool cancel_pended = false;
691+
692+
K_SPINLOCK(&pthread_pool_lock) {
693+
t = to_posix_thread(pthread_self());
694+
if (t == NULL) {
695+
K_SPINLOCK_BREAK;
696+
}
697+
if (t->attr.cancelstate != PTHREAD_CANCEL_ENABLE) {
698+
K_SPINLOCK_BREAK;
699+
}
700+
if (t->attr.cancelpending) {
701+
cancel_pended = true;
702+
t->attr.cancelstate = PTHREAD_CANCEL_DISABLE;
703+
}
704+
}
705+
706+
if (cancel_pended) {
707+
posix_thread_finalize(t, PTHREAD_CANCELED);
708+
}
709+
}
710+
682711
/**
683712
* @brief Cancel execution of a thread.
684713
*

0 commit comments

Comments
 (0)