Skip to content

tests: posix: common: separate posix threads base into a standalone test #81339

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions tests/posix/threads_base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(posix_threads_base)

FILE(GLOB app_sources src/*.c)

target_sources(app PRIVATE ${app_sources})

target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L)
13 changes: 13 additions & 0 deletions tests/posix/threads_base/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CONFIG_POSIX_API=y
CONFIG_ZTEST=y

CONFIG_POSIX_AEP_CHOICE_BASE=y
CONFIG_POSIX_THREADS=y
CONFIG_THREAD_NAME=y
CONFIG_DYNAMIC_THREAD=y
CONFIG_THREAD_STACK_INFO=y
CONFIG_DYNAMIC_THREAD_POOL_SIZE=3
CONFIG_POSIX_THREAD_THREADS_MAX=3
CONFIG_HEAP_MEM_POOL_SIZE=4096
CONFIG_POSIX_THREAD_KEYS_MAX=6
CONFIG_TEST_EXTRA_STACK_SIZE=4096
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @details Exactly CONFIG_MAX_PTHREAD_COND_COUNT can be in use at once.
*/
ZTEST(cond, test_cond_resource_exhausted)
ZTEST(posix_threads_base, test_cond_resource_exhausted)
{
size_t i;
pthread_cond_t m[CONFIG_MAX_PTHREAD_COND_COUNT + 1];
Expand All @@ -37,7 +37,7 @@ ZTEST(cond, test_cond_resource_exhausted)
*
* @details Demonstrate that condition variables may be used over and over again.
*/
ZTEST(cond, test_cond_resource_leak)
ZTEST(posix_threads_base, test_cond_resource_leak)
{
pthread_cond_t cond;

Expand All @@ -47,7 +47,7 @@ ZTEST(cond, test_cond_resource_leak)
}
}

ZTEST(cond, test_pthread_condattr)
ZTEST(posix_threads_base, test_pthread_condattr)
{
pthread_condattr_t att = {0};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <zephyr/sys/util.h>
#include <zephyr/ztest.h>

#define N_THR 2
#define N_THR 2
#define N_KEY 2
#define BUFFSZ 48

Expand Down Expand Up @@ -74,7 +74,7 @@ static void make_keys(void)
* multiple keys.
*/

ZTEST(key, test_key_1toN_thread)
ZTEST(posix_threads_base, test_key_1toN_thread)
{
void *retval;
pthread_t newthread[N_THR];
Expand All @@ -95,7 +95,7 @@ ZTEST(key, test_key_1toN_thread)
zassert_ok(pthread_key_delete(key), "attempt to delete key failed");
}

ZTEST(key, test_key_Nto1_thread)
ZTEST(posix_threads_base, test_key_Nto1_thread)
{
pthread_t newthread;

Expand All @@ -113,7 +113,7 @@ ZTEST(key, test_key_Nto1_thread)
}
}

ZTEST(key, test_key_resource_leak)
ZTEST(posix_threads_base, test_key_resource_leak)
{
pthread_key_t key;

Expand All @@ -123,7 +123,7 @@ ZTEST(key, test_key_resource_leak)
}
}

ZTEST(key, test_correct_key_is_deleted)
ZTEST(posix_threads_base, test_correct_key_is_deleted)
{
pthread_key_t key;
size_t j = CONFIG_POSIX_THREAD_KEYS_MAX - 1;
Expand Down Expand Up @@ -162,34 +162,21 @@ static void *setspecific_thread(void *count)
return NULL;
}

ZTEST(key, test_thread_specific_data_deallocation)
ZTEST(posix_threads_base, test_thread_specific_data_deallocation)
{
pthread_t thread;
static int alloc_count_t0;
static int alloc_count_t1;

zassert_ok(pthread_create(&thread, NULL, setspecific_thread, &alloc_count_t0),
"attempt to create thread failed");
"attempt to create thread failed");
zassert_ok(pthread_join(thread, NULL), "failed to join thread");
printk("first thread allocated %d keys", alloc_count_t0);

zassert_ok(pthread_create(&thread, NULL, setspecific_thread, &alloc_count_t1),
"attempt to create thread failed");
"attempt to create thread failed");
zassert_ok(pthread_join(thread, NULL), "failed to join thread");
printk("second thread allocated %d keys", alloc_count_t1);

zassert_equal(alloc_count_t0, alloc_count_t1,
"failed to deallocate thread specific data");
}

static void before(void *arg)
{
ARG_UNUSED(arg);

if (!IS_ENABLED(CONFIG_DYNAMIC_THREAD)) {
/* skip redundant testing if there is no thread pool / heap allocation */
ztest_test_skip();
}
zassert_equal(alloc_count_t0, alloc_count_t1, "failed to deallocate thread specific data");
}

ZTEST_SUITE(key, NULL, NULL, before, NULL, NULL);
13 changes: 13 additions & 0 deletions tests/posix/threads_base/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) 2023, Meta
* Copyright (c) 2024, Marvin Ouma <pancakesdeath@protonmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/ztest.h>

extern void before(void *arg);
extern void after(void *arg);

ZTEST_SUITE(posix_threads_base, NULL, NULL, before, after, NULL);
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static void test_mutex_common(int type, void *(*entry)(void *arg))
zassert_ok(pthread_mutex_destroy(&mutex), "Destroying mutex is failed");
}

ZTEST(mutex, test_mutex_prioceiling_stubs)
ZTEST(posix_threads_base, test_mutex_prioceiling_stubs)
{
#ifdef CONFIG_POSIX_THREAD_PRIO_PROTECT
zassert_equal(pthread_mutex_getprioceiling(NULL, NULL), ENOSYS);
Expand All @@ -104,7 +104,7 @@ ZTEST(mutex, test_mutex_prioceiling_stubs)
* and pthread_mutex_lock are tested with mutex type being
* normal.
*/
ZTEST(mutex, test_mutex_normal)
ZTEST(posix_threads_base, test_mutex_normal)
{
test_mutex_common(PTHREAD_MUTEX_NORMAL, normal_mutex_entry);
}
Expand All @@ -116,7 +116,7 @@ ZTEST(mutex, test_mutex_normal)
* twice and unlocked for the same number of time.
*
*/
ZTEST(mutex, test_mutex_recursive)
ZTEST(posix_threads_base, test_mutex_recursive)
{
test_mutex_common(PTHREAD_MUTEX_RECURSIVE, recursive_mutex_entry);
}
Expand All @@ -126,7 +126,7 @@ ZTEST(mutex, test_mutex_recursive)
*
* @details Exactly CONFIG_MAX_PTHREAD_MUTEX_COUNT can be in use at once.
*/
ZTEST(mutex, test_mutex_resource_exhausted)
ZTEST(posix_threads_base, test_mutex_resource_exhausted)
{
size_t i;
pthread_mutex_t m[CONFIG_MAX_PTHREAD_MUTEX_COUNT + 1];
Expand All @@ -150,7 +150,7 @@ ZTEST(mutex, test_mutex_resource_exhausted)
*
* @details Demonstrate that mutexes may be used over and over again.
*/
ZTEST(mutex, test_mutex_resource_leak)
ZTEST(posix_threads_base, test_mutex_resource_leak)
{
pthread_mutex_t m;

Expand Down Expand Up @@ -189,7 +189,7 @@ static void *test_mutex_timedlock_fn(void *arg)
}

/** @brief Test to verify @ref pthread_mutex_timedlock returns ETIMEDOUT */
ZTEST(mutex, test_mutex_timedlock)
ZTEST(posix_threads_base, test_mutex_timedlock)
{
void *ret;
pthread_t th;
Expand All @@ -215,15 +215,3 @@ ZTEST(mutex, test_mutex_timedlock)

zassert_ok(pthread_mutex_destroy(&mutex));
}

static void before(void *arg)
{
ARG_UNUSED(arg);

if (!IS_ENABLED(CONFIG_DYNAMIC_THREAD)) {
/* skip redundant testing if there is no thread pool / heap allocation */
ztest_test_skip();
}
}

ZTEST_SUITE(mutex, NULL, NULL, before, NULL, NULL);
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include <zephyr/ztest.h>

ZTEST(mutex_attr, test_pthread_mutexattr_init)
ZTEST(posix_threads_base, test_pthread_mutexattr_init)
{
pthread_mutexattr_t attr;

Expand All @@ -22,7 +22,7 @@ ZTEST(mutex_attr, test_pthread_mutexattr_init)
zassert_ok(pthread_mutexattr_destroy(&attr));
}

ZTEST(mutex_attr, test_pthread_mutexattr_destroy)
ZTEST(posix_threads_base, test_pthread_mutexattr_destroy)
{
pthread_mutexattr_t attr;

Expand All @@ -42,5 +42,3 @@ ZTEST(mutex_attr, test_pthread_mutexattr_destroy)
zassert_equal(EINVAL, pthread_mutexattr_destroy(&attr));
}
}

ZTEST_SUITE(mutex_attr, NULL, NULL, NULL, NULL, NULL);
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ static void *thread_top_term(void *p1)
/* Test the internal priority conversion functions */
int zephyr_to_posix_priority(int z_prio, int *policy);
int posix_to_zephyr_priority(int priority, int policy);
ZTEST(pthread, test_pthread_priority_conversion)
ZTEST(posix_threads_base, test_pthread_priority_conversion)
{
/*
* ZEPHYR [-CONFIG_NUM_COOP_PRIORITIES, -1]
Expand Down Expand Up @@ -256,7 +256,7 @@ ZTEST(pthread, test_pthread_priority_conversion)
}
}

ZTEST(pthread, test_pthread_execution)
ZTEST(posix_threads_base, test_pthread_execution)
{
int i, ret;
pthread_t newthread[N_THR_E];
Expand Down Expand Up @@ -346,7 +346,7 @@ ZTEST(pthread, test_pthread_execution)
printk("Barrier test OK\n");
}

ZTEST(pthread, test_pthread_termination)
ZTEST(posix_threads_base, test_pthread_termination)
{
int32_t i, ret;
pthread_t newthread[N_THR_T] = {0};
Expand Down Expand Up @@ -376,7 +376,7 @@ ZTEST(pthread, test_pthread_termination)
zassert_equal(ret, ESRCH, "cancelled a terminated thread!");
}

ZTEST(pthread, test_pthread_tryjoin)
ZTEST(posix_threads_base, test_pthread_tryjoin)
{
pthread_t th = {0};
int sleep_duration_ms = 200;
Expand All @@ -396,7 +396,7 @@ ZTEST(pthread, test_pthread_tryjoin)
zassert_ok(pthread_tryjoin_np(th, &retval));
}

ZTEST(pthread, test_pthread_timedjoin)
ZTEST(posix_threads_base, test_pthread_timedjoin)
{
pthread_t th = {0};
int sleep_duration_ms = 200;
Expand Down Expand Up @@ -444,7 +444,7 @@ static void *create_thread1(void *p1)
return NULL;
}

ZTEST(pthread, test_pthread_descriptor_leak)
ZTEST(posix_threads_base, test_pthread_descriptor_leak)
{
pthread_t pthread1;

Expand All @@ -456,7 +456,7 @@ ZTEST(pthread, test_pthread_descriptor_leak)
}
}

ZTEST(pthread, test_pthread_equal)
ZTEST(posix_threads_base, test_pthread_equal)
{
zassert_true(pthread_equal(pthread_self(), pthread_self()));
zassert_false(pthread_equal(pthread_self(), (pthread_t)4242));
Expand Down Expand Up @@ -484,7 +484,7 @@ static void *test_pthread_cleanup_entry(void *arg)
return NULL;
}

ZTEST(pthread, test_pthread_cleanup)
ZTEST(posix_threads_base, test_pthread_cleanup)
{
pthread_t th;

Expand Down Expand Up @@ -522,7 +522,7 @@ static void *test_pthread_cancel_fn(void *arg)
return NULL;
}

ZTEST(pthread, test_pthread_testcancel)
ZTEST(posix_threads_base, test_pthread_testcancel)
{
pthread_t th;

Expand Down Expand Up @@ -550,22 +550,10 @@ static void *test_pthread_setschedprio_fn(void *arg)
return NULL;
}

ZTEST(pthread, test_pthread_setschedprio)
ZTEST(posix_threads_base, test_pthread_setschedprio)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test belongs in the xsi_realtime_threads testsuite with the _POSIX_THREAD_PRIORITY_SCHEDULING Option.

{
pthread_t th;

zassert_ok(pthread_create(&th, NULL, test_pthread_setschedprio_fn, NULL));
zassert_ok(pthread_join(th, NULL));
}

static void before(void *arg)
{
ARG_UNUSED(arg);

if (!IS_ENABLED(CONFIG_DYNAMIC_THREAD)) {
/* skip redundant testing if there is no thread pool / heap allocation */
ztest_test_skip();
}
}

ZTEST_SUITE(pthread, NULL, NULL, before, NULL, NULL);
Loading
Loading