summaryrefslogtreecommitdiff
path: root/examples/kernel/semaphore_static.c
diff options
context:
space:
mode:
authorbernard.xiong <[email protected]>2010-03-17 09:57:07 +0000
committerbernard.xiong <[email protected]>2010-03-17 09:57:07 +0000
commit2d54fcc40581c348959e50a853ef7d4c0478974f (patch)
tree34028e9e3a19c6f3f1755b9ffdf17724f0f5f0a6 /examples/kernel/semaphore_static.c
parent824dc0b00e6d390f78de43d806bebb170635cda5 (diff)
add heap and semaphore examples.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@501 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'examples/kernel/semaphore_static.c')
-rw-r--r--examples/kernel/semaphore_static.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/examples/kernel/semaphore_static.c b/examples/kernel/semaphore_static.c
new file mode 100644
index 0000000000..3b5cda3038
--- /dev/null
+++ b/examples/kernel/semaphore_static.c
@@ -0,0 +1,102 @@
+#include <rtthread.h>
+#include "tc_comm.h"
+
+static struct rt_semaphore sem;
+struct rt_thread thread;
+static char thread_stack[THREAD_STACK_SIZE];
+static void thread_entry(void* parameter)
+{
+ rt_err_t result;
+ rt_tick_t tick;
+
+ /* get current tick */
+ tick = rt_tick_get();
+
+ /* take a semaphore for 10 OS Tick */
+ result = rt_sem_take(&sem, 10);
+ if (result == -RT_ETIMEOUT)
+ {
+ if (rt_tick_get() - tick != 10)
+ {
+ tc_done(TC_STAT_FAILED);
+ rt_sem_detach(&sem);
+ return;
+ }
+ rt_kprintf("take semaphore timeout");
+ }
+ else
+ {
+ tc_done(TC_STAT_FAILED);
+ rt_sem_detach(&sem);
+ return;
+ }
+
+ /* release semaphore one time */
+ 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;
+ }
+
+ /* testcase passed */
+ tc_done(TC_STAT_PASSED);
+ /* detach semaphore */
+ rt_sem_detach(&sem);
+}
+
+int semaphore_static_init()
+{
+ rt_err_t result;
+
+ 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;
+ }
+
+ result = rt_thread_init(&thread, "test",
+ thread_entry, RT_NULL,
+ thread_stack, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
+ if (result == RT_EOK)
+ rt_thread_startup(&thread);
+ else
+ tc_stat(TC_STAT_END | TC_STAT_FAILED);
+
+ return 0;
+}
+
+#ifdef RT_USING_TC
+static void _tc_cleanup()
+{
+ /* lock scheduler */
+ rt_enter_critical();
+
+ if (thread.stat != RT_THREAD_CLOSE)
+ rt_thread_detach(&thread);
+
+ /* unlock scheduler */
+ rt_exit_critical();
+}
+
+int _tc_semaphore_static()
+{
+ /* set tc cleanup */
+ tc_cleanup(_tc_cleanup);
+ semaphore_static_init();
+
+ return 30;
+}
+FINSH_FUNCTION_EXPORT(_tc_semaphore_static, a static semaphore test);
+#else
+int rt_application_init()
+{
+ semaphore_static_init();
+
+ return 0;
+}
+#endif