diff --git a/tests/kernel/threads_lifecycle/thread_init/Makefile b/tests/kernel/threads_lifecycle/thread_init/Makefile new file mode 100644 index 00000000000..4de50f93d4d --- /dev/null +++ b/tests/kernel/threads_lifecycle/thread_init/Makefile @@ -0,0 +1,4 @@ +BOARD ?= qemu_x86 +CONF_FILE = prj.conf + +include ${ZEPHYR_BASE}/Makefile.inc diff --git a/tests/kernel/threads_lifecycle/thread_init/prj.conf b/tests/kernel/threads_lifecycle/thread_init/prj.conf new file mode 100644 index 00000000000..c99d02b5c7b --- /dev/null +++ b/tests/kernel/threads_lifecycle/thread_init/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ZTEST=y +CONFIG_NANO_TIMEOUTS=y diff --git a/tests/kernel/threads_lifecycle/thread_init/src/Makefile b/tests/kernel/threads_lifecycle/thread_init/src/Makefile new file mode 100644 index 00000000000..19c71af1ae3 --- /dev/null +++ b/tests/kernel/threads_lifecycle/thread_init/src/Makefile @@ -0,0 +1,3 @@ +include $(ZEPHYR_BASE)/tests/Makefile.test + +obj-y = main.o test_thread_init.o diff --git a/tests/kernel/threads_lifecycle/thread_init/src/main.c b/tests/kernel/threads_lifecycle/thread_init/src/main.c new file mode 100644 index 00000000000..250bceeb37d --- /dev/null +++ b/tests/kernel/threads_lifecycle/thread_init/src/main.c @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +/*declare test cases */ +extern void test_kdefine_preempt_thread(void); +extern void test_kdefine_coop_thread(void); +extern void test_kinit_preempt_thread(void); +extern void test_kinit_coop_thread(void); + +/*test case main entry*/ +void test_main(void *p1, void *p2, void *p3) +{ + ztest_test_suite(test_thread_init, + ztest_unit_test(test_kdefine_preempt_thread), + ztest_unit_test(test_kdefine_coop_thread), + ztest_unit_test(test_kinit_preempt_thread), + ztest_unit_test(test_kinit_coop_thread)); + ztest_run_test_suite(test_thread_init); +} diff --git a/tests/kernel/threads_lifecycle/thread_init/src/test_thread_init.c b/tests/kernel/threads_lifecycle/thread_init/src/test_thread_init.c new file mode 100644 index 00000000000..60c16b814dc --- /dev/null +++ b/tests/kernel/threads_lifecycle/thread_init/src/test_thread_init.c @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup t_lifecycle + * @{ + * @defgroup t_thread_init test_thread_init + * @brief TestPurpose: verify thread initialization in all supported means + * @details + * - Test Steps + * -# initialize a thread in specific mean + * -# from thread entry function verify initialized fields + * - Expected Results + * -# TESTPOINT: check initialized fields. Expect delay start, priority, + * params of entry function are initialized as expected + * - Verified Platforms + * -# qemu_x86, quark_se x86, quark_se arc, quark_d2000 x86, qemu_cortex_m3 + * @} + */ + +#include + +/*macro definition*/ +#define INIT_COOP_PRIO -2 +#define INIT_COOP_STACK_SIZE 500 +#define INIT_COOP_P1 ((void *)0xFFFF0000) +#define INIT_COOP_P2 ((void *)0xCDEF) +#define INIT_COOP_P3 ((void *)0x1234) +#define INIT_COOP_OPTION 0 +#define INIT_COOP_DELAY 2000 +#define INIT_PREEMPT_PRIO 1 +#define INIT_PREEMPT_STACK_SIZE 499 +#define INIT_PREEMPT_P1 ((void *)5) +#define INIT_PREEMPT_P2 ((void *)6) +#define INIT_PREEMPT_P3 ((void *)7) +#define INIT_PREEMPT_OPTION 0 +#define INIT_PREEMPT_DELAY K_NO_WAIT + +static void thread_entry(void *p1, void *p2, void *p3); + +static const int stack_size = 500; + +K_THREAD_DEFINE(T_KDEFINE_COOP_THREAD, INIT_COOP_STACK_SIZE, + thread_entry, INIT_COOP_P1, INIT_COOP_P2, INIT_COOP_P3, + INIT_COOP_PRIO, INIT_COOP_OPTION, INIT_COOP_DELAY); + +K_THREAD_DEFINE(T_KDEFINE_PREEMPT_THREAD, INIT_PREEMPT_STACK_SIZE, + thread_entry, INIT_PREEMPT_P1, INIT_PREEMPT_P2, INIT_PREEMPT_P3, + INIT_PREEMPT_PRIO, INIT_PREEMPT_OPTION, INIT_PREEMPT_DELAY); + +K_SEM_DEFINE(start_sema, 0, 1); + +K_SEM_DEFINE(end_sema, 0, 1); + +/*local variables*/ +static char __noinit __stack stack_coop[INIT_COOP_STACK_SIZE]; +static char __noinit __stack stack_preempt[INIT_PREEMPT_STACK_SIZE]; +static uint64_t t_create; +static struct thread_data{ + int init_prio; + int32_t init_delay; + void *init_p1; + void *init_p2; + void *init_p3; +} expected; + +/*entry routines*/ +static void thread_entry(void *p1, void *p2, void *p3) +{ + if (t_create) { + uint64_t t_delay = k_uptime_get() - t_create; + /**TESTPOINT: check delay start*/ + assert_true(t_delay >= expected.init_delay, + "k_thread_spawn delay start failed\n"); + } + + k_sem_take(&start_sema, K_FOREVER); + + k_tid_t tid = k_current_get(); + /**TESTPOINT: check priority and params*/ + assert_equal(k_thread_priority_get(tid), expected.init_prio, NULL); + assert_equal(p1, expected.init_p1, NULL); + assert_equal(p2, expected.init_p2, NULL); + assert_equal(p3, expected.init_p3, NULL); + /*option, stack size, not checked, no public API to get these values*/ + + k_sem_give(&end_sema); +} + +/*test cases*/ +/** + * @ingroup t_thread_init + * @brief test preempt thread initialization via K_THREAD_DEFINE + */ +void test_kdefine_preempt_thread(void) +{ + /*static thread created time unknown, skip it*/ + t_create = 0; + expected.init_p1 = INIT_PREEMPT_P1; + expected.init_p2 = INIT_PREEMPT_P2; + expected.init_p3 = INIT_PREEMPT_P3; + expected.init_prio = INIT_PREEMPT_PRIO; + expected.init_delay = INIT_PREEMPT_DELAY; + k_sem_reset(&start_sema); + k_sem_reset(&end_sema); + + /*signal thread to start*/ + k_sem_give(&start_sema); + /*wait for thread to exit*/ + k_sem_take(&end_sema, K_FOREVER); +} + +/** + * @ingroup t_thread_init + * @brief test coop thread initialization via K_THREAD_DEFINE + */ +void test_kdefine_coop_thread(void) +{ + /*static thread creation time unknown, skip it*/ + t_create = 0; + expected.init_p1 = INIT_COOP_P1; + expected.init_p2 = INIT_COOP_P2; + expected.init_p3 = INIT_COOP_P3; + expected.init_prio = INIT_COOP_PRIO; + expected.init_delay = INIT_COOP_DELAY; + k_sem_reset(&start_sema); + k_sem_reset(&end_sema); + + /*signal thread to start*/ + k_sem_give(&start_sema); + /*wait for thread to exit*/ + k_sem_take(&end_sema, K_FOREVER); +} + +/** + * @ingroup t_thread_init + * @brief test preempt thread initialization via k_thread_spawn + */ +void test_kinit_preempt_thread(void) +{ + /*create preempt thread*/ + k_tid_t pthread = k_thread_spawn(stack_preempt, INIT_PREEMPT_STACK_SIZE, + thread_entry, INIT_PREEMPT_P1, INIT_PREEMPT_P2, INIT_PREEMPT_P3, + INIT_PREEMPT_PRIO, INIT_PREEMPT_OPTION, INIT_PREEMPT_DELAY); + /*record time stamp of thread creation*/ + t_create = k_uptime_get(); + assert_not_null(pthread, "thread spawn failed\n"); + + expected.init_p1 = INIT_PREEMPT_P1; + expected.init_p2 = INIT_PREEMPT_P2; + expected.init_p3 = INIT_PREEMPT_P3; + expected.init_prio = INIT_PREEMPT_PRIO; + expected.init_delay = INIT_PREEMPT_DELAY; + k_sem_reset(&start_sema); + k_sem_reset(&end_sema); + + /*signal thread to start*/ + k_sem_give(&start_sema); + /*wait for thread to exit*/ + k_sem_take(&end_sema, K_FOREVER); +} + +/** + * @ingroup t_thread_init + * @brief test coop thread initialization via k_thread_spawn + */ +void test_kinit_coop_thread(void) +{ + /*create coop thread*/ + k_tid_t pthread = k_thread_spawn(stack_coop, INIT_COOP_STACK_SIZE, + thread_entry, INIT_COOP_P1, INIT_COOP_P2, INIT_COOP_P3, + INIT_COOP_PRIO, INIT_COOP_OPTION, INIT_COOP_DELAY); + /*record time stamp of thread creation*/ + t_create = k_uptime_get(); + assert_not_null(pthread, "thread spawn failed\n"); + + expected.init_p1 = INIT_COOP_P1; + expected.init_p2 = INIT_COOP_P2; + expected.init_p3 = INIT_COOP_P3; + expected.init_prio = INIT_COOP_PRIO; + expected.init_delay = INIT_COOP_DELAY; + k_sem_reset(&start_sema); + k_sem_reset(&end_sema); + + /*signal thread to start*/ + k_sem_give(&start_sema); + /*wait for thread to exit*/ + k_sem_take(&end_sema, K_FOREVER); +} diff --git a/tests/kernel/threads_lifecycle/thread_init/testcase.ini b/tests/kernel/threads_lifecycle/thread_init/testcase.ini new file mode 100644 index 00000000000..58c4d1a1747 --- /dev/null +++ b/tests/kernel/threads_lifecycle/thread_init/testcase.ini @@ -0,0 +1,2 @@ +[test] +tags = kernel