3 * Shared memory library header file.
6 * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
7 * Björn Döbel <doebel@os.inf.tu-dresden.de>
8 * economic rights: Technische Universität Dresden (Germany)
9 * This file is part of TUD:OS and distributed under the terms of the
10 * GNU Lesser General Public License 2.1.
11 * Please see the COPYING-LGPL-2.1 file for details.
15 #include <l4/sys/linkage.h>
16 #include <l4/sys/types.h>
17 #include <l4/sys/err.h>
20 * \defgroup api_l4shm Shared Memory Library
22 * L4SHM provides a shared memory infrastructure that establishes a
23 * shared memory area between multiple parties and uses a fast notification
26 * A shared memory area consists of chunks and signals. A chunk is a
27 * defined chunk of memory within the memory area with a maximum size. A
28 * chunk is filled (written) by a producer and read by a consumer. When a
29 * producer has finished writing to the chunk it signals a data ready
30 * notification to the consumer.
32 * A consumer attaches to a chunk and waits for the producer to fill the
33 * chunk. After reading out the chunk it marks the chunk free again.
35 * A shared memory area can have multiple chunks.
37 * The interface is divided in three roles.
38 * - The master role, responsible for setting up a shared memory area.
39 * - A producer, generating data into a chunk
40 * - A consumer, receiving data.
42 * A signal can be connected with a chunk or can be used independently
43 * (e.g. for multiple chunks).
45 * \example examples/libs/shmc/prodcons.c
46 * Simple shared memory example.
50 * \defgroup api_l4shmc_chunk Chunks
53 * \defgroup api_l4shmc_chunk_prod Producer
54 * \ingroup api_l4shmc_chunk
56 * \defgroup api_l4shmc_chunk_cons Consumer
57 * \ingroup api_l4shmc_chunk
59 * \defgroup api_l4shmc_signal Signals
62 * \defgroup api_l4shmc_signal_prod Producer
63 * \ingroup api_l4shmc_signal
65 * \defgroup api_l4shmc_signal_cons Consumer
66 * \ingroup api_l4shmc_signal
69 #define __INCLUDED_FROM_L4SHMC_H__
70 #include <l4/shmc/types.h>
75 * Create a shared memory area.
78 * \param shmc_name Name of the shared memory area.
79 * \param shm_size Size of the whole shared memory area.
85 l4shmc_create(const char *shmc_name, l4_umword_t shm_size);
88 * Attach to a shared memory area.
91 * \param shmc_name Name of the shared memory area.
92 * \param[out] shmarea Pointer to shared memory area descriptor to be filled
93 * with information for the shared memory area.
99 l4shmc_attach(const char *shmc_name, l4shmc_area_t *shmarea);
102 * Attach to a shared memory area, with limited waiting.
105 * \param shmc_name Name of the shared memory area.
106 * \param timeout_ms Timeout to wait for shm area in milliseconds.
107 * \param[out] shmarea Pointer to shared memory area descriptor to be
108 * filled with information for the shared memory area.
114 l4shmc_attach_to(const char *shmc_name, l4_umword_t timeout_ms,
115 l4shmc_area_t *shmarea);
118 * Add a chunk in the shared memory area.
119 * \ingroup api_l4shmc_chunk
121 * \param shmarea The shared memory area to put the chunk in.
122 * \param chunk_name Name of the chunk.
123 * \param chunk_capacity Capacity for payload of the chunk in bytes.
124 * \param[out] chunk Chunk structure to fill in.
130 l4shmc_add_chunk(l4shmc_area_t *shmarea,
131 const char *chunk_name,
132 l4_umword_t chunk_capacity,
133 l4shmc_chunk_t *chunk);
136 * Add a signal for the shared memory area.
137 * \ingroup api_l4shmc_signal
139 * \param shmarea The shared memory area to put the chunk in.
140 * \param signal_name Name of the signal.
141 * \param[out] signal Signal structure to fill in.
147 l4shmc_add_signal(l4shmc_area_t *shmarea,
148 const char *signal_name,
149 l4shmc_signal_t *signal);
153 * \ingroup api_l4shmc_signal_prod
155 * \param signal Signal to trigger.
161 l4shmc_trigger(l4shmc_signal_t *signal);
164 * Try to mark chunk busy.
165 * \ingroup api_l4shmc_chunk_prod
167 * \param chunk chunk to mark.
169 * \retval 0 Chunk could be taken.
170 * \retval <0 Chunk could not be taken, try again.
173 l4shmc_chunk_try_to_take(l4shmc_chunk_t *chunk);
176 * Mark chunk as filled (ready).
177 * \ingroup api_l4shmc_chunk_prod
179 * \param chunk chunk.
180 * \param size Size of data in the chunk, in bytes.
186 l4shmc_chunk_ready(l4shmc_chunk_t *chunk, l4_umword_t size);
189 * Mark chunk as filled (ready) and signal consumer.
190 * \ingroup api_l4shmc_chunk_prod
192 * \param chunk chunk.
193 * \param size Size of data in the chunk, in bytes.
199 l4shmc_chunk_ready_sig(l4shmc_chunk_t *chunk, l4_umword_t size);
202 * Get chunk out of shared memory area.
203 * \ingroup api_l4shmc_chunk
205 * \param shmarea Shared memory area.
206 * \param chunk_name Name of the chunk.
207 * \param[out] chunk Chunk data structure to fill.
213 l4shmc_get_chunk(l4shmc_area_t *shmarea,
214 const char *chunk_name,
215 l4shmc_chunk_t *chunk);
218 * Get chunk out of shared memory area, with timeout.
219 * \ingroup api_l4shmc_chunk
221 * \param shmarea Shared memory area.
222 * \param chunk_name Name of the chunk.
223 * \param timeout_ms Timeout in milliseconds to wait for the chunk to appear
224 * in the shared memory area.
225 * \param[out] chunk Chunk data structure to fill.
231 l4shmc_get_chunk_to(l4shmc_area_t *shmarea,
232 const char *chunk_name,
233 l4_umword_t timeout_ms,
234 l4shmc_chunk_t *chunk);
237 * Iterate over names of all existing chunks
238 * \ingroup api_l4shmc_chunk
240 * \param shmarea Shared memory area.
241 * \param chunk_name Where the name of the current chunk will be stored
242 * \param offs 0 to start iteration, return value of previous
243 * call to l4shmc_iterate_chunk() to get next chunk
245 * \retval 0 No more chunks available.
247 * \retval >0 Iterator value for the next call.
250 l4shmc_iterate_chunk(l4shmc_area_t *shmarea, const char **chunk_name,
255 * \ingroup api_l4shmc_signal
257 * \param shmarea Shared memory area.
258 * \param signal_name Name of the signal.
259 * \param thread Thread capability index to attach the signal to.
260 * \param[out] signal Signal data structure to fill.
266 l4shmc_attach_signal(l4shmc_area_t *shmarea,
267 const char *signal_name,
269 l4shmc_signal_t *signal);
272 * Attach to signal, with timeout.
273 * \ingroup api_l4shmc_signal
275 * \param shmarea Shared memory area.
276 * \param signal_name Name of the signal.
277 * \param thread Thread capability index to attach the signal to.
278 * \param timeout_ms Timeout in milliseconds to wait for the chunk to appear
279 * in the shared memory area.
280 * \param[out] signal Signal data structure to fill.
286 l4shmc_attach_signal_to(l4shmc_area_t *shmarea,
287 const char *signal_name,
289 l4_umword_t timeout_ms,
290 l4shmc_signal_t *signal);
293 * Get signal object from the shared memory area.
294 * \ingroup api_l4shmc_signal
296 * \param shmarea Shared memory area.
297 * \param signal_name Name of the signal.
298 * \param timeout_ms Timeout in milliseconds to wait for signal of a chunk
299 * to appear in the shared memory area.
300 * \param[out] signal Signal data structure to fill.
306 l4shmc_get_signal_to(l4shmc_area_t *shmarea,
307 const char *signal_name,
308 l4_umword_t timeout_ms,
309 l4shmc_signal_t *signal);
312 l4shmc_get_signal(l4shmc_area_t *shmarea,
313 const char *signal_name,
314 l4shmc_signal_t *signal);
319 * \ingroup api_l4shmc_signal_cons
321 * \param signal Signal to enable.
326 * A signal must be enabled before waiting when the consumer waits on any
327 * signal. Enabling is not needed if the consumer waits for a specific
331 l4shmc_enable_signal(l4shmc_signal_t *signal);
334 * Enable a signal connected with a chunk.
335 * \ingroup api_l4shmc_chunk_cons
337 * \param chunk Chunk to enable.
342 * A signal must be enabled before waiting when the consumer waits on any
343 * signal. Enabling is not needed if the consumer waits for a specific
347 l4shmc_enable_chunk(l4shmc_chunk_t *chunk);
350 * Wait on any signal.
351 * \ingroup api_l4shmc_signal_cons
353 * \param[out] retsignal Signal received.
359 l4shmc_wait_any(l4shmc_signal_t **retsignal);
362 * Check whether any waited signal has an event pending.
363 * \ingroup api_l4shmc_signal_cons
365 * \param[out] retsignal Signal that has the event pending if any.
370 * The return code indicates whether an event was pending or not. Success
371 * means an event was pending, if an receive timeout error is returned no
375 l4shmc_wait_any_try(l4shmc_signal_t **retsignal);
378 * Wait for any signal with timeout.
379 * \ingroup api_l4shmc_signal_cons
381 * \param timeout Timeout.
382 * \param[out] retsignal Signal that has the event pending if any.
387 * The return code indicates whether an event was pending or not. Success
388 * means an event was pending, if an receive timeout error is returned no
392 l4shmc_wait_any_to(l4_timeout_t timeout, l4shmc_signal_t **retsignal);
395 * Wait on a specific signal.
396 * \ingroup api_l4shmc_signal_cons
398 * \param signal Signal to wait for.
404 l4shmc_wait_signal(l4shmc_signal_t *signal);
407 * Wait on a specific signal, with timeout.
408 * \ingroup api_l4shmc_signal_cons
410 * \param signal Signal to wait for.
411 * \param timeout Timeout.
417 l4shmc_wait_signal_to(l4shmc_signal_t *signal, l4_timeout_t timeout);
420 * Check whether a specific signal has an event pending.
421 * \ingroup api_l4shmc_signal_cons
423 * \param signal Signal to check.
428 * The return code indicates whether an event was pending or not. Success
429 * means an event was pending, if an receive timeout error is returned no
433 l4shmc_wait_signal_try(l4shmc_signal_t *signal);
436 * Wait on a specific chunk.
437 * \ingroup api_l4shmc_chunk_cons
439 * \param chunk Chunk to wait for.
445 l4shmc_wait_chunk(l4shmc_chunk_t *chunk);
448 * Check whether a specific chunk has an event pending, with timeout.
449 * \ingroup api_l4shmc_chunk_cons
451 * \param chunk Chunk to check.
452 * \param timeout Timeout.
457 * The return code indicates whether an event was pending or not. Success
458 * means an event was pending, if an receive timeout error is returned no
462 l4shmc_wait_chunk_to(l4shmc_chunk_t *chunk, l4_timeout_t timeout);
465 * Check whether a specific chunk has an event pending.
466 * \ingroup api_l4shmc_chunk_cons
468 * \param chunk Chunk to check.
473 * The return code indicates whether an event was pending or not. Success
474 * means an event was pending, if an receive timeout error is returned no
478 l4shmc_wait_chunk_try(l4shmc_chunk_t *chunk);
481 * Mark a chunk as free.
482 * \ingroup api_l4shmc_chunk_cons
484 * \param chunk Chunk to mark as free.
490 l4shmc_chunk_consumed(l4shmc_chunk_t *chunk);
493 * Connect a signal with a chunk.
496 * \param chunk Chunk to attach the signal to.
497 * \param signal Signal to attach.
499 * \return 0 on success, <0 on error
502 l4shmc_connect_chunk_signal(l4shmc_chunk_t *chunk,
503 l4shmc_signal_t *signal);
506 * Check whether data is available.
507 * \ingroup api_l4shmc_chunk_cons
509 * \param chunk Chunk to check.
515 l4shmc_is_chunk_ready(l4shmc_chunk_t *chunk);
518 * Check whether chunk is free.
519 * \ingroup api_l4shmc_chunk_prod
521 * \param chunk Chunk to check.
527 l4shmc_is_chunk_clear(l4shmc_chunk_t *chunk);
530 * Get data pointer to chunk.
531 * \ingroup api_l4shmc_chunk
533 * \param chunk Chunk.
538 L4_CV L4_INLINE void *
539 l4shmc_chunk_ptr(l4shmc_chunk_t *chunk);
542 * Get current size of a chunk.
543 * \ingroup api_l4shmc_chunk_cons
545 * \param chunk Chunk.
551 l4shmc_chunk_size(l4shmc_chunk_t *chunk);
554 * Get capacity of a chunk.
555 * \ingroup api_l4shmc_chunk
557 * \param chunk Chunk.
563 l4shmc_chunk_capacity(l4shmc_chunk_t *chunk);
566 * Get the signal of a chunk.
567 * \ingroup api_l4shmc_chunk
569 * \param chunk Chunk.
571 * \retval 0 No signal has been registered with this chunk.
572 * \retval >0 Pointer to signal otherwise.
574 L4_CV L4_INLINE l4shmc_signal_t *
575 l4shmc_chunk_signal(l4shmc_chunk_t *chunk);
578 * Get the signal capability of a signal.
579 * \ingroup api_l4shmc_signal
581 * \param signal Signal.
583 * \return Capability of the signal object.
585 L4_CV L4_INLINE l4_cap_idx_t
586 l4shmc_signal_cap(l4shmc_signal_t *signal);
589 * Check magic value of a chunk.
590 * \ingroup api_l4shmc_signal
592 * \param chunk Chunk.
594 * \retval 0 Magic value is not valid.
595 * \retval >0 Chunk is ok, the magic value is valid.
598 l4shmc_check_magic(l4shmc_chunk_t *chunk);
601 * Get size of shared memory area.
604 * \param shmarea Shared memory area.
606 * \retval >0 Size of the shared memory area.
610 l4shmc_area_size(l4shmc_area_t *shmarea);
613 * Get free size of shared memory area. To get the max size to
614 * pass to l4shmc_add_chunk, substract l4shmc_chunk_overhead().
617 * \param shmarea Shared memory area.
619 * \retval 0 Free capacity in the area.
624 l4shmc_area_size_free(l4shmc_area_t *shmarea);
627 * Get memory overhead per area that is not available for chunks
630 * \return size of the overhead in bytes
633 l4shmc_area_overhead(void);
636 * Get memory overhead required in addition to the chunk capacity
637 * for adding one chunk
640 * \return size of the overhead in bytes
643 l4shmc_chunk_overhead(void);
645 #include <l4/shmc/internal.h>