Statistics
| Branch: | Revision:

root / prex-0.9.0 / usr / test / ipc / ipc.c @ 03e9c04a

History | View | Annotate | Download (3.83 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.c - IPC test program.
32
 */
33

    
34
#include <sys/prex.h>
35
#include <ipc/ipc.h>
36
#include <stdio.h>
37

    
38
static char stack[1024];
39

    
40
/*
41
 * Run specified thread
42
 */
43
static int
44
thread_run(void (*start)(void), void *stack)
45
{
46
        thread_t t;
47
        int error;
48

    
49
        error = thread_create(task_self(), &t);
50
        if (error)
51
                return error;
52

    
53
        error = thread_load(t, start, stack);
54
        if (error)
55
                return error;
56

    
57
        error = thread_resume(t);
58
        if (error)
59
                return error;
60

    
61
        return 0;
62
}
63

    
64
/*
65
 * Send thread
66
 */
67
static void
68
send_thread(void)
69
{
70
        struct msg msg;
71
        object_t o1, o2;
72
        int error;
73

    
74
        printf("Send thread is starting...\n");
75

    
76
        /*
77
         * Find objects.
78
         */
79
        error = object_lookup("test-A", &o1);
80
        error = object_lookup("test-B", &o2);
81

    
82
        /*
83
         * Wait a sec.
84
         */
85
        timer_sleep(1000, 0);
86

    
87
        /*
88
         * Delete object A.
89
         */
90
        printf("Delete object A\n");
91
        object_destroy(o1);
92

    
93
        /*
94
         * Wait a sec.
95
         */
96
        timer_sleep(1000, 0);
97

    
98
        /*
99
         * Send message to object B.
100
         */
101
        printf("Send message to object B.\n");
102
        msg_send(o2, &msg, sizeof(msg));
103

    
104
        printf("Send completed.\n");
105
        for (;;) ;
106
}
107

    
108
int
109
main(int argc, char *argv[])
110
{
111
        object_t o1, o2, o3;
112
        struct msg msg;
113
        int error;
114

    
115
        printf("IPC test program\n");
116

    
117
        /*
118
         * Create two objects.
119
         */
120
        error = object_create("test-A", &o1);
121
        error = object_create("test-B", &o2);
122

    
123
        /*
124
         * Create existing object.
125
         * This must be error.
126
         */
127
        error = object_create("test-B", &o3);
128
        if (error == 0)
129
                panic("Oops! object exist...");
130

    
131
        /*
132
         * Start sender thread.
133
         */
134
        error = thread_run(send_thread, stack + 1024);
135
        if (error)
136
                panic("failed to run thread");
137

    
138
        /*
139
         * Wait message from non-existing object
140
         * This must be error.
141
         */
142
        o3 = 0x12345678;
143
        error = msg_receive(o3, &msg, sizeof(msg));
144
        if (error == 0)
145
                panic("Oops! invalid object...");
146

    
147
        /*
148
         * Wait message from object 'A'. However, it will be failed
149
         * because the sender thread will delete the object A.
150
         */
151
        printf("Wait message from object A\n");
152
        error = msg_receive(o1, &msg, sizeof(msg));
153
        if (error)
154
                printf("Receive error!\n");
155
        else {
156
                printf("Rreceive ok!\n");
157
                msg_reply(o1, &msg, sizeof(msg));
158
        }
159

    
160
        /*
161
         * Wait message from object 'B'.
162
         */
163
        printf("Wait message from object B\n");
164
        error = msg_receive(o2, &msg, sizeof(msg));
165
        if (error)
166
                printf("Receive error!\n");
167
        else {
168
                printf("Receive ok!\n");
169
                msg_reply(o2, &msg, sizeof(msg));
170
        }
171

    
172
        printf("Test completed...\n");
173
        return 0;
174
}