L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
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/thread.h>
17
19
46/*
47 * Turn on ringbuf poisoning. This will add magic values to the ringbuf
48 * header as well as each packet header and check that these values are
49 * valid all the time.
50 */
51#define L4SHMC_RINGBUF_POISONING 1
52
58typedef struct
59{
60 volatile l4_uint32_t lock;
61 unsigned data_size;
62#if L4SHMC_RINGBUF_POISONING
63 char magic1;
64#endif
65 unsigned next_read;
66 unsigned next_write;
67#if L4SHMC_RINGBUF_POISONING
68 char magic2;
69#endif
70 unsigned bytes_filled;
71 unsigned sender_waits;
72#if L4SHMC_RINGBUF_POISONING
73 char magic3;
74#endif
75 char data[];
77
78
84typedef struct
85{
86 l4shmc_area_t *_area;
88 l4shmc_chunk_t _chunk;
89 unsigned _size;
90 char *_chunkname;
91 char *_signame;
93 l4shmc_signal_t _signal_full;
94 l4shmc_signal_t _signal_empty;
96
97
104#define L4SHMC_RINGBUF_HEAD(ringbuf) ((l4shmc_ringbuf_head_t*)((ringbuf)->_addr))
105
106
113#define L4SHMC_RINGBUF_DATA(ringbuf) (L4SHMC_RINGBUF_HEAD(ringbuf)->data)
114
115
122#define L4SHMC_RINGBUF_DATA_SIZE(ringbuf) ((ringbuf)->_size - sizeof(l4shmc_ringbuf_head_t))
123
124enum lock_content
125{
126 lock_cont_min = 4,
127 locked = 5,
128 unlocked = 6,
129 lock_cont_max = 7,
130};
131
132static L4_CV inline void l4shmc_rb_lock(l4shmc_ringbuf_head_t *head)
133{
134 ASSERT_NOT_NULL(head);
135 ASSERT_ASSERT(head->lock > lock_cont_min);
136 ASSERT_ASSERT(head->lock < lock_cont_max);
137
138 while (!l4util_cmpxchg32(&head->lock, unlocked, locked))
140}
141
142
143static L4_CV inline void l4shmc_rb_unlock(l4shmc_ringbuf_head_t *head)
144{
145 ASSERT_NOT_NULL(head);
146 ASSERT_ASSERT(head->lock > lock_cont_min);
147 ASSERT_ASSERT(head->lock < lock_cont_max);
148
149 head->lock = unlocked;
150}
151
152/******************
153 * Initialization *
154 ******************/
155
172L4_CV int l4shmc_rb_init_buffer(l4shmc_ringbuf_t *buf, l4shmc_area_t *area,
173 char const *chunk_name,
174 char const *signal_name, unsigned size);
175
182
183
184
185/***************************
186 * RINGBUF SENDER *
187 ***************************/
188
205L4_CV int l4shmc_rb_attach_sender(l4shmc_ringbuf_t *buf, char const *signal_name,
206 l4_cap_idx_t owner);
207
208
222 unsigned psize);
223
224
236 char *data, unsigned dsize);
237
238
254 unsigned size, int block_if_necessary);
255
256
263
264
265/***************************
266 * RINGBUF RECEIVER *
267 ***************************/
268
285L4_CV int l4shmc_rb_init_receiver(l4shmc_ringbuf_t *buf, l4shmc_area_t *area,
286 char const *chunk_name,
287 char const *signal_name);
288
289
303
304
316
317
328 unsigned *tsize);
329
330
337
338
346
unsigned int l4_uint32_t
Unsigned 32bit value.
Definition l4int.h:40
unsigned long l4_cap_idx_t
Capability selector type.
Definition types.h:359
l4_msgtag_t l4_thread_yield(void) L4_NOTHROW
Yield current time slice.
Definition thread.h:861
#define __END_DECLS
End section with C types and functions.
Definition compiler.h:199
#define L4_CV
Define calling convention.
Definition linkage.h:44
#define __BEGIN_DECLS
Start section with C types and functions.
Definition compiler.h:196
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:357
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:59
unsigned sender_waits
sender waiting?
Definition ringbuf.h:71
unsigned next_read
offset to next read packet
Definition ringbuf.h:65
unsigned next_write
offset to next write packet
Definition ringbuf.h:66
unsigned bytes_filled
bytes filled in buffer
Definition ringbuf.h:70
Ring buffer.
Definition ringbuf.h:85
char * _signame
base name of the ring buffer signals
Definition ringbuf.h:91
l4shmc_area_t * _area
L4SHM area this buffer is located in.
Definition ringbuf.h:86
l4shmc_chunk_t _chunk
chunk descriptor
Definition ringbuf.h:88
l4shmc_signal_t _signal_full
"full" signal - triggered when data is produced
Definition ringbuf.h:93
l4shmc_ringbuf_head_t * _addr
pointer to ring buffer head
Definition ringbuf.h:92
l4_cap_idx_t _owner
owner (attached to send/recv signal)
Definition ringbuf.h:87
unsigned _size
chunk size // XXX do we need this?
Definition ringbuf.h:89
char * _chunkname
name of the ring buffer chunk
Definition ringbuf.h:90
l4shmc_signal_t _signal_empty
"empty" signal - triggered when data is consumed
Definition ringbuf.h:94
Some useful assert-style macros.