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

Annotation of prex-old/usr/sample/thread/thread.c, Revision 1.1.1.1.2.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 - sample program to create three threads.
                     32:  */
                     33:
                     34: #include <prex/prex.h>
                     35: #include <stdio.h>
                     36:
                     37: #define VERBOSE_MODE
                     38:
                     39: #ifdef VERBOSE_MODE
                     40: #define thr_printf(s) printf(s)
                     41: #else
                     42: #define thr_printf(s)
                     43: #endif
                     44:
                     45: static thread_t main_th;
                     46: static char stack[3][1024];
                     47:
                     48: /*
                     49:  * Run specified routine as new thread.
                     50:  */
                     51: static int
                     52: thread_run(void (*start)(void), void *stack)
                     53: {
                     54:        thread_t th;
                     55:
                     56:        if (thread_create(task_self(), &th) != 0)
                     57:                return -1;
                     58:
                     59:        if (thread_load(th, start, stack) != 0)
                     60:                return -1;
                     61:
                     62:        if (thread_resume(th) != 0)
                     63:                return -1;
                     64:        return 0;
                     65: }
                     66:
                     67: /*
                     68:  * Display 'AAAA...'
                     69:  */
                     70: static void
                     71: thread_A(void)
                     72: {
                     73:        int i;
                     74:
                     75:        thr_printf("\nthread A is starting\n");
                     76:
                     77:        for (i = 0; i < 1024; i++) {
                     78:                thr_printf("A");
                     79:                if ((i & 0xff) == 0)
                     80:                        thread_yield();
                     81:        }
                     82:        thr_printf("\nthread A is terminated\n");
                     83:        thread_terminate(thread_self());
                     84: }
                     85:
                     86: /*
                     87:  * Display 'BBBB...'
                     88:  */
                     89: static void
                     90: thread_B(void)
                     91: {
                     92:        int i;
                     93:
                     94:        thr_printf("\nthread B is starting\n");
                     95:
                     96:        for (i = 0; i < 4096; i++) {
                     97:                thr_printf("B");
                     98:                if ((i & 0xff) == 0)
                     99:                        thread_yield();
                    100:        }
                    101:        thr_printf("\nthread B is terminated\n");
                    102:        thread_terminate(thread_self());
                    103: }
                    104:
                    105: /*
                    106:  * Display 'CCCC...'
                    107:  */
                    108: static void
                    109: thread_C(void)
                    110: {
                    111:        int i;
                    112:
                    113:        thr_printf("\nthread C is starting\n");
                    114:
                    115:        for (i = 0; i < 8192; i++) {
                    116:                thr_printf("C");
                    117:                if ((i & 0xff) == 0)
                    118:                        thread_yield();
                    119:        }
                    120:        thr_printf("\nthread C is terminated\n");
                    121:        thread_terminate(thread_self());
                    122: }
                    123:
                    124: int
                    125: main(int argc, char *argv[])
                    126: {
                    127:        int err;
                    128:
                    129:        printf("Thread sample program\n");
                    130:        main_th = thread_self();
                    131:
                    132:        /*
                    133:         * Raise this thread's priority.
                    134:         */
                    135:        err = thread_setprio(main_th, 100);
1.1.1.1.2.1! nbrk      136:
1.1       nbrk      137:        /*
                    138:         * Run threads as normal priority thread.
                    139:         */
                    140:        thread_run(thread_A, stack[0]+1024);
                    141:        thread_run(thread_B, stack[1]+1024);
                    142:        thread_run(thread_C, stack[2]+1024);
1.1.1.1.2.1! nbrk      143:
1.1       nbrk      144:        /*
                    145:         * Lower this thread's priority.
                    146:         */
                    147:        err = thread_setprio(main_th, 254);
                    148:
                    149:        /*
                    150:         * Since other threads have higher priority than
                    151:         * this thread, the control will come here only when
                    152:         * all other threads are terminated.
                    153:         */
                    154:        printf("test - OK!\n");
                    155:        return 0;
                    156: }

CVSweb