1 /*
2  * Copyright (c) 2018 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/kernel.h>
9 #include <cmsis_os.h>
10 
11 #ifdef CONFIG_COVERAGE_GCOV
12 #define STACKSZ		(512 + CONFIG_TEST_EXTRA_STACK_SIZE)
13 #else
14 #define STACKSZ		(512U)
15 #endif
16 
thread_inst_check(void const * argument)17 void thread_inst_check(void const *argument)
18 {
19 	osThreadId id = osThreadGetId();
20 
21 	zassert_true(id != NULL, "Failed getting ThreadId");
22 }
23 
24 osThreadDef(thread_inst_check, osPriorityNormal, 3, STACKSZ);
25 
ZTEST(thread_instance,test_thread_instances)26 ZTEST(thread_instance, test_thread_instances)
27 {
28 	osThreadId id1, id2, id3, id4;
29 	osStatus status;
30 
31 	id1 = osThreadCreate(osThread(thread_inst_check), NULL);
32 	zassert_true(id1 != NULL, "Failed creating thread_inst_check");
33 
34 	id2 = osThreadCreate(osThread(thread_inst_check), NULL);
35 	zassert_true(id2 != NULL, "Failed creating thread_inst_check");
36 
37 	id3 = osThreadCreate(osThread(thread_inst_check), NULL);
38 	zassert_true(id3 != NULL, "Failed creating thread_inst_check");
39 
40 	id4 = osThreadCreate(osThread(thread_inst_check), NULL);
41 	zassert_true(id4 == NULL, "Something wrong with thread instances");
42 
43 	status = osThreadTerminate(id2);
44 	zassert_true(status == osOK, "Error terminating thread_inst_check");
45 
46 	/* after terminating thread id2, when creating a new thread,
47 	 * it should re-use the available thread instance of id2.
48 	 */
49 	id4 = osThreadCreate(osThread(thread_inst_check), NULL);
50 	zassert_true(id4 != NULL, "Failed creating thread_inst_check");
51 	zassert_true(id2 == id4, "Error creating thread_inst_check");
52 }
53 ZTEST_SUITE(thread_instance, NULL, NULL, NULL, NULL, NULL);
54