Simple shared memory example.
Simple shared memory example.
#include <l4/util/util.h>
#include <stdio.h>
#include <pthread-l4.h>
#include <l4/sys/thread.h>
#define LOG(args...) printf(NAME ": " args)
#define CHK(func) \
do \
{ \
long r = (func); \
if (r) \
{ \
printf(NAME ": Failure %ld (%s) at line %d.\n", \
r, l4sys_errtostr(r), __LINE__); \
return (void *)-1; \
} \
} while (0)
static const char some_data[] = "Hi consumer!";
static inline l4_cap_idx_t self(
void) {
return pthread_l4_cap(pthread_self()); }
#define NAME "PRODUCER"
static void *thread_producer(void *d)
{
(void)d;
l4shmc_chunk_t p_one;
l4shmc_signal_t s_one, s_done;
l4shmc_area_t shmarea;
for (;;)
{
if (clients == 3UL)
break;
{
LOG("consumer not initialized within time\n");
return (void *)-1;
}
}
LOG("Ready.\n");
while (1)
{
printf("Uh, should not happen!\n");
LOG("Sent data.\n");
}
return NULL;
}
#undef NAME
#define NAME "CONSUMER"
static void *thread_consumer(void *d)
{
(void)d;
l4shmc_area_t shmarea;
l4shmc_chunk_t p_one;
l4shmc_signal_t s_one, s_done;
for (;;)
{
if (clients == 3UL)
break;
{
LOG("producer not initialized within time\n");
return (void *)-1;
}
}
LOG("Ready.\n");
while (1)
{
LOG("Received from chunk one: '%s'.\n",
}
return NULL;
}
int main(void)
{
pthread_t one, two;
long r;
{
printf("Error %ld (%s) creating shared memory area\n",
r, l4sys_errtostr(r));
return 1;
}
pthread_create(&one, 0, thread_producer, 0);
pthread_create(&two, 0, thread_consumer, 0);
return 0;
}
Debugger related definitions.
l4_kernel_info_t const * l4re_kip(void) L4_NOTHROW
Get Kernel Info Page.
long l4shmc_connect_chunk_signal(l4shmc_chunk_t *chunk, l4shmc_signal_t *signal)
Connect a signal with a chunk.
long l4shmc_mark_client_initialized(l4shmc_area_t *shmarea)
Mark this shared memory client as 'initialized'.
long l4shmc_get_initialized_clients(l4shmc_area_t *shmarea, l4_umword_t *bitmask)
Fetch the _clients_init_done bitmask of the shared memory area.
long l4shmc_attach(char const *shmc_name, l4shmc_area_t *shmarea)
Attach to a shared memory area.
long l4shmc_create(char const *shmc_name)
Create a shared memory area.
long l4shmc_chunk_consumed(l4shmc_chunk_t *chunk)
Mark a chunk as free.
long l4shmc_wait_chunk(l4shmc_chunk_t *chunk)
Wait on a specific chunk.
long l4shmc_chunk_size(l4shmc_chunk_t const *chunk)
Get current size of a chunk.
long l4shmc_chunk_ready_sig(l4shmc_chunk_t *chunk, l4_umword_t size)
Mark chunk as filled (ready) and signal consumer.
long l4shmc_chunk_try_to_take(l4shmc_chunk_t *chunk)
Try to mark chunk busy.
long l4shmc_get_chunk(l4shmc_area_t *shmarea, char const *chunk_name, l4shmc_chunk_t *chunk)
Get chunk out of shared memory area.
long l4shmc_add_chunk(l4shmc_area_t *shmarea, char const *chunk_name, l4_umword_t chunk_capacity, l4shmc_chunk_t *chunk)
Add a chunk in the shared memory area.
void * l4shmc_chunk_ptr(l4shmc_chunk_t const *chunk)
Get data pointer to chunk.
long l4shmc_wait_signal(l4shmc_signal_t *signal)
Wait on a specific signal.
long l4shmc_trigger(l4shmc_signal_t *signal)
Trigger a signal.
long l4shmc_attach_signal(l4shmc_area_t *shmarea, char const *signal_name, l4_cap_idx_t thread, l4shmc_signal_t *signal)
Attach to signal.
long l4shmc_add_signal(l4shmc_area_t *shmarea, char const *signal_name, l4shmc_signal_t *signal)
Add a signal for the shared memory area.
unsigned long l4_umword_t
Unsigned machine word.
l4_uint64_t l4_kernel_clock_t
Kernel clock type.
unsigned long l4_cap_idx_t
Capability selector type.
l4_msgtag_t l4_debugger_set_object_name(l4_cap_idx_t cap, const char *name) L4_NOTHROW
Set the name of a kernel object.
l4_cpu_time_t l4_kip_clock(l4_kernel_info_t const *kip) L4_NOTHROW
Return clock value from the KIP.
void l4_sleep_forever(void) L4_NOTHROW L4_NORETURN
Go sleep and never wake up.
Shared memory library header file.
Kernel Info Page access functions.