From 23e5c10ca59b1f1d82577bc59dda56f717cc1eec Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Thu, 23 Nov 2023 12:06:27 -0500 Subject: [PATCH] tests: posix: test pthread_cleanup_push() / pop() Add a test for pthread_cleanup_push() and pthread_cleanup_pop(). Signed-off-by: Christopher Friedt --- tests/posix/common/src/pthread.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/posix/common/src/pthread.c b/tests/posix/common/src/pthread.c index 6b7cb25616e33d4..9586a82cabdb66e 100644 --- a/tests/posix/common/src/pthread.c +++ b/tests/posix/common/src/pthread.c @@ -882,3 +882,33 @@ ZTEST(posix_apis, test_pthread_join_detached) /* need to allow this thread to be clean-up by the recycler */ k_msleep(500); } + +static void cleanup_handler(void *arg) +{ + bool *boolp = (bool *)arg; + + *boolp = true; +} + +static void *test_pthread_cleanup_entry(void *arg) +{ + bool executed[2] = {0}; + + pthread_cleanup_push(cleanup_handler, &executed[0]); + pthread_cleanup_push(cleanup_handler, &executed[1]); + pthread_cleanup_pop(false); + pthread_cleanup_pop(true); + + zassert_true(executed[0]); + zassert_false(executed[1]); + + return NULL; +} + +ZTEST(posix_apis, test_pthread_cleanup) +{ + pthread_t th; + + zassert_ok(pthread_create(&th, NULL, test_pthread_cleanup_entry, NULL)); + zassert_ok(pthread_join(th, NULL)); +}