add more thread example and Chinese comments.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@519 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong 2010-03-20 15:15:54 +00:00
parent dd7cbec8d9
commit 911d179125
8 changed files with 532 additions and 45 deletions

View File

@ -9,6 +9,10 @@ thread_same_priority.c
thread_static_simple.c thread_static_simple.c
thread_dynamic_simple.c thread_dynamic_simple.c
thread_delete.c thread_delete.c
thread_detach.c
thread_yield.c
thread_suspend.c
thread_resume.c
semaphore_static.c semaphore_static.c
semaphore_dynamic.c semaphore_dynamic.c
semaphore_priority.c semaphore_priority.c

View File

@ -1,43 +1,72 @@
/*
* 线
*
* 线线线
*/
#include <rtthread.h> #include <rtthread.h>
#include "tc_comm.h" #include "tc_comm.h"
/* /*
* This is an example for dynamic thread * 线(rt_thread_delete)线线
* 访线线
* 访
*/ */
static rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL; static rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL;
/* 线程1的入口函数 */
static void thread1_entry(void* parameter) static void thread1_entry(void* parameter)
{ {
rt_uint32_t count = 0; rt_uint32_t count = 0;
while (1) while (1)
{ {
/* 线程1采用低优先级运行一直打印计数值 */
rt_kprintf("thread count: %d\n", count ++); rt_kprintf("thread count: %d\n", count ++);
} }
} }
/* 线程2的入口函数 */
static void thread2_entry(void* parameter) static void thread2_entry(void* parameter)
{ {
/* 线程2拥有较高的优先级以抢占线程1而获得执行 */
/* 线程2启动后先睡眠10个OS Tick */
rt_thread_delay(10); rt_thread_delay(10);
/*
* 线2线1线1线1线
*
*/
rt_thread_delete(tid1); rt_thread_delete(tid1);
/* delay thread2 to switch to idle thread */ /*
* 线210OS Tick然后退出线2idle线程
* idle线程将执行真正的线程1控制块和线程栈的删除
*/
rt_thread_delay(10); rt_thread_delay(10);
/*
* 线2(线线idle线
* )
*/
} }
/* 线程删除示例的初始化 */
int thread_delete_init() int thread_delete_init()
{ {
tid1 = rt_thread_create("t1", /* 创建线程1 */
thread1_entry, (void*)1, tid1 = rt_thread_create("t1", /* 线程1的名称是t1 */
thread1_entry, RT_NULL, /* 入口时thread1_entry参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL) if (tid1 != RT_NULL) /* 如果获得线程控制块,启动这个线程 */
rt_thread_startup(tid1); rt_thread_startup(tid1);
else else
tc_stat(TC_STAT_END | TC_STAT_FAILED); tc_stat(TC_STAT_END | TC_STAT_FAILED);
tid2 = rt_thread_create("t2", /* 创建线程1 */
thread2_entry, (void*)2, tid2 = rt_thread_create("t2", /* 线程1的名称是t2 */
thread2_entry, RT_NULL, /* 入口时thread2_entry参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE); THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
if (tid2 != RT_NULL) if (tid2 != RT_NULL) /* 如果获得线程控制块,启动这个线程 */
rt_thread_startup(tid2); rt_thread_startup(tid2);
else else
tc_stat(TC_STAT_END | TC_STAT_FAILED); tc_stat(TC_STAT_END | TC_STAT_FAILED);

View File

@ -0,0 +1,118 @@
/*
* 线
*
* 线线线
*/
#include <rtthread.h>
#include "tc_comm.h"
/* 线程1控制块 */
static struct rt_thread thread1;
/* 线程1栈 */
static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
/* 线程2控制块 */
static struct rt_thread thread2;
/* 线程2栈 */
static rt_uint8_t thread2_stack[THREAD_STACK_SIZE];
/* 线程1入口 */
static void thread1_entry(void* parameter)
{
rt_uint32_t count = 0;
while (1)
{
/* 线程1采用低优先级运行一直打印计数值 */
rt_kprintf("thread count: %d\n", count ++);
}
}
/* 线程2入口 */
static void thread2_entry(void* parameter)
{
/* 线程2拥有较高的优先级以抢占线程1而获得执行 */
/* 线程2启动后先睡眠10个OS Tick */
rt_thread_delay(10);
/*
* 线2线1线1线
*/
rt_thread_detach(&thread1);
/*
* 线210OS Tick然后退出
*/
rt_thread_delay(10);
/*
* 线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;
}
#ifdef RT_USING_TC
static void _tc_cleanup()
{
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical();
/* 执行线程脱离 */
if (thread1.stat != RT_THREAD_CLOSE)
rt_thread_detach(&thread1);
if (thread2.stat != RT_THREAD_CLOSE)
rt_thread_detach(&thread2);
/* 调度器解锁 */
rt_exit_critical();
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
}
int _tc_thread_detach()
{
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
thread_detach_init();
/* 返回TestCase运行的最长时间 */
return 100;
}
/* 输出函数命令到finsh shell中 */
FINSH_FUNCTION_EXPORT(_tc_thread_detach, a static thread example);
#else
/* 用户应用入口 */
int rt_application_init()
{
thread_detach_init();
return 0;
}
#endif

View File

@ -1,35 +1,45 @@
/*
* 线
*
* 2线
*/
#include <rtthread.h> #include <rtthread.h>
#include "tc_comm.h" #include "tc_comm.h"
/* /* 指向线程控制块的指针 */
* This is an example for dynamic thread static rt_thread_t tid1 = RT_NULL;
*/ static rt_thread_t tid2 = RT_NULL;
static rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL; /* 线程入口 */
static void thread_entry(void* parameter) static void thread_entry(void* parameter)
{ {
rt_uint32_t count = 0; rt_uint32_t count = 0;
rt_uint32_t no = (rt_uint32_t) parameter; rt_uint32_t no = (rt_uint32_t) parameter; /* 获得正确的入口参数 */
while (1) while (1)
{ {
/* 打印线程计数值输出 */
rt_kprintf("thread%d count: %d\n", no, count ++); rt_kprintf("thread%d count: %d\n", no, count ++);
/* 休眠10个OS Tick */
rt_thread_delay(10); rt_thread_delay(10);
} }
} }
int thread_dynamic_simple_init() int thread_dynamic_simple_init()
{ {
tid1 = rt_thread_create("t1", /* 创建线程1 */
thread_entry, (void*)1, tid1 = rt_thread_create("thread",
thread_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL) if (tid1 != RT_NULL)
rt_thread_startup(tid1); rt_thread_startup(tid1);
else else
tc_stat(TC_STAT_END | TC_STAT_FAILED); tc_stat(TC_STAT_END | TC_STAT_FAILED);
tid2 = rt_thread_create("t2", /* 创建线程2 */
thread_entry, (void*)2, tid2 = rt_thread_create("thread",
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE); thread_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid2 != RT_NULL) if (tid2 != RT_NULL)
rt_thread_startup(tid2); rt_thread_startup(tid2);
else else
@ -41,31 +51,35 @@ int thread_dynamic_simple_init()
#ifdef RT_USING_TC #ifdef RT_USING_TC
static void _tc_cleanup() static void _tc_cleanup()
{ {
/* lock scheduler */ /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical(); rt_enter_critical();
/* delete thread */ /* 删除线程 */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE) if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1); rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE) if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2); rt_thread_delete(tid2);
/* unlock scheduler */ /* 调度器解锁 */
rt_exit_critical(); rt_exit_critical();
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED); tc_done(TC_STAT_PASSED);
} }
int _tc_thread_dynamic_simple() int _tc_thread_dynamic_simple()
{ {
/* set tc cleanup */ /* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup); tc_cleanup(_tc_cleanup);
thread_dynamic_simple_init(); thread_dynamic_simple_init();
/* 返回TestCase运行的最长时间 */
return 100; return 100;
} }
/* 输出函数命令到finsh shell中 */
FINSH_FUNCTION_EXPORT(_tc_thread_dynamic_simple, a dynamic thread example); FINSH_FUNCTION_EXPORT(_tc_thread_dynamic_simple, a dynamic thread example);
#else #else
/* 用户应用入口 */
int rt_application_init() int rt_application_init()
{ {
thread_dynamic_simple_init(); thread_dynamic_simple_init();

View File

@ -0,0 +1,105 @@
/*
* 线
*
* 线线
* 线线
*/
#include <rtthread.h>
#include "tc_comm.h"
/* 指向线程控制块的指针 */
static rt_thread_t tid1 = RT_NULL;
static rt_thread_t tid2 = RT_NULL;
/* 线程1入口 */
static void thread1_entry(void* parameter)
{
/* 低优先级线程1开始运行 */
rt_kprintf("thread1 startup%d\n");
/* 挂起自身 */
rt_kprintf("suspend thread self\n");
rt_thread_suspend(tid1);
/* 主动执行线程调度 */
rt_schedule();
/* 当线程1被唤醒时 */
rt_kprintf("thread1 resumed\n");
}
/* 线程2入口 */
static void thread2_entry(void* parameter)
{
/* 延时10个OS Tick */
rt_thread_delay(10);
/* 唤醒线程1 */
rt_thread_resume(tid1);
/* 延时10个OS Tick */
rt_thread_delay(10);
/* 线程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)
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();
/* 删除线程 */
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();
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
}
int _tc_thread_resume()
{
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
thread_resume_init();
/* 返回TestCase运行的最长时间 */
return 100;
}
/* 输出函数命令到finsh shell中 */
FINSH_FUNCTION_EXPORT(_tc_thread_resume, a thread resume example);
#else
/* 用户应用入口 */
int rt_application_init()
{
thread_resume_init();
return 0;
}
#endif

View File

@ -1,78 +1,95 @@
/*
* 线
*
* 2线
*/
#include <rtthread.h> #include <rtthread.h>
#include "tc_comm.h" #include "tc_comm.h"
/* /* 线程1控制块 */
* This is an example for static thread
*/
static struct rt_thread thread1; static struct rt_thread thread1;
/* 线程1栈 */
static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
/* 线程2控制块 */
static struct rt_thread thread2; static struct rt_thread thread2;
static char thread1_stack[THREAD_STACK_SIZE]; /* 线程2栈 */
static char thread2_stack[THREAD_STACK_SIZE]; static rt_uint8_t thread2_stack[THREAD_STACK_SIZE];
/* 线程入口 */
static void thread_entry(void* parameter) static void thread_entry(void* parameter)
{ {
rt_uint32_t count = 0; rt_uint32_t count = 0;
rt_uint32_t no = (rt_uint32_t) parameter; rt_uint32_t no = (rt_uint32_t) parameter; /* 获得正确的入口参数 */
while (1) while (1)
{ {
/* 打印线程计数值输出 */
rt_kprintf("thread%d count: %d\n", no, count ++); rt_kprintf("thread%d count: %d\n", no, count ++);
/* 休眠10个OS Tick */
rt_thread_delay(10); rt_thread_delay(10);
} }
} }
rt_err_t thread_static_simple_init() int thread_static_simple_init()
{ {
rt_err_t result; rt_err_t result;
result = rt_thread_init(&thread1, /* 初始化线程1 */
"t1", result = rt_thread_init(&thread1, "t1", /* 线程名t1 */
thread_entry, (void*)1, thread_entry, (void*)1, /* 线程的入口是thread_entry入口参数是1 */
&thread1_stack[0], sizeof(thread1_stack), &thread1_stack[0], sizeof(thread1_stack), /* 线程栈是thread1_stack */
THREAD_PRIORITY, 10); THREAD_PRIORITY, 10);
if (result == RT_EOK) if (result == RT_EOK) /* 如果返回正确启动线程1 */
rt_thread_startup(&thread1); rt_thread_startup(&thread1);
else else
tc_stat(TC_STAT_END | TC_STAT_FAILED); tc_stat(TC_STAT_END | TC_STAT_FAILED);
result = rt_thread_init(&thread2, /* 初始化线程2 */
"t2", result = rt_thread_init(&thread2, "t2", /* 线程名t2 */
thread_entry, (void*)2, thread_entry, RT_NULL, /* 线程的入口是thread_entry入口参数是2 */
&thread2_stack[0], sizeof(thread2_stack), &thread2_stack[0], sizeof(thread2_stack), /* 线程栈是thread2_stack */
THREAD_PRIORITY + 1, 10); THREAD_PRIORITY + 1, 10);
if (result == RT_EOK) if (result == RT_EOK) /* 如果返回正确启动线程2 */
rt_thread_startup(&thread2); rt_thread_startup(&thread2);
else else
tc_stat(TC_STAT_END | TC_STAT_FAILED); tc_stat(TC_STAT_END | TC_STAT_FAILED);
return result; return 0;
} }
#ifdef RT_USING_TC #ifdef RT_USING_TC
static void _tc_cleanup() static void _tc_cleanup()
{ {
/* lock scheduler */ /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical(); rt_enter_critical();
/* 执行线程脱离 */
if (thread1.stat != RT_THREAD_CLOSE) if (thread1.stat != RT_THREAD_CLOSE)
rt_thread_detach(&thread1); rt_thread_detach(&thread1);
if (thread2.stat != RT_THREAD_CLOSE) if (thread2.stat != RT_THREAD_CLOSE)
rt_thread_detach(&thread2); rt_thread_detach(&thread2);
/* unlock scheduler */ /* 调度器解锁 */
rt_exit_critical(); rt_exit_critical();
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
} }
int _tc_thread_static_simple() int _tc_thread_static_simple()
{ {
/* set tc cleanup */ /* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup); tc_cleanup(_tc_cleanup);
thread_static_simple_init(); thread_static_simple_init();
return 20; /* 返回TestCase运行的最长时间 */
return 100;
} }
/* 输出函数命令到finsh shell中 */
FINSH_FUNCTION_EXPORT(_tc_thread_static_simple, a static thread example); FINSH_FUNCTION_EXPORT(_tc_thread_static_simple, a static thread example);
#else #else
/* 用户应用入口 */
int rt_application_init() int rt_application_init()
{ {
thread_static_simple_init(); thread_static_simple_init();
@ -80,4 +97,3 @@ int rt_application_init()
return 0; return 0;
} }
#endif #endif

View File

@ -0,0 +1,100 @@
/*
* 线
*
* 线线线
*/
#include <rtthread.h>
#include "tc_comm.h"
/* 指向线程控制块的指针 */
static rt_thread_t tid1 = RT_NULL;
static rt_thread_t 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 ++);
}
}
/* 线程2入口 */
static void thread2_entry(void* parameter)
{
/* 延时10个OS Tick */
rt_thread_delay(10);
/* 挂起线程1 */
rt_thread_suspend(tid1);
/* 延时10个OS Tick */
rt_thread_delay(10);
/* 线程2自动退出 */
}
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;
}
#ifdef RT_USING_TC
static void _tc_cleanup()
{
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
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);
/* 调度器解锁 */
rt_exit_critical();
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
}
int _tc_thread_suspend()
{
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
thread_suspend_init();
/* 返回TestCase运行的最长时间 */
return 100;
}
/* 输出函数命令到finsh shell中 */
FINSH_FUNCTION_EXPORT(_tc_thread_suspend, a thread suspend example);
#else
/* 用户应用入口 */
int rt_application_init()
{
thread_suspend_init();
return 0;
}
#endif

View File

@ -0,0 +1,101 @@
/*
*
*/
#include <rtthread.h>
#include "tc_comm.h"
/* 指向线程控制块的指针 */
static rt_thread_t tid1 = RT_NULL;
static rt_thread_t tid2 = RT_NULL;
/* 线程1入口 */
static void thread1_entry(void* parameter)
{
rt_uint32_t count = 0;
while (1)
{
/* 打印线程1的输出 */
rt_kprintf("thread1: count = %d\n", count ++);
/* 执行yield后应该切换到thread2执行 */
rt_thread_yield();
}
}
/* 线程2入口 */
static void thread2_entry(void* parameter)
{
rt_uint32_t count = 0;
while (1)
{
/* 打印线程2的输出 */
rt_kprintf("thread2: count = %d\n", count ++);
/* 执行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;
}
#ifdef RT_USING_TC
static void _tc_cleanup()
{
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
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);
/* 调度器解锁 */
rt_exit_critical();
/* 设置TestCase状态 */
tc_done(TC_STAT_PASSED);
}
int _tc_thread_yield()
{
/* 设置TestCase清理回调函数 */
tc_cleanup(_tc_cleanup);
thread_yield_init();
/* 返回TestCase运行的最长时间 */
return 100;
}
/* 输出函数命令到finsh shell中 */
FINSH_FUNCTION_EXPORT(_tc_thread_yield, a thread yield example);
#else
/* 用户应用入口 */
int rt_application_init()
{
thread_yield_init();
return 0;
}
#endif