summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbernard.xiong <[email protected]>2010-03-22 09:17:38 +0000
committerbernard.xiong <[email protected]>2010-03-22 09:17:38 +0000
commit8d19f32ac25997d0045f5abfc2d7ff7ca9bd26ed (patch)
treeb038b1f5e9d7f4af06e98694f01494ef2a6a142a
parent000bba8cb1090968dd48cb05589d16c8ac697293 (diff)
add event, mailbox, message queue and timer simple examples.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@524 bbd45198-f89e-11dd-88c7-29a3b14d5316
-rw-r--r--examples/kernel/SConscript5
-rw-r--r--examples/kernel/event_simple.c144
-rw-r--r--examples/kernel/mbox_simple.c122
-rw-r--r--examples/kernel/memp_simple.c135
-rw-r--r--examples/kernel/messageq_simple.c148
-rw-r--r--examples/kernel/timer_simple.c66
6 files changed, 620 insertions, 0 deletions
diff --git a/examples/kernel/SConscript b/examples/kernel/SConscript
index e3325b3bfe..e8c73e9942 100644
--- a/examples/kernel/SConscript
+++ b/examples/kernel/SConscript
@@ -19,8 +19,13 @@ semaphore_priority.c
semaphore_buffer_worker.c
semaphore_producer_consumer.c
mutex_simple.c
+event_simple.c
+mbox_simple.c
+messageq_simple.c
+timer_simple.c
heap_malloc.c
heap_realloc.c
+memp_simple.c
""")
# The set of source files associated with this SConscript file.
diff --git a/examples/kernel/event_simple.c b/examples/kernel/event_simple.c
new file mode 100644
index 0000000000..c98d99a2ab
--- /dev/null
+++ b/examples/kernel/event_simple.c
@@ -0,0 +1,144 @@
+/*
+ * 程序清单:事件例程
+ *
+ * 这个程序会创建3个动态线程及初始化一个静态事件对象
+ * 一个线程等于事件对象上以接收事件;
+ * 一个线程定时发送事件 (事件3)
+ * 一个线程定时发送事件 (事件5)
+ */
+#include <rtthread.h>
+#include "tc_comm.h"
+
+/* 指向线程控制块的指针 */
+static rt_thread_t tid1 = RT_NULL;
+static rt_thread_t tid2 = RT_NULL;
+static rt_thread_t tid3 = RT_NULL;
+
+struct rt_event event;
+
+void thread1_entry(void *param)
+{
+ rt_uint32_t e;
+
+ while (1)
+ {
+ /* receive first event */
+ if (rt_event_recv(&event, ((1 << 3) | (1 << 5)),
+ RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
+ RT_WAITING_FOREVER, &e) == RT_EOK)
+ {
+ rt_kprintf("thread1: AND recv event 0x%x\n", e);
+ }
+
+ rt_kprintf("thread1: delay 1s to prepare second event\n");
+ rt_thread_delay(100);
+
+ /* receive second event */
+ if (rt_event_recv(&event, ((1 << 3) | (1 << 5)),
+ RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
+ RT_WAITING_FOREVER, &e) == RT_EOK)
+ {
+ rt_kprintf("thread1: OR recv event 0x%x\n", e);
+ }
+
+ rt_thread_delay(50);
+ }
+}
+
+void thread2_entry(void *param)
+{
+ while (1)
+ {
+ rt_kprintf("thread2: send event1\n");
+ rt_event_send(&event, (1 << 3));
+
+ rt_thread_delay(100);
+ }
+}
+
+void thread3_entry(void *param)
+{
+ while (1)
+ {
+ rt_kprintf("thread3: send event2\n");
+ rt_event_send(&event, (1 << 5));
+
+ rt_thread_delay(200);
+ }
+}
+
+int event_simple_init()
+{
+ rt_event_init(&event, "event", RT_IPC_FLAG_FIFO);
+
+ /* 创建线程1 */
+ tid1 = rt_thread_create("t1",
+ thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
+ THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
+ if (tid1 != RT_NULL)
+ rt_thread_startup(tid1);
+ else
+ tc_stat(TC_STAT_END | TC_STAT_FAILED);
+
+ /* 创建线程2 */
+ tid2 = rt_thread_create("t2",
+ thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
+ THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
+ if (tid2 != RT_NULL)
+ rt_thread_startup(tid2);
+ else
+ tc_stat(TC_STAT_END | TC_STAT_FAILED);
+
+ /* 创建线程3 */
+ tid3 = rt_thread_create("t3",
+ thread3_entry, RT_NULL, /* 线程入口是thread3_entry, 入口参数是RT_NULL */
+ THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
+ if (tid3 != RT_NULL)
+ rt_thread_startup(tid3);
+ else
+ tc_stat(TC_STAT_END | TC_STAT_FAILED);
+
+ 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);
+ if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
+ rt_thread_delete(tid3);
+
+ /* 调度器解锁 */
+ rt_exit_critical();
+
+ /* 设置TestCase状态 */
+ tc_done(TC_STAT_PASSED);
+}
+
+int _tc_event_simple()
+{
+ /* 设置TestCase清理回调函数 */
+ tc_cleanup(_tc_cleanup);
+ event_simple_init();
+
+ /* 返回TestCase运行的最长时间 */
+ return 100;
+}
+/* 输出函数命令到finsh shell中 */
+FINSH_FUNCTION_EXPORT(_tc_event_simple, a simple event example);
+#else
+/* 用户应用入口 */
+int rt_application_init()
+{
+ event_simple_init();
+
+ return 0;
+}
+#endif
diff --git a/examples/kernel/mbox_simple.c b/examples/kernel/mbox_simple.c
new file mode 100644
index 0000000000..5e176b043e
--- /dev/null
+++ b/examples/kernel/mbox_simple.c
@@ -0,0 +1,122 @@
+/*
+ * 程序清单:邮箱例程
+ *
+ * 这个程序会创建2个动态线程,一个静态的邮箱对象,其中一个线程往邮箱中发送邮件,
+ * 一个线程往邮箱中收取邮件。
+ */
+#include <rtthread.h>
+#include "tc_comm.h"
+
+/* 指向线程控制块的指针 */
+static rt_thread_t tid1 = RT_NULL;
+static rt_thread_t tid2 = RT_NULL;
+
+static struct rt_mailbox mb;
+static char mb_pool[128];
+static char mb_str1[] = "I'm a mail!";
+static char mb_str2[] = "this is another mail!";
+
+/* 线程1入口 */
+static void thread1_entry(void* parameter)
+{
+ unsigned char* str;
+
+ while (1)
+ {
+ rt_kprintf("thread1: try to recv a mail\n");
+ if (rt_mb_recv(&mb, (rt_uint32_t*)&str, RT_WAITING_FOREVER) == RT_EOK)
+ {
+ rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str);
+
+ rt_thread_delay(100);
+ }
+ }
+}
+
+/* 线程2入口 */
+static void thread2_entry(void* parameter)
+{
+ rt_uint8_t count;
+
+ count = 0;
+ while (1)
+ {
+ count ++;
+ if (count & 0x1)
+ {
+ rt_mb_send(&mb, (rt_uint32_t)&mb_str1[0]);
+ }
+ else
+ {
+ rt_mb_send(&mb, (rt_uint32_t)&mb_str2[0]);
+ }
+
+ rt_thread_delay(200);
+ }
+}
+
+int mbox_simple_init()
+{
+ /* 初始化一个mailbox */
+ rt_mb_init(&mb, "mbt", &mb_pool[0], 128 / 4, RT_IPC_FLAG_FIFO);
+
+ /* 创建线程1 */
+ tid1 = rt_thread_create("t1",
+ thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
+ THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
+ if (tid1 != RT_NULL)
+ rt_thread_startup(tid1);
+ else
+ tc_stat(TC_STAT_END | TC_STAT_FAILED);
+
+ /* 创建线程2 */
+ tid2 = rt_thread_create("t2",
+ thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
+ THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
+ if (tid2 != RT_NULL)
+ rt_thread_startup(tid2);
+ else
+ tc_stat(TC_STAT_END | TC_STAT_FAILED);
+
+ return 0;
+}
+
+#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_mbox_simple()
+{
+ /* 设置TestCase清理回调函数 */
+ tc_cleanup(_tc_cleanup);
+ mbox_simple_init();
+
+ /* 返回TestCase运行的最长时间 */
+ return 100;
+}
+/* 输出函数命令到finsh shell中 */
+FINSH_FUNCTION_EXPORT(_tc_mbox_simple, a simple mailbox example);
+#else
+/* 用户应用入口 */
+int rt_application_init()
+{
+ mbox_simple_init();
+
+ return 0;
+}
+#endif
diff --git a/examples/kernel/memp_simple.c b/examples/kernel/memp_simple.c
new file mode 100644
index 0000000000..705fde3c68
--- /dev/null
+++ b/examples/kernel/memp_simple.c
@@ -0,0 +1,135 @@
+/*
+ * 程序清单:内存池例程
+ *
+ * 这个程序会创建2个动态线程,一个静态的内存池对象,它们会试图分别从内存池中获得
+ * 内存块
+ */
+#include <rtthread.h>
+#include "tc_comm.h"
+
+static rt_uint8_t *ptr[48];
+static rt_uint8_t mempool[4096];
+static struct rt_mempool mp;
+
+/* 指向线程控制块的指针 */
+static rt_thread_t tid1 = RT_NULL;
+static rt_thread_t tid2 = RT_NULL;
+
+/* 线程1入口 */
+static void thread1_entry(void* parameter)
+{
+ int i;
+ char *block;
+
+ while(1)
+ {
+ for (i = 0; i < 48; i++)
+
+ {
+ rt_kprintf("allocate No.%d\n", i);
+ ptr[i] = rt_mp_alloc(&mp, RT_WAITING_FOREVER);
+ }
+
+ block = rt_mp_alloc(&mp, RT_WAITING_FOREVER);
+ rt_kprintf("allocate the block mem\n");
+ rt_mp_free(block);
+ block = RT_NULL;
+ }
+}
+
+/* 线程2入口 */
+static void thread2_entry(void *parameter)
+{
+ int i;
+
+ while(1)
+ {
+ rt_kprintf("try to release block\n");
+
+ for (i = 0 ; i < 48; i ++)
+ {
+ if (ptr[i] != RT_NULL)
+ {
+ rt_kprintf("release block %d\n", i);
+
+ rt_mp_free(ptr[i]);
+
+ ptr[i] = RT_NULL;
+
+ }
+ }
+
+ // rt_thread_delay(100);
+ }
+}
+
+int mempool_simple_init()
+{
+ int i;
+ for (i = 0; i < 48; i ++) ptr[i] = RT_NULL;
+
+ rt_mp_init(&mp, "mp1", &mempool[0], sizeof(mempool), 80);
+
+ /* 创建线程1 */
+ tid1 = rt_thread_create("t1",
+ thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
+ THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
+ if (tid1 != RT_NULL)
+ rt_thread_startup(tid1);
+ else
+ tc_stat(TC_STAT_END | TC_STAT_FAILED);
+
+ /* 创建线程2 */
+ tid2 = rt_thread_create("t2",
+ thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
+ THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
+ if (tid2 != RT_NULL)
+ rt_thread_startup(tid2);
+ else
+ tc_stat(TC_STAT_END | TC_STAT_FAILED);
+
+ return 0;
+}
+
+#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_mp_detach(&mp);
+
+ /* 调度器解锁 */
+ rt_exit_critical();
+
+ /* 设置TestCase状态 */
+ tc_done(TC_STAT_PASSED);
+}
+
+int _tc_mempool_simple()
+{
+ /* 设置TestCase清理回调函数 */
+ tc_cleanup(_tc_cleanup);
+ mempool_simple_init();
+
+ /* 返回TestCase运行的最长时间 */
+ return 100;
+}
+/* 输出函数命令到finsh shell中 */
+FINSH_FUNCTION_EXPORT(_tc_mempool_simple, a memory pool example);
+#else
+/* 用户应用入口 */
+int rt_application_init()
+{
+ mempool_simple_init();
+
+ return 0;
+}
+#endif
diff --git a/examples/kernel/messageq_simple.c b/examples/kernel/messageq_simple.c
new file mode 100644
index 0000000000..1e2298936a
--- /dev/null
+++ b/examples/kernel/messageq_simple.c
@@ -0,0 +1,148 @@
+/*
+ * 程序清单:动态线程
+ *
+ * 这个程序会初始化2个动态线程,它们拥有共同的入口函数,但参数不相同
+ */
+#include <rtthread.h>
+#include "tc_comm.h"
+
+/* 指向线程控制块的指针 */
+static rt_thread_t tid1 = RT_NULL;
+static rt_thread_t tid2 = RT_NULL;
+static rt_thread_t tid3 = RT_NULL;
+
+static struct rt_messagequeue mq;
+static char msg_pool[2048];
+
+static void thread1_entry(void* parameter)
+{
+ char buf[128];
+
+ while (1)
+ {
+ rt_memset(&buf[0], 0, sizeof(buf));
+
+ if (rt_mq_recv(&mq, &buf[0], sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
+ {
+ rt_kprintf("thread1: recv msg from message queue, the content:%s\n", buf);
+ }
+
+ rt_thread_delay(100);
+ }
+}
+
+static void thread2_entry(void* parameter)
+{
+ int i, result;
+ char buf[] = "this is message No.x";
+
+ while (1)
+ {
+ for (i = 0; i < 10; i++)
+ {
+ buf[sizeof(buf) - 2] = '0' + i;
+
+ rt_kprintf("thread2: send message - %s\n", buf);
+ result = rt_mq_send(&mq, &buf[0], sizeof(buf));
+ if ( result == -RT_EFULL);
+ {
+ rt_kprintf("message queue full, delay 10s\n");
+ rt_thread_delay(1000);
+ }
+ }
+
+ rt_thread_delay(100);
+ }
+}
+
+static void thread3_entry(void* parameter)
+{
+ char buf[] = "this is an urgent message!";
+
+ while (1)
+ {
+ rt_kprintf("thread3: send an urgent message\n");
+ rt_mq_urgent(&mq, &buf[0], sizeof(buf));
+
+ rt_thread_delay(250);
+ }
+}
+
+int messageq_simple_init()
+{
+ rt_mq_init(&mq, "mqt", &msg_pool[0], 128 - sizeof(void*), sizeof(msg_pool), RT_IPC_FLAG_FIFO);
+
+ /* 创建线程1 */
+ tid1 = rt_thread_create("t1",
+ thread1_entry, RT_NULL, /* 线程入口是thread1_entry, 入口参数是RT_NULL */
+ THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
+ if (tid1 != RT_NULL)
+ rt_thread_startup(tid1);
+ else
+ tc_stat(TC_STAT_END | TC_STAT_FAILED);
+
+ /* 创建线程2 */
+ tid2 = rt_thread_create("t2",
+ thread2_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
+ THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
+ if (tid2 != RT_NULL)
+ rt_thread_startup(tid2);
+ else
+ tc_stat(TC_STAT_END | TC_STAT_FAILED);
+
+ /* 创建线程3 */
+ tid3 = rt_thread_create("t3",
+ thread3_entry, RT_NULL, /* 线程入口是thread2_entry, 入口参数是RT_NULL */
+ THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
+ if (tid3 != RT_NULL)
+ rt_thread_startup(tid3);
+ else
+ tc_stat(TC_STAT_END | TC_STAT_FAILED);
+
+ 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);
+ if (tid3 != RT_NULL && tid3->stat != RT_THREAD_CLOSE)
+ rt_thread_delete(tid3);
+
+ /* 执行消息队列脱离 */
+ rt_mq_detach(&mq);
+
+ /* 调度器解锁 */
+ rt_exit_critical();
+
+ /* 设置TestCase状态 */
+ tc_done(TC_STAT_PASSED);
+}
+
+int _tc_messageq_simple()
+{
+ /* 设置TestCase清理回调函数 */
+ tc_cleanup(_tc_cleanup);
+ messageq_simple_init();
+
+ /* 返回TestCase运行的最长时间 */
+ return 100;
+}
+/* 输出函数命令到finsh shell中 */
+FINSH_FUNCTION_EXPORT(_tc_messageq_simple, a simple message queue example);
+#else
+/* 用户应用入口 */
+int rt_application_init()
+{
+ messageq_simple_init();
+
+ return 0;
+}
+#endif
diff --git a/examples/kernel/timer_simple.c b/examples/kernel/timer_simple.c
new file mode 100644
index 0000000000..30d9e63f59
--- /dev/null
+++ b/examples/kernel/timer_simple.c
@@ -0,0 +1,66 @@
+/*
+ * 程序清单:定时器例程
+ *
+ * 这个程序会初始化2个静态定时器,一个是一次定时,一个是周期性的定时
+ */
+#include <rtthread.h>
+#include "tc_comm.h"
+
+/* 定时器的控制块 */
+struct rt_timer timer1;
+struct rt_timer timer2;
+
+/* 定时器1超时函数 */
+void timeout1(void* parameter)
+{
+ rt_kprintf("periodic timer is timeout\n");
+}
+
+/* 定时器2超时函数 */
+void timeout2(void* parameter)
+{
+ rt_kprintf("one shot timer is timeout\n");
+}
+
+#ifdef RT_USING_TC
+static void _tc_cleanup()
+{
+ /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
+ rt_enter_critical();
+
+ /* 执行定时器脱离 */
+ rt_timer_detach(&timer1);
+ rt_timer_detach(&timer2);
+
+ /* 调度器解锁 */
+ rt_exit_critical();
+
+ /* 设置TestCase状态 */
+ tc_done(TC_STAT_PASSED);
+}
+
+int _tc_thread_timer_simple()
+{
+ /* 设置TestCase清理回调函数 */
+ tc_cleanup(_tc_cleanup);
+
+ rt_timer_init(&timer1, "timer1", timeout1, RT_NULL, 10, RT_TIMER_FLAG_PERIODIC);
+ rt_timer_init(&timer2, "timer2", timeout2, RT_NULL, 30, RT_TIMER_FLAG_ONE_SHOT);
+
+ rt_timer_start(&timer1);
+ rt_timer_start(&timer2);
+
+ /* 返回TestCase运行的最长时间 */
+ return 100;
+}
+/* 输出函数命令到finsh shell中 */
+FINSH_FUNCTION_EXPORT(_tc_thread_timer_simple, a simple timer example);
+#else
+/* 用户应用入口 */
+int rt_application_init()
+{
+ _tc_thread_timer_simple();
+
+ return 0;
+}
+#endif