Annotation of prex/usr/test/thread/thread.c, Revision 1.1.1.1
1.1 nbrk 1: /*
2: * Copyright (c) 2005, Kohsuke Ohtani
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. Neither the name of the author nor the names of any co-contributors
14: * may be used to endorse or promote products derived from this software
15: * without specific prior written permission.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: */
29:
30: /*
31: * thread.c - test to run threads
32: */
33:
34: #include <prex/prex.h>
35: #include <stdio.h>
36:
37: static char stack[1024];
38:
39: static thread_t
40: thread_run(void (*start)(void), char *stack)
41: {
42: thread_t th;
43: int err;
44:
45: if ((err = thread_create(task_self(), &th)) != 0)
46: panic("thread_create() is failed");
47:
48: if ((err = thread_load(th, start, stack)) != 0)
49: panic("thread_load() is failed");
50:
51: if ((err = thread_resume(th)) != 0)
52: panic("thread_resume() is failed");
53:
54: return th;
55: }
56:
57: static void
58: test_thread(void)
59: {
60: printf("test thread is starting...\n");
61: for (;;)
62: putchar('@');
63: }
64:
65: int
66: main(int argc, char *argv[])
67: {
68: int err;
69: thread_t self, th;
70:
71: printf("Thread test program\n");
72:
73: self = thread_self();
74:
75: /*
76: * Create new thread
77: */
78: printf("Start test thread\n");
79: th = thread_run(test_thread, stack+1024);
80:
81: /*
82: * Wait 1 sec
83: */
84: timer_sleep(1000, 0);
85:
86: /*
87: * Suspend test thread
88: */
89: printf("\nSuspend test thread\n");
90: err = thread_suspend(th);
91:
92: /*
93: * Wait 2 sec
94: */
95: timer_sleep(2000, 0);
96:
97: /*
98: * Resume test thread
99: */
100: printf("\nResume test thread\n");
101: err = thread_resume(th);
102:
103: /*
104: * Wait 100 msec
105: */
106: timer_sleep(100, 0);
107:
108: /*
109: * Suspend test thread
110: */
111: thread_suspend(th);
112:
113: /*
114: * Wait 2 sec
115: */
116: timer_sleep(2000, 0);
117:
118: /*
119: * Resume test thread
120: */
121: thread_resume(th);
122:
123: /*
124: * We can check this thread can run 10 times than test thread,
125: */
126: for (;;)
127: putchar('!');
128:
129: return 0;
130: }
CVSweb