Statistics
| Branch: | Revision:

scoutos / prex-0.9.0 / usr / sample / thread / thread.c @ 03e9c04a

History | View | Annotate | Download (3.57 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
 * thread.c - sample program to create three threads.
32
 */
33

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

    
37
#define VERBOSE_MODE
38

    
39
#ifdef VERBOSE_MODE
40
#define thr_printf(s) sys_log(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 t;
55

    
56
        if (thread_create(task_self(), &t) != 0)
57
                return -1;
58

    
59
        if (thread_load(t, start, stack) != 0)
60
                return -1;
61

    
62
        if (thread_setpri(t, 128) != 0)
63
                return -1;
64

    
65
        if (thread_resume(t) != 0)
66
                return -1;
67
        return 0;
68
}
69

    
70
/*
71
 * Display 'AAAA...'
72
 */
73
static void
74
thread_A(void)
75
{
76
        int i;
77

    
78
        thr_printf("\nthread A is starting\n");
79

    
80
        for (i = 0; i < 1024; i++) {
81
                thr_printf("A");
82
                if ((i & 0xff) == 0)
83
                        thread_yield();
84
        }
85
        thr_printf("\nthread A is terminated\n");
86
        thread_terminate(thread_self());
87
}
88

    
89
/*
90
 * Display 'BBBB...'
91
 */
92
static void
93
thread_B(void)
94
{
95
        int i;
96

    
97
        thr_printf("\nthread B is starting\n");
98

    
99
        for (i = 0; i < 4096; i++) {
100
                thr_printf("B");
101
                if ((i & 0xff) == 0)
102
                        thread_yield();
103
        }
104
        thr_printf("\nthread B is terminated\n");
105
        thread_terminate(thread_self());
106
}
107

    
108
/*
109
 * Display 'CCCC...'
110
 */
111
static void
112
thread_C(void)
113
{
114
        int i;
115

    
116
        thr_printf("\nthread C is starting\n");
117

    
118
        for (i = 0; i < 8192; i++) {
119
                thr_printf("C");
120
                if ((i & 0xff) == 0)
121
                        thread_yield();
122
        }
123
        thr_printf("\nthread C is terminated\n");
124
        thread_terminate(thread_self());
125
}
126

    
127
int
128
main(int argc, char *argv[])
129
{
130
        int error;
131

    
132
        printf("Thread sample program\n");
133
        main_th = thread_self();
134

    
135
        timer_sleep(1000, 0);
136

    
137
        /*
138
         * Raise this thread's priority.
139
         */
140
        error = thread_setpri(main_th, 100);
141

    
142
        /*
143
         * Run threads as normal priority thread.
144
         */
145
        thread_run(thread_A, stack[0]+1024);
146
        thread_run(thread_B, stack[1]+1024);
147
        thread_run(thread_C, stack[2]+1024);
148

    
149
        /*
150
         * Lower this thread's priority.
151
         */
152
        error = thread_setpri(main_th, 254);
153
        /*
154
         * Since other threads have higher priority than
155
         * this thread, the control will come here only when
156
         * all other threads are terminated.
157
         */
158
        printf("test - OK!\n");
159
        return 0;
160
}