scoutos / prex-0.9.0 / usr / test / ipc_mt / ipc_mt.c @ 03e9c04a
History | View | Annotate | Download (3.04 KB)
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 <sys/prex.h> |
35 |
#include <ipc/ipc.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 t; |
49 |
int error;
|
50 |
|
51 |
error = thread_create(task_self(), &t); |
52 |
if (error)
|
53 |
return error;
|
54 |
|
55 |
error = thread_load(t, start, stack); |
56 |
if (error)
|
57 |
return error;
|
58 |
|
59 |
error = thread_resume(t); |
60 |
if (error)
|
61 |
return error;
|
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 error;
|
76 |
|
77 |
printf("Receiver thread is starting...\n");
|
78 |
|
79 |
thread_setpri(thread_self(), 240);
|
80 |
|
81 |
/*
|
82 |
* Find objects.
|
83 |
*/
|
84 |
error = 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 error, i;
|
111 |
|
112 |
printf("IPC test for multi threads\n");
|
113 |
|
114 |
/*
|
115 |
* Create an object.
|
116 |
*/
|
117 |
error = object_create("test-A", &obj);
|
118 |
if (error)
|
119 |
panic("failed to create object");
|
120 |
|
121 |
/*
|
122 |
* Start receiver thread.
|
123 |
*/
|
124 |
for (i = 0; i < NR_THREADS; i++) { |
125 |
error = thread_run(receive_thread, stack[i] + 1024);
|
126 |
if (error)
|
127 |
panic("failed to run thread");
|
128 |
} |
129 |
printf("ok?\n");
|
130 |
thread_setpri(thread_self(), 241);
|
131 |
for (;;)
|
132 |
thread_yield(); |
133 |
return 0; |
134 |
} |