3 * \brief Low-level example of communication.
5 * This example shows how two threads can exchange data using the L4 IPC
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)
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.
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>
28 static unsigned char stack2[8 << 10];
29 static l4_cap_idx_t thread1_cap, thread2_cap;
31 static void thread1(void)
33 l4_msg_regs_t *mr = l4_utcb_mr();
37 printf("Thread1 up (%p)\n", l4_utcb());
39 for (i = 0; i < 10; i++)
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");
49 static void thread2(void)
55 printf("Thread2 up (%p)\n", l4_utcb());
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]);
73 thread1_cap = l4re_env()->main_thread;
74 thread2_cap = l4re_util_cap_alloc();
76 if (l4_is_invalid_cap(thread2_cap))
79 tag = l4_factory_create_thread(l4re_env()->factory, thread2_cap);
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,
88 tag = l4_thread_control_commit(thread2_cap);
92 tag = l4_thread_ex_regs(thread2_cap,
94 (l4_umword_t)(stack2 + sizeof(stack2)), 0);
98 l4_sched_param_t sp = l4_sched_param(1, 0);
99 tag = l4_scheduler_run_thread(l4re_env()->scheduler, thread2_cap, &sp);