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 
193 static inline void mbox_unset(mbox_t *mbox)
194 {
195  mbox->msg_array = NULL;
196  mbox->cib.mask = 0;
197 }
198 
199 #ifdef __cplusplus
200 }
201 #endif
202 
204 #endif /* MBOX_H */
Circular integer buffer interface.
static unsigned int cib_avail(const cib_t *cib)
Calculates difference between cib_put() and cib_get() accesses.
Definition: cib.h:88
static void mbox_unset(mbox_t *mbox)
Unset's the mbox, effectively deinitializing and invalidating it.
Definition: mbox.h:193
static void mbox_get(mbox_t *mbox, msg_t *msg)
Get message from mailbox.
Definition: mbox.h:141
static void mbox_init(mbox_t *mbox, msg_t *queue, unsigned int queue_size)
Initialize mbox object.
Definition: mbox.h:62
#define MBOX_INIT(queue, queue_size)
Static initializer for mbox objects.
Definition: mbox.h:34
static size_t mbox_size(mbox_t *mbox)
Get mbox queue size (capacity)
Definition: mbox.h:169
static size_t mbox_avail(mbox_t *mbox)
Get messages available in mbox.
Definition: mbox.h:183
static int mbox_try_put(mbox_t *mbox, msg_t *msg)
Add message to mailbox.
Definition: mbox.h:127
static void mbox_put(mbox_t *mbox, msg_t *msg)
Add message to mailbox.
Definition: mbox.h:111
int _mbox_get(mbox_t *mbox, msg_t *msg, int blocking)
Get message from mailbox.
int _mbox_put(mbox_t *mbox, msg_t *msg, int blocking)
Add message to mailbox.
static int mbox_try_get(mbox_t *mbox, msg_t *msg)
Get message from mailbox.
Definition: mbox.h:157
@ NON_BLOCKING
non-blocking mode
Definition: mbox.h:49
@ BLOCKING
blocking mode
Definition: mbox.h:50
Intrusive linked list.
circular integer buffer structure
Definition: cib.h:34
unsigned int mask
Size of buffer -1, i.e.
Definition: cib.h:37
List node structure.
Definition: list.h:40
Mailbox struct definition.
Definition: mbox.h:41
list_node_t writers
list of threads waiting to send
Definition: mbox.h:43
list_node_t readers
list of threads waiting for message
Definition: mbox.h:42
cib_t cib
cib for msg array
Definition: mbox.h:44
msg_t * msg_array
ptr to array of msg queue
Definition: mbox.h:45
Describes a message object which can be sent between threads.
Definition: msg.h:196
Definitions for parsing and composition of DNS messages.