1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2023-12-25     Shell        the first version
9  */
10 #include <rtthread.h>
11 #include "utest.h"
12 
13 #define TEST_LOOP_TIMES 20
14 
15 static struct rt_semaphore _thr_exit_sem;
16 
_thread_entry(void * param)17 static void _thread_entry(void *param)
18 {
19     for (size_t i = 0; i < TEST_LOOP_TIMES; i++)
20     {
21         rt_kprintf("This is thread %p\n", rt_thread_self());
22         rt_thread_mdelay(1);
23     }
24 
25     rt_sem_release(&_thr_exit_sem);
26     return;
27 }
28 
29 #define TEST_THREAD_COUNT 16
30 
mtsafe_kprint_tc(void)31 static void mtsafe_kprint_tc(void)
32 {
33     for (size_t i = 0; i < TEST_THREAD_COUNT; i++)
34     {
35         rt_thread_t new_thread =
36             rt_thread_create(
37                 "test",
38                 _thread_entry,
39                 NULL,
40                 UTEST_THR_STACK_SIZE,
41                 UTEST_THR_PRIORITY,
42                 100);
43         rt_thread_startup(new_thread);
44     }
45 
46     for (size_t i = 0; i < TEST_THREAD_COUNT; i++)
47     {
48         rt_sem_take(&_thr_exit_sem, RT_WAITING_FOREVER);
49     }
50 }
51 
utest_tc_init(void)52 static rt_err_t utest_tc_init(void)
53 {
54     rt_sem_init(&_thr_exit_sem, "test", 0, RT_IPC_FLAG_PRIO);
55     return RT_EOK;
56 }
57 
utest_tc_cleanup(void)58 static rt_err_t utest_tc_cleanup(void)
59 {
60     rt_sem_detach(&_thr_exit_sem);
61     return RT_EOK;
62 }
63 
testcase(void)64 static void testcase(void)
65 {
66     UTEST_UNIT_RUN(mtsafe_kprint_tc);
67 }
68 UTEST_TC_EXPORT(testcase, "testcases.kernel.mtsafe_kprint", utest_tc_init, utest_tc_cleanup, 10);
69