diff --git a/examples/kernel/SConscript b/examples/kernel/SConscript index f07ccbb84bb586b07d0f3e39a87f2da1c2271faa..361008515770dc7a43cab5d88c9f665cf41aaa7b 100644 --- a/examples/kernel/SConscript +++ b/examples/kernel/SConscript @@ -31,9 +31,9 @@ timer_timeout.c heap_malloc.c heap_realloc.c memp_simple.c +tc_sample.c """) -CPPDEFINES = ['RT_USING_TC'] -group = DefineGroup('examples', src, depend = [''], CPPDEFINES = CPPDEFINES) +group = DefineGroup('examples', src, depend = ['RT_USING_TC']) Return('group') diff --git a/examples/kernel/tc_comm.c b/examples/kernel/tc_comm.c index 47d34359328d7cd4844c57940dfd74b3ff271688..de9bb3ce31d450bca766d55dc888d4695658fd5e 100644 --- a/examples/kernel/tc_comm.c +++ b/examples/kernel/tc_comm.c @@ -67,6 +67,7 @@ void tc_thread_entry(void* parameter) } } + rt_kprintf("RT-Thread TestCase Running Done!\n"); /* detach tc semaphore */ rt_sem_detach(&_tc_sem); } diff --git a/examples/kernel/tc_sample.c b/examples/kernel/tc_sample.c new file mode 100644 index 0000000000000000000000000000000000000000..ea77ccda39398da34f822f27d1695b308f59f082 --- /dev/null +++ b/examples/kernel/tc_sample.c @@ -0,0 +1,62 @@ +#include +#include "tc_comm.h" + +static rt_thread_t tid = RT_NULL; +static void sample_thread(void* parameter) +{ + rt_kprintf("I'm sample!\n"); +} +static void sample_thread_cleanup(struct rt_thread *p) +{ + tid = RT_NULL; + tc_done(TC_STAT_PASSED); +} + +int sample_init() +{ + tid = rt_thread_create("t", + sample_thread, RT_NULL, + THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); + if (tid != RT_NULL) + { + rt_thread_startup(tid); + tid->cleanup = sample_thread_cleanup; + } + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + return 0; +} + +#ifdef RT_USING_TC +static void _tc_cleanup() +{ + /* lock scheduler */ + rt_enter_critical(); + /* delete thread */ + if (tid != RT_NULL) + { + rt_kprintf("tid1 is bad\n"); + tc_stat(TC_STAT_FAILED); + } + /* unlock scheduler */ + rt_exit_critical(); +} + +int _tc_sample() +{ + /* set tc cleanup */ + tc_cleanup(_tc_cleanup); + sample_init(); + + return 25; +} +FINSH_FUNCTION_EXPORT(_tc_sample, a thread testcase example); +#else +int rt_application_init() +{ + sample_init(); + + return 0; +} +#endif diff --git a/examples/kernel/thread_delete.c b/examples/kernel/thread_delete.c index 994c5e84ddfc88978ec32a7543a682c82e0d1c62..a394f282d06aa06d9268069faa07db08c76b3d4d 100644 --- a/examples/kernel/thread_delete.c +++ b/examples/kernel/thread_delete.c @@ -20,9 +20,20 @@ static void thread1_entry(void* parameter) while (1) { /* 线程1采用低优先级运行,一直打印计数值 */ - rt_kprintf("thread count: %d\n", count ++); + // rt_kprintf("thread count: %d\n", count ++); + count ++; } } +static void thread1_cleanup(struct rt_thread *tid) +{ + if (tid != tid1) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + return ; + } + rt_kprintf("thread1 end\n"); + tid1 = RT_NULL; +} /* 线程2的入口函数 */ static void thread2_entry(void* parameter) @@ -37,19 +48,29 @@ static void thread2_entry(void* parameter) * 队列 */ rt_thread_delete(tid1); - tid1 = RT_NULL; /* * 线程2继续休眠10个OS Tick然后退出,线程2休眠后应切换到idle线程 * idle线程将执行真正的线程1控制块和线程栈的删除 */ rt_thread_delay(10); +} +static void thread2_cleanup(struct rt_thread *tid) +{ /* - * 线程2运行结束后也将自动被删除(线程控制块和线程栈依然在idle线 + * 线程2运行结束后也将自动被删除(线程控制块和线程栈在idle线 * 程中释放) */ + + if (tid != tid2) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + return ; + } + rt_kprintf("thread2 end\n"); tid2 = RT_NULL; + tc_done(TC_STAT_PASSED); } /* 线程删除示例的初始化 */ @@ -60,7 +81,10 @@ int thread_delete_init() thread1_entry, RT_NULL, /* 入口是thread1_entry,参数是RT_NULL */ THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); if (tid1 != RT_NULL) /* 如果获得线程控制块,启动这个线程 */ + { + tid1->cleanup = thread1_cleanup; rt_thread_startup(tid1); + } else tc_stat(TC_STAT_END | TC_STAT_FAILED); @@ -69,7 +93,10 @@ int thread_delete_init() thread2_entry, RT_NULL, /* 入口是thread2_entry,参数是RT_NULL */ THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE); if (tid2 != RT_NULL) /* 如果获得线程控制块,启动这个线程 */ + { + tid2->cleanup = thread2_cleanup; rt_thread_startup(tid2); + } else tc_stat(TC_STAT_END | TC_STAT_FAILED); @@ -83,10 +110,16 @@ static void _tc_cleanup() rt_enter_critical(); /* delete thread */ - if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE) + if (tid1 != RT_NULL) + { + rt_kprintf("tid1 is bad\n"); tc_stat(TC_STAT_FAILED); - if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE) + } + if (tid2 != RT_NULL) + { + rt_kprintf("tid2 is bad\n"); tc_stat(TC_STAT_FAILED); + } /* unlock scheduler */ rt_exit_critical(); @@ -98,7 +131,7 @@ int _tc_thread_delete() tc_cleanup(_tc_cleanup); thread_delete_init(); - return 100; + return 25; } FINSH_FUNCTION_EXPORT(_tc_thread_delete, a thread delete example); #else diff --git a/examples/kernel/thread_detach.c b/examples/kernel/thread_detach.c index ac7a92558e4a16bcc4007beb2fda31ac9881ef9a..84c85977fb1928f67b38af377b92cef98e34a868 100644 --- a/examples/kernel/thread_detach.c +++ b/examples/kernel/thread_detach.c @@ -91,6 +91,9 @@ static void _tc_cleanup() /* 调度器解锁 */ rt_exit_critical(); + + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_thread_detach() @@ -100,7 +103,7 @@ int _tc_thread_detach() thread_detach_init(); /* 返回TestCase运行的最长时间 */ - return 100; + return 25; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_thread_detach, a static thread example); diff --git a/examples/kernel/thread_priority.c b/examples/kernel/thread_priority.c index c64e329a6fffb17d58af753ce2833d003459b935..6d0692e4dc4afeb73465c8f65222f8834d1bfec4 100644 --- a/examples/kernel/thread_priority.c +++ b/examples/kernel/thread_priority.c @@ -28,7 +28,7 @@ static void thread2_entry(void* parameter) tick = rt_tick_get(); while (1) { - if (rt_tick_get() - tick >= 100) + if (rt_tick_get() - tick >= 50) { if (count == 0) tc_done(TC_STAT_FAILED); diff --git a/examples/kernel/thread_resume.c b/examples/kernel/thread_resume.c index 23dfcdc132953c30f7125ac0f3cd46badcba0f11..e0ed6fa5f79527b659a5bee968d0c23464314848 100644 --- a/examples/kernel/thread_resume.c +++ b/examples/kernel/thread_resume.c @@ -25,6 +25,17 @@ static void thread1_entry(void* parameter) /* 当线程1被唤醒时 */ rt_kprintf("thread1 resumed\n"); } +static void thread_cleanup(rt_thread_t tid) +{ + if (tid == tid1) + { + tid1 = RT_NULL; + } + if (tid == tid2) + { + tid = RT_NULL; + } +} /* 线程2入口 */ static void thread2_entry(void* parameter) @@ -34,6 +45,7 @@ static void thread2_entry(void* parameter) /* 唤醒线程1 */ rt_thread_resume(tid1); + rt_kprintf("thread2: to resume thread1\n"); /* 延时10个OS Tick */ rt_thread_delay(10); @@ -48,7 +60,10 @@ int thread_resume_init() thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */ THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); if (tid1 != RT_NULL) + { + tid1->cleanup = thread_cleanup; rt_thread_startup(tid1); + } else tc_stat(TC_STAT_END | TC_STAT_FAILED); @@ -57,7 +72,10 @@ int thread_resume_init() thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */ THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE); if (tid2 != RT_NULL) + { + tid2->cleanup = thread_cleanup; rt_thread_startup(tid2); + } else tc_stat(TC_STAT_END | TC_STAT_FAILED); @@ -90,7 +108,7 @@ int _tc_thread_resume() thread_resume_init(); /* 返回TestCase运行的最长时间 */ - return 100; + return 25; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_thread_resume, a thread resume example); diff --git a/examples/kernel/thread_suspend.c b/examples/kernel/thread_suspend.c index 2be237a2a537b9ca17f9b76f27bd3765ddecadd7..b38cb8fdfbd75672bb5cb20bff3494dfdd1f3e96 100644 --- a/examples/kernel/thread_suspend.c +++ b/examples/kernel/thread_suspend.c @@ -34,6 +34,7 @@ static void thread2_entry(void* parameter) rt_thread_delay(10); /* 线程2自动退出 */ + tid2 = RT_NULL; } int thread_suspend_init() diff --git a/examples/kernel/thread_yield.c b/examples/kernel/thread_yield.c index e9fb7f57f9e7926de37d212da01666689c2e604a..71244589b84750065b74126d3b5e88dec7c76989 100644 --- a/examples/kernel/thread_yield.c +++ b/examples/kernel/thread_yield.c @@ -86,7 +86,7 @@ int _tc_thread_yield() thread_yield_init(); /* 返回TestCase运行的最长时间 */ - return 100; + return 30; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_thread_yield, a thread yield example);