L4Re - L4 Runtime Environment
examples/libs/shmc/prodcons.c

Simple shared memory example.

/*
* (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>
* economic rights: Technische Universit├Ąt Dresden (Germany)
*
* This file is part of TUD:OS and distributed under the terms of the
* GNU General Public License 2.
* Please see the COPYING-GPL-2 file for details.
*/
/*
* This example uses shared memory between two threads, one producer, one
* consumer.
*/
#include <l4/shmc/shmc.h>
#include <l4/util/util.h>
#include <stdio.h>
#include <string.h>
#include <pthread-l4.h>
#include <l4/sys/thread.h>
// a small helper
#define CHK(func) if (func) { printf("failure: %d\n", __LINE__); return (void *)-1; }
static const char some_data[] = "Hi consumer!";
static void *thread_producer(void *d)
{
(void)d;
l4shmc_chunk_t p_one;
l4shmc_signal_t s_one, s_done;
l4shmc_area_t shmarea;
// attach this thread to the shm object
CHK(l4shmc_attach("testshm", &shmarea));
// add a chunk
CHK(l4shmc_add_chunk(&shmarea, "one", 1024, &p_one));
// add a signal
CHK(l4shmc_add_signal(&shmarea, "prod", &s_one));
CHK(l4shmc_attach_signal_to(&shmarea, "done",
pthread_l4_cap(pthread_self()), 10000, &s_done));
// connect chunk and signal
CHK(l4shmc_connect_chunk_signal(&p_one, &s_one));
printf("PRODUCER: ready\n");
while (1)
{
while (l4shmc_chunk_try_to_take(&p_one))
printf("Uh, should not happen!\n"); //l4_thread_yield();
memcpy(l4shmc_chunk_ptr(&p_one), some_data, sizeof(some_data));
CHK(l4shmc_chunk_ready_sig(&p_one, sizeof(some_data)));
printf("PRODUCER: Sent data\n");
CHK(l4shmc_wait_signal(&s_done));
}
return NULL;
}
static void *thread_consume(void *d)
{
(void)d;
l4shmc_area_t shmarea;
l4shmc_chunk_t p_one;
l4shmc_signal_t s_one, s_done;
// attach to shared memory area
CHK(l4shmc_attach("testshm", &shmarea));
// get chunk 'one'
CHK(l4shmc_get_chunk(&shmarea, "one", &p_one));
// add a signal
CHK(l4shmc_add_signal(&shmarea, "done", &s_done));
// attach signal to this thread
CHK(l4shmc_attach_signal_to(&shmarea, "prod",
pthread_l4_cap(pthread_self()), 10000, &s_one));
// connect chunk and signal
CHK(l4shmc_connect_chunk_signal(&p_one, &s_one));
while (1)
{
CHK(l4shmc_wait_chunk(&p_one));
printf("CONSUMER: Received from chunk one: %s\n",
(char *)l4shmc_chunk_ptr(&p_one));
memset(l4shmc_chunk_ptr(&p_one), 0, l4shmc_chunk_size(&p_one));
CHK(l4shmc_chunk_consumed(&p_one));
CHK(l4shmc_trigger(&s_done));
}
return NULL;
}
int main(void)
{
pthread_t one, two;
// create new shared memory area, 8K in size
if (l4shmc_create("testshm", 8192))
return 1;
// create two threads, one for producer, one for consumer
pthread_create(&one, 0, thread_producer, 0);
pthread_create(&two, 0, thread_consume, 0);
// now sleep, the two threads are doing the work
return 0;
}