Annotation of prex/usr/test/ipc_mt/ipc_mt.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: * ipc_mt.c - IPC test for multi threaded servers.
32: */
33:
34: #include <prex/prex.h>
35: #include <server/stdmsg.h>
36: #include <stdio.h>
37:
38: #define NR_THREADS 5
39:
40: static char stack[NR_THREADS][1024];
41:
42: /*
43: * Run specified thread
44: */
45: static int
46: thread_run(void (*start)(void), void *stack)
47: {
48: thread_t th;
49: int err;
50:
51: err = thread_create(task_self(), &th);
52: if (err)
53: return err;
54:
55: err = thread_load(th, start, stack);
56: if (err)
57: return err;
58:
59: err = thread_resume(th);
60: if (err)
61: return err;
62:
63:
64: return 0;
65: }
66:
67: /*
68: * Receiver thread
69: */
70: static void
71: receive_thread(void)
72: {
73: struct msg msg;
74: object_t obj;
75: int err;
76:
77: printf("Receiver thread is starting...\n");
78:
79: thread_setprio(thread_self(), 240);
80:
81: /*
82: * Find objects.
83: */
84: err = object_lookup("/test/A", &obj);
85:
86: for (;;) {
87: /*
88: * Receive message from object.
89: */
90: printf("Wait message.\n");
91: msg_receive(obj, &msg, sizeof(msg));
92:
93: printf("Message received.\n");
94: /*
95: * Wait a sec.
96: */
97: timer_sleep(1000, 0);
98:
99: printf("Reply message.\n");
100: msg_reply(obj, &msg, sizeof(msg));
101:
102: for (;;);
103: }
104: }
105:
106: int
107: main(int argc, char *argv[])
108: {
109: object_t obj;
110: int err, i;
111:
112: printf("IPC test for multi threads\n");
113:
114: /*
115: * Create an object.
116: */
117: err = object_create("/test/A", &obj);
118: if (err)
119: panic("failed to create object");
120:
121: /*
122: * Start receiver thread.
123: */
124: for (i = 0; i < NR_THREADS; i++) {
125: err = thread_run(receive_thread, stack[i] + 1024);
126: if (err)
127: panic("failed to run thread");
128: }
129: printf("ok?\n");
130: thread_setprio(thread_self(), 241);
131: for (;;) ;
132: return 0;
133: }
CVSweb