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