]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/examples/libs/shmc/prodcons.c
update
[l4.git] / l4 / pkg / examples / libs / shmc / prodcons.c
1 /*
2  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>
3  *     economic rights: Technische Universität Dresden (Germany)
4  *
5  * This file is part of TUD:OS and distributed under the terms of the
6  * GNU General Public License 2.
7  * Please see the COPYING-GPL-2 file for details.
8  */
9
10 /*
11  * This example uses shared memory between two threads, one producer, one
12  * consumer.
13  */
14
15 #include <l4/shmc/shmc.h>
16
17 #include <l4/util/util.h>
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <pthread-l4.h>
22
23 #include <l4/sys/thread.h>
24
25 // a small helper
26 #define CHK(func) if (func) { printf("failure: %d\n", __LINE__); return (void *)-1; }
27
28 static const char some_data[] = "Hi consumer!";
29
30 static void *thread_producer(void *d)
31 {
32   (void)d;
33   l4shmc_chunk_t p_one;
34   l4shmc_signal_t s_one, s_done;
35   l4shmc_area_t shmarea;
36
37   // attach this thread to the shm object
38   CHK(l4shmc_attach("testshm", &shmarea));
39
40   // add a chunk
41   CHK(l4shmc_add_chunk(&shmarea, "one", 1024, &p_one));
42
43   // add a signal
44   CHK(l4shmc_add_signal(&shmarea, "prod", &s_one));
45
46   CHK(l4shmc_attach_signal_to(&shmarea, "done",
47                               pthread_getl4cap(pthread_self()), 10000, &s_done));
48
49   // connect chunk and signal
50   CHK(l4shmc_connect_chunk_signal(&p_one, &s_one));
51
52   printf("PRODUCER: ready\n");
53
54   while (1)
55     {
56       while (l4shmc_chunk_try_to_take(&p_one))
57         printf("Uh, should not happen!\n"); //l4_thread_yield();
58
59       memcpy(l4shmc_chunk_ptr(&p_one), some_data, sizeof(some_data));
60
61       CHK(l4shmc_chunk_ready_sig(&p_one, sizeof(some_data)));
62
63       printf("PRODUCER: Sent data\n");
64
65       CHK(l4shmc_wait_signal(&s_done));
66     }
67
68   l4_sleep_forever();
69   return NULL;
70 }
71
72
73 static void *thread_consume(void *d)
74 {
75   (void)d;
76   l4shmc_area_t shmarea;
77   l4shmc_chunk_t p_one;
78   l4shmc_signal_t s_one, s_done;
79
80   // attach to shared memory area
81   CHK(l4shmc_attach("testshm", &shmarea));
82
83   // get chunk 'one'
84   CHK(l4shmc_get_chunk(&shmarea, "one", &p_one));
85
86   // add a signal
87   CHK(l4shmc_add_signal(&shmarea, "done", &s_done));
88
89   // attach signal to this thread
90   CHK(l4shmc_attach_signal_to(&shmarea, "prod",
91                               pthread_getl4cap(pthread_self()), 10000, &s_one));
92
93   // connect chunk and signal
94   CHK(l4shmc_connect_chunk_signal(&p_one, &s_one));
95
96   while (1)
97     {
98       CHK(l4shmc_wait_chunk(&p_one));
99
100       printf("CONSUMER: Received from chunk one: %s\n",
101              (char *)l4shmc_chunk_ptr(&p_one));
102       memset(l4shmc_chunk_ptr(&p_one), 0, l4shmc_chunk_size(&p_one));
103
104       CHK(l4shmc_chunk_consumed(&p_one));
105       CHK(l4shmc_trigger(&s_done));
106     }
107
108   return NULL;
109 }
110
111
112 int main(void)
113 {
114   pthread_t one, two;
115
116   // create new shared memory area, 8K in size
117   if (l4shmc_create("testshm", 8192))
118     return 1;
119
120   // create two threads, one for producer, one for consumer
121   pthread_create(&one, 0, thread_producer, 0);
122   pthread_create(&two, 0, thread_consume, 0);
123
124   // now sleep, the two threads are doing the work
125   l4_sleep_forever();
126
127   return 0;
128 }