1 /* 2 * 程序清单: 3 */ 4 #include <rtthread.h> 5 #include "tc_comm.h" 6 7 /* 指向线程控制块的指针 */ 8 static rt_thread_t tid1 = RT_NULL; 9 static rt_thread_t tid2 = RT_NULL; 10 static rt_thread_t tid3 = RT_NULL; 11 static rt_mutex_t mutex = RT_NULL; 12 13 /* 线程1入口 */ 14 static void thread1_entry(void* parameter) 15 { 16 /* 先让低优先级线程运行 */ 17 rt_thread_delay(10); 18 19 /* 此时thread3持有mutex,并且thread2等待持有mutex */ 20 21 /* 检查thread2与thread3的优先级情况 */ 22 if (tid2->current_priority != tid3->current_priority) 23 { 24 /* 优先级不相同,测试失败 */ 25 tc_stat(TC_STAT_END | TC_STAT_FAILED); 26 return; 27 } 28 } 29 30 /* 线程2入口 */ 31 static void thread2_entry(void* parameter) 32 { 33 rt_err_t result; 34 35 /* 先让低优先级线程运行 */ 36 rt_thread_delay(5); 37 38 while (1) 39 { 40 /* 41 * 试图持有互斥锁,此时thread3持有,应把thread3的优先级提升到thread2相同 42 * 的优先级 43 */ 44 result = rt_mutex_take(mutex, RT_WAITING_FOREVER); 45 46 if (result == RT_EOK) 47 { 48 /* 释放互斥锁 */ 49 rt_mutex_release(mutex); 50 } 51 } 52 } 53 54 /* 线程3入口 */ 55 static void thread3_entry(void* parameter) 56 { 57 rt_tick_t tick; 58 rt_err_t result; 59 60 while (1) 61 { 62 result = rt_mutex_take(mutex, RT_WAITING_FOREVER); 63 if (result != RT_EOK) 64 { 65 tc_stat(TC_STAT_END | TC_STAT_FAILED); 66 } 67 result = rt_mutex_take(mutex, RT_WAITING_FOREVER); 68 if (result != RT_EOK) 69 { 70 tc_stat(TC_STAT_END | TC_STAT_FAILED); 71 } 72 73 /* 做一个长时间的循环,总共50个OS Tick */ 74 tick = rt_tick_get(); 75 while (rt_tick_get() - tick < 50) ; 76 77 rt_mutex_release(mutex); 78 rt_mutex_release(mutex); 79 } 80 } 81 82 int mutex_simple_init() 83 { 84 /* 创建互斥锁 */ 85 mutex = rt_mutex_create("mutex", RT_IPC_FLAG_FIFO); 86 if (mutex == RT_NULL) 87 { 88 tc_stat(TC_STAT_END | TC_STAT_FAILED); 89 return 0; 90 } 91 92 /* 创建线程1 */ 93 tid1 = rt_thread_create("t1", 94 thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */ 95 THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE); 96 if (tid1 != RT_NULL) 97 rt_thread_startup(tid1); 98 else 99 tc_stat(TC_STAT_END | TC_STAT_FAILED); 100 101 /* 创建线程2 */ 102 tid2 = rt_thread_create("t2", 103 thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */ 104 THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); 105 if (tid2 != RT_NULL) 106 rt_thread_startup(tid2); 107 else 108 tc_stat(TC_STAT_END | TC_STAT_FAILED); 109 110 /* 创建线程3 */ 111 tid3 = rt_thread_create("t3", 112 thread3_entry, RT_NULL, /* 线程入口是thread3_entry, 入口参数是RT_NULL */ 113 THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE); 114 if (tid3 != RT_NULL) 115 rt_thread_startup(tid3); 116 else 117 tc_stat(TC_STAT_END | TC_STAT_FAILED); 118 119 return 0; 120 } 121 122 #ifdef RT_USING_TC 123 static void _tc_cleanup() 124 { 125 /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ 126 rt_enter_critical(); 127 128 /* 删除线程 */ 129 if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE) 130 rt_thread_delete(tid1); 131 if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE) 132 rt_thread_delete(tid2); 133 if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE) 134 rt_thread_delete(tid3); 135 136 if (mutex != RT_NULL) 137 { 138 rt_mutex_delete(mutex); 139 } 140 141 /* 调度器解锁 */ 142 rt_exit_critical(); 143 144 /* 设置TestCase状态 */ 145 tc_done(TC_STAT_PASSED); 146 } 147 148 int _tc_mutex_simple() 149 { 150 /* 设置TestCase清理回调函数 */ 151 tc_cleanup(_tc_cleanup); 152 mutex_simple_init(); 153 154 /* 返回TestCase运行的最长时间 */ 155 return 100; 156 } 157 /* 输出函数命令到finsh shell中 */ 158 FINSH_FUNCTION_EXPORT(_tc_mutex_simple, sime mutex example); 159 #else 160 /* 用户应用入口 */ 161 int rt_application_init() 162 { 163 mutex_simple_init(); 164 165 return 0; 166 } 167 #endif 168