]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/examples/sys/utcb-ipc/main.c
update
[l4.git] / l4 / pkg / examples / sys / utcb-ipc / main.c
1 /**
2  * \file
3  * \brief Low-level example of communication.
4  *
5  * This example shows how two threads can exchange data using the L4 IPC
6  * mechanism.
7  */
8 /*
9  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
10  *               Alexander Warg <warg@os.inf.tu-dresden.de>,
11  *               Björn Döbel <doebel@os.inf.tu-dresden.de>
12  *     economic rights: Technische Universität Dresden (Germany)
13  *
14  * This file is part of TUD:OS and distributed under the terms of the
15  * GNU General Public License 2.
16  * Please see the COPYING-GPL-2 file for details.
17  */
18 #include <l4/sys/ipc.h>
19 #include <l4/sys/thread.h>
20 #include <l4/sys/factory.h>
21 #include <l4/sys/utcb.h>
22 #include <l4/re/env.h>
23 #include <l4/re/c/util/cap_alloc.h>
24
25 #include <stdio.h>
26 #include <string.h>
27
28 static unsigned char stack2[8 << 10];
29 static l4_cap_idx_t thread1_cap, thread2_cap;
30
31 static void thread1(void)
32 {
33   l4_msg_regs_t *mr = l4_utcb_mr();
34   l4_msgtag_t tag;
35   int i, j;
36
37   printf("Thread1 up (%p)\n", l4_utcb());
38
39   for (i = 0; i < 10; i++)
40     {
41       for (j = 0; j < L4_UTCB_GENERIC_DATA_SIZE; j++)
42         mr->mr[j] = 'A' + (i + j) % ('~' - 'A' + 1);
43       tag = l4_msgtag(0, L4_UTCB_GENERIC_DATA_SIZE, 0, 0);
44       if (l4_msgtag_has_error(l4_ipc_send(thread2_cap, l4_utcb(), tag, L4_IPC_NEVER)))
45         printf("IPC-send error\n");
46     }
47 }
48
49 static void thread2(void)
50 {
51   l4_msgtag_t tag;
52   l4_msg_regs_t mr;
53   unsigned i;
54
55   printf("Thread2 up (%p)\n", l4_utcb());
56
57   while (1)
58     {
59       if (l4_msgtag_has_error(tag = l4_ipc_receive(thread1_cap, l4_utcb(), L4_IPC_NEVER)))
60         printf("IPC receive error\n");
61       memcpy(&mr, l4_utcb_mr(), sizeof(mr));
62       printf("Thread2 receive (%d): ", l4_msgtag_words(tag));
63       for (i = 0; i < l4_msgtag_words(tag); i++)
64         printf("%c", (char)mr.mr[i]);
65       printf("\n");
66     }
67 }
68
69 int main(void)
70 {
71   l4_msgtag_t tag;
72
73   thread1_cap = l4re_env()->main_thread;
74   thread2_cap = l4re_util_cap_alloc();
75
76   if (l4_is_invalid_cap(thread2_cap))
77     return 1;
78
79   tag = l4_factory_create_thread(l4re_env()->factory, thread2_cap);
80   if (l4_error(tag))
81     return 1;
82
83   l4_thread_control_start();
84   l4_thread_control_pager(l4re_env()->rm);
85   l4_thread_control_exc_handler(l4re_env()->rm);
86   l4_thread_control_bind((l4_utcb_t *)l4re_env()->first_free_utcb,
87                           L4RE_THIS_TASK_CAP);
88   tag = l4_thread_control_commit(thread2_cap);
89   if (l4_error(tag))
90     return 2;
91
92   tag = l4_thread_ex_regs(thread2_cap,
93                           (l4_umword_t)thread2,
94                           (l4_umword_t)(stack2 + sizeof(stack2)), 0);
95   if (l4_error(tag))
96     return 3;
97
98   l4_sched_param_t sp = l4_sched_param(1, 0);
99   tag = l4_scheduler_run_thread(l4re_env()->scheduler, thread2_cap, &sp);
100   if (l4_error(tag))
101     return 4;
102
103   thread1();
104
105   return 0;
106 }