From 042b52250677d737eac5828411f50c43219311ea Mon Sep 17 00:00:00 2001 From: Gaetan Perrot Date: Sun, 28 Jan 2024 19:47:31 +0900 Subject: [PATCH] tests: posix: add tests for pthread_testcancel() Add tests for pthread_testcancel() signed-off-by: Gaetan Perrot --- tests/posix/common/src/pthread.c | 40 +++++++++++++++++++++++++++++ tests/posix/headers/src/pthread_h.c | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/posix/common/src/pthread.c b/tests/posix/common/src/pthread.c index ffc408d9da..184cb77e54 100644 --- a/tests/posix/common/src/pthread.c +++ b/tests/posix/common/src/pthread.c @@ -487,6 +487,46 @@ ZTEST(pthread, test_pthread_cleanup) zassert_ok(pthread_join(th, NULL)); } +static bool testcancel_ignored; +static bool testcancel_failed; + +static void *test_pthread_cancel_fn(void *arg) +{ + zassert_ok(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL)); + + testcancel_ignored = false; + + /* this should be ignored */ + pthread_testcancel(); + + testcancel_ignored = true; + + /* this will mark it pending */ + zassert_ok(pthread_cancel(pthread_self())); + + /* enable the thread to be cancelled */ + zassert_ok(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)); + + testcancel_failed = false; + + /* this should terminate the thread */ + pthread_testcancel(); + + testcancel_failed = true; + + return NULL; +} + +ZTEST(pthread, test_pthread_testcancel) +{ + pthread_t th; + + zassert_ok(pthread_create(&th, NULL, test_pthread_cancel_fn, NULL)); + zassert_ok(pthread_join(th, NULL)); + zassert_true(testcancel_ignored); + zassert_false(testcancel_failed); +} + static void before(void *arg) { ARG_UNUSED(arg); diff --git a/tests/posix/headers/src/pthread_h.c b/tests/posix/headers/src/pthread_h.c index 82e71c5c21..73467d14bf 100644 --- a/tests/posix/headers/src/pthread_h.c +++ b/tests/posix/headers/src/pthread_h.c @@ -158,7 +158,7 @@ ZTEST(posix_headers, test_pthread_h) zassert_not_null(pthread_spin_lock); zassert_not_null(pthread_spin_trylock); zassert_not_null(pthread_spin_unlock); - /* zassert_not_null(pthread_testcancel); */ /* not implemented */ + zassert_not_null(pthread_testcancel); } } #pragma GCC diagnostic pop