2013-01-08 21:05:02 +08:00
|
|
|
|
/*
|
|
|
|
|
* 程序清单:静态信号量
|
|
|
|
|
*
|
|
|
|
|
* 这个例子中将创建一个静态信号量(初始值为0 )及一个静态线程,在这个静态线程中
|
|
|
|
|
* 将试图采用超时方式去持有信号量,应该超时返回。然后这个线程释放一次信号量,并
|
|
|
|
|
* 在后面继续采用永久等待方式去持有信号量, 成功获得信号量后返回。
|
|
|
|
|
*/
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
#include "tc_comm.h"
|
|
|
|
|
|
|
|
|
|
/* 线程控制块及栈 */
|
|
|
|
|
static struct rt_thread thread;
|
|
|
|
|
static rt_uint8_t thread_stack[THREAD_STACK_SIZE];
|
|
|
|
|
/* 信号量控制块 */
|
|
|
|
|
static struct rt_semaphore sem;
|
|
|
|
|
|
|
|
|
|
/* 线程入口 */
|
|
|
|
|
static void thread_entry(void* parameter)
|
|
|
|
|
{
|
2013-12-21 12:51:52 +08:00
|
|
|
|
rt_err_t result;
|
|
|
|
|
rt_tick_t tick;
|
|
|
|
|
|
|
|
|
|
/* 获得当前的OS Tick */
|
|
|
|
|
tick = rt_tick_get();
|
|
|
|
|
|
|
|
|
|
/* 试图持有信号量,最大等待10个OS Tick后返回 */
|
|
|
|
|
result = rt_sem_take(&sem, 10);
|
|
|
|
|
if (result == -RT_ETIMEOUT)
|
|
|
|
|
{
|
|
|
|
|
/* 超时后判断是否刚好是10个OS Tick */
|
|
|
|
|
if (rt_tick_get() - tick != 10)
|
|
|
|
|
{
|
|
|
|
|
tc_done(TC_STAT_FAILED);
|
|
|
|
|
rt_sem_detach(&sem);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
rt_kprintf("take semaphore timeout\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* 因为没有其他地方是否信号量,所以不应该成功持有信号量,否则测试失败 */
|
|
|
|
|
tc_done(TC_STAT_FAILED);
|
|
|
|
|
rt_sem_detach(&sem);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 释放一次信号量 */
|
|
|
|
|
rt_sem_release(&sem);
|
|
|
|
|
|
|
|
|
|
/* 永久等待方式持有信号量 */
|
|
|
|
|
result = rt_sem_take(&sem, RT_WAITING_FOREVER);
|
|
|
|
|
if (result != RT_EOK)
|
|
|
|
|
{
|
|
|
|
|
/* 不成功则测试失败 */
|
|
|
|
|
tc_done(TC_STAT_FAILED);
|
|
|
|
|
rt_sem_detach(&sem);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 测试通过 */
|
|
|
|
|
tc_done(TC_STAT_PASSED);
|
|
|
|
|
/* 脱离信号量对象 */
|
|
|
|
|
rt_sem_detach(&sem);
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-02 16:13:11 +08:00
|
|
|
|
int semaphore_static_init(void)
|
2013-01-08 21:05:02 +08:00
|
|
|
|
{
|
2013-12-21 12:51:52 +08:00
|
|
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
|
|
/* 初始化信号量,初始值是0 */
|
|
|
|
|
result = rt_sem_init(&sem, "sem", 0, RT_IPC_FLAG_FIFO);
|
|
|
|
|
if (result != RT_EOK)
|
|
|
|
|
{
|
|
|
|
|
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 初始化线程1 */
|
|
|
|
|
result = rt_thread_init(&thread, "thread", /* 线程名:thread */
|
|
|
|
|
thread_entry, RT_NULL, /* 线程的入口是thread_entry,入口参数是RT_NULL*/
|
|
|
|
|
&thread_stack[0], sizeof(thread_stack), /* 线程栈是thread_stack */
|
|
|
|
|
THREAD_PRIORITY, 10);
|
|
|
|
|
if (result == RT_EOK) /* 如果返回正确,启动线程1 */
|
|
|
|
|
rt_thread_startup(&thread);
|
|
|
|
|
else
|
|
|
|
|
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
|
|
|
|
|
|
|
|
|
return 0;
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef RT_USING_TC
|
2013-04-02 16:13:11 +08:00
|
|
|
|
static void _tc_cleanup(void)
|
2013-01-08 21:05:02 +08:00
|
|
|
|
{
|
2013-12-21 12:51:52 +08:00
|
|
|
|
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
|
|
|
|
rt_enter_critical();
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2013-12-21 12:51:52 +08:00
|
|
|
|
/* 执行线程脱离 */
|
|
|
|
|
if (thread.stat != RT_THREAD_CLOSE)
|
|
|
|
|
{
|
|
|
|
|
rt_thread_detach(&thread);
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2013-12-21 12:51:52 +08:00
|
|
|
|
/* 执行信号量对象脱离 */
|
|
|
|
|
rt_sem_detach(&sem);
|
|
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2013-12-21 12:51:52 +08:00
|
|
|
|
/* 调度器解锁 */
|
|
|
|
|
rt_exit_critical();
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2013-12-21 12:51:52 +08:00
|
|
|
|
/* 设置TestCase状态 */
|
|
|
|
|
tc_done(TC_STAT_PASSED);
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-02 16:13:11 +08:00
|
|
|
|
int _tc_semaphore_static(void)
|
2013-01-08 21:05:02 +08:00
|
|
|
|
{
|
2013-12-21 12:51:52 +08:00
|
|
|
|
/* 设置TestCase清理回调函数 */
|
|
|
|
|
tc_cleanup(_tc_cleanup);
|
|
|
|
|
semaphore_static_init();
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2013-12-21 12:51:52 +08:00
|
|
|
|
/* 返回TestCase运行的最长时间 */
|
|
|
|
|
return 100;
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
/* 输出函数命令到finsh shell中 */
|
|
|
|
|
FINSH_FUNCTION_EXPORT(_tc_semaphore_static, a static semaphore example);
|
|
|
|
|
#else
|
|
|
|
|
/* 用户应用入口 */
|
2013-04-02 16:13:11 +08:00
|
|
|
|
int rt_application_init(void)
|
2013-01-08 21:05:02 +08:00
|
|
|
|
{
|
2013-12-21 12:51:52 +08:00
|
|
|
|
semaphore_static_init();
|
2013-01-08 21:05:02 +08:00
|
|
|
|
|
2013-12-21 12:51:52 +08:00
|
|
|
|
return 0;
|
2013-01-08 21:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|