diff --git a/examples/kernel/cpuusage.c b/examples/kernel/cpuusage.c index 93cac1bb994b908fa5d4966c0473b61d6fd792c7..490061ddb765e3e76f8193f18e5281aa294dc395 100644 --- a/examples/kernel/cpuusage.c +++ b/examples/kernel/cpuusage.c @@ -1,8 +1,8 @@ #include #include -#define CPU_USAGE_CALC_TICK 10 -#define CPU_USAGE_LOOP 100 +#define CPU_USAGE_CALC_TICK 10 +#define CPU_USAGE_LOOP 100 static rt_uint8_t cpu_usage_major = 0, cpu_usage_minor= 0; static rt_uint32_t total_count = 0; diff --git a/examples/kernel/heap_realloc.c b/examples/kernel/heap_realloc.c index 9acdf573647cf7af4c29757f29c948f961a157c4..bc7b56a999d85e513227f17e8dc50d4174fb9179 100644 --- a/examples/kernel/heap_realloc.c +++ b/examples/kernel/heap_realloc.c @@ -72,7 +72,7 @@ static void heap_realloc_init() res = TC_STAT_FAILED; 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: diff --git a/examples/kernel/semaphore_buffer_worker.c b/examples/kernel/semaphore_buffer_worker.c index 6e5920dd6fbcf90e4ab4f19c8ce037ef29de62ac..de4647576940bc93970c2c6d2551a4705d008459 100644 --- a/examples/kernel/semaphore_buffer_worker.c +++ b/examples/kernel/semaphore_buffer_worker.c @@ -12,9 +12,9 @@ /* 一个环形buffer的实现 */ struct rb { - rt_uint16_t read_index, write_index; - rt_uint8_t *buffer_ptr; - rt_uint16_t buffer_size; + rt_uint16_t read_index, write_index; + rt_uint8_t *buffer_ptr; + rt_uint16_t buffer_size; }; /* 指向信号量控制块的指针 */ @@ -23,236 +23,236 @@ static rt_sem_t sem = RT_NULL; static rt_thread_t tid = RT_NULL, worker = RT_NULL; /* 环形buffer的内存块(用数组体现出来) */ -#define BUFFER_SIZE 256 -#define BUFFER_ITEM 32 +#define BUFFER_SIZE 256 +#define BUFFER_ITEM 32 static rt_uint8_t working_buffer[BUFFER_SIZE]; struct rb working_rb; /* 初始化环形buffer,size指的是buffer的大小。注:这里并没对数据地址对齐做处理 */ static void rb_init(struct rb* rb, rt_uint8_t *pool, rt_uint16_t size) { - RT_ASSERT(rb != RT_NULL); + RT_ASSERT(rb != RT_NULL); - /* 对读写指针清零*/ - rb->read_index = rb->write_index = 0; + /* 对读写指针清零*/ + rb->read_index = rb->write_index = 0; - /* 设置环形buffer的内存数据块 */ - rb->buffer_ptr = pool; - rb->buffer_size = size; + /* 设置环形buffer的内存数据块 */ + rb->buffer_ptr = pool; + rb->buffer_size = size; } /* 向环形buffer中写入数据 */ static rt_bool_t rb_put(struct rb* rb, const rt_uint8_t *ptr, rt_uint16_t length) { - rt_size_t size; - - /* 判断是否有足够的剩余空间 */ - if (rb->read_index > rb->write_index) - size = rb->read_index - rb->write_index; - else - size = rb->buffer_size - rb->write_index + rb->read_index; - - /* 没有多余的空间 */ - if (size < length) return RT_FALSE; - - if (rb->read_index > rb->write_index) - { - /* read_index - write_index 即为总的空余空间 */ - memcpy(&rb->buffer_ptr[rb->write_index], ptr, length); - rb->write_index += length; - } - else - { - if (rb->buffer_size - rb->write_index > length) - { - /* write_index 后面剩余的空间有足够的长度 */ - memcpy(&rb->buffer_ptr[rb->write_index], ptr, length); - rb->write_index += length; - } - else - { - /* - * write_index 后面剩余的空间不存在足够的长度,需要把部分数据复制到 - * 前面的剩余空间中 - */ - memcpy(&rb->buffer_ptr[rb->write_index], ptr, - rb->buffer_size - rb->write_index); - memcpy(&rb->buffer_ptr[0], &ptr[rb->buffer_size - rb->write_index], - length - (rb->buffer_size - rb->write_index)); - rb->write_index = length - (rb->buffer_size - rb->write_index); - } - } - - return RT_TRUE; + rt_size_t size; + + /* 判断是否有足够的剩余空间 */ + if (rb->read_index > rb->write_index) + size = rb->read_index - rb->write_index; + else + size = rb->buffer_size - rb->write_index + rb->read_index; + + /* 没有多余的空间 */ + if (size < length) return RT_FALSE; + + if (rb->read_index > rb->write_index) + { + /* read_index - write_index 即为总的空余空间 */ + memcpy(&rb->buffer_ptr[rb->write_index], ptr, length); + rb->write_index += length; + } + else + { + if (rb->buffer_size - rb->write_index > length) + { + /* write_index 后面剩余的空间有足够的长度 */ + memcpy(&rb->buffer_ptr[rb->write_index], ptr, length); + rb->write_index += length; + } + else + { + /* + * write_index 后面剩余的空间不存在足够的长度,需要把部分数据复制到 + * 前面的剩余空间中 + */ + memcpy(&rb->buffer_ptr[rb->write_index], ptr, + rb->buffer_size - rb->write_index); + memcpy(&rb->buffer_ptr[0], &ptr[rb->buffer_size - rb->write_index], + length - (rb->buffer_size - rb->write_index)); + rb->write_index = length - (rb->buffer_size - rb->write_index); + } + } + + return RT_TRUE; } /* 从环形buffer中读出数据 */ static rt_bool_t rb_get(struct rb* rb, rt_uint8_t *ptr, rt_uint16_t length) { - rt_size_t size; - - /* 判断是否有足够的数据 */ - if (rb->read_index > rb->write_index) - size = rb->buffer_size - rb->read_index + rb->write_index; - else - size = rb->write_index - rb->read_index; - - /* 没有足够的数据 */ - if (size < length) return RT_FALSE; - - if (rb->read_index > rb->write_index) - { - if (rb->buffer_size - rb->read_index > length) - { - /* read_index的数据足够多,直接复制 */ - memcpy(ptr, &rb->buffer_ptr[rb->read_index], length); - rb->read_index += length; - } - else - { - /* read_index的数据不够,需要分段复制 */ - memcpy(ptr, &rb->buffer_ptr[rb->read_index], - rb->buffer_size - rb->read_index); - memcpy(&ptr[rb->buffer_size - rb->read_index], &rb->buffer_ptr[0], - length - rb->buffer_size + rb->read_index); - rb->read_index = length - rb->buffer_size + rb->read_index; - } - } - else - { - /* - * read_index要比write_index小,总的数据量够(前面已经有总数据量的判 - * 断),直接复制出数据。 - */ - memcpy(ptr, &rb->buffer_ptr[rb->read_index], length); - rb->read_index += length; - } - - return RT_TRUE; + rt_size_t size; + + /* 判断是否有足够的数据 */ + if (rb->read_index > rb->write_index) + size = rb->buffer_size - rb->read_index + rb->write_index; + else + size = rb->write_index - rb->read_index; + + /* 没有足够的数据 */ + if (size < length) return RT_FALSE; + + if (rb->read_index > rb->write_index) + { + if (rb->buffer_size - rb->read_index > length) + { + /* read_index的数据足够多,直接复制 */ + memcpy(ptr, &rb->buffer_ptr[rb->read_index], length); + rb->read_index += length; + } + else + { + /* read_index的数据不够,需要分段复制 */ + memcpy(ptr, &rb->buffer_ptr[rb->read_index], + rb->buffer_size - rb->read_index); + memcpy(&ptr[rb->buffer_size - rb->read_index], &rb->buffer_ptr[0], + length - rb->buffer_size + rb->read_index); + rb->read_index = length - rb->buffer_size + rb->read_index; + } + } + else + { + /* + * read_index要比write_index小,总的数据量够(前面已经有总数据量的判 + * 断),直接复制出数据。 + */ + memcpy(ptr, &rb->buffer_ptr[rb->read_index], length); + rb->read_index += length; + } + + return RT_TRUE; } /* 生产者线程入口 */ static void thread_entry(void* parameter) { - rt_bool_t result; - rt_uint8_t data_buffer[BUFFER_ITEM + 1]; - - while (1) - { - /* 持有信号量 */ - rt_sem_take(sem, RT_WAITING_FOREVER); - /* 从环buffer中获得数据 */ - result = rb_get(&working_rb, &data_buffer[0], BUFFER_ITEM); - /* 释放信号量 */ - rt_sem_release(sem); - data_buffer[BUFFER_ITEM] = '\0'; - - if (result == RT_TRUE) - { - /* 获取数据成功,打印数据 */ - rt_kprintf("%s\n", data_buffer); - } - - /* 做一个5 OS Tick的休眠 */ - rt_thread_delay(5); - } + rt_bool_t result; + rt_uint8_t data_buffer[BUFFER_ITEM + 1]; + + while (1) + { + /* 持有信号量 */ + rt_sem_take(sem, RT_WAITING_FOREVER); + /* 从环buffer中获得数据 */ + result = rb_get(&working_rb, &data_buffer[0], BUFFER_ITEM); + /* 释放信号量 */ + rt_sem_release(sem); + data_buffer[BUFFER_ITEM] = '\0'; + + if (result == RT_TRUE) + { + /* 获取数据成功,打印数据 */ + rt_kprintf("%s\n", data_buffer); + } + + /* 做一个5 OS Tick的休眠 */ + rt_thread_delay(5); + } } /* worker线程入口 */ static void worker_entry(void* parameter) { - rt_bool_t result; - rt_uint32_t index, setchar; - rt_uint8_t data_buffer[BUFFER_ITEM]; - - setchar = 0x21; - while (1) - { - /* 构造数据 */ - for(index = 0; index < BUFFER_ITEM; index++) - { - data_buffer[index] = setchar; - if (++setchar == 0x7f) - setchar = 0x21; - } - - /* 持有信号量 */ - rt_sem_take(sem, RT_WAITING_FOREVER); - /* 把数据放到环形buffer中 */ - result = rb_put(&working_rb, &data_buffer[0], BUFFER_ITEM); - /* 释放信号量 */ - rt_sem_release(sem); - - /* 放入成功,做一个10 OS Tick的休眠 */ - rt_thread_delay(10); - } + rt_bool_t result; + rt_uint32_t index, setchar; + rt_uint8_t data_buffer[BUFFER_ITEM]; + + setchar = 0x21; + while (1) + { + /* 构造数据 */ + for(index = 0; index < BUFFER_ITEM; index++) + { + data_buffer[index] = setchar; + if (++setchar == 0x7f) + setchar = 0x21; + } + + /* 持有信号量 */ + rt_sem_take(sem, RT_WAITING_FOREVER); + /* 把数据放到环形buffer中 */ + result = rb_put(&working_rb, &data_buffer[0], BUFFER_ITEM); + /* 释放信号量 */ + rt_sem_release(sem); + + /* 放入成功,做一个10 OS Tick的休眠 */ + rt_thread_delay(10); + } } int semaphore_buffer_worker_init() { - /* 初始化ring buffer */ - rb_init(&working_rb, working_buffer, BUFFER_SIZE); - - /* 创建信号量 */ - sem = rt_sem_create("sem", 1, RT_IPC_FLAG_FIFO); - if (sem == RT_NULL) - { - tc_stat(TC_STAT_END | TC_STAT_FAILED); - return 0; - } - - /* 创建线程1 */ - tid = rt_thread_create("thread", - thread_entry, RT_NULL, /* 线程入口是thread_entry, 入口参数是RT_NULL */ - THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); - if (tid != RT_NULL) - rt_thread_startup(tid); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); - - /* 创建线程2 */ - worker = rt_thread_create("worker", - worker_entry, RT_NULL, /* 线程入口是worker_entry, 入口参数是RT_NULL */ - THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); - if (worker != RT_NULL) - rt_thread_startup(worker); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); - - return 0; + /* 初始化ring buffer */ + rb_init(&working_rb, working_buffer, BUFFER_SIZE); + + /* 创建信号量 */ + sem = rt_sem_create("sem", 1, RT_IPC_FLAG_FIFO); + if (sem == RT_NULL) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + return 0; + } + + /* 创建线程1 */ + tid = rt_thread_create("thread", + thread_entry, RT_NULL, /* 线程入口是thread_entry, 入口参数是RT_NULL */ + THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); + if (tid != RT_NULL) + rt_thread_startup(tid); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + /* 创建线程2 */ + worker = rt_thread_create("worker", + worker_entry, RT_NULL, /* 线程入口是worker_entry, 入口参数是RT_NULL */ + THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); + if (worker != RT_NULL) + rt_thread_startup(worker); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + return 0; } #ifdef RT_USING_TC static void _tc_cleanup() { - /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ - rt_enter_critical(); + /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ + rt_enter_critical(); - /* 删除信号量 */ - if (sem != RT_NULL) - rt_sem_delete(sem); + /* 删除信号量 */ + if (sem != RT_NULL) + rt_sem_delete(sem); - /* 删除线程 */ - if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE) - rt_thread_delete(tid); - if (worker != RT_NULL && worker->stat != RT_THREAD_CLOSE) - rt_thread_delete(worker); + /* 删除线程 */ + if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE) + rt_thread_delete(tid); + if (worker != RT_NULL && worker->stat != RT_THREAD_CLOSE) + rt_thread_delete(worker); - /* 调度器解锁 */ - rt_exit_critical(); + /* 调度器解锁 */ + rt_exit_critical(); - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_semaphore_buffer_worker() { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); - semaphore_buffer_worker_init(); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); + semaphore_buffer_worker_init(); - /* 返回TestCase运行的最长时间 */ - return 100; + /* 返回TestCase运行的最长时间 */ + return 100; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_semaphore_buffer_worker, a buffer worker with semaphore example); @@ -260,8 +260,8 @@ FINSH_FUNCTION_EXPORT(_tc_semaphore_buffer_worker, a buffer worker with semaphor /* 用户应用入口 */ int rt_application_init() { - semaphore_buffer_worker_init(); + semaphore_buffer_worker_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/semaphore_dynamic.c b/examples/kernel/semaphore_dynamic.c index 579d9daa9ab1ba9053a8392db5ffe04d6d3f8f32..dc088e6e44233a13a4809b2ce21d00055a103181 100644 --- a/examples/kernel/semaphore_dynamic.c +++ b/examples/kernel/semaphore_dynamic.c @@ -15,108 +15,112 @@ static rt_sem_t sem = RT_NULL; /* 线程入口 */ static void thread_entry(void* parameter) { - 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_delete(sem); - return; - } - rt_kprintf("take semaphore timeout\n"); - } - else - { - /* 因为并没释放信号量,应该是超时返回,否则测试失败 */ - tc_done(TC_STAT_FAILED); - rt_sem_delete(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_delete(sem); - return; - } - - /* 测试成功 */ - tc_done(TC_STAT_PASSED); - /* 删除信号量 */ - rt_sem_delete(sem); + 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) + { + rt_tick_t new_tick = rt_tick_get(); + /* 可以有两个 tick 的误差 */ + if (new_tick - tick >= 12) + { + rt_kprintf("tick error to large: expect: 10, get %d\n", + new_tick - tick); + + /* 如果失败,则测试失败 */ + tc_done(TC_STAT_FAILED); + rt_sem_delete(sem); + return; + } + rt_kprintf("take semaphore timeout\n"); + } + else + { + /* 因为并没释放信号量,应该是超时返回,否则测试失败 */ + tc_done(TC_STAT_FAILED); + rt_sem_delete(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_delete(sem); + return; + } + + /* 测试成功 */ + tc_done(TC_STAT_PASSED); + /* 删除信号量 */ + rt_sem_delete(sem); } int semaphore_dynamic_init() { - /* 创建一个信号量,初始值是0 */ - sem = rt_sem_create("sem", 0, RT_IPC_FLAG_FIFO); - if (sem == RT_NULL) - { - tc_stat(TC_STAT_END | TC_STAT_FAILED); - return 0; - } - - /* 创建线程 */ - tid = rt_thread_create("thread", - thread_entry, RT_NULL, /* 线程入口是thread_entry, 入口参数是RT_NULL */ - 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; + /* 创建一个信号量,初始值是0 */ + sem = rt_sem_create("sem", 0, RT_IPC_FLAG_FIFO); + if (sem == RT_NULL) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + return 0; + } + + /* 创建线程 */ + tid = rt_thread_create("thread", + thread_entry, RT_NULL, /* 线程入口是thread_entry, 入口参数是RT_NULL */ + 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 static void _tc_cleanup() { - /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ - rt_enter_critical(); - - if (sem) - { - rt_sem_delete(sem); - sem = RT_NULL; - } - - /* 删除线程 */ - if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE) - { - rt_thread_delete(tid); - } - - /* 调度器解锁 */ - rt_exit_critical(); - - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ + rt_enter_critical(); + + if (sem) + { + rt_sem_delete(sem); + sem = RT_NULL; + } + + /* 删除线程 */ + if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE) + { + rt_thread_delete(tid); + } + + /* 调度器解锁 */ + rt_exit_critical(); + + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_semaphore_dynamic() { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); - semaphore_dynamic_init(); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); + semaphore_dynamic_init(); - /* 返回TestCase运行的最长时间 */ - return 100; + /* 返回TestCase运行的最长时间 */ + return 100; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_semaphore_dynamic, a dynamic semaphore example); @@ -124,8 +128,8 @@ FINSH_FUNCTION_EXPORT(_tc_semaphore_dynamic, a dynamic semaphore example); /* 用户应用入口 */ int rt_application_init() { - semaphore_dynamic_init(); + semaphore_dynamic_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/semaphore_priority.c b/examples/kernel/semaphore_priority.c index 2bbeb8038967b40408c6f9a5460fdca3275dae73..d6e3ce39140018225cfba0e05e8d01fc0e3bebbe 100644 --- a/examples/kernel/semaphore_priority.c +++ b/examples/kernel/semaphore_priority.c @@ -6,129 +6,129 @@ static rt_uint8_t t1_count, t2_count; static rt_thread_t t1, t2, worker; static void thread1_entry(void* parameter) { - rt_err_t result; - - while (1) - { - result = rt_sem_take(sem, RT_WAITING_FOREVER); - if (result != RT_EOK) - { - tc_done(TC_STAT_FAILED); - return; - } - - t1_count ++; - rt_kprintf("thread1: got semaphore, count: %d\n", t1_count); - } + rt_err_t result; + + while (1) + { + result = rt_sem_take(sem, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + tc_done(TC_STAT_FAILED); + return; + } + + t1_count ++; + rt_kprintf("thread1: got semaphore, count: %d\n", t1_count); + } } static void thread2_entry(void* parameter) { - rt_err_t result; - - while (1) - { - result = rt_sem_take(sem, RT_WAITING_FOREVER); - if (result != RT_EOK) - { - tc_done(TC_STAT_FAILED); - return; - } - - t2_count ++; - rt_kprintf("thread2: got semaphore, count: %d\n", t2_count); - } + rt_err_t result; + + while (1) + { + result = rt_sem_take(sem, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + tc_done(TC_STAT_FAILED); + return; + } + + t2_count ++; + rt_kprintf("thread2: got semaphore, count: %d\n", t2_count); + } } static void worker_thread_entry(void* parameter) { - rt_thread_delay(10); + rt_thread_delay(10); - while (1) - { - rt_sem_release(sem); - rt_thread_delay(5); - } + while (1) + { + rt_sem_release(sem); + rt_thread_delay(5); + } } int semaphore_priority_init() { - sem = rt_sem_create("sem", 0, RT_IPC_FLAG_PRIO); - if (sem == RT_NULL) - { - tc_stat(TC_STAT_END | TC_STAT_FAILED); - return 0; - } - - t1_count = t2_count = 0; - - t1 = rt_thread_create("t1", - thread1_entry, RT_NULL, - THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE); - if (t1 != RT_NULL) - rt_thread_startup(t1); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); - - t2 = rt_thread_create("t2", - thread2_entry, RT_NULL, - THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE); - if (t2 != RT_NULL) - rt_thread_startup(t2); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); - - worker = rt_thread_create("worker", - worker_thread_entry, RT_NULL, - THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); - if (worker != RT_NULL) - rt_thread_startup(worker); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); - - return 0; + sem = rt_sem_create("sem", 0, RT_IPC_FLAG_PRIO); + if (sem == RT_NULL) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + return 0; + } + + t1_count = t2_count = 0; + + t1 = rt_thread_create("t1", + thread1_entry, RT_NULL, + THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE); + if (t1 != RT_NULL) + rt_thread_startup(t1); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + t2 = rt_thread_create("t2", + thread2_entry, RT_NULL, + THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE); + if (t2 != RT_NULL) + rt_thread_startup(t2); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + worker = rt_thread_create("worker", + worker_thread_entry, RT_NULL, + THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); + if (worker != RT_NULL) + rt_thread_startup(worker); + 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 t1, t2 and worker thread */ - rt_thread_delete(t1); - rt_thread_delete(t2); - rt_thread_delete(worker); - - if (sem) - { - rt_sem_delete(sem); - sem = RT_NULL; - } - - if (t1_count > t2_count) - tc_done(TC_STAT_FAILED); - else - tc_done(TC_STAT_PASSED); - - /* unlock scheduler */ - rt_exit_critical(); + /* lock scheduler */ + rt_enter_critical(); + + /* delete t1, t2 and worker thread */ + rt_thread_delete(t1); + rt_thread_delete(t2); + rt_thread_delete(worker); + + if (sem) + { + rt_sem_delete(sem); + sem = RT_NULL; + } + + if (t1_count > t2_count) + tc_done(TC_STAT_FAILED); + else + tc_done(TC_STAT_PASSED); + + /* unlock scheduler */ + rt_exit_critical(); } int _tc_semaphore_priority() { - /* set tc cleanup */ - tc_cleanup(_tc_cleanup); - semaphore_priority_init(); + /* set tc cleanup */ + tc_cleanup(_tc_cleanup); + semaphore_priority_init(); - return 50; + return 50; } FINSH_FUNCTION_EXPORT(_tc_semaphore_priority, a priority semaphore test); #else int rt_application_init() { - semaphore_priority_init(); + semaphore_priority_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/semaphore_producer_consumer.c b/examples/kernel/semaphore_producer_consumer.c index e7c052222334c8a7f99186ec3ed910e1955f9a9b..48da71430656c37701da612597c09c952c00f309 100644 --- a/examples/kernel/semaphore_producer_consumer.c +++ b/examples/kernel/semaphore_producer_consumer.c @@ -24,126 +24,126 @@ struct rt_semaphore sem_empty, sem_full; /* 生成者线程入口 */ void producer_thread_entry(void* parameter) { - int cnt = 0; - - /* 运行100次 */ - while( cnt < 100) - { - /* 获取一个空位 */ - rt_sem_take(&sem_empty, RT_WAITING_FOREVER); - - /* 修改array内容,上锁 */ - rt_sem_take(&sem_lock, RT_WAITING_FOREVER); - array[set%MAXSEM] = cnt + 1; - rt_kprintf("the producer generates a number: %d\n", array[set%MAXSEM]); - set++; - rt_sem_release(&sem_lock); - - /* 发布一个满位 */ - rt_sem_release(&sem_full); - cnt++; - - /* 暂停一段时间 */ - rt_thread_delay(50); - } - - rt_kprintf("the producer exit!\n"); + int cnt = 0; + + /* 运行100次 */ + while( cnt < 100) + { + /* 获取一个空位 */ + rt_sem_take(&sem_empty, RT_WAITING_FOREVER); + + /* 修改array内容,上锁 */ + rt_sem_take(&sem_lock, RT_WAITING_FOREVER); + array[set%MAXSEM] = cnt + 1; + rt_kprintf("the producer generates a number: %d\n", array[set%MAXSEM]); + set++; + rt_sem_release(&sem_lock); + + /* 发布一个满位 */ + rt_sem_release(&sem_full); + cnt++; + + /* 暂停一段时间 */ + rt_thread_delay(50); + } + + rt_kprintf("the producer exit!\n"); } /* 消费者线程入口 */ void consumer_thread_entry(void* parameter) { - rt_uint32_t no; - rt_uint32_t sum; + rt_uint32_t no; + rt_uint32_t sum; - /* 第n个线程,由入口参数传进来 */ - no = (rt_uint32_t)parameter; + /* 第n个线程,由入口参数传进来 */ + no = (rt_uint32_t)parameter; - while(1) - { - /* 获取一个满位 */ - rt_sem_take(&sem_full, RT_WAITING_FOREVER); + while(1) + { + /* 获取一个满位 */ + rt_sem_take(&sem_full, RT_WAITING_FOREVER); - /* 临界区,上锁进行操作 */ - rt_sem_take(&sem_lock, RT_WAITING_FOREVER); - sum += array[get%MAXSEM]; - rt_kprintf("the consumer[%d] get a number: %d\n", no, array[get%MAXSEM] ); - get++; - rt_sem_release(&sem_lock); + /* 临界区,上锁进行操作 */ + rt_sem_take(&sem_lock, RT_WAITING_FOREVER); + sum += array[get%MAXSEM]; + rt_kprintf("the consumer[%d] get a number: %d\n", no, array[get%MAXSEM] ); + get++; + rt_sem_release(&sem_lock); - /* 释放一个空位 */ - rt_sem_release(&sem_empty); + /* 释放一个空位 */ + rt_sem_release(&sem_empty); - /* 生产者生产到100个数目,停止,消费者线程相应停止 */ - if (get == 100) break; + /* 生产者生产到100个数目,停止,消费者线程相应停止 */ + if (get == 100) break; - /* 暂停一小会时间 */ - rt_thread_delay(10); - } + /* 暂停一小会时间 */ + rt_thread_delay(10); + } - rt_kprintf("the consumer[%d] sum is %d \n ", no, sum); - rt_kprintf("the consumer[%d] exit!\n"); + rt_kprintf("the consumer[%d] sum is %d \n ", no, sum); + rt_kprintf("the consumer[%d] exit!\n"); } int semaphore_producer_consumer_init() { - /* 初始化3个信号量 */ - rt_sem_init(&sem_lock , "lock", 1, RT_IPC_FLAG_FIFO); - rt_sem_init(&sem_empty, "empty", MAXSEM, RT_IPC_FLAG_FIFO); - rt_sem_init(&sem_full , "full", 0, RT_IPC_FLAG_FIFO); - - /* 创建线程1 */ - producer_tid = rt_thread_create("producer", - producer_thread_entry, RT_NULL, /* 线程入口是producer_thread_entry, 入口参数是RT_NULL */ - THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE); - if (producer_tid != RT_NULL) - rt_thread_startup(producer_tid); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); - - /* 创建线程2 */ - consumer_tid = rt_thread_create("consumer", - consumer_thread_entry, RT_NULL, /* 线程入口是consumer_thread_entry, 入口参数是RT_NULL */ - THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE); - if (consumer_tid != RT_NULL) - rt_thread_startup(consumer_tid); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); - - return 0; + /* 初始化3个信号量 */ + rt_sem_init(&sem_lock , "lock", 1, RT_IPC_FLAG_FIFO); + rt_sem_init(&sem_empty, "empty", MAXSEM, RT_IPC_FLAG_FIFO); + rt_sem_init(&sem_full , "full", 0, RT_IPC_FLAG_FIFO); + + /* 创建线程1 */ + producer_tid = rt_thread_create("producer", + producer_thread_entry, RT_NULL, /* 线程入口是producer_thread_entry, 入口参数是RT_NULL */ + THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE); + if (producer_tid != RT_NULL) + rt_thread_startup(producer_tid); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + /* 创建线程2 */ + consumer_tid = rt_thread_create("consumer", + consumer_thread_entry, RT_NULL, /* 线程入口是consumer_thread_entry, 入口参数是RT_NULL */ + THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE); + if (consumer_tid != RT_NULL) + rt_thread_startup(consumer_tid); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + return 0; } #ifdef RT_USING_TC static void _tc_cleanup() { - /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ - rt_enter_critical(); + /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ + rt_enter_critical(); - rt_sem_detach(&sem_lock); - rt_sem_detach(&sem_empty); - rt_sem_detach(&sem_full); + rt_sem_detach(&sem_lock); + rt_sem_detach(&sem_empty); + rt_sem_detach(&sem_full); - /* 删除线程 */ - if (producer_tid != RT_NULL && producer_tid->stat != RT_THREAD_CLOSE) - rt_thread_delete(producer_tid); - if (consumer_tid != RT_NULL && consumer_tid->stat != RT_THREAD_CLOSE) - rt_thread_delete(consumer_tid); + /* 删除线程 */ + if (producer_tid != RT_NULL && producer_tid->stat != RT_THREAD_CLOSE) + rt_thread_delete(producer_tid); + if (consumer_tid != RT_NULL && consumer_tid->stat != RT_THREAD_CLOSE) + rt_thread_delete(consumer_tid); - /* 调度器解锁 */ - rt_exit_critical(); + /* 调度器解锁 */ + rt_exit_critical(); - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_semaphore_producer_consumer() { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); - semaphore_producer_consumer_init(); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); + semaphore_producer_consumer_init(); - /* 返回TestCase运行的最长时间 */ - return 100; + /* 返回TestCase运行的最长时间 */ + return 100; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_semaphore_producer_consumer, producer and consumer example); @@ -151,8 +151,8 @@ FINSH_FUNCTION_EXPORT(_tc_semaphore_producer_consumer, producer and consumer exa /* 用户应用入口 */ int rt_application_init() { - semaphore_producer_consumer_init(); + semaphore_producer_consumer_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/semaphore_static.c b/examples/kernel/semaphore_static.c index b64d5cef47c8c47fbf57439304fbc75ee00a08ad..cb199886a4998feabfb781941745d9bc9251a305 100644 --- a/examples/kernel/semaphore_static.c +++ b/examples/kernel/semaphore_static.c @@ -17,107 +17,111 @@ static struct rt_semaphore sem; /* 线程入口 */ static void thread_entry(void* parameter) { - 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); + 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) + { + rt_tick_t new_tick = rt_tick_get(); + /* 可以有两个 tick 的误差 */ + if (new_tick - tick >= 12) + { + rt_kprintf("tick error to large: expect: 10, get %d\n", + new_tick - tick); + + 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); } int semaphore_static_init(void) { - 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; + 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; } #ifdef RT_USING_TC static void _tc_cleanup(void) { - /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ - rt_enter_critical(); + /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ + rt_enter_critical(); - /* 执行线程脱离 */ - if (thread.stat != RT_THREAD_CLOSE) - { - rt_thread_detach(&thread); + /* 执行线程脱离 */ + if (thread.stat != RT_THREAD_CLOSE) + { + rt_thread_detach(&thread); - /* 执行信号量对象脱离 */ - rt_sem_detach(&sem); - } + /* 执行信号量对象脱离 */ + rt_sem_detach(&sem); + } - /* 调度器解锁 */ - rt_exit_critical(); + /* 调度器解锁 */ + rt_exit_critical(); - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_semaphore_static(void) { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); - semaphore_static_init(); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); + semaphore_static_init(); - /* 返回TestCase运行的最长时间 */ - return 100; + /* 返回TestCase运行的最长时间 */ + return 100; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_semaphore_static, a static semaphore example); @@ -125,8 +129,8 @@ FINSH_FUNCTION_EXPORT(_tc_semaphore_static, a static semaphore example); /* 用户应用入口 */ int rt_application_init(void) { - semaphore_static_init(); + semaphore_static_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/tc_comm.c b/examples/kernel/tc_comm.c index bdc6471b3f471f891270932029fe0ff702907ccf..c944755279418847d52d530c8a3d3985f87a59ee 100644 --- a/examples/kernel/tc_comm.c +++ b/examples/kernel/tc_comm.c @@ -4,8 +4,8 @@ #endif #ifdef RT_USING_TC -#define TC_PRIORITY 25 -#define TC_STACK_SIZE 0x400 +#define TC_PRIORITY 25 +#define TC_STACK_SIZE 0x400 static rt_uint8_t _tc_stat; static struct rt_semaphore _tc_sem; @@ -20,24 +20,24 @@ 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 */ - 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; FINSH_NEXT_SYSCALL(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; + unsigned int fail_count = 0; + 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; FINSH_NEXT_SYSCALL(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) { @@ -60,130 +60,130 @@ 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); - 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); + } + + 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); } 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); + _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; + _tc_stat |= stat; + _tc_stat &= ~TC_STAT_RUNNING; - /* release semaphore */ - rt_sem_release(&_tc_sem); + /* 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; + if (stat & TC_STAT_FAILED) + { + rt_kprintf("TestCases[%s] failed\n", _tc_current); + } + _tc_stat |= stat; } void tc_cleanup(void (*cleanup)()) { - _tc_cleanup = 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); + 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; FINSH_NEXT_SYSCALL(index)) - { - /* search testcase */ - if (rt_strstr(index->name, "_tc_") == index->name) - { + struct finsh_syscall* index; + + rt_kprintf("TestCases List:\n"); + for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(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); + rt_kprintf("%-16s -- %s\n", index->name + 4, index->desc); #else - rt_kprintf("%s\n", index->name + 4); + rt_kprintf("%s\n", index->name + 4); #endif - } - } + } + } } FINSH_FUNCTION_EXPORT(list_tc, list all testcases); #endif diff --git a/examples/kernel/tc_comm.h b/examples/kernel/tc_comm.h index 714609768463c73d9f1a162da8a6131963cbaa60..aee1fec2d26c7ebcea1e0ab3bb212fda11d8dbcb 100644 --- a/examples/kernel/tc_comm.h +++ b/examples/kernel/tc_comm.h @@ -11,19 +11,19 @@ #endif #if RT_THREAD_PRIORITY_MAX == 8 -#define THREAD_PRIORITY 6 +#define THREAD_PRIORITY 6 #elif RT_THREAD_PRIORITY_MAX == 32 -#define THREAD_PRIORITY 25 +#define THREAD_PRIORITY 25 #elif RT_THREAD_PRIORITY_MAX == 256 -#define THREAD_PRIORITY 200 +#define THREAD_PRIORITY 200 #endif -#define THREAD_STACK_SIZE 512 -#define THREAD_TIMESLICE 5 +#define THREAD_STACK_SIZE 512 +#define THREAD_TIMESLICE 5 -#define TC_STAT_END 0x00 -#define TC_STAT_RUNNING 0x01 -#define TC_STAT_FAILED 0x10 -#define TC_STAT_PASSED 0x00 +#define TC_STAT_END 0x00 +#define TC_STAT_RUNNING 0x01 +#define TC_STAT_FAILED 0x10 +#define TC_STAT_PASSED 0x00 #ifdef RT_USING_TC void tc_start(const char* tc_prefix); diff --git a/examples/kernel/tc_sample.c b/examples/kernel/tc_sample.c index ea77ccda39398da34f822f27d1695b308f59f082..212875b716527bc30c641be712be323f03d818c9 100644 --- a/examples/kernel/tc_sample.c +++ b/examples/kernel/tc_sample.c @@ -4,59 +4,59 @@ static rt_thread_t tid = RT_NULL; static void sample_thread(void* parameter) { - rt_kprintf("I'm sample!\n"); + rt_kprintf("I'm sample!\n"); } static void sample_thread_cleanup(struct rt_thread *p) { - tid = RT_NULL; - tc_done(TC_STAT_PASSED); + 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); + 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; + 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(); + /* 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(); + /* set tc cleanup */ + tc_cleanup(_tc_cleanup); + sample_init(); - return 25; + return 25; } FINSH_FUNCTION_EXPORT(_tc_sample, a thread testcase example); #else int rt_application_init() { - sample_init(); + sample_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/thread_delay.c b/examples/kernel/thread_delay.c index 23d25d6d5c46c85c4b72480b7d11c20ba67aae09..f9404d704c18b6bb66b60eb7509bf1fa91f55844 100644 --- a/examples/kernel/thread_delay.c +++ b/examples/kernel/thread_delay.c @@ -8,63 +8,63 @@ static struct rt_thread thread; static char thread_stack[THREAD_STACK_SIZE]; static void thread_entry(void* parameter) { - rt_tick_t tick; - rt_kprintf("thread inited ok\n"); + rt_tick_t tick; + rt_kprintf("thread inited ok\n"); - rt_kprintf("thread delay 10 tick\n"); - tick = rt_tick_get(); - rt_thread_delay(10); - if (rt_tick_get() - tick > 10) - { - tc_done(TC_STAT_FAILED); - return; - } + rt_kprintf("thread delay 10 tick\n"); + tick = rt_tick_get(); + rt_thread_delay(10); + if (rt_tick_get() - tick > 11) + { + tc_done(TC_STAT_FAILED); + return; + } - rt_kprintf("thread delay 15 tick\n"); - tick = rt_tick_get(); - rt_thread_delay(15); - if (rt_tick_get() - tick > 15) - { - tc_done(TC_STAT_FAILED); - return; - } + rt_kprintf("thread delay 15 tick\n"); + tick = rt_tick_get(); + rt_thread_delay(15); + if (rt_tick_get() - tick > 16) + { + tc_done(TC_STAT_FAILED); + return; + } - rt_kprintf("thread exit\n"); + rt_kprintf("thread exit\n"); - tc_done(TC_STAT_PASSED); + tc_done(TC_STAT_PASSED); } rt_err_t thread_delay_init() { - rt_err_t result; + rt_err_t result; - result = rt_thread_init(&thread, - "test", - thread_entry, RT_NULL, - &thread_stack[0], sizeof(thread_stack), - THREAD_PRIORITY, 10); + result = rt_thread_init(&thread, + "test", + thread_entry, RT_NULL, + &thread_stack[0], sizeof(thread_stack), + THREAD_PRIORITY, 10); - if (result == RT_EOK) - rt_thread_startup(&thread); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); + if (result == RT_EOK) + rt_thread_startup(&thread); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); - return result; + return result; } #ifdef RT_USING_TC int _tc_thread_delay() { - thread_delay_init(); + thread_delay_init(); - return 30; + return 30; } FINSH_FUNCTION_EXPORT(_tc_thread_delay, a thread delay test); #else int rt_application_init() { - thread_delay_init(); + thread_delay_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/thread_delete.c b/examples/kernel/thread_delete.c index 69d5ab8ebc6db1dc645b7f170f0427c5c7735d48..72eb4ec00886b9bf4fdbd1de8a2db8bec533b6d3 100644 --- a/examples/kernel/thread_delete.c +++ b/examples/kernel/thread_delete.c @@ -15,128 +15,128 @@ static rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL; /* 线程1的入口函数 */ static void thread1_entry(void* parameter) { - rt_uint32_t count = 0; - - while (1) - { - /* 线程1采用低优先级运行,一直打印计数值 */ - // rt_kprintf("thread count: %d\n", count ++); - count ++; - } + rt_uint32_t count = 0; + + while (1) + { + /* 线程1采用低优先级运行,一直打印计数值 */ + // 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; + 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) { - /* 线程2拥有较高的优先级,以抢占线程1而获得执行 */ - - /* 线程2启动后先睡眠10个OS Tick */ - rt_thread_delay(RT_TICK_PER_SECOND); - - /* - * 线程2唤醒后直接删除线程1,删除线程1后,线程1自动脱离就绪线程 - * 队列 - */ - rt_thread_delete(tid1); - - /* - * 线程2继续休眠10个OS Tick然后退出,线程2休眠后应切换到idle线程 - * idle线程将执行真正的线程1控制块和线程栈的删除 - */ - rt_thread_delay(RT_TICK_PER_SECOND); + /* 线程2拥有较高的优先级,以抢占线程1而获得执行 */ + + /* 线程2启动后先睡眠10个OS Tick */ + rt_thread_delay(RT_TICK_PER_SECOND); + + /* + * 线程2唤醒后直接删除线程1,删除线程1后,线程1自动脱离就绪线程 + * 队列 + */ + rt_thread_delete(tid1); + + /* + * 线程2继续休眠10个OS Tick然后退出,线程2休眠后应切换到idle线程 + * idle线程将执行真正的线程1控制块和线程栈的删除 + */ + rt_thread_delay(RT_TICK_PER_SECOND); } static void thread2_cleanup(struct rt_thread *tid) { - /* - * 线程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); + /* + * 线程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); } /* 线程删除示例的初始化 */ int thread_delete_init() { - /* 创建线程1 */ - tid1 = rt_thread_create("t1", /* 线程1的名称是t1 */ - 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); - - /* 创建线程1 */ - tid2 = rt_thread_create("t2", /* 线程1的名称是t2 */ - 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); - - return 10 * RT_TICK_PER_SECOND; + /* 创建线程1 */ + tid1 = rt_thread_create("t1", /* 线程1的名称是t1 */ + 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); + + /* 创建线程1 */ + tid2 = rt_thread_create("t2", /* 线程1的名称是t2 */ + 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); + + return 10 * RT_TICK_PER_SECOND; } #ifdef RT_USING_TC static void _tc_cleanup() { - /* lock scheduler */ - rt_enter_critical(); - - /* delete thread */ - if (tid1 != RT_NULL) - { - rt_kprintf("tid1 is %p, should be NULL\n", tid1); - tc_stat(TC_STAT_FAILED); - } - if (tid2 != RT_NULL) - { - rt_kprintf("tid2 is %p, should be NULL\n", tid2); - tc_stat(TC_STAT_FAILED); - } - - /* unlock scheduler */ - rt_exit_critical(); + /* lock scheduler */ + rt_enter_critical(); + + /* delete thread */ + if (tid1 != RT_NULL) + { + rt_kprintf("tid1 is %p, should be NULL\n", tid1); + tc_stat(TC_STAT_FAILED); + } + if (tid2 != RT_NULL) + { + rt_kprintf("tid2 is %p, should be NULL\n", tid2); + tc_stat(TC_STAT_FAILED); + } + + /* unlock scheduler */ + rt_exit_critical(); } int _tc_thread_delete() { - /* set tc cleanup */ - tc_cleanup(_tc_cleanup); - return thread_delete_init(); + /* set tc cleanup */ + tc_cleanup(_tc_cleanup); + return thread_delete_init(); } FINSH_FUNCTION_EXPORT(_tc_thread_delete, a thread delete example); #else int rt_application_init() { - thread_delete_init(); + thread_delete_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/thread_detach.c b/examples/kernel/thread_detach.c index af1824ae30d45f6482166779f5975e3362a5bc3a..9370f212e13fea76858f2b320a586f31d2d947fb 100644 --- a/examples/kernel/thread_detach.c +++ b/examples/kernel/thread_detach.c @@ -18,92 +18,92 @@ static rt_uint8_t thread2_stack[THREAD_STACK_SIZE]; /* 线程1入口 */ static void thread1_entry(void* parameter) { - rt_uint32_t count = 0; + rt_uint32_t count = 0; - while (1) - { - /* 线程1采用低优先级运行,一直打印计数值 */ - rt_kprintf("thread count: %d\n", count ++); - } + while (1) + { + /* 线程1采用低优先级运行,一直打印计数值 */ + rt_kprintf("thread count: %d\n", count ++); + } } /* 线程2入口 */ static void thread2_entry(void* parameter) { - /* 线程2拥有较高的优先级,以抢占线程1而获得执行 */ + /* 线程2拥有较高的优先级,以抢占线程1而获得执行 */ - /* 线程2启动后先睡眠10个OS Tick */ - rt_thread_delay(10); + /* 线程2启动后先睡眠10个OS Tick */ + rt_thread_delay(10); - /* - * 线程2唤醒后直接执行线程1脱离,线程1将从就绪线程队列中删除 - */ - rt_thread_detach(&thread1); + /* + * 线程2唤醒后直接执行线程1脱离,线程1将从就绪线程队列中删除 + */ + rt_thread_detach(&thread1); - /* - * 线程2继续休眠10个OS Tick然后退出 - */ - rt_thread_delay(10); + /* + * 线程2继续休眠10个OS Tick然后退出 + */ + rt_thread_delay(10); - /* - * 线程2运行结束后也将自动被从就绪队列中删除,并脱离线程队列 - */ + /* + * 线程2运行结束后也将自动被从就绪队列中删除,并脱离线程队列 + */ } int thread_detach_init() { - rt_err_t result; - - /* 初始化线程1 */ - result = rt_thread_init(&thread1, "t1", /* 线程名:t1 */ - thread1_entry, RT_NULL, /* 线程的入口是thread1_entry,入口参数是RT_NULL*/ - &thread1_stack[0], sizeof(thread1_stack), /* 线程栈是thread1_stack */ - THREAD_PRIORITY, 10); - if (result == RT_EOK) /* 如果返回正确,启动线程1 */ - rt_thread_startup(&thread1); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); - - /* 初始化线程2 */ - result = rt_thread_init(&thread2, "t2", /* 线程名:t2 */ - thread2_entry, RT_NULL, /* 线程的入口是thread2_entry,入口参数是RT_NULL*/ - &thread2_stack[0], sizeof(thread2_stack), /* 线程栈是thread2_stack */ - THREAD_PRIORITY - 1, 10); - if (result == RT_EOK) /* 如果返回正确,启动线程2 */ - rt_thread_startup(&thread2); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); - - return 0; + rt_err_t result; + + /* 初始化线程1 */ + result = rt_thread_init(&thread1, "t1", /* 线程名:t1 */ + thread1_entry, RT_NULL, /* 线程的入口是thread1_entry,入口参数是RT_NULL*/ + &thread1_stack[0], sizeof(thread1_stack), /* 线程栈是thread1_stack */ + THREAD_PRIORITY, 10); + if (result == RT_EOK) /* 如果返回正确,启动线程1 */ + rt_thread_startup(&thread1); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + /* 初始化线程2 */ + result = rt_thread_init(&thread2, "t2", /* 线程名:t2 */ + thread2_entry, RT_NULL, /* 线程的入口是thread2_entry,入口参数是RT_NULL*/ + &thread2_stack[0], sizeof(thread2_stack), /* 线程栈是thread2_stack */ + THREAD_PRIORITY - 1, 10); + if (result == RT_EOK) /* 如果返回正确,启动线程2 */ + rt_thread_startup(&thread2); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + return 0; } #ifdef RT_USING_TC static void _tc_cleanup() { - /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ - rt_enter_critical(); + /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ + rt_enter_critical(); - /* 执行线程脱离 */ - if (thread1.stat != RT_THREAD_CLOSE) - rt_thread_detach(&thread1); - if (thread2.stat != RT_THREAD_CLOSE) - rt_thread_detach(&thread2); + /* 执行线程脱离 */ + if (thread1.stat != RT_THREAD_CLOSE) + rt_thread_detach(&thread1); + if (thread2.stat != RT_THREAD_CLOSE) + rt_thread_detach(&thread2); - /* 调度器解锁 */ - rt_exit_critical(); + /* 调度器解锁 */ + rt_exit_critical(); - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_thread_detach() { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); - thread_detach_init(); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); + thread_detach_init(); - /* 返回TestCase运行的最长时间 */ - return 25; + /* 返回TestCase运行的最长时间 */ + return 25; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_thread_detach, a static thread example); @@ -111,8 +111,8 @@ FINSH_FUNCTION_EXPORT(_tc_thread_detach, a static thread example); /* 用户应用入口 */ int rt_application_init() { - thread_detach_init(); + thread_detach_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/thread_dynamic.c b/examples/kernel/thread_dynamic.c index 960a40de9d4b543241b54c2180350a9a696fc286..0afb7f8f846b3572f599514612843c52214d9bb3 100644 --- a/examples/kernel/thread_dynamic.c +++ b/examples/kernel/thread_dynamic.c @@ -3,42 +3,42 @@ static void thread_entry(void* parameter) { - rt_kprintf("thread dynamicly created ok\n"); - rt_thread_delay(10); - rt_kprintf("thread exit\n"); + rt_kprintf("thread dynamicly created ok\n"); + rt_thread_delay(10); + rt_kprintf("thread exit\n"); - tc_done(TC_STAT_PASSED); + tc_done(TC_STAT_PASSED); } int thread_dynamic_init() { - rt_thread_t tid; + rt_thread_t tid; - tid = rt_thread_create("test", - thread_entry, RT_NULL, - THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); - if (tid != RT_NULL) - rt_thread_startup(tid); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); + tid = rt_thread_create("test", + thread_entry, RT_NULL, + 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; + return 0; } #ifdef RT_USING_TC int _tc_thread_dynamic() { - thread_dynamic_init(); + thread_dynamic_init(); - return 20; + return 20; } FINSH_FUNCTION_EXPORT(_tc_thread_dynamic, a dynamic thread test); #else int rt_application_init() { - thread_dynamic_init(); + thread_dynamic_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/thread_dynamic_simple.c b/examples/kernel/thread_dynamic_simple.c index 9d7612bddc06dfd383364bd3a3e9ff82b6dbe42f..28944b1f345271bc5e08eb3945d16a8fc56ffa2a 100644 --- a/examples/kernel/thread_dynamic_simple.c +++ b/examples/kernel/thread_dynamic_simple.c @@ -12,69 +12,69 @@ static rt_thread_t tid2 = RT_NULL; /* 线程入口 */ static void thread_entry(void* parameter) { - rt_uint32_t count = 0; - rt_uint32_t no = (rt_uint32_t) parameter; /* 获得正确的入口参数 */ + rt_uint32_t count = 0; + rt_uint32_t no = (rt_uint32_t) parameter; /* 获得正确的入口参数 */ - while (1) - { - /* 打印线程计数值输出 */ - rt_kprintf("thread%d count: %d\n", no, count ++); + while (1) + { + /* 打印线程计数值输出 */ + rt_kprintf("thread%d count: %d\n", no, count ++); - /* 休眠10个OS Tick */ - rt_thread_delay(10); - } + /* 休眠10个OS Tick */ + rt_thread_delay(10); + } } int thread_dynamic_simple_init() { - /* 创建线程1 */ - tid1 = rt_thread_create("t1", - thread_entry, (void*)1, /* 线程入口是thread_entry, 入口参数是1 */ - 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", + thread_entry, (void*)1, /* 线程入口是thread_entry, 入口参数是1 */ + 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", - thread_entry, (void*)2, /* 线程入口是thread_entry, 入口参数是2 */ - 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", + thread_entry, (void*)2, /* 线程入口是thread_entry, 入口参数是2 */ + 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_exit_critical(); + /* 调度器解锁 */ + rt_exit_critical(); - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_thread_dynamic_simple() { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); - thread_dynamic_simple_init(); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); + thread_dynamic_simple_init(); - /* 返回TestCase运行的最长时间 */ - return 100; + /* 返回TestCase运行的最长时间 */ + return 100; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_thread_dynamic_simple, a dynamic thread example); @@ -82,8 +82,8 @@ FINSH_FUNCTION_EXPORT(_tc_thread_dynamic_simple, a dynamic thread example); /* 用户应用入口 */ int rt_application_init() { - thread_dynamic_simple_init(); + thread_dynamic_simple_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/thread_priority.c b/examples/kernel/thread_priority.c index 1d6fb4a80ebc2e2e178b8d3ffc706bab67011527..0045b0e578b571527dbaaa0a2ea0d50af97e464a 100644 --- a/examples/kernel/thread_priority.c +++ b/examples/kernel/thread_priority.c @@ -12,94 +12,94 @@ static rt_uint32_t count = 0; */ static void thread1_entry(void* parameter) { - while (1) - { - count ++; - rt_kprintf("count = %d\n", count); + while (1) + { + count ++; + rt_kprintf("count = %d\n", count); - rt_thread_delay(10); - } + rt_thread_delay(10); + } } static void thread2_entry(void* parameter) { - rt_tick_t tick; - - tick = rt_tick_get(); - while (1) - { - if (rt_tick_get() - tick >= 50) - { - if (count == 0) - tc_done(TC_STAT_FAILED); - else - tc_done(TC_STAT_PASSED); - - break; - } - } + rt_tick_t tick; + + tick = rt_tick_get(); + while (1) + { + if (rt_tick_get() - tick >= 50) + { + if (count == 0) + tc_done(TC_STAT_FAILED); + else + tc_done(TC_STAT_PASSED); + + break; + } + } } int thread_priority_init() { - rt_err_t result; - - result = rt_thread_init(&thread1, - "t1", - thread1_entry, RT_NULL, - &thread1_stack[0], sizeof(thread1_stack), - THREAD_PRIORITY - 1, THREAD_TIMESLICE); - - if (result == RT_EOK) - rt_thread_startup(&thread1); - else - tc_stat(TC_STAT_FAILED); - - rt_thread_init(&thread2, - "t2", - thread2_entry, RT_NULL, - &thread2_stack[0], sizeof(thread2_stack), - THREAD_PRIORITY + 1, THREAD_TIMESLICE); - - if (result == RT_EOK) - rt_thread_startup(&thread2); - else - tc_stat(TC_STAT_FAILED); - - return 0; + rt_err_t result; + + result = rt_thread_init(&thread1, + "t1", + thread1_entry, RT_NULL, + &thread1_stack[0], sizeof(thread1_stack), + THREAD_PRIORITY - 1, THREAD_TIMESLICE); + + if (result == RT_EOK) + rt_thread_startup(&thread1); + else + tc_stat(TC_STAT_FAILED); + + rt_thread_init(&thread2, + "t2", + thread2_entry, RT_NULL, + &thread2_stack[0], sizeof(thread2_stack), + THREAD_PRIORITY + 1, THREAD_TIMESLICE); + + if (result == RT_EOK) + rt_thread_startup(&thread2); + else + tc_stat(TC_STAT_FAILED); + + return 0; } #ifdef RT_USING_TC static void _tc_cleanup() { - /* lock scheduler */ - rt_enter_critical(); + /* lock scheduler */ + rt_enter_critical(); - if (thread1.stat != RT_THREAD_CLOSE) - rt_thread_detach(&thread1); - if (thread2.stat != RT_THREAD_CLOSE) - rt_thread_detach(&thread2); + if (thread1.stat != RT_THREAD_CLOSE) + rt_thread_detach(&thread1); + if (thread2.stat != RT_THREAD_CLOSE) + rt_thread_detach(&thread2); - /* unlock scheduler */ - rt_exit_critical(); + /* unlock scheduler */ + rt_exit_critical(); } int _tc_thread_priority() { - count = 0; + count = 0; - /* set tc cleanup */ - tc_cleanup(_tc_cleanup); - thread_priority_init(); + /* set tc cleanup */ + tc_cleanup(_tc_cleanup); + thread_priority_init(); - return RT_TICK_PER_SECOND; + return RT_TICK_PER_SECOND; } FINSH_FUNCTION_EXPORT(_tc_thread_priority, a priority thread test); #else int rt_application_init() { - thread_priority_init(); + thread_priority_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/thread_resume.c b/examples/kernel/thread_resume.c index ac8b6cb4896fa8601db03412ec8af7c2ecbfd914..cea814616f92047339f38ddb2631e0fb516df045 100644 --- a/examples/kernel/thread_resume.c +++ b/examples/kernel/thread_resume.c @@ -13,102 +13,102 @@ static rt_thread_t tid2 = RT_NULL; /* 线程1入口 */ static void thread1_entry(void* parameter) { - /* 低优先级线程1开始运行 */ - rt_kprintf("thread1 startup%d\n"); + /* 低优先级线程1开始运行 */ + rt_kprintf("thread1 startup%d\n"); - /* 挂起自身 */ - rt_kprintf("suspend thread self\n"); - rt_thread_suspend(tid1); - /* 主动执行线程调度 */ - rt_schedule(); + /* 挂起自身 */ + rt_kprintf("suspend thread self\n"); + rt_thread_suspend(tid1); + /* 主动执行线程调度 */ + rt_schedule(); - /* 当线程1被唤醒时 */ - rt_kprintf("thread1 resumed\n"); + /* 当线程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; - } + if (tid == tid1) + { + tid1 = RT_NULL; + } + if (tid == tid2) + { + tid = RT_NULL; + } } /* 线程2入口 */ static void thread2_entry(void* parameter) { - /* 延时10个OS Tick */ - rt_thread_delay(10); + /* 延时10个OS Tick */ + rt_thread_delay(10); - /* 唤醒线程1 */ - rt_thread_resume(tid1); - rt_kprintf("thread2: to resume thread1\n"); + /* 唤醒线程1 */ + rt_thread_resume(tid1); + rt_kprintf("thread2: to resume thread1\n"); - /* 延时10个OS Tick */ - rt_thread_delay(10); + /* 延时10个OS Tick */ + rt_thread_delay(10); - /* 线程2自动退出 */ + /* 线程2自动退出 */ } int thread_resume_init() { - /* 创建线程1 */ - tid1 = rt_thread_create("thread", - 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); - - /* 创建线程2 */ - tid2 = rt_thread_create("thread", - 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); - - return 0; + /* 创建线程1 */ + tid1 = rt_thread_create("thread", + 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); + + /* 创建线程2 */ + tid2 = rt_thread_create("thread", + 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); + + 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_exit_critical(); + /* 调度器解锁 */ + rt_exit_critical(); - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_thread_resume() { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); - thread_resume_init(); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); + thread_resume_init(); - /* 返回TestCase运行的最长时间 */ - return 25; + /* 返回TestCase运行的最长时间 */ + return 25; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_thread_resume, a thread resume example); @@ -116,8 +116,8 @@ FINSH_FUNCTION_EXPORT(_tc_thread_resume, a thread resume example); /* 用户应用入口 */ int rt_application_init() { - thread_resume_init(); + thread_resume_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/thread_same_priority.c b/examples/kernel/thread_same_priority.c index 5e0fb8111641853ba505d2b515ec6f814e6505b3..c7da2087a0d86e6d79a11599035e36ea4ae68d33 100644 --- a/examples/kernel/thread_same_priority.c +++ b/examples/kernel/thread_same_priority.c @@ -10,87 +10,87 @@ volatile static rt_uint32_t t1_count = 0; volatile static rt_uint32_t t2_count = 0; static void thread1_entry(void* parameter) { - while (1) - { - t1_count ++; - } + while (1) + { + t1_count ++; + } } static void thread2_entry(void* parameter) { - while (1) - { - t2_count ++; - } + while (1) + { + t2_count ++; + } } rt_err_t thread_same_priority_init() { - rt_err_t result; - - result = rt_thread_init(&thread1, - "t1", - thread1_entry, RT_NULL, - &thread1_stack[0], sizeof(thread1_stack), - THREAD_PRIORITY, 10); - if (result == RT_EOK) - rt_thread_startup(&thread1); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); - - result = rt_thread_init(&thread2, - "t2", - thread2_entry, RT_NULL, - &thread2_stack[0], sizeof(thread2_stack), - THREAD_PRIORITY, 5); - if (result == RT_EOK) - rt_thread_startup(&thread2); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); - - return result; + rt_err_t result; + + result = rt_thread_init(&thread1, + "t1", + thread1_entry, RT_NULL, + &thread1_stack[0], sizeof(thread1_stack), + THREAD_PRIORITY, 10); + if (result == RT_EOK) + rt_thread_startup(&thread1); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + result = rt_thread_init(&thread2, + "t2", + thread2_entry, RT_NULL, + &thread2_stack[0], sizeof(thread2_stack), + THREAD_PRIORITY, 5); + if (result == RT_EOK) + rt_thread_startup(&thread2); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + return result; } #ifdef RT_USING_TC static void _tc_cleanup() { - /* lock scheduler */ - rt_enter_critical(); + /* lock scheduler */ + rt_enter_critical(); - if (thread1.stat != RT_THREAD_CLOSE) - rt_thread_detach(&thread1); - if (thread2.stat != RT_THREAD_CLOSE) - rt_thread_detach(&thread2); + if (thread1.stat != RT_THREAD_CLOSE) + rt_thread_detach(&thread1); + if (thread2.stat != RT_THREAD_CLOSE) + rt_thread_detach(&thread2); - /* unlock scheduler */ - rt_exit_critical(); + /* unlock scheduler */ + rt_exit_critical(); - rt_kprintf("t1_count=%d t2_count=%d\n",t1_count,t2_count); + rt_kprintf("t1_count=%d t2_count=%d\n",t1_count,t2_count); - if (t1_count / t2_count != 2) - tc_stat(TC_STAT_END | TC_STAT_FAILED); - else - tc_done(TC_STAT_PASSED); + if (t1_count / t2_count != 2) + tc_stat(TC_STAT_END | TC_STAT_FAILED); + else + tc_done(TC_STAT_PASSED); } int _tc_thread_same_priority() { - t1_count = 0; - t2_count = 0; + t1_count = 0; + t2_count = 0; - /* set tc cleanup */ - tc_cleanup(_tc_cleanup); + /* set tc cleanup */ + tc_cleanup(_tc_cleanup); - thread_same_priority_init(); + thread_same_priority_init(); - return 100; + return 100; } FINSH_FUNCTION_EXPORT(_tc_thread_same_priority, a same priority thread test); #else int rt_application_init() { - thread_same_priority_init(); + thread_same_priority_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/thread_static.c b/examples/kernel/thread_static.c index 4547304b5815242c9c192a29b4023ad9e192735d..3033f2cda1e11ef40bdcb8f80893a6a5f75a1037 100644 --- a/examples/kernel/thread_static.c +++ b/examples/kernel/thread_static.c @@ -8,45 +8,45 @@ static struct rt_thread thread; static char thread_stack[THREAD_STACK_SIZE]; static void thread_entry(void* parameter) { - rt_kprintf("thread staticly inited ok\n"); - rt_thread_delay(10); - rt_kprintf("thread exit\n"); + rt_kprintf("thread staticly inited ok\n"); + rt_thread_delay(10); + rt_kprintf("thread exit\n"); - tc_done(TC_STAT_PASSED); + tc_done(TC_STAT_PASSED); } rt_err_t thread_static_init() { - rt_err_t result; + rt_err_t result; - result = rt_thread_init(&thread, - "test", - thread_entry, RT_NULL, - &thread_stack[0], sizeof(thread_stack), - THREAD_PRIORITY, 10); + result = rt_thread_init(&thread, + "test", + thread_entry, RT_NULL, + &thread_stack[0], sizeof(thread_stack), + THREAD_PRIORITY, 10); - if (result == RT_EOK) - rt_thread_startup(&thread); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); + if (result == RT_EOK) + rt_thread_startup(&thread); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); - return result; + return result; } #ifdef RT_USING_TC int _tc_thread_static() { - thread_static_init(); + thread_static_init(); - return 20; + return 20; } FINSH_FUNCTION_EXPORT(_tc_thread_static, a static thread test); #else int rt_application_init() { - thread_static_init(); + thread_static_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/thread_static_simple.c b/examples/kernel/thread_static_simple.c index 3ab8ffda012ce24a023dafce3b3c55ed3be8cdf3..d9e3ff6f611226e6377a026508a7e77bb22a0d05 100644 --- a/examples/kernel/thread_static_simple.c +++ b/examples/kernel/thread_static_simple.c @@ -18,73 +18,73 @@ static rt_uint8_t thread2_stack[THREAD_STACK_SIZE]; /* 线程入口 */ static void thread_entry(void* parameter) { - rt_uint32_t count = 0; - rt_uint32_t no = (rt_uint32_t) parameter; /* 获得正确的入口参数 */ + rt_uint32_t count = 0; + rt_uint32_t no = (rt_uint32_t) parameter; /* 获得正确的入口参数 */ - while (1) - { - /* 打印线程计数值输出 */ - rt_kprintf("thread%d count: %d\n", no, count ++); + while (1) + { + /* 打印线程计数值输出 */ + rt_kprintf("thread%d count: %d\n", no, count ++); - /* 休眠10个OS Tick */ - rt_thread_delay(10); - } + /* 休眠10个OS Tick */ + rt_thread_delay(10); + } } int thread_static_simple_init() { - rt_err_t result; + rt_err_t result; - /* 初始化线程1 */ - result = rt_thread_init(&thread1, "t1", /* 线程名:t1 */ - thread_entry, (void*)1, /* 线程的入口是thread_entry,入口参数是1 */ - &thread1_stack[0], sizeof(thread1_stack), /* 线程栈是thread1_stack */ - THREAD_PRIORITY, 10); - if (result == RT_EOK) /* 如果返回正确,启动线程1 */ - rt_thread_startup(&thread1); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); + /* 初始化线程1 */ + result = rt_thread_init(&thread1, "t1", /* 线程名:t1 */ + thread_entry, (void*)1, /* 线程的入口是thread_entry,入口参数是1 */ + &thread1_stack[0], sizeof(thread1_stack), /* 线程栈是thread1_stack */ + THREAD_PRIORITY, 10); + if (result == RT_EOK) /* 如果返回正确,启动线程1 */ + rt_thread_startup(&thread1); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); - /* 初始化线程2 */ - result = rt_thread_init(&thread2, "t2", /* 线程名:t2 */ - thread_entry, RT_NULL, /* 线程的入口是thread_entry,入口参数是2 */ - &thread2_stack[0], sizeof(thread2_stack), /* 线程栈是thread2_stack */ - THREAD_PRIORITY + 1, 10); - if (result == RT_EOK) /* 如果返回正确,启动线程2 */ - rt_thread_startup(&thread2); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); + /* 初始化线程2 */ + result = rt_thread_init(&thread2, "t2", /* 线程名:t2 */ + thread_entry, RT_NULL, /* 线程的入口是thread_entry,入口参数是2 */ + &thread2_stack[0], sizeof(thread2_stack), /* 线程栈是thread2_stack */ + THREAD_PRIORITY + 1, 10); + if (result == RT_EOK) /* 如果返回正确,启动线程2 */ + rt_thread_startup(&thread2); + 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 (thread1.stat != RT_THREAD_CLOSE) - rt_thread_detach(&thread1); - if (thread2.stat != RT_THREAD_CLOSE) - rt_thread_detach(&thread2); + /* 执行线程脱离 */ + if (thread1.stat != RT_THREAD_CLOSE) + rt_thread_detach(&thread1); + if (thread2.stat != RT_THREAD_CLOSE) + rt_thread_detach(&thread2); - /* 调度器解锁 */ - rt_exit_critical(); + /* 调度器解锁 */ + rt_exit_critical(); - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_thread_static_simple() { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); - thread_static_simple_init(); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); + thread_static_simple_init(); - /* 返回TestCase运行的最长时间 */ - return 100; + /* 返回TestCase运行的最长时间 */ + return 100; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_thread_static_simple, a static thread example); @@ -92,8 +92,8 @@ FINSH_FUNCTION_EXPORT(_tc_thread_static_simple, a static thread example); /* 用户应用入口 */ int rt_application_init() { - thread_static_simple_init(); + thread_static_simple_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/thread_suspend.c b/examples/kernel/thread_suspend.c index 7c019573b1242ccb6f59130eb578b4f089e695b3..947cc3cbf61a6f605bc1043f231ccec6efc0910c 100644 --- a/examples/kernel/thread_suspend.c +++ b/examples/kernel/thread_suspend.c @@ -12,81 +12,81 @@ static rt_thread_t tid2 = RT_NULL; /* 线程1入口 */ static void thread1_entry(void* parameter) { - rt_uint32_t count = 0; + rt_uint32_t count = 0; - while (1) - { - /* 线程1采用低优先级运行,一直打印计数值 */ - rt_kprintf("thread count: %d\n", count ++); - } + while (1) + { + /* 线程1采用低优先级运行,一直打印计数值 */ + rt_kprintf("thread count: %d\n", count ++); + } } /* 线程2入口 */ static void thread2_entry(void* parameter) { - /* 延时10个OS Tick */ - rt_thread_delay(10); + /* 延时10个OS Tick */ + rt_thread_delay(10); - /* 挂起线程1 */ - rt_thread_suspend(tid1); + /* 挂起线程1 */ + rt_thread_suspend(tid1); - /* 延时10个OS Tick */ - rt_thread_delay(10); + /* 延时10个OS Tick */ + rt_thread_delay(10); - /* 线程2自动退出 */ - tid2 = RT_NULL; + /* 线程2自动退出 */ + tid2 = RT_NULL; } int thread_suspend_init() { - /* 创建线程1 */ - tid1 = rt_thread_create("thread", - 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("thread", - 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; + /* 创建线程1 */ + tid1 = rt_thread_create("thread", + 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("thread", + 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; } #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_exit_critical(); + /* 调度器解锁 */ + rt_exit_critical(); - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_thread_suspend() { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); - thread_suspend_init(); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); + thread_suspend_init(); - /* 返回TestCase运行的最长时间 */ - return 100; + /* 返回TestCase运行的最长时间 */ + return 100; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_thread_suspend, a thread suspend example); @@ -94,8 +94,8 @@ FINSH_FUNCTION_EXPORT(_tc_thread_suspend, a thread suspend example); /* 用户应用入口 */ int rt_application_init() { - thread_suspend_init(); + thread_suspend_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/thread_yield.c b/examples/kernel/thread_yield.c index de2c3a53acdb9d78db2c612f67c1985e4858526d..4ed6b90e2a5d5b48c3dde668b2147421d08ede9e 100644 --- a/examples/kernel/thread_yield.c +++ b/examples/kernel/thread_yield.c @@ -10,83 +10,83 @@ static rt_thread_t tid2 = RT_NULL; /* 线程1入口 */ static void thread1_entry(void* parameter) { - rt_uint32_t count = 0; + rt_uint32_t count = 0; - while (1) - { - /* 打印线程1的输出 */ - rt_kprintf("thread1: count = %d\n", count ++); + while (1) + { + /* 打印线程1的输出 */ + rt_kprintf("thread1: count = %d\n", count ++); - /* 执行yield后应该切换到thread2执行 */ - rt_thread_yield(); - } + /* 执行yield后应该切换到thread2执行 */ + rt_thread_yield(); + } } /* 线程2入口 */ static void thread2_entry(void* parameter) { - rt_uint32_t count = 0; + rt_uint32_t count = 0; - while (1) - { - /* 打印线程2的输出 */ - rt_kprintf("thread2: count = %d\n", count ++); + while (1) + { + /* 打印线程2的输出 */ + rt_kprintf("thread2: count = %d\n", count ++); - /* 执行yield后应该切换到thread1执行 */ - rt_thread_yield(); - } + /* 执行yield后应该切换到thread1执行 */ + rt_thread_yield(); + } } int thread_yield_init() { - /* 创建线程1 */ - tid1 = rt_thread_create("thread", - 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("thread", - 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; + /* 创建线程1 */ + tid1 = rt_thread_create("thread", + 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("thread", + 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; } #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_exit_critical(); + /* 调度器解锁 */ + rt_exit_critical(); - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_thread_yield() { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); - thread_yield_init(); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); + thread_yield_init(); - /* 返回TestCase运行的最长时间 */ - return 30; + /* 返回TestCase运行的最长时间 */ + return 30; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_thread_yield, a thread yield example); @@ -94,8 +94,8 @@ FINSH_FUNCTION_EXPORT(_tc_thread_yield, a thread yield example); /* 用户应用入口 */ int rt_application_init() { - thread_yield_init(); + thread_yield_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/timer_control.c b/examples/kernel/timer_control.c index f685f140eb663e245910159882b2aa968797ab2e..9fcb0d4c4d92417d91d907f1e0e0e16a9cdbf39c 100644 --- a/examples/kernel/timer_control.c +++ b/examples/kernel/timer_control.c @@ -13,63 +13,63 @@ static rt_uint8_t count; /* 定时器超时函数 */ static void timeout1(void* parameter) { - rt_tick_t timeout = 50; - - rt_kprintf("periodic timer is timeout\n"); + rt_tick_t timeout = 50; + + rt_kprintf("periodic timer is timeout\n"); - count ++; - /* 停止定时器自身 */ - if (count >= 8) - { - /* 控制定时器然后更改超时时间长度 */ - rt_timer_control(timer1, RT_TIMER_CTRL_SET_TIME, (void *)&timeout); - count = 0; - } + count ++; + /* 停止定时器自身 */ + if (count >= 8) + { + /* 控制定时器然后更改超时时间长度 */ + rt_timer_control(timer1, RT_TIMER_CTRL_SET_TIME, (void *)&timeout); + count = 0; + } } void timer_control_init() { - /* 创建定时器1 */ - timer1 = rt_timer_create("timer1", /* 定时器名字是 timer1 */ - timeout1, /* 超时时回调的处理函数 */ - RT_NULL, /* 超时函数的入口参数 */ - 10, /* 定时长度,以OS Tick为单位,即10个OS Tick */ - RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */ - /* 启动定时器 */ - if (timer1 != RT_NULL) - rt_timer_start(timer1); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); + /* 创建定时器1 */ + timer1 = rt_timer_create("timer1", /* 定时器名字是 timer1 */ + timeout1, /* 超时时回调的处理函数 */ + RT_NULL, /* 超时函数的入口参数 */ + 10, /* 定时长度,以OS Tick为单位,即10个OS Tick */ + RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */ + /* 启动定时器 */ + if (timer1 != RT_NULL) + rt_timer_start(timer1); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); } #ifdef RT_USING_TC static void _tc_cleanup() { - /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ - rt_enter_critical(); + /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ + rt_enter_critical(); - /* 删除定时器对象 */ - rt_timer_delete(timer1); - timer1 = RT_NULL; + /* 删除定时器对象 */ + rt_timer_delete(timer1); + timer1 = RT_NULL; - /* 调度器解锁 */ - rt_exit_critical(); + /* 调度器解锁 */ + rt_exit_critical(); - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_timer_control() { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); - /* 执行定时器例程 */ - count = 0; - timer_control_init(); + /* 执行定时器例程 */ + count = 0; + timer_control_init(); - /* 返回TestCase运行的最长时间 */ - return 100; + /* 返回TestCase运行的最长时间 */ + return 100; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_timer_control, a timer control example); @@ -77,8 +77,8 @@ FINSH_FUNCTION_EXPORT(_tc_timer_control, a timer control example); /* 用户应用入口 */ int rt_application_init() { - timer_control_init(); + timer_control_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/timer_dynamic.c b/examples/kernel/timer_dynamic.c index 49262b8193cf1b75443cf98ec610dcde9d055d42..1260d50c095ac133161cd7a777a10f5af41d4e9f 100644 --- a/examples/kernel/timer_dynamic.c +++ b/examples/kernel/timer_dynamic.c @@ -13,70 +13,70 @@ static rt_timer_t timer2; /* 定时器1超时函数 */ static void timeout1(void* parameter) { - rt_kprintf("periodic timer is timeout\n"); + rt_kprintf("periodic timer is timeout\n"); } /* 定时器2超时函数 */ static void timeout2(void* parameter) { - rt_kprintf("one shot timer is timeout\n"); + rt_kprintf("one shot timer is timeout\n"); } void timer_create_init() { - /* 创建定时器1 */ - timer1 = rt_timer_create("timer1", /* 定时器名字是 timer1 */ - timeout1, /* 超时时回调的处理函数 */ - RT_NULL, /* 超时函数的入口参数 */ - 10, /* 定时长度,以OS Tick为单位,即10个OS Tick */ - RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */ - /* 启动定时器 */ - if (timer1 != RT_NULL) - rt_timer_start(timer1); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); + /* 创建定时器1 */ + timer1 = rt_timer_create("timer1", /* 定时器名字是 timer1 */ + timeout1, /* 超时时回调的处理函数 */ + RT_NULL, /* 超时函数的入口参数 */ + 10, /* 定时长度,以OS Tick为单位,即10个OS Tick */ + RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */ + /* 启动定时器 */ + if (timer1 != RT_NULL) + rt_timer_start(timer1); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); - /* 创建定时器2 */ - timer2 = rt_timer_create("timer2", /* 定时器名字是 timer2 */ - timeout2, /* 超时时回调的处理函数 */ - RT_NULL, /* 超时函数的入口参数 */ - 30, /* 定时长度为30个OS Tick */ - RT_TIMER_FLAG_ONE_SHOT); /* 单次定时器 */ + /* 创建定时器2 */ + timer2 = rt_timer_create("timer2", /* 定时器名字是 timer2 */ + timeout2, /* 超时时回调的处理函数 */ + RT_NULL, /* 超时函数的入口参数 */ + 30, /* 定时长度为30个OS Tick */ + RT_TIMER_FLAG_ONE_SHOT); /* 单次定时器 */ - /* 启动定时器 */ - if (timer2 != RT_NULL) - rt_timer_start(timer2); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); + /* 启动定时器 */ + if (timer2 != RT_NULL) + rt_timer_start(timer2); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); } #ifdef RT_USING_TC static void _tc_cleanup() { - /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ - rt_enter_critical(); + /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ + rt_enter_critical(); - /* 删除定时器对象 */ - rt_timer_delete(timer1); - rt_timer_delete(timer2); + /* 删除定时器对象 */ + rt_timer_delete(timer1); + rt_timer_delete(timer2); - /* 调度器解锁 */ - rt_exit_critical(); + /* 调度器解锁 */ + rt_exit_critical(); - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_timer_create() { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); - /* 执行定时器例程 */ - timer_create_init(); + /* 执行定时器例程 */ + timer_create_init(); - /* 返回TestCase运行的最长时间 */ - return 100; + /* 返回TestCase运行的最长时间 */ + return 100; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_timer_create, a dynamic timer example); @@ -84,8 +84,8 @@ FINSH_FUNCTION_EXPORT(_tc_timer_create, a dynamic timer example); /* 用户应用入口 */ int rt_application_init() { - timer_create_init(); + timer_create_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/timer_static.c b/examples/kernel/timer_static.c index bddfe63af3ea472e9839b831ffd9b70230603fe1..884927bbc3f33c6be0e9372d5ff53bb98243172f 100644 --- a/examples/kernel/timer_static.c +++ b/examples/kernel/timer_static.c @@ -13,61 +13,61 @@ static struct rt_timer timer2; /* 定时器1超时函数 */ static void timeout1(void* parameter) { - rt_kprintf("periodic timer is timeout\n"); + rt_kprintf("periodic timer is timeout\n"); } /* 定时器2超时函数 */ static void timeout2(void* parameter) { - rt_kprintf("one shot timer is timeout\n"); + rt_kprintf("one shot timer is timeout\n"); } void timer_static_init() { - /* 初始化定时器 */ - rt_timer_init(&timer1, "timer1", /* 定时器名字是 timer1 */ - timeout1, /* 超时时回调的处理函数 */ - RT_NULL, /* 超时函数的入口参数 */ - 10, /* 定时长度,以OS Tick为单位,即10个OS Tick */ - RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */ - rt_timer_init(&timer2, "timer2", /* 定时器名字是 timer2 */ - timeout2, /* 超时时回调的处理函数 */ - RT_NULL, /* 超时函数的入口参数 */ - 30, /* 定时长度为30个OS Tick */ - RT_TIMER_FLAG_ONE_SHOT); /* 单次定时器 */ + /* 初始化定时器 */ + rt_timer_init(&timer1, "timer1", /* 定时器名字是 timer1 */ + timeout1, /* 超时时回调的处理函数 */ + RT_NULL, /* 超时函数的入口参数 */ + 10, /* 定时长度,以OS Tick为单位,即10个OS Tick */ + RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */ + rt_timer_init(&timer2, "timer2", /* 定时器名字是 timer2 */ + timeout2, /* 超时时回调的处理函数 */ + RT_NULL, /* 超时函数的入口参数 */ + 30, /* 定时长度为30个OS Tick */ + RT_TIMER_FLAG_ONE_SHOT); /* 单次定时器 */ - /* 启动定时器 */ - rt_timer_start(&timer1); - rt_timer_start(&timer2); + /* 启动定时器 */ + rt_timer_start(&timer1); + rt_timer_start(&timer2); } #ifdef RT_USING_TC static void _tc_cleanup() { - /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ - rt_enter_critical(); + /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ + rt_enter_critical(); - /* 执行定时器脱离 */ - rt_timer_detach(&timer1); - rt_timer_detach(&timer2); + /* 执行定时器脱离 */ + rt_timer_detach(&timer1); + rt_timer_detach(&timer2); - /* 调度器解锁 */ - rt_exit_critical(); + /* 调度器解锁 */ + rt_exit_critical(); - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_timer_static() { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); - /* 执行定时器例程 */ - timer_static_init(); + /* 执行定时器例程 */ + timer_static_init(); - /* 返回TestCase运行的最长时间 */ - return 100; + /* 返回TestCase运行的最长时间 */ + return 100; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_timer_static, a static timer example); @@ -75,8 +75,8 @@ FINSH_FUNCTION_EXPORT(_tc_timer_static, a static timer example); /* 用户应用入口 */ int rt_application_init() { - timer_static_init(); + timer_static_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/timer_stop_self.c b/examples/kernel/timer_stop_self.c index 6d1e287e54a81207ec88b775a327f3bb39e00fbc..38ec9c441bf699c1e4a8f8fdf483c40aa39fa460 100644 --- a/examples/kernel/timer_stop_self.c +++ b/examples/kernel/timer_stop_self.c @@ -13,61 +13,61 @@ static rt_uint8_t count; /* 定时器超时函数 */ static void timeout1(void* parameter) { - rt_kprintf("periodic timer is timeout\n"); + rt_kprintf("periodic timer is timeout\n"); - count ++; - /* 停止定时器自身 */ - if (count >= 8) - { - /* 停止定时器 */ - rt_timer_stop(timer1); - count = 0; - } + count ++; + /* 停止定时器自身 */ + if (count >= 8) + { + /* 停止定时器 */ + rt_timer_stop(timer1); + count = 0; + } } void timer_stop_self_init() { - /* 创建定时器1 */ - timer1 = rt_timer_create("timer1", /* 定时器名字是 timer1 */ - timeout1, /* 超时时回调的处理函数 */ - RT_NULL, /* 超时函数的入口参数 */ - 10, /* 定时长度,以OS Tick为单位,即10个OS Tick */ - RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */ - /* 启动定时器 */ - if (timer1 != RT_NULL) - rt_timer_start(timer1); - else - tc_stat(TC_STAT_END | TC_STAT_FAILED); + /* 创建定时器1 */ + timer1 = rt_timer_create("timer1", /* 定时器名字是 timer1 */ + timeout1, /* 超时时回调的处理函数 */ + RT_NULL, /* 超时函数的入口参数 */ + 10, /* 定时长度,以OS Tick为单位,即10个OS Tick */ + RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */ + /* 启动定时器 */ + if (timer1 != RT_NULL) + rt_timer_start(timer1); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); } #ifdef RT_USING_TC static void _tc_cleanup() { - /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ - rt_enter_critical(); + /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ + rt_enter_critical(); - /* 删除定时器对象 */ - rt_timer_delete(timer1); - timer1 = RT_NULL; + /* 删除定时器对象 */ + rt_timer_delete(timer1); + timer1 = RT_NULL; - /* 调度器解锁 */ - rt_exit_critical(); + /* 调度器解锁 */ + rt_exit_critical(); - /* 设置TestCase状态 */ - tc_done(TC_STAT_PASSED); + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); } int _tc_timer_stop_self() { - /* 设置TestCase清理回调函数 */ - tc_cleanup(_tc_cleanup); + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); - /* 执行定时器例程 */ - count = 0; - timer_stop_self_init(); + /* 执行定时器例程 */ + count = 0; + timer_stop_self_init(); - /* 返回TestCase运行的最长时间 */ - return 100; + /* 返回TestCase运行的最长时间 */ + return 100; } /* 输出函数命令到finsh shell中 */ FINSH_FUNCTION_EXPORT(_tc_timer_stop_self, a dynamic timer example); @@ -75,8 +75,8 @@ FINSH_FUNCTION_EXPORT(_tc_timer_stop_self, a dynamic timer example); /* 用户应用入口 */ int rt_application_init() { - timer_stop_self_init(); + timer_stop_self_init(); - return 0; + return 0; } #endif diff --git a/examples/kernel/timer_timeout.c b/examples/kernel/timer_timeout.c index 80241e53f21d0fcfdb6259cb3678a54bfcf9bcd4..d1a7099e2ee9c8075b841efa536ada68c155d843 100644 --- a/examples/kernel/timer_timeout.c +++ b/examples/kernel/timer_timeout.c @@ -20,94 +20,94 @@ static struct rt_timer timer; static rt_uint16_t no = 0; static void timer_timeout(void* parameter) { - char buf[32]; - rt_uint32_t length; + char buf[32]; + rt_uint32_t length; - length = rt_snprintf(buf, sizeof(buf), "message %d", no++); - rt_mq_send(&mq, &buf[0], length); + length = rt_snprintf(buf, sizeof(buf), "message %d", no++); + rt_mq_send(&mq, &buf[0], length); } /* ߳ں */ static void thread_entry(void* parameter) { - char buf[64]; - rt_err_t result; - - /* ʼʱ */ - rt_timer_init(&timer, "timer", /* ʱ timer1 */ - timer_timeout, /* ʱʱصĴ */ - RT_NULL, /* ʱڲ */ - 1, /* ʱȣOS TickΪλ1OS Tick */ - RT_TIMER_FLAG_PERIODIC); /* Զʱ */ - - while (1) - { - rt_memset(&buf[0], 0, sizeof(buf)); - - /* ϢнϢ */ - result = rt_mq_recv(&mq, &buf[0], sizeof(buf), 1); - if (result == RT_EOK) - { - rt_kprintf("recv msg: %s\n", buf); - } - else if (result == -RT_ETIMEOUT) - { - rt_kprintf("recv msg timeout\n"); - } - } + char buf[64]; + rt_err_t result; + + /* ʼʱ */ + rt_timer_init(&timer, "timer", /* ʱ timer1 */ + timer_timeout, /* ʱʱصĴ */ + RT_NULL, /* ʱڲ */ + 1, /* ʱȣOS TickΪλ1OS Tick */ + RT_TIMER_FLAG_PERIODIC); /* Զʱ */ + + while (1) + { + rt_memset(&buf[0], 0, sizeof(buf)); + + /* ϢнϢ */ + result = rt_mq_recv(&mq, &buf[0], sizeof(buf), 1); + if (result == RT_EOK) + { + rt_kprintf("recv msg: %s\n", buf); + } + else if (result == -RT_ETIMEOUT) + { + rt_kprintf("recv msg timeout\n"); + } + } } int timer_timeout_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); /* ж̵߳ȴȵõķϢ */ - - /* ߳ */ - tid = rt_thread_create("t", - thread_entry, RT_NULL, /* ߳thread_entry, ڲRT_NULL */ - 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; + /* ʼϢ */ + rt_mq_init(&mq, "mqt", + &msg_pool[0], /* ڴָmsg_pool */ + 128 - sizeof(void*), /* ÿϢĴС 128 - void* */ + sizeof(msg_pool), /* ڴصĴСmsg_poolĴС */ + RT_IPC_FLAG_FIFO); /* ж̵߳ȴȵõķϢ */ + + /* ߳ */ + tid = rt_thread_create("t", + thread_entry, RT_NULL, /* ߳thread_entry, ڲRT_NULL */ + 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 static void _tc_cleanup() { - /* 󣬽л̣߳Ӧж */ - rt_enter_critical(); + /* 󣬽л̣߳Ӧж */ + rt_enter_critical(); - /* ɾ߳ */ - if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE) - rt_thread_delete(tid); + /* ɾ߳ */ + if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE) + rt_thread_delete(tid); - /* ִϢж */ - rt_mq_detach(&mq); - /* ִжʱ */ - rt_timer_detach(&timer); + /* ִϢж */ + rt_mq_detach(&mq); + /* ִжʱ */ + rt_timer_detach(&timer); - /* */ - rt_exit_critical(); + /* */ + rt_exit_critical(); - /* TestCase״̬ */ - tc_done(TC_STAT_PASSED); + /* TestCase״̬ */ + tc_done(TC_STAT_PASSED); } int _tc_timer_timeout() { - /* TestCaseص */ - tc_cleanup(_tc_cleanup); - timer_timeout_init(); + /* TestCaseص */ + tc_cleanup(_tc_cleanup); + timer_timeout_init(); - /* TestCaseеʱ */ - return 100; + /* TestCaseеʱ */ + return 100; } /* finsh shell */ FINSH_FUNCTION_EXPORT(_tc_timer_timeout, a thread timer testcase); @@ -115,8 +115,8 @@ FINSH_FUNCTION_EXPORT(_tc_timer_timeout, a thread timer testcase); /* ûӦ */ int rt_application_init() { - timer_timeout_init(); + timer_timeout_init(); - return 0; + return 0; } #endif