Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.89 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 - test to run threads
32
 */
33

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

    
37
static char stack[1024];
38

    
39
static thread_t
40
thread_run(void (*start)(void), char *stack)
41
{
42
        thread_t t;
43
        int error;
44

    
45
        if ((error = thread_create(task_self(), &t)) != 0)
46
                panic("thread_create() is failed");
47

    
48
        if ((error = thread_load(t, start, stack)) != 0)
49
                panic("thread_load() is failed");
50

    
51
        if ((error = thread_resume(t)) != 0)
52
                panic("thread_resume() is failed");
53

    
54
        return t;
55
}
56

    
57
static void
58
test_thread(void)
59
{
60
        printf("test thread is starting...\n");
61
        for (;;)
62
                putchar('@');
63
}
64

    
65
int
66
main(int argc, char *argv[])
67
{
68
        int error;
69
        thread_t self, t;
70

    
71
        printf("Thread test program\n");
72

    
73
        self = thread_self();
74

    
75
        /*
76
         * Create new thread
77
         */
78
        printf("Start test thread\n");
79
        t = thread_run(test_thread, stack+1024);
80

    
81
        /*
82
         * Wait 1 sec
83
         */
84
        timer_sleep(3000, 0);
85

    
86
        /*
87
         * Suspend test thread
88
         */
89
        printf("\nSuspend test thread\n");
90
        error = thread_suspend(t);
91

    
92
        /*
93
         * Wait 2 sec
94
         */
95
        timer_sleep(2000, 0);
96

    
97
        /*
98
         * Resume test thread
99
         */
100
        printf("\nResume test thread\n");
101
        error = thread_resume(t);
102

    
103
        /*
104
         * Wait 100 msec
105
         */
106
        timer_sleep(100, 0);
107

    
108
        /*
109
         * Suspend test thread
110
         */
111
        thread_suspend(t);
112

    
113
        /*
114
         * Wait 2 sec
115
         */
116
        timer_sleep(2000, 0);
117

    
118
        /*
119
         * Resume test thread
120
         */
121
        thread_resume(t);
122

    
123
        /*
124
         * We can check this thread can run 10 times than test thread,
125
         */
126
        for (;;)
127
                putchar('!');
128

    
129
        return 0;
130
}