Project

General

Profile

Statistics
| Branch: | Revision:

root / prex-0.9.0 / usr / sample / task / task.c @ 03e9c04a

History | View | Annotate | Download (2.61 KB)

1 03e9c04a Brad Neuman
/*
2
 * Copyright (c) 2005-2006, 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
 * task.c - a sample program to run tasks.
32
 */
33
34
#include <sys/prex.h>
35
#include <stdio.h>
36
#include <stdlib.h>
37
38
#define NR_TASKS 10
39
40
/*
41
 * Create new task
42
 */
43
static task_t
44
task_run(void (*func)(void))
45
{
46
        char *stack;
47
        task_t task;
48
        thread_t t;
49
50
        stack = malloc(1024);
51
        if (task_create(task_self(), VM_SHARE, &task) != 0)
52
                return 0;
53
54
        if (thread_create(task, &t) != 0)
55
                return 0;
56
57
        if (thread_load(t, func, stack + 1024) != 0)
58
                return 0;
59
60
        if (thread_resume(t) != 0)
61
                return 0;
62
63
        return task;
64
}
65
66
/*
67
 * Function to be called in new task.
68
 */
69
static void
70
hey_yo(void)
71
{
72
        task_t self;
73
74
        /*
75
         * Display string
76
         */
77
        self = task_self();
78
        printf("Task %x: Hey, Yo!\n", (u_int)self);
79
80
        /*
81
         * Wait 5 sec
82
         */
83
        timer_sleep(5000, 0);
84
85
        /*
86
         * Terminate current task.
87
         */
88
        printf("Task %x: Bye!\n", (u_int)self);
89
        task_terminate(task_self());
90
}
91
92
int
93
main(int argc, char *argv[])
94
{
95
        int i;
96
97
        printf("Task sample program\n");
98
99
        /*  Create new tasks */
100
        for (i = 0; i < NR_TASKS; i++) {
101
                if (task_run(hey_yo) == 0)
102
                        panic("failed to create task");
103
        }
104
105
        /* Wait here... */
106
        task_suspend(task_self());
107
        return 0;
108
}