Skip to content

posix: pthread: support for pthread_setcanceltype() #65644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/services/portability/posix/option_groups/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ multiple processes.
pthread_once(),yes
pthread_self(),yes
pthread_setcancelstate(),yes
pthread_setcanceltype(),
pthread_setcanceltype(),yes
pthread_setspecific(),yes
pthread_sigmask(),
pthread_testcancel(),
Expand Down
3 changes: 3 additions & 0 deletions include/zephyr/posix/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lib/posix/posix_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct posix_thread {

/* Pthread cancellation */
uint8_t cancel_state;
uint8_t cancel_type;
bool cancel_pending;

/* Detach state */
Expand Down
28 changes: 28 additions & 0 deletions lib/posix/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
9 changes: 5 additions & 4 deletions tests/posix/headers/src/pthread_h.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ ZTEST(posix_headers, test_pthread_h)
{
zassert_not_equal(-1, PTHREAD_BARRIER_SERIAL_THREAD);

/* zassert_not_equal(-1, PTHREAD_CANCEL_ASYNCHRONOUS); */ /* not implemented */
zassert_not_equal(-1, PTHREAD_CANCEL_ASYNCHRONOUS);
zassert_not_equal(-1, PTHREAD_CANCEL_DEFERRED);

zassert_not_equal(-1, PTHREAD_CANCEL_ENABLE);
/* zassert_not_equal(-1, PTHREAD_CANCEL_DEFERRED); */ /* not implemented */
zassert_not_equal(-1, PTHREAD_CANCEL_DISABLE);

zassert_not_equal((void *)-42, PTHREAD_CANCELED);
Expand Down Expand Up @@ -146,8 +147,8 @@ ZTEST(posix_headers, test_pthread_h)
zassert_not_null(pthread_rwlockattr_init);
/* zassert_not_null(pthread_rwlockattr_setpshared); */ /* not implemented */
zassert_not_null(pthread_self);
/* zassert_not_null(pthread_setcancelstate); */ /* not implemented */
/* zassert_not_null(pthread_setcanceltype); */ /* not implemented */
zassert_not_null(pthread_setcancelstate);
zassert_not_null(pthread_setcanceltype);
/* zassert_not_null(pthread_setconcurrency); */ /* not implemented */
zassert_not_null(pthread_setschedparam);
/* zassert_not_null(pthread_setschedprio); */ /* not implemented */
Expand Down