[BACK]Return to ipc.c CVS log [TXT][DIR] Up to [local] / prex-old / usr / test / ipc

Annotation of prex-old/usr/test/ipc/ipc.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.c - IPC test program.
                     32:  */
                     33:
                     34: #include <prex/prex.h>
                     35: #include <server/stdmsg.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 th;
                     47:        int err;
                     48:
                     49:        err = thread_create(task_self(), &th);
                     50:        if (err)
                     51:                return err;
                     52:
                     53:        err = thread_load(th, start, stack);
                     54:        if (err)
                     55:                return err;
                     56:
                     57:        err = thread_resume(th);
                     58:        if (err)
                     59:                return err;
                     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 err;
                     73:
                     74:        printf("Send thread is starting...\n");
                     75:
                     76:        /*
                     77:         * Find objects.
                     78:         */
                     79:        err = object_lookup("/test/A", &o1);
                     80:        err = 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 err;
                    114:
                    115:        printf("IPC test program\n");
                    116:
                    117:        /*
                    118:         * Create two objects.
                    119:         */
                    120:        err = object_create("/test/A", &o1);
                    121:        err = object_create("/test/B", &o2);
                    122:
                    123:        /*
                    124:         * Create existing object.
                    125:         * This must be error.
                    126:         */
                    127:        err = object_create("/test/B", &o3);
                    128:        if (err == 0)
                    129:                panic("Oops! object exist...");
                    130:
                    131:        /*
                    132:         * Start sender thread.
                    133:         */
                    134:        err = thread_run(send_thread, stack + 1024);
                    135:        if (err)
                    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:        err = msg_receive(o3, &msg, sizeof(msg));
                    144:        if (err == 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:        err = msg_receive(o1, &msg, sizeof(msg));
                    153:        if (err)
                    154:                printf("Receive err!\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:        err = msg_receive(o2, &msg, sizeof(msg));
                    165:        if (err)
                    166:                printf("Receive err!\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: }

CVSweb