Merge pull request #216 from grissiom/more-tc

More tc
This commit is contained in:
Bernard Xiong 2013-12-20 20:28:02 -08:00
commit 46f9280a9d
10 changed files with 637 additions and 623 deletions

View File

@ -9,62 +9,62 @@ static rt_uint32_t total_count = 0;
static void cpu_usage_idle_hook()
{
rt_tick_t tick;
rt_uint32_t count;
volatile rt_uint32_t loop;
rt_tick_t tick;
rt_uint32_t count;
volatile rt_uint32_t loop;
if (total_count == 0)
{
/* get total count */
rt_enter_critical();
tick = rt_tick_get();
while(rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
{
total_count ++;
loop = 0;
while (loop < CPU_USAGE_LOOP) loop ++;
}
rt_exit_critical();
}
if (total_count == 0)
{
/* get total count */
rt_enter_critical();
tick = rt_tick_get();
while(rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
{
total_count ++;
loop = 0;
while (loop < CPU_USAGE_LOOP) loop ++;
}
rt_exit_critical();
}
count = 0;
/* get CPU usage */
tick = rt_tick_get();
while (rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
{
count ++;
loop = 0;
while (loop < CPU_USAGE_LOOP) loop ++;
}
count = 0;
/* get CPU usage */
tick = rt_tick_get();
while (rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
{
count ++;
loop = 0;
while (loop < CPU_USAGE_LOOP) loop ++;
}
/* calculate major and minor */
if (count < total_count)
{
count = total_count - count;
cpu_usage_major = (count * 100) / total_count;
cpu_usage_minor = ((count * 100) % total_count) * 100 / total_count;
}
else
{
total_count = count;
/* calculate major and minor */
if (count < total_count)
{
count = total_count - count;
cpu_usage_major = (count * 100) / total_count;
cpu_usage_minor = ((count * 100) % total_count) * 100 / total_count;
}
else
{
total_count = count;
/* no CPU usage */
cpu_usage_major = 0;
cpu_usage_minor = 0;
}
/* no CPU usage */
cpu_usage_major = 0;
cpu_usage_minor = 0;
}
}
void cpu_usage_get(rt_uint8_t *major, rt_uint8_t *minor)
{
RT_ASSERT(major != RT_NULL);
RT_ASSERT(minor != RT_NULL);
RT_ASSERT(major != RT_NULL);
RT_ASSERT(minor != RT_NULL);
*major = cpu_usage_major;
*minor = cpu_usage_minor;
*major = cpu_usage_major;
*minor = cpu_usage_minor;
}
void cpu_usage_init()
{
/* set idle thread hook */
rt_thread_idle_sethook(cpu_usage_idle_hook);
/* set idle thread hook */
rt_thread_idle_sethook(cpu_usage_idle_hook);
}

View File

@ -20,124 +20,124 @@ static struct rt_event event;
/* 线程1入口函数 */
static void thread1_entry(void *param)
{
rt_uint32_t e;
rt_uint32_t e;
while (1)
{
/* receive first event */
if (rt_event_recv(&event, ((1 << 3) | (1 << 5)),
RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, &e) == RT_EOK)
{
rt_kprintf("thread1: AND recv event 0x%x\n", e);
}
while (1)
{
/* receive first event */
if (rt_event_recv(&event, ((1 << 3) | (1 << 5)),
RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, &e) == RT_EOK)
{
rt_kprintf("thread1: AND recv event 0x%x\n", e);
}
rt_kprintf("thread1: delay 1s to prepare second event\n");
rt_thread_delay(10);
rt_kprintf("thread1: delay 1s to prepare second event\n");
rt_thread_delay(10);
/* receive second event */
if (rt_event_recv(&event, ((1 << 3) | (1 << 5)),
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, &e) == RT_EOK)
{
rt_kprintf("thread1: OR recv event 0x%x\n", e);
}
/* receive second event */
if (rt_event_recv(&event, ((1 << 3) | (1 << 5)),
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, &e) == RT_EOK)
{
rt_kprintf("thread1: OR recv event 0x%x\n", e);
}
rt_thread_delay(5);
}
rt_thread_delay(5);
}
}
/* 线程2入口函数 */
static void thread2_entry(void *param)
{
while (1)
{
rt_kprintf("thread2: send event1\n");
rt_event_send(&event, (1 << 3));
while (1)
{
rt_kprintf("thread2: send event1\n");
rt_event_send(&event, (1 << 3));
rt_thread_delay(10);
}
rt_thread_delay(10);
}
}
/* 线程3入口函数 */
static void thread3_entry(void *param)
{
while (1)
{
rt_kprintf("thread3: send event2\n");
rt_event_send(&event, (1 << 5));
while (1)
{
rt_kprintf("thread3: send event2\n");
rt_event_send(&event, (1 << 5));
rt_thread_delay(20);
}
rt_thread_delay(20);
}
}
int event_simple_init()
{
/* 初始化事件对象 */
rt_event_init(&event, "event", RT_IPC_FLAG_FIFO);
/* 初始化事件对象 */
rt_event_init(&event, "event", RT_IPC_FLAG_FIFO);
/* 创建线程1 */
tid1 = rt_thread_create("t1",
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程1 */
tid1 = rt_thread_create("t1",
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程2 */
tid2 = rt_thread_create("t2",
thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程2 */
tid2 = rt_thread_create("t2",
thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程3 */
tid3 = rt_thread_create("t3",
thread3_entry, RT_NULL, /* 线程入口是thread3_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid3 != RT_NULL)
rt_thread_startup(tid3);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程3 */
tid3 = rt_thread_create("t3",
thread3_entry, RT_NULL, /* 线程入口是thread3_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid3 != RT_NULL)
rt_thread_startup(tid3);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
return 0;
return 0;
}
#ifdef RT_USING_TC
static void _tc_cleanup()
{
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical();
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical();
/* 删除线程 */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2);
if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid3);
/* 删除线程 */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2);
if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid3);
/* 执行事件对象脱离 */
rt_event_detach(&event);
/* 执行事件对象脱离 */
rt_event_detach(&event);
/* 调度器解锁 */
rt_exit_critical();
/* 调度器解锁 */
rt_exit_critical();
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
}
int _tc_event_simple()
{
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
event_simple_init();
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
event_simple_init();
/* 返回TestCase运行的最长时间 */
return 100;
/* 返回TestCase运行的最长时间 */
return 100;
}
/* 输出函数命令到finsh shell中 */
FINSH_FUNCTION_EXPORT(_tc_event_simple, a simple event example);
@ -145,8 +145,8 @@ FINSH_FUNCTION_EXPORT(_tc_event_simple, a simple event example);
/* 用户应用入口 */
int rt_application_init()
{
event_simple_init();
event_simple_init();
return 0;
return 0;
}
#endif

View File

@ -7,68 +7,68 @@
static rt_bool_t mem_check(rt_uint8_t *ptr, rt_uint8_t value, rt_uint32_t len)
{
while (len)
{
if (*ptr != value)
while (len)
{
if (*ptr != value)
return RT_FALSE;
ptr ++;
len --;
}
ptr ++;
len --;
}
return RT_TRUE;
return RT_TRUE;
}
static void heap_malloc_init()
{
rt_uint8_t res = TC_STAT_PASSED;
rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
rt_uint8_t res = TC_STAT_PASSED;
rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
ptr1 = rt_malloc(1);
ptr2 = rt_malloc(13);
ptr3 = rt_malloc(31);
ptr4 = rt_malloc(127);
ptr5 = rt_malloc(0);
ptr1 = rt_malloc(1);
ptr2 = rt_malloc(13);
ptr3 = rt_malloc(31);
ptr4 = rt_malloc(127);
ptr5 = rt_malloc(0);
memset(ptr1, 1, 1);
memset(ptr2, 2, 13);
memset(ptr3, 3, 31);
memset(ptr4, 4, 127);
memset(ptr1, 1, 1);
memset(ptr2, 2, 13);
memset(ptr3, 3, 31);
memset(ptr4, 4, 127);
if (mem_check(ptr1, 1, 1) == RT_FALSE)
if (mem_check(ptr1, 1, 1) == RT_FALSE)
res = TC_STAT_FAILED;
if (mem_check(ptr2, 2, 13) == RT_FALSE)
if (mem_check(ptr2, 2, 13) == RT_FALSE)
res = TC_STAT_FAILED;
if (mem_check(ptr3, 3, 31) == RT_FALSE)
if (mem_check(ptr3, 3, 31) == RT_FALSE)
res = TC_STAT_FAILED;
if (mem_check(ptr4, 4, 127) == RT_FALSE)
if (mem_check(ptr4, 4, 127) == RT_FALSE)
res = TC_STAT_FAILED;
rt_free(ptr4);
rt_free(ptr3);
rt_free(ptr2);
rt_free(ptr1);
rt_free(ptr4);
rt_free(ptr3);
rt_free(ptr2);
rt_free(ptr1);
if (ptr5 != RT_NULL)
{
rt_free(ptr5);
}
if (ptr5 != RT_NULL)
{
rt_free(ptr5);
}
tc_done(res);
tc_done(res);
}
#ifdef RT_USING_TC
int _tc_heap_malloc()
{
heap_malloc_init();
heap_malloc_init();
return 0;
return 0;
}
FINSH_FUNCTION_EXPORT(_tc_heap_malloc, a heap malloc test);
#else
int rt_application_init()
{
heap_malloc_init();
heap_malloc_init();
return 0;
return 0;
}
#endif

View File

@ -7,96 +7,96 @@
static rt_bool_t mem_check(rt_uint8_t *ptr, rt_uint8_t value, rt_uint32_t len)
{
while (len)
{
if (*ptr != value) return RT_FALSE;
while (len)
{
if (*ptr != value) return RT_FALSE;
ptr ++;
len --;
}
ptr ++;
len --;
}
return RT_TRUE;
return RT_TRUE;
}
static void heap_realloc_init()
{
rt_uint8_t res = TC_STAT_PASSED;
rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
rt_uint8_t res = TC_STAT_PASSED;
rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
ptr1 = rt_malloc(1);
ptr2 = rt_malloc(13);
ptr3 = rt_malloc(31);
ptr4 = rt_malloc(127);
ptr5 = rt_malloc(0);
ptr1 = rt_malloc(1);
ptr2 = rt_malloc(13);
ptr3 = rt_malloc(31);
ptr4 = rt_malloc(127);
ptr5 = rt_malloc(0);
memset(ptr1, 1, 1);
memset(ptr2, 2, 13);
memset(ptr3, 3, 31);
memset(ptr4, 4, 127);
memset(ptr1, 1, 1);
memset(ptr2, 2, 13);
memset(ptr3, 3, 31);
memset(ptr4, 4, 127);
if (mem_check(ptr1, 1, 1) == RT_FALSE)
if (mem_check(ptr1, 1, 1) == RT_FALSE)
{
res = TC_STAT_FAILED;
goto _free;
}
if (mem_check(ptr2, 2, 13) == RT_FALSE)
if (mem_check(ptr2, 2, 13) == RT_FALSE)
{
res = TC_STAT_FAILED;
goto _free;
}
if (mem_check(ptr3, 3, 31) == RT_FALSE)
if (mem_check(ptr3, 3, 31) == RT_FALSE)
{
res = TC_STAT_FAILED;
goto _free;
}
if (mem_check(ptr4, 4, 127) == RT_FALSE)
if (mem_check(ptr4, 4, 127) == RT_FALSE)
{
res = TC_STAT_FAILED;
goto _free;
}
ptr1 = rt_realloc(ptr1, 13);
ptr2 = rt_realloc(ptr2, 31);
ptr3 = rt_realloc(ptr3, 127);
ptr4 = rt_realloc(ptr4, 1);
ptr5 = rt_realloc(ptr5, 0);
if (ptr5)
{
rt_kprintf("realloc(ptr, 0) should return NULL\n");
res = TC_STAT_FAILED;
}
ptr1 = rt_realloc(ptr1, 13);
ptr2 = rt_realloc(ptr2, 31);
ptr3 = rt_realloc(ptr3, 127);
ptr4 = rt_realloc(ptr4, 1);
ptr5 = rt_realloc(ptr5, 0);
if (ptr5)
{
rt_kprintf("realloc(ptr, 0) should return NULL\n");
res = TC_STAT_FAILED;
}
if (mem_check(ptr1, 1, 1) == RT_FALSE)
if (mem_check(ptr1, 1, 1) == RT_FALSE)
res = TC_STAT_FAILED;
if (mem_check(ptr2, 2, 13) == RT_FALSE)
if (mem_check(ptr2, 2, 13) == RT_FALSE)
res = TC_STAT_FAILED;
if (mem_check(ptr3, 3, 31) == RT_FALSE)
if (mem_check(ptr3, 3, 31) == RT_FALSE)
res = TC_STAT_FAILED;
if (mem_check(ptr4, 4, 1) == RT_FALSE)
if (mem_check(ptr4, 4, 1) == RT_FALSE)
res = TC_STAT_FAILED;
_free:
rt_free(ptr4);
rt_free(ptr3);
rt_free(ptr2);
rt_free(ptr1);
rt_free(ptr4);
rt_free(ptr3);
rt_free(ptr2);
rt_free(ptr1);
tc_done(res);
tc_done(res);
}
#ifdef RT_USING_TC
int _tc_heap_realloc()
{
heap_realloc_init();
heap_realloc_init();
return 0;
return 0;
}
FINSH_FUNCTION_EXPORT(_tc_heap_realloc, a heap re-malloc test);
#else
int rt_application_init()
{
heap_realloc_init();
heap_realloc_init();
return 0;
return 0;
}
#endif

View File

@ -22,110 +22,110 @@ static char mb_str2[] = "this is another mail!";
/* 线程1入口 */
static void thread1_entry(void* parameter)
{
unsigned char* str;
unsigned char* str;
while (1)
{
/* 从邮箱中收取邮件 */
if (rt_mb_recv(&mb, (rt_uint32_t*)&str, RT_WAITING_FOREVER) == RT_EOK)
{
rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str);
while (1)
{
/* 从邮箱中收取邮件 */
if (rt_mb_recv(&mb, (rt_uint32_t*)&str, RT_WAITING_FOREVER) == RT_EOK)
{
rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str);
/* 延时20个OS Tick */
rt_thread_delay(50);
}
}
/* 延时20个OS Tick */
rt_thread_delay(50);
}
}
}
/* 线程2入口 */
static void thread2_entry(void* parameter)
{
rt_uint8_t count;
rt_uint8_t count;
char *str;
count = 0;
while (1)
{
count ++;
if (count & 0x1)
{
/* 发送mb_str1地址到邮箱中 */
count = 0;
while (1)
{
count ++;
if (count & 0x1)
{
/* 发送mb_str1地址到邮箱中 */
str = mb_str1;
}
else
{
/* 发送mb_str2地址到邮箱中 */
}
else
{
/* 发送mb_str2地址到邮箱中 */
str = mb_str2;
}
}
/* 不停的发送邮件如果满了则等待10个tick然后超时 */
if( rt_mb_send_wait(&mb, (rt_uint32_t)str,10) == RT_EOK )
rt_kprintf("thread2: sent a mail to mailbox, the content:%s\n", str);
else
rt_kprintf("thread2: timeout while waiting to send a mail.\n");
}
}
}
int mbox_send_wait_init()
{
/* 初始化一个mailbox */
rt_mb_init(&mb,
"mbt", /* 名称是mbt */
&mb_pool[0], /* 邮箱用到的内存池是mb_pool */
sizeof(mb_pool)/4, /* 大小是mb_pool大小除以4因为一封邮件的大小是4字节 */
RT_IPC_FLAG_FIFO); /* 采用FIFO方式进行线程等待 */
/* 初始化一个mailbox */
rt_mb_init(&mb,
"mbt", /* 名称是mbt */
&mb_pool[0], /* 邮箱用到的内存池是mb_pool */
sizeof(mb_pool)/4, /* 大小是mb_pool大小除以4因为一封邮件的大小是4字节 */
RT_IPC_FLAG_FIFO); /* 采用FIFO方式进行线程等待 */
/* 创建线程1 */
tid1 = rt_thread_create("t1",
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程1 */
tid1 = rt_thread_create("t1",
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程2 */
tid2 = rt_thread_create("t2",
thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程2 */
tid2 = rt_thread_create("t2",
thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
return 0;
return 0;
}
#ifdef RT_USING_TC
static void _tc_cleanup()
{
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical();
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical();
/* 删除线程 */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2);
/* 删除线程 */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2);
/* 执行邮箱对象脱离 */
rt_mb_detach(&mb);
/* 执行邮箱对象脱离 */
rt_mb_detach(&mb);
/* 调度器解锁 */
rt_exit_critical();
/* 调度器解锁 */
rt_exit_critical();
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
}
int _tc_mbox_send_wait()
{
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
mbox_send_wait_init();
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
mbox_send_wait_init();
/* 返回TestCase运行的最长时间 */
return 300;
/* 返回TestCase运行的最长时间 */
return 300;
}
/* 输出函数命令到finsh shell中 */
FINSH_FUNCTION_EXPORT(_tc_mbox_send_wait, a example of mailbox send wait);
@ -133,9 +133,9 @@ FINSH_FUNCTION_EXPORT(_tc_mbox_send_wait, a example of mailbox send wait);
/* 用户应用入口 */
int rt_application_init()
{
mbox_send_wait_init();
mbox_send_wait_init();
return 0;
return 0;
}
#endif

View File

@ -22,108 +22,108 @@ static char mb_str2[] = "this is another mail!";
/* 线程1入口 */
static void thread1_entry(void* parameter)
{
unsigned char* str;
unsigned char* str;
while (1)
{
rt_kprintf("thread1: try to recv a mail\n");
while (1)
{
rt_kprintf("thread1: try to recv a mail\n");
/* 从邮箱中收取邮件 */
if (rt_mb_recv(&mb, (rt_uint32_t*)&str, RT_WAITING_FOREVER) == RT_EOK)
{
rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str);
/* 从邮箱中收取邮件 */
if (rt_mb_recv(&mb, (rt_uint32_t*)&str, RT_WAITING_FOREVER) == RT_EOK)
{
rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str);
/* 延时10个OS Tick */
rt_thread_delay(10);
}
}
/* 延时10个OS Tick */
rt_thread_delay(10);
}
}
}
/* 线程2入口 */
static void thread2_entry(void* parameter)
{
rt_uint8_t count;
rt_uint8_t count;
count = 0;
while (1)
{
count ++;
if (count & 0x1)
{
/* 发送mb_str1地址到邮箱中 */
rt_mb_send(&mb, (rt_uint32_t)&mb_str1[0]);
}
else
{
/* 发送mb_str2地址到邮箱中 */
rt_mb_send(&mb, (rt_uint32_t)&mb_str2[0]);
}
count = 0;
while (1)
{
count ++;
if (count & 0x1)
{
/* 发送mb_str1地址到邮箱中 */
rt_mb_send(&mb, (rt_uint32_t)&mb_str1[0]);
}
else
{
/* 发送mb_str2地址到邮箱中 */
rt_mb_send(&mb, (rt_uint32_t)&mb_str2[0]);
}
/* 延时20个OS Tick */
rt_thread_delay(20);
}
/* 延时20个OS Tick */
rt_thread_delay(20);
}
}
int mbox_simple_init()
{
/* 初始化一个mailbox */
rt_mb_init(&mb,
"mbt", /* 名称是mbt */
&mb_pool[0], /* 邮箱用到的内存池是mb_pool */
sizeof(mb_pool)/4, /* 大小是mb_pool大小除以4因为一封邮件的大小是4字节 */
RT_IPC_FLAG_FIFO); /* 采用FIFO方式进行线程等待 */
/* 初始化一个mailbox */
rt_mb_init(&mb,
"mbt", /* 名称是mbt */
&mb_pool[0], /* 邮箱用到的内存池是mb_pool */
sizeof(mb_pool)/4, /* 大小是mb_pool大小除以4因为一封邮件的大小是4字节 */
RT_IPC_FLAG_FIFO); /* 采用FIFO方式进行线程等待 */
/* 创建线程1 */
tid1 = rt_thread_create("t1",
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程1 */
tid1 = rt_thread_create("t1",
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程2 */
tid2 = rt_thread_create("t2",
thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程2 */
tid2 = rt_thread_create("t2",
thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
return 0;
return 0;
}
#ifdef RT_USING_TC
static void _tc_cleanup()
{
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical();
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical();
/* 删除线程 */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2);
/* 删除线程 */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2);
/* 执行邮箱对象脱离 */
rt_mb_detach(&mb);
/* 执行邮箱对象脱离 */
rt_mb_detach(&mb);
/* 调度器解锁 */
rt_exit_critical();
/* 调度器解锁 */
rt_exit_critical();
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
}
int _tc_mbox_simple()
{
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
mbox_simple_init();
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
mbox_simple_init();
/* 返回TestCase运行的最长时间 */
return 100;
/* 返回TestCase运行的最长时间 */
return 100;
}
/* 输出函数命令到finsh shell中 */
FINSH_FUNCTION_EXPORT(_tc_mbox_simple, a simple mailbox example);
@ -131,8 +131,8 @@ FINSH_FUNCTION_EXPORT(_tc_mbox_simple, a simple mailbox example);
/* 用户应用入口 */
int rt_application_init()
{
mbox_simple_init();
mbox_simple_init();
return 0;
return 0;
}
#endif

View File

@ -18,115 +18,115 @@ static rt_thread_t tid2 = RT_NULL;
/* 线程1入口 */
static void thread1_entry(void* parameter)
{
int i;
char *block;
int i;
char *block;
while(1)
{
for (i = 0; i < 48; i++)
{
/* 申请内存块 */
rt_kprintf("allocate No.%d\n", i);
if (ptr[i] == RT_NULL)
{
ptr[i] = rt_mp_alloc(&mp, RT_WAITING_FOREVER);
}
}
while(1)
{
for (i = 0; i < 48; i++)
{
/* 申请内存块 */
rt_kprintf("allocate No.%d\n", i);
if (ptr[i] == RT_NULL)
{
ptr[i] = rt_mp_alloc(&mp, RT_WAITING_FOREVER);
}
}
/* 继续申请一个内存块,因为已经没有内存块,线程应该被挂起 */
block = rt_mp_alloc(&mp, RT_WAITING_FOREVER);
rt_kprintf("allocate the block mem\n");
/* 释放这个内存块 */
rt_mp_free(block);
block = RT_NULL;
}
/* 继续申请一个内存块,因为已经没有内存块,线程应该被挂起 */
block = rt_mp_alloc(&mp, RT_WAITING_FOREVER);
rt_kprintf("allocate the block mem\n");
/* 释放这个内存块 */
rt_mp_free(block);
block = RT_NULL;
}
}
/* 线程2入口线程2的优先级比线程1低应该线程1先获得执行。*/
static void thread2_entry(void *parameter)
{
int i;
int i;
while(1)
{
rt_kprintf("try to release block\n");
while(1)
{
rt_kprintf("try to release block\n");
for (i = 0 ; i < 48; i ++)
{
/* 释放所有分配成功的内存块 */
if (ptr[i] != RT_NULL)
{
rt_kprintf("release block %d\n", i);
for (i = 0 ; i < 48; i ++)
{
/* 释放所有分配成功的内存块 */
if (ptr[i] != RT_NULL)
{
rt_kprintf("release block %d\n", i);
rt_mp_free(ptr[i]);
ptr[i] = RT_NULL;
}
}
rt_mp_free(ptr[i]);
ptr[i] = RT_NULL;
}
}
/* 休眠10个OS Tick */
rt_thread_delay(10);
}
/* 休眠10个OS Tick */
rt_thread_delay(10);
}
}
int mempool_simple_init()
{
int i;
for (i = 0; i < 48; i ++) ptr[i] = RT_NULL;
int i;
for (i = 0; i < 48; i ++) ptr[i] = RT_NULL;
/* 初始化内存池对象 */
rt_mp_init(&mp, "mp1", &mempool[0], sizeof(mempool), 80);
/* 初始化内存池对象 */
rt_mp_init(&mp, "mp1", &mempool[0], sizeof(mempool), 80);
/* 创建线程1 */
tid1 = rt_thread_create("t1",
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程1 */
tid1 = rt_thread_create("t1",
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程2 */
tid2 = rt_thread_create("t2",
thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程2 */
tid2 = rt_thread_create("t2",
thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
return 0;
return 0;
}
#ifdef RT_USING_TC
static void _tc_cleanup()
{
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical();
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical();
/* 删除线程 */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2);
/* 删除线程 */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2);
/* 执行内存池脱离 */
rt_mp_detach(&mp);
/* 执行内存池脱离 */
rt_mp_detach(&mp);
/* 调度器解锁 */
rt_exit_critical();
/* 调度器解锁 */
rt_exit_critical();
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
}
int _tc_mempool_simple()
{
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
mempool_simple_init();
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
mempool_simple_init();
/* 返回TestCase运行的最长时间 */
return 100;
/* 返回TestCase运行的最长时间 */
return 100;
}
/* 输出函数命令到finsh shell中 */
FINSH_FUNCTION_EXPORT(_tc_mempool_simple, a memory pool example);
@ -134,8 +134,8 @@ FINSH_FUNCTION_EXPORT(_tc_mempool_simple, a memory pool example);
/* 用户应用入口 */
int rt_application_init()
{
mempool_simple_init();
mempool_simple_init();
return 0;
return 0;
}
#endif

View File

@ -20,139 +20,139 @@ static char msg_pool[2048];
/* 线程1入口函数 */
static void thread1_entry(void* parameter)
{
char buf[128];
char buf[128];
while (1)
{
rt_memset(&buf[0], 0, sizeof(buf));
while (1)
{
rt_memset(&buf[0], 0, sizeof(buf));
/* 从消息队列中接收消息 */
if (rt_mq_recv(&mq, &buf[0], sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
{
rt_kprintf("thread1: recv msg from message queue, the content:%s\n", buf);
}
/* 从消息队列中接收消息 */
if (rt_mq_recv(&mq, &buf[0], sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
{
rt_kprintf("thread1: recv msg from message queue, the content:%s\n", buf);
}
/* 延迟10个OS Tick */
rt_thread_delay(10);
}
/* 延迟10个OS Tick */
rt_thread_delay(10);
}
}
/* 线程2入口函数 */
static void thread2_entry(void* parameter)
{
int i, result;
char buf[] = "this is message No.x";
int i, result;
char buf[] = "this is message No.x";
while (1)
{
for (i = 0; i < 10; i++)
{
buf[sizeof(buf) - 2] = '0' + i;
while (1)
{
for (i = 0; i < 10; i++)
{
buf[sizeof(buf) - 2] = '0' + i;
rt_kprintf("thread2: send message - %s\n", buf);
/* 发送消息到消息队列中 */
result = rt_mq_send(&mq, &buf[0], sizeof(buf));
if ( result == -RT_EFULL)
{
/* 消息队列满, 延迟1s时间 */
rt_kprintf("message queue full, delay 1s\n");
rt_thread_delay(100);
}
}
rt_kprintf("thread2: send message - %s\n", buf);
/* 发送消息到消息队列中 */
result = rt_mq_send(&mq, &buf[0], sizeof(buf));
if ( result == -RT_EFULL)
{
/* 消息队列满, 延迟1s时间 */
rt_kprintf("message queue full, delay 1s\n");
rt_thread_delay(100);
}
}
/* 延时10个OS Tick */
rt_thread_delay(10);
}
/* 延时10个OS Tick */
rt_thread_delay(10);
}
}
/* 线程3入口函数 */
static void thread3_entry(void* parameter)
{
char buf[] = "this is an urgent message!";
char buf[] = "this is an urgent message!";
while (1)
{
rt_kprintf("thread3: send an urgent message\n");
while (1)
{
rt_kprintf("thread3: send an urgent message\n");
/* 发送紧急消息到消息队列中 */
rt_mq_urgent(&mq, &buf[0], sizeof(buf));
/* 发送紧急消息到消息队列中 */
rt_mq_urgent(&mq, &buf[0], sizeof(buf));
/* 延时25个OS Tick */
rt_thread_delay(25);
}
/* 延时25个OS Tick */
rt_thread_delay(25);
}
}
int messageq_simple_init()
{
/* 初始化消息队列 */
rt_mq_init(&mq, "mqt",
&msg_pool[0], /* 内存池指向msg_pool */
128 - sizeof(void*), /* 每个消息的大小是 128 - void* */
sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */
RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
/* 初始化消息队列 */
rt_mq_init(&mq, "mqt",
&msg_pool[0], /* 内存池指向msg_pool */
128 - sizeof(void*), /* 每个消息的大小是 128 - void* */
sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */
RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
/* 创建线程1 */
tid1 = rt_thread_create("t1",
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程1 */
tid1 = rt_thread_create("t1",
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程2 */
tid2 = rt_thread_create("t2",
thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程2 */
tid2 = rt_thread_create("t2",
thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程3 */
tid3 = rt_thread_create("t3",
thread3_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid3 != RT_NULL)
rt_thread_startup(tid3);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程3 */
tid3 = rt_thread_create("t3",
thread3_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid3 != RT_NULL)
rt_thread_startup(tid3);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
return 0;
return 0;
}
#ifdef RT_USING_TC
static void _tc_cleanup()
{
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical();
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical();
/* 删除线程 */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2);
if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid3);
/* 删除线程 */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2);
if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid3);
/* 执行消息队列对象脱离 */
rt_mq_detach(&mq);
/* 执行消息队列对象脱离 */
rt_mq_detach(&mq);
/* 调度器解锁 */
rt_exit_critical();
/* 调度器解锁 */
rt_exit_critical();
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
}
int _tc_messageq_simple()
{
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
messageq_simple_init();
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
messageq_simple_init();
/* 返回TestCase运行的最长时间 */
return 100;
/* 返回TestCase运行的最长时间 */
return 100;
}
/* 输出函数命令到finsh shell中 */
FINSH_FUNCTION_EXPORT(_tc_messageq_simple, a simple message queue example);
@ -160,8 +160,8 @@ FINSH_FUNCTION_EXPORT(_tc_messageq_simple, a simple message queue example);
/* 用户应用入口 */
int rt_application_init()
{
messageq_simple_init();
messageq_simple_init();
return 0;
return 0;
}
#endif

View File

@ -13,142 +13,142 @@ static rt_mutex_t mutex = RT_NULL;
/* 线程1入口 */
static void thread1_entry(void* parameter)
{
/* 先让低优先级线程运行 */
rt_thread_delay(10);
/* 先让低优先级线程运行 */
rt_thread_delay(10);
/* 此时thread3持有mutex并且thread2等待持有mutex */
/* 此时thread3持有mutex并且thread2等待持有mutex */
/* 检查thread2与thread3的优先级情况 */
if (tid2->current_priority != tid3->current_priority)
{
/* 优先级不相同,测试失败 */
tc_stat(TC_STAT_END | TC_STAT_FAILED);
return;
}
/* 检查thread2与thread3的优先级情况 */
if (tid2->current_priority != tid3->current_priority)
{
/* 优先级不相同,测试失败 */
tc_stat(TC_STAT_END | TC_STAT_FAILED);
return;
}
}
/* 线程2入口 */
static void thread2_entry(void* parameter)
{
rt_err_t result;
rt_err_t result;
/* 先让低优先级线程运行 */
rt_thread_delay(5);
/* 先让低优先级线程运行 */
rt_thread_delay(5);
while (1)
{
/*
* thread3持有thread3的优先级提升到thread2相同
*
*/
result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
while (1)
{
/*
* thread3持有thread3的优先级提升到thread2相同
*
*/
result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
if (result == RT_EOK)
{
/* 释放互斥锁 */
rt_mutex_release(mutex);
}
}
if (result == RT_EOK)
{
/* 释放互斥锁 */
rt_mutex_release(mutex);
}
}
}
/* 线程3入口 */
static void thread3_entry(void* parameter)
{
rt_tick_t tick;
rt_err_t result;
rt_tick_t tick;
rt_err_t result;
while (1)
{
result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
if (result != RT_EOK)
{
tc_stat(TC_STAT_END | TC_STAT_FAILED);
}
while (1)
{
result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
result = rt_mutex_take(mutex, RT_WAITING_FOREVER);
if (result != RT_EOK)
{
tc_stat(TC_STAT_END | TC_STAT_FAILED);
}
/* 做一个长时间的循环总共50个OS Tick */
tick = rt_tick_get();
while (rt_tick_get() - tick < 50) ;
/* 做一个长时间的循环总共50个OS Tick */
tick = rt_tick_get();
while (rt_tick_get() - tick < 50) ;
rt_mutex_release(mutex);
rt_mutex_release(mutex);
}
rt_mutex_release(mutex);
rt_mutex_release(mutex);
}
}
int mutex_simple_init()
{
/* 创建互斥锁 */
mutex = rt_mutex_create("mutex", RT_IPC_FLAG_FIFO);
if (mutex == RT_NULL)
{
tc_stat(TC_STAT_END | TC_STAT_FAILED);
return 0;
}
/* 创建互斥锁 */
mutex = rt_mutex_create("mutex", RT_IPC_FLAG_FIFO);
if (mutex == RT_NULL)
{
tc_stat(TC_STAT_END | TC_STAT_FAILED);
return 0;
}
/* 创建线程1 */
tid1 = rt_thread_create("t1",
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程1 */
tid1 = rt_thread_create("t1",
thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程2 */
tid2 = rt_thread_create("t2",
thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程2 */
tid2 = rt_thread_create("t2",
thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程3 */
tid3 = rt_thread_create("t3",
thread3_entry, RT_NULL, /* 线程入口是thread3_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
if (tid3 != RT_NULL)
rt_thread_startup(tid3);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* 创建线程3 */
tid3 = rt_thread_create("t3",
thread3_entry, RT_NULL, /* 线程入口是thread3_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
if (tid3 != RT_NULL)
rt_thread_startup(tid3);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
return 0;
return 0;
}
#ifdef RT_USING_TC
static void _tc_cleanup()
{
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical();
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical();
/* 删除线程 */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2);
if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid3);
/* 删除线程 */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2);
if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid3);
if (mutex != RT_NULL)
{
rt_mutex_delete(mutex);
}
if (mutex != RT_NULL)
{
rt_mutex_delete(mutex);
}
/* 调度器解锁 */
rt_exit_critical();
/* 调度器解锁 */
rt_exit_critical();
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
}
int _tc_mutex_simple()
{
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
mutex_simple_init();
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
mutex_simple_init();
/* 返回TestCase运行的最长时间 */
return 100;
/* 返回TestCase运行的最长时间 */
return 100;
}
/* 输出函数命令到finsh shell中 */
FINSH_FUNCTION_EXPORT(_tc_mutex_simple, sime mutex example);
@ -156,8 +156,8 @@ FINSH_FUNCTION_EXPORT(_tc_mutex_simple, sime mutex example);
/* 用户应用入口 */
int rt_application_init()
{
mutex_simple_init();
mutex_simple_init();
return 0;
return 0;
}
#endif

View File

@ -20,6 +20,7 @@ FINSH_VAR_EXPORT(_tc_scale, finsh_type_int, the testcase timer timeout scale)
void tc_thread_entry(void* parameter)
{
unsigned int fail_count = 0;
struct finsh_syscall* index;
/* create tc semaphore */
@ -59,17 +60,30 @@ void tc_thread_entry(void* parameter)
_tc_current);
/* If the TC forgot to clear the flag, we do it. */
_tc_stat &= ~TC_STAT_RUNNING;
}
}
if (_tc_stat & TC_STAT_FAILED)
rt_kprintf("TestCase[%s] failed\n", _tc_current);
else
rt_kprintf("TestCase[%s] passed\n", _tc_current);
if (_tc_stat & TC_STAT_FAILED)
{
rt_kprintf("TestCase[%s] failed\n", _tc_current);
fail_count++;
}
else
{
rt_kprintf("TestCase[%s] passed\n", _tc_current);
}
}
}
}
rt_kprintf("RT-Thread TestCase Running Done!\n");
if (fail_count)
{
rt_kprintf("%d tests failed\n", fail_count);
}
else
{
rt_kprintf("All tests passed\n");
}
/* detach tc semaphore */
rt_sem_detach(&_tc_sem);
}