diff options
| author | bernard.xiong <[email protected]> | 2010-03-01 09:37:59 +0000 |
|---|---|---|
| committer | bernard.xiong <[email protected]> | 2010-03-01 09:37:59 +0000 |
| commit | 6ccd04543c7928151d9b4fb782470e7d8f3e1e14 (patch) | |
| tree | 8fa48391b956a9683b8cb76341672c8ce379f822 /examples/kernel/tc_comm.c | |
| parent | 8aaddbaf384c7a1599145e15919ae10c2373bf04 (diff) | |
add thread example.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@456 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'examples/kernel/tc_comm.c')
| -rw-r--r-- | examples/kernel/tc_comm.c | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/examples/kernel/tc_comm.c b/examples/kernel/tc_comm.c new file mode 100644 index 0000000000..ad0a200e88 --- /dev/null +++ b/examples/kernel/tc_comm.c @@ -0,0 +1,165 @@ +#include "tc_comm.h"
+#include <finsh.h>
+
+#ifdef RT_USING_TC
+#define TC_PRIORITY 25
+#define TC_STACK_SIZE 0x400
+
+static rt_uint8_t _tc_stat;
+static struct rt_semaphore _tc_sem;
+static struct rt_thread _tc_thread;
+static rt_uint8_t _tc_stack[TC_STACK_SIZE];
+static char _tc_prefix[64];
+static const char* _tc_current;
+static void (*_tc_cleanup)(void) = RT_NULL;
+
+void tc_thread_entry(void* parameter)
+{
+ rt_err_t result;
+ struct finsh_syscall* index;
+
+ /* create tc semaphore */
+ rt_sem_init(&_tc_sem, "tc", 0, RT_IPC_FLAG_FIFO);
+
+ while (_tc_stat & TC_STAT_RUNNING)
+ {
+ for (index = _syscall_table_begin; index < _syscall_table_end; index ++)
+ {
+ /* search testcase */
+ if (rt_strstr(index->name, _tc_prefix) == index->name)
+ {
+ long tick;
+
+ _tc_current = index->name + 4;
+ rt_kprintf("Run TestCase: %s\n", _tc_current);
+ _tc_stat = TC_STAT_PASSED | TC_STAT_RUNNING;
+ tick = index->func();
+ if (tick > 0)
+ {
+ result = rt_sem_take(&_tc_sem, tick);
+ if (result != RT_EOK)
+ rt_kprintf("TestCase[%s] failed\n", _tc_current);
+ else
+ {
+ 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_cleanup != RT_NULL)
+ {
+ /* perform testcase cleanup */
+ _tc_cleanup();
+ _tc_cleanup = RT_NULL;
+ }
+ }
+ }
+ }
+
+ /* detach tc semaphore */
+ rt_sem_detach(&_tc_sem);
+}
+
+void tc_stop()
+{
+ _tc_stat &= ~TC_STAT_RUNNING;
+
+ rt_thread_delay(RT_TICK_PER_SECOND/2);
+ if (_tc_thread.stat != RT_THREAD_INIT)
+ {
+ /* lock scheduler */
+ rt_enter_critical();
+
+ /* detach old tc thread */
+ rt_thread_detach(&_tc_thread);
+ rt_sem_detach(&_tc_sem);
+
+ /* unlock scheduler */
+ rt_exit_critical();
+ }
+ rt_thread_delay(RT_TICK_PER_SECOND/2);
+}
+FINSH_FUNCTION_EXPORT(tc_stop, stop testcase thread);
+
+void tc_done(rt_uint8_t stat)
+{
+ _tc_stat |= stat;
+ _tc_stat &= ~TC_STAT_RUNNING;
+
+ /* release semaphore */
+ rt_sem_release(&_tc_sem);
+}
+
+void tc_stat(rt_uint8_t stat)
+{
+ if (stat & TC_STAT_FAILED)
+ {
+ rt_kprintf("TestCases[%s] failed\n", _tc_current);
+ }
+ _tc_stat |= stat;
+}
+
+void tc_cleanup(void (*cleanup)())
+{
+ _tc_cleanup = cleanup;
+}
+
+void tc_start(const char* tc_prefix)
+{
+ rt_err_t result;
+
+ /* tesecase prefix is null */
+ if (tc_prefix == RT_NULL)
+ {
+ rt_kprintf("TestCase Usage: tc_start(prefix)\n\n");
+ rt_kprintf("list_tc() can list all testcases.\n");
+ return ;
+ }
+
+ /* init tc thread */
+ if (_tc_stat & TC_STAT_RUNNING)
+ {
+ /* stop old tc thread */
+ tc_stop();
+ }
+
+ rt_memset(_tc_prefix, 0, sizeof(_tc_prefix));
+ rt_snprintf(_tc_prefix, sizeof(_tc_prefix),
+ "_tc_%s", tc_prefix);
+
+ result = rt_thread_init(&_tc_thread, "tc",
+ tc_thread_entry, RT_NULL,
+ &_tc_stack[0], sizeof(_tc_stack),
+ TC_PRIORITY - 3, 5);
+
+ /* set tc stat */
+ _tc_stat = TC_STAT_RUNNING | TC_STAT_FAILED;
+
+ if (result == RT_EOK)
+ rt_thread_startup(&_tc_thread);
+}
+FINSH_FUNCTION_EXPORT(tc_start, start testcase with testcase prefix or name);
+
+void list_tc()
+{
+ struct finsh_syscall* index;
+
+ rt_kprintf("TestCases List:\n");
+ for (index = _syscall_table_begin; index < _syscall_table_end; index ++)
+ {
+ /* search testcase */
+ if (rt_strstr(index->name, "_tc_") == index->name)
+ {
+#ifdef FINSH_USING_DESCRIPTION
+ rt_kprintf("%-16s -- %s\n", index->name + 4, index->desc);
+#else
+ rt_kprintf("%s\n", index->name + 4);
+#endif
+ }
+ }
+}
+FINSH_FUNCTION_EXPORT(list_tc, list all testcases);
+#endif
+
|
