xref: /nrf52832-nimble/rt-thread/examples/kernel/thread_same_priority.c (revision 104654410c56c573564690304ae786df310c91fc)
1 #include <rtthread.h>
2 #include "tc_comm.h"
3 
4 static struct rt_thread thread1;
5 static struct rt_thread thread2;
6 static char thread1_stack[THREAD_STACK_SIZE];
7 static char thread2_stack[THREAD_STACK_SIZE];
8 
9 volatile static rt_uint32_t t1_count = 0;
10 volatile static rt_uint32_t t2_count = 0;
thread1_entry(void * parameter)11 static void thread1_entry(void* parameter)
12 {
13     while (1)
14     {
15         t1_count ++;
16     }
17 }
18 
thread2_entry(void * parameter)19 static void thread2_entry(void* parameter)
20 {
21     while (1)
22     {
23         t2_count ++;
24     }
25 }
26 
thread_same_priority_init()27 rt_err_t thread_same_priority_init()
28 {
29     rt_err_t result;
30 
31     result = rt_thread_init(&thread1,
32         "t1",
33         thread1_entry, RT_NULL,
34         &thread1_stack[0], sizeof(thread1_stack),
35         THREAD_PRIORITY, 10);
36     if (result == RT_EOK)
37         rt_thread_startup(&thread1);
38     else
39         tc_stat(TC_STAT_END | TC_STAT_FAILED);
40 
41     result = rt_thread_init(&thread2,
42         "t2",
43         thread2_entry, RT_NULL,
44         &thread2_stack[0], sizeof(thread2_stack),
45         THREAD_PRIORITY, 5);
46     if (result == RT_EOK)
47         rt_thread_startup(&thread2);
48     else
49         tc_stat(TC_STAT_END | TC_STAT_FAILED);
50 
51     return result;
52 }
53 
54 #ifdef RT_USING_TC
_tc_cleanup()55 static void _tc_cleanup()
56 {
57     /* lock scheduler */
58     rt_enter_critical();
59 
60     if (thread1.stat != RT_THREAD_CLOSE)
61         rt_thread_detach(&thread1);
62     if (thread2.stat != RT_THREAD_CLOSE)
63         rt_thread_detach(&thread2);
64 
65     /* unlock scheduler */
66     rt_exit_critical();
67 
68     rt_kprintf("t1_count=%d t2_count=%d\n",t1_count,t2_count);
69 
70     if (t1_count / t2_count != 2)
71         tc_stat(TC_STAT_END | TC_STAT_FAILED);
72     else
73         tc_done(TC_STAT_PASSED);
74 }
75 
_tc_thread_same_priority()76 int _tc_thread_same_priority()
77 {
78     t1_count = 0;
79     t2_count = 0;
80 
81     /* set tc cleanup */
82     tc_cleanup(_tc_cleanup);
83 
84     thread_same_priority_init();
85 
86     return 100;
87 }
88 FINSH_FUNCTION_EXPORT(_tc_thread_same_priority, a same priority thread test);
89 #else
rt_application_init()90 int rt_application_init()
91 {
92     thread_same_priority_init();
93 
94     return 0;
95 }
96 #endif
97