1 #include <rtthread.h>
2 #include "tc_comm.h"
3
4 static rt_sem_t sem;
5 static rt_uint8_t t1_count, t2_count;
6 static rt_thread_t t1, t2, worker;
thread1_entry(void * parameter)7 static void thread1_entry(void* parameter)
8 {
9 rt_err_t result;
10
11 while (1)
12 {
13 result = rt_sem_take(sem, RT_WAITING_FOREVER);
14 if (result != RT_EOK)
15 {
16 tc_done(TC_STAT_FAILED);
17 return;
18 }
19
20 t1_count ++;
21 rt_kprintf("thread1: got semaphore, count: %d\n", t1_count);
22 }
23 }
24
thread2_entry(void * parameter)25 static void thread2_entry(void* parameter)
26 {
27 rt_err_t result;
28
29 while (1)
30 {
31 result = rt_sem_take(sem, RT_WAITING_FOREVER);
32 if (result != RT_EOK)
33 {
34 tc_done(TC_STAT_FAILED);
35 return;
36 }
37
38 t2_count ++;
39 rt_kprintf("thread2: got semaphore, count: %d\n", t2_count);
40 }
41 }
42
worker_thread_entry(void * parameter)43 static void worker_thread_entry(void* parameter)
44 {
45 rt_thread_delay(10);
46
47 while (1)
48 {
49 rt_sem_release(sem);
50 rt_thread_delay(5);
51 }
52 }
53
semaphore_priority_init()54 int semaphore_priority_init()
55 {
56 sem = rt_sem_create("sem", 0, RT_IPC_FLAG_PRIO);
57 if (sem == RT_NULL)
58 {
59 tc_stat(TC_STAT_END | TC_STAT_FAILED);
60 return 0;
61 }
62
63 t1_count = t2_count = 0;
64
65 t1 = rt_thread_create("t1",
66 thread1_entry, RT_NULL,
67 THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
68 if (t1 != RT_NULL)
69 rt_thread_startup(t1);
70 else
71 tc_stat(TC_STAT_END | TC_STAT_FAILED);
72
73 t2 = rt_thread_create("t2",
74 thread2_entry, RT_NULL,
75 THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
76 if (t2 != RT_NULL)
77 rt_thread_startup(t2);
78 else
79 tc_stat(TC_STAT_END | TC_STAT_FAILED);
80
81 worker = rt_thread_create("worker",
82 worker_thread_entry, RT_NULL,
83 THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
84 if (worker != RT_NULL)
85 rt_thread_startup(worker);
86 else
87 tc_stat(TC_STAT_END | TC_STAT_FAILED);
88
89 return 0;
90 }
91
92 #ifdef RT_USING_TC
_tc_cleanup()93 static void _tc_cleanup()
94 {
95 /* lock scheduler */
96 rt_enter_critical();
97
98 /* delete t1, t2 and worker thread */
99 rt_thread_delete(t1);
100 rt_thread_delete(t2);
101 rt_thread_delete(worker);
102
103 if (sem)
104 {
105 rt_sem_delete(sem);
106 sem = RT_NULL;
107 }
108
109 if (t1_count > t2_count)
110 tc_done(TC_STAT_FAILED);
111 else
112 tc_done(TC_STAT_PASSED);
113
114 /* unlock scheduler */
115 rt_exit_critical();
116 }
117
_tc_semaphore_priority()118 int _tc_semaphore_priority()
119 {
120 /* set tc cleanup */
121 tc_cleanup(_tc_cleanup);
122 semaphore_priority_init();
123
124 return 50;
125 }
126 FINSH_FUNCTION_EXPORT(_tc_semaphore_priority, a priority semaphore test);
127 #else
rt_application_init()128 int rt_application_init()
129 {
130 semaphore_priority_init();
131
132 return 0;
133 }
134 #endif
135