tests: posix: add tests for pthread_testcancel()

Add tests for pthread_testcancel()

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
This commit is contained in:
Gaetan Perrot 2024-01-28 19:47:31 +09:00 committed by Carles Cufí
parent fe5806aff0
commit 042b522506
2 changed files with 41 additions and 1 deletions

View file

@ -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);

View file

@ -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