mbox.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
22 #ifndef MBOX_H
23 #define MBOX_H
24 
25 #include "list.h"
26 #include "cib.h"
27 #include "msg.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
34 #define MBOX_INIT(queue, queue_size) { \
35  { 0 }, { 0 }, CIB_INIT(queue_size), queue \
36 }
37 
41 typedef struct {
46 } mbox_t;
47 
48 enum {
51 };
52 
62 static inline void mbox_init(mbox_t *mbox, msg_t *queue,
63  unsigned int queue_size)
64 {
65  mbox_t m = MBOX_INIT(queue, queue_size);
66 
67  *mbox = m;
68 }
69 
84 int _mbox_put(mbox_t *mbox, msg_t *msg, int blocking);
85 
100 int _mbox_get(mbox_t *mbox, msg_t *msg, int blocking);
101 
111 static inline void mbox_put(mbox_t *mbox, msg_t *msg)
112 {
113  _mbox_put(mbox, msg, BLOCKING);
114 }
115 
127 static inline int mbox_try_put(mbox_t *mbox, msg_t *msg)
128 {
129  return _mbox_put(mbox, msg, NON_BLOCKING);
130 }
131 
141 static inline void mbox_get(mbox_t *mbox, msg_t *msg)
142 {
143  _mbox_get(mbox, msg, BLOCKING);
144 }
145 
157 static inline int mbox_try_get(mbox_t *mbox, msg_t *msg)
158 {
159  return _mbox_get(mbox, msg, NON_BLOCKING);
160 }
161 
169 static inline size_t mbox_size(mbox_t *mbox)
170 {
171  return mbox->cib.mask ? mbox->cib.mask + 1 : 0;
172 }
173 
183 static inline size_t mbox_avail(mbox_t *mbox)
184 {
185  return cib_avail(&mbox->cib);
186 }
187 
188 #ifdef __cplusplus
189 }
190 #endif
191 
193 #endif /* MBOX_H */
mbox_t::msg_array
msg_t * msg_array
ptr to array of msg queue
Definition: mbox.h:45
BLOCKING
@ BLOCKING
blocking mode
Definition: mbox.h:50
_mbox_put
int _mbox_put(mbox_t *mbox, msg_t *msg, int blocking)
Add message to mailbox.
mbox_init
static void mbox_init(mbox_t *mbox, msg_t *queue, unsigned int queue_size)
Initialize mbox object.
Definition: mbox.h:62
mbox_try_put
static int mbox_try_put(mbox_t *mbox, msg_t *msg)
Add message to mailbox.
Definition: mbox.h:127
msg.h
Messaging API for inter process communication.
MBOX_INIT
#define MBOX_INIT(queue, queue_size)
Static initializer for mbox objects.
Definition: mbox.h:34
cib_t::mask
unsigned int mask
Size of buffer -1, i.e.
Definition: cib.h:37
NON_BLOCKING
@ NON_BLOCKING
non-blocking mode
Definition: mbox.h:49
mbox_t::writers
list_node_t writers
list of threads waiting to send
Definition: mbox.h:43
mbox_t
Mailbox struct definition.
Definition: mbox.h:41
mbox_put
static void mbox_put(mbox_t *mbox, msg_t *msg)
Add message to mailbox.
Definition: mbox.h:111
mbox_try_get
static int mbox_try_get(mbox_t *mbox, msg_t *msg)
Get message from mailbox.
Definition: mbox.h:157
mbox_size
static size_t mbox_size(mbox_t *mbox)
Get mbox queue size (capacity)
Definition: mbox.h:169
cib_avail
static unsigned int cib_avail(const cib_t *cib)
Calculates difference between cib_put() and cib_get() accesses.
Definition: cib.h:74
cib_t
circular integer buffer structure
Definition: cib.h:34
_mbox_get
int _mbox_get(mbox_t *mbox, msg_t *msg, int blocking)
Get message from mailbox.
cib.h
Circular integer buffer interface.
mbox_get
static void mbox_get(mbox_t *mbox, msg_t *msg)
Get message from mailbox.
Definition: mbox.h:141
list_node
List node structure.
Definition: list.h:40
msg_t
Describes a message object which can be sent between threads.
Definition: msg.h:185
list.h
Intrusive linked list.
mbox_t::readers
list_node_t readers
list of threads waiting for message
Definition: mbox.h:42
mbox_avail
static size_t mbox_avail(mbox_t *mbox)
Get messages available in mbox.
Definition: mbox.h:183
mbox_t::cib
cib_t cib
cib for msg array
Definition: mbox.h:44