OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [baremetal/] [threads_demo/] [main.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * Main function for this container
3
 */
4
#include <l4lib/macros.h>
5
#include L4LIB_INC_ARCH(syslib.h)
6
#include L4LIB_INC_ARCH(syscalls.h)
7
#include <l4/api/space.h>
8
#include <l4lib/thread/thread.h>
9
 
10
/* Symbolic constants */
11
#define STACK_SIZE      0x1000
12
#define NTHREADS        10
13
 
14
/* Stack and utcb region */
15
static char stack[NTHREADS * STACK_SIZE];
16
DECLARE_UTCB_SPACE(utcb, NTHREADS)
17
 
18
/* Function definitions */
19
static void init_thread_lib(void)
20
{
21
        /* Thread lib is informed about the stack region. */
22
        l4_set_stack_params((unsigned long)stack,
23
                                (unsigned long)(stack + sizeof(stack)),
24
                                STACK_SIZE);
25
 
26
        /* Thread lib is informed about the utcb region. */
27
        l4_set_utcb_params((unsigned long)utcb,
28
                                (unsigned long)(utcb + sizeof(utcb)));
29
 
30
        /* Now, we are ready to make calls to the library. */
31
}
32
 
33
static int do_some_work1(void *arg)
34
{
35
        struct task_ids ids;
36
        int value = *(int *)arg;
37
        int j;
38
 
39
        l4_getid(&ids);
40
        printf("tid = %d is called with the value of (%d).\n",
41
                __raw_tid(ids.tid), value);
42
 
43
        /* Wait for a while before exiting */
44
        j = 0x400000;
45
        while (--j)
46
                ;
47
 
48
        return ids.tid;
49
}
50
 
51
static int do_some_work2(void *arg)
52
{
53
        struct task_ids ids;
54
        int value = *(int *)arg;
55
        int j;
56
 
57
        l4_getid(&ids);
58
        printf("tid = %d is called with the value of (%d).\n",
59
                __raw_tid(ids.tid), value);
60
 
61
        /* Wait for a while before exiting */
62
        j = 0x400000;
63
        while (--j)
64
                ;
65
 
66
        l4_thread_exit(ids.tid);
67
 
68
        /* Should never reach here */
69
        return 0;
70
}
71
 
72
static int thread_demo(void)
73
{
74
        struct task_ids ids[NTHREADS];
75
        int arg[NTHREADS];
76
        int j;
77
 
78
        memset(ids, 0, sizeof(ids));
79
 
80
        /* Create threads. */
81
        for (int i = 0; i < NTHREADS; ++i) {
82
                /* The argument passed to the thread in question. */
83
                arg[i] = i;
84
 
85
                /* Threads are created. */
86
                if (i % 2)
87
                        l4_thread_create(&ids[i], TC_SHARE_SPACE | TC_SHARE_PAGER,
88
                                         do_some_work1, (void *)&arg[i]);
89
                else
90
                        l4_thread_create(&ids[i], TC_SHARE_SPACE | TC_SHARE_PAGER,
91
                                         do_some_work2, (void *)&arg[i]);
92
 
93
                /* Wait for a while before launching another thread. */
94
                j = 0x100000;
95
                while (--j)
96
                        ;
97
        }
98
 
99
        /* Wait for them to exit. */
100
        for (int i = 0; i < NTHREADS; ++i)
101
                printf("tid = %d exited with (%d).\n", __raw_tid(ids[i].tid),
102
                       l4_thread_control(THREAD_WAIT, &ids[i]));
103
 
104
        return 0;
105
}
106
 
107
int main(void)
108
{
109
        /* Before using the thread lib, we have to initialize it. */
110
        init_thread_lib();
111
 
112
        /* Demonstrates the usage of the thread lib. */
113
        thread_demo();
114
 
115
        return 0;
116
}
117
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.