2010-03-21 11:02:15 +08:00
|
|
|
|
/*
|
|
|
|
|
* 程序清单:动态信号量
|
|
|
|
|
*
|
|
|
|
|
* 这个例子中将创建一个动态信号量(初始值为0 )及一个动态线程,在这个动态线程中
|
|
|
|
|
* 将试图采用超时方式去持有信号量,应该超时返回。然后这个线程释放一次信号量,并
|
|
|
|
|
* 在后面继续采用永久等待方式去持有信号量, 成功获得信号量后返回。
|
|
|
|
|
*/
|
2010-03-17 17:57:07 +08:00
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
#include "tc_comm.h"
|
|
|
|
|
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 指向线程控制块的指针 */
|
|
|
|
|
static rt_thread_t tid = RT_NULL;
|
|
|
|
|
/* 指向信号量的指针 */
|
|
|
|
|
static rt_sem_t sem = RT_NULL;
|
|
|
|
|
/* 线程入口 */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
static void thread_entry(void* parameter)
|
|
|
|
|
{
|
|
|
|
|
rt_err_t result;
|
|
|
|
|
rt_tick_t tick;
|
|
|
|
|
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 获得当前的OS Tick */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
tick = rt_tick_get();
|
|
|
|
|
|
2010-04-14 08:00:41 +08:00
|
|
|
|
/* 试图持有一个信号量,如果10个OS Tick依然没拿到,则超时返回 */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
result = rt_sem_take(sem, 10);
|
|
|
|
|
if (result == -RT_ETIMEOUT)
|
|
|
|
|
{
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 判断是否刚好过去10个OS Tick */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
if (rt_tick_get() - tick != 10)
|
|
|
|
|
{
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 如果失败,则测试失败 */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
tc_done(TC_STAT_FAILED);
|
|
|
|
|
rt_sem_delete(sem);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-03-21 11:02:15 +08:00
|
|
|
|
rt_kprintf("take semaphore timeout\n");
|
2010-03-17 17:57:07 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 因为并没释放信号量,应该是超时返回,否则测试失败 */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
tc_done(TC_STAT_FAILED);
|
|
|
|
|
rt_sem_delete(sem);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 释放一次信号量 */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
rt_sem_release(sem);
|
|
|
|
|
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 继续持有信号量,并永远等待直到持有到信号量 */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
result = rt_sem_take(sem, RT_WAITING_FOREVER);
|
|
|
|
|
if (result != RT_EOK)
|
|
|
|
|
{
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 返回不正确,测试失败 */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
tc_done(TC_STAT_FAILED);
|
|
|
|
|
rt_sem_delete(sem);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 测试成功 */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
tc_done(TC_STAT_PASSED);
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 删除信号量 */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
rt_sem_delete(sem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int semaphore_dynamic_init()
|
|
|
|
|
{
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 创建一个信号量,初始值是0 */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
sem = rt_sem_create("sem", 0, RT_IPC_FLAG_FIFO);
|
|
|
|
|
if (sem == RT_NULL)
|
|
|
|
|
{
|
|
|
|
|
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 创建线程 */
|
|
|
|
|
tid = rt_thread_create("thread",
|
|
|
|
|
thread_entry, RT_NULL, /* 线程入口是thread_entry, 入口参数是RT_NULL */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
|
|
|
|
|
if (tid != RT_NULL)
|
|
|
|
|
rt_thread_startup(tid);
|
|
|
|
|
else
|
|
|
|
|
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef RT_USING_TC
|
2010-03-21 11:02:15 +08:00
|
|
|
|
static void _tc_cleanup()
|
|
|
|
|
{
|
|
|
|
|
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
|
|
|
|
|
rt_enter_critical();
|
|
|
|
|
|
|
|
|
|
/* 删除线程 */
|
|
|
|
|
if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE)
|
|
|
|
|
{
|
|
|
|
|
rt_thread_delete(tid);
|
|
|
|
|
|
|
|
|
|
/* 删除信号量 */
|
|
|
|
|
rt_sem_delete(sem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 调度器解锁 */
|
|
|
|
|
rt_exit_critical();
|
|
|
|
|
|
|
|
|
|
/* 设置TestCase状态 */
|
|
|
|
|
tc_done(TC_STAT_PASSED);
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-17 17:57:07 +08:00
|
|
|
|
int _tc_semaphore_dynamic()
|
|
|
|
|
{
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 设置TestCase清理回调函数 */
|
|
|
|
|
tc_cleanup(_tc_cleanup);
|
2010-03-17 17:57:07 +08:00
|
|
|
|
semaphore_dynamic_init();
|
|
|
|
|
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 返回TestCase运行的最长时间 */
|
|
|
|
|
return 100;
|
2010-03-17 17:57:07 +08:00
|
|
|
|
}
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 输出函数命令到finsh shell中 */
|
|
|
|
|
FINSH_FUNCTION_EXPORT(_tc_semaphore_dynamic, a dynamic semaphore example);
|
2010-03-17 17:57:07 +08:00
|
|
|
|
#else
|
2010-03-21 11:02:15 +08:00
|
|
|
|
/* 用户应用入口 */
|
2010-03-17 17:57:07 +08:00
|
|
|
|
int rt_application_init()
|
|
|
|
|
{
|
|
|
|
|
semaphore_dynamic_init();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|