L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ringbuf.h
Go to the documentation of this file.
1/*
2 * (c) 2010 Björn Döbel <doebel@os.inf.tu-dresden.de>
3 * economic rights: Technische Universität Dresden (Germany)
4 * This file is part of TUD:OS and distributed under the terms of the
5 * GNU Lesser General Public License 2.1.
6 * Please see the COPYING-LGPL-2.1 file for details.
7 */
8
12#pragma once
13
14#include <l4/shmc/shmc.h>
15#include <l4/util/assert.h>
16#include <l4/sys/compiler.h>
17#include <l4/sys/thread.h>
18
20
47/*
48 * Turn on ringbuf poisoning. This will add magic values to the ringbuf
49 * header as well as each packet header and check that these values are
50 * valid all the time.
51 */
52#define L4SHMC_RINGBUF_POISONING 1
53
59typedef struct
60{
61 volatile l4_uint32_t lock;
62 unsigned data_size;
63#if L4SHMC_RINGBUF_POISONING
64 char magic1;
65#endif
66 unsigned next_read;
67 unsigned next_write;
68#if L4SHMC_RINGBUF_POISONING
69 char magic2;
70#endif
71 unsigned bytes_filled;
72 unsigned sender_waits;
73#if L4SHMC_RINGBUF_POISONING
74 char magic3;
75#endif
76 char data[];
78
79
85typedef struct
86{
87 l4shmc_area_t *_area;
89 l4shmc_chunk_t _chunk;
90 unsigned _size;
91 char *_chunkname;
92 char *_signame;
94 l4shmc_signal_t _signal_full;
95 l4shmc_signal_t _signal_empty;
97
98
105#define L4SHMC_RINGBUF_HEAD(ringbuf) ((l4shmc_ringbuf_head_t*)((ringbuf)->_addr))
106
107
114#define L4SHMC_RINGBUF_DATA(ringbuf) (L4SHMC_RINGBUF_HEAD(ringbuf)->data)
115
116
123#define L4SHMC_RINGBUF_DATA_SIZE(ringbuf) ((ringbuf)->_size - sizeof(l4shmc_ringbuf_head_t))
124
125enum lock_content
126{
127 lock_cont_min = 4,
128 locked = 5,
129 unlocked = 6,
130 lock_cont_max = 7,
131};
132
133static L4_CV inline void l4shmc_rb_lock(l4shmc_ringbuf_head_t *head)
134{
135 ASSERT_NOT_NULL(head);
136 ASSERT_ASSERT(head->lock > lock_cont_min);
137 ASSERT_ASSERT(head->lock < lock_cont_max);
138
139 while (!l4util_cmpxchg32(&head->lock, unlocked, locked))
141}
142
143
144static L4_CV inline void l4shmc_rb_unlock(l4shmc_ringbuf_head_t *head)
145{
146 ASSERT_NOT_NULL(head);
147 ASSERT_ASSERT(head->lock > lock_cont_min);
148 ASSERT_ASSERT(head->lock < lock_cont_max);
149
150 head->lock = unlocked;
151}
152
153/******************
154 * Initialization *
155 ******************/
156
173L4_CV int l4shmc_rb_init_buffer(l4shmc_ringbuf_t *buf, l4shmc_area_t *area,
174 char const *chunk_name,
175 char const *signal_name, unsigned size);
176
183
184
185
186/***************************
187 * RINGBUF SENDER *
188 ***************************/
189
206L4_CV int l4shmc_rb_attach_sender(l4shmc_ringbuf_t *buf, char const *signal_name,
207 l4_cap_idx_t owner);
208
209
223 unsigned psize);
224
225
237 char *data, unsigned dsize);
238
239
255 unsigned size, int block_if_necessary);
256
257
264
265
266/***************************
267 * RINGBUF RECEIVER *
268 ***************************/
269
286L4_CV int l4shmc_rb_init_receiver(l4shmc_ringbuf_t *buf, l4shmc_area_t *area,
287 char const *chunk_name,
288 char const *signal_name);
289
290
304
305
317
318
329 unsigned *tsize);
330
331
338
339
347
L4 compiler related defines.
unsigned int l4_uint32_t
Unsigned 32bit value.
Definition l4int.h:29
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:336
l4_msgtag_t l4_thread_yield(void) L4_NOTHROW
Yield current time slice.
Definition thread.h:891
#define L4_CV
Define calling convention.
Definition linkage.h:33
#define L4_BEGIN_DECLS
Start section with C types and functions.
Definition compiler.h:165
#define L4_END_DECLS
End section with C types and functions.
Definition compiler.h:166
int l4util_cmpxchg32(volatile l4_uint32_t *dest, l4_uint32_t cmp_val, l4_uint32_t new_val)
Atomic compare and exchange (32 bit version)
Definition atomic.h:355
void l4shmc_rb_sender_commit_packet(l4shmc_ringbuf_t *buf)
Tell the consumer that new data is available.
char * l4shmc_rb_sender_alloc_packet(l4shmc_ringbuf_head_t *head, unsigned psize)
Allocate a packet of a given size within the ring buffer.
int l4shmc_rb_attach_sender(l4shmc_ringbuf_t *buf, char const *signal_name, l4_cap_idx_t owner)
Attach to sender signal of a ring buffer.
void l4shmc_rb_attach_receiver(l4shmc_ringbuf_t *buf, l4_cap_idx_t owner)
Attach to receiver signal of a ring buffer.
int l4shmc_rb_receiver_copy_out(l4shmc_ringbuf_head_t *head, char *target, unsigned *tsize)
Copy data out of the buffer.
void l4shmc_rb_deinit_buffer(l4shmc_ringbuf_t *buf)
De-init a ring buffer.
int l4shmc_rb_init_receiver(l4shmc_ringbuf_t *buf, l4shmc_area_t *area, char const *chunk_name, char const *signal_name)
Initialize receive buffer.
int l4shmc_rb_receiver_wait_for_data(l4shmc_ringbuf_t *buf, int blocking)
Check if (and optionally block until) new data is ready.
int l4shmc_rb_init_buffer(l4shmc_ringbuf_t *buf, l4shmc_area_t *area, char const *chunk_name, char const *signal_name, unsigned size)
Initialize a ring buffer by creating an SHMC chunk and the corresponding signals.
int l4shmc_rb_receiver_read_next_size(l4shmc_ringbuf_head_t *head)
Have a look at the ring buffer and see which size the next packet to be read has.
int l4shmc_rb_sender_next_copy_in(l4shmc_ringbuf_t *buf, char *data, unsigned size, int block_if_necessary)
Copy in packet from an external data source.
void l4shmc_rb_receiver_notify_done(l4shmc_ringbuf_t *buf)
Notify producer that space is available.
void l4shmc_rb_sender_put_data(l4shmc_ringbuf_t *buf, char *addr, char *data, unsigned dsize)
Copy data into a previously allocated packet.
Shared memory library header file.
Head field of a ring buffer.
Definition ringbuf.h:60
unsigned sender_waits
sender waiting?
Definition ringbuf.h:72
unsigned next_read
offset to next read packet
Definition ringbuf.h:66
unsigned next_write
offset to next write packet
Definition ringbuf.h:67
unsigned bytes_filled
bytes filled in buffer
Definition ringbuf.h:71
Ring buffer.
Definition ringbuf.h:86
char * _signame
base name of the ring buffer signals
Definition ringbuf.h:92
l4shmc_area_t * _area
L4SHM area this buffer is located in.
Definition ringbuf.h:87
l4shmc_chunk_t _chunk
chunk descriptor
Definition ringbuf.h:89
l4shmc_signal_t _signal_full
"full" signal - triggered when data is produced
Definition ringbuf.h:94
l4shmc_ringbuf_head_t * _addr
pointer to ring buffer head
Definition ringbuf.h:93
l4_cap_idx_t _owner
owner (attached to send/recv signal)
Definition ringbuf.h:88
unsigned _size
chunk size // XXX do we need this?
Definition ringbuf.h:90
char * _chunkname
name of the ring buffer chunk
Definition ringbuf.h:91
l4shmc_signal_t _signal_empty
"empty" signal - triggered when data is consumed
Definition ringbuf.h:95
Some useful assert-style macros.