mbox.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016 Kaspar Schleiser <kaspar@schleiser.de>
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
21 #include "list.h"
22 #include "cib.h"
23 #include "msg.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
30 #define MBOX_INIT(queue, queue_size) { \
31  { 0 }, { 0 }, CIB_INIT(queue_size), queue \
32 }
33 
37 typedef struct {
42 } mbox_t;
43 
44 enum {
47 };
48 
58 static inline void mbox_init(mbox_t *mbox, msg_t *queue,
59  unsigned int queue_size)
60 {
61  mbox_t m = MBOX_INIT(queue, queue_size);
62 
63  *mbox = m;
64 }
65 
80 int _mbox_put(mbox_t *mbox, msg_t *msg, int blocking);
81 
96 int _mbox_get(mbox_t *mbox, msg_t *msg, int blocking);
97 
107 static inline void mbox_put(mbox_t *mbox, msg_t *msg)
108 {
109  _mbox_put(mbox, msg, BLOCKING);
110 }
111 
123 static inline int mbox_try_put(mbox_t *mbox, msg_t *msg)
124 {
125  return _mbox_put(mbox, msg, NON_BLOCKING);
126 }
127 
137 static inline void mbox_get(mbox_t *mbox, msg_t *msg)
138 {
139  _mbox_get(mbox, msg, BLOCKING);
140 }
141 
153 static inline int mbox_try_get(mbox_t *mbox, msg_t *msg)
154 {
155  return _mbox_get(mbox, msg, NON_BLOCKING);
156 }
157 
165 static inline size_t mbox_size(mbox_t *mbox)
166 {
167  return mbox->cib.mask ? mbox->cib.mask + 1 : 0;
168 }
169 
179 static inline size_t mbox_avail(mbox_t *mbox)
180 {
181  return cib_avail(&mbox->cib);
182 }
183 
189 static inline void mbox_unset(mbox_t *mbox)
190 {
191  mbox->msg_array = NULL;
192  mbox->cib.mask = 0;
193 }
194 
195 #ifdef __cplusplus
196 }
197 #endif
198 
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:98
static void mbox_unset(mbox_t *mbox)
Unset's the mbox, effectively deinitializing and invalidating it.
Definition: mbox.h:189
static void mbox_get(mbox_t *mbox, msg_t *msg)
Get message from mailbox.
Definition: mbox.h:137
static void mbox_init(mbox_t *mbox, msg_t *queue, unsigned int queue_size)
Initialize mbox object.
Definition: mbox.h:58
#define MBOX_INIT(queue, queue_size)
Static initializer for mbox objects.
Definition: mbox.h:30
static size_t mbox_size(mbox_t *mbox)
Get mbox queue size (capacity)
Definition: mbox.h:165
static size_t mbox_avail(mbox_t *mbox)
Get messages available in mbox.
Definition: mbox.h:179
static int mbox_try_put(mbox_t *mbox, msg_t *msg)
Add message to mailbox.
Definition: mbox.h:123
static void mbox_put(mbox_t *mbox, msg_t *msg)
Add message to mailbox.
Definition: mbox.h:107
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:153
@ NON_BLOCKING
non-blocking mode
Definition: mbox.h:45
@ BLOCKING
blocking mode
Definition: mbox.h:46
Intrusive linked list.
circular integer buffer structure
Definition: cib.h:41
unsigned int mask
Size of buffer -1, i.e.
Definition: cib.h:44
List node structure.
Definition: list.h:36
Mailbox struct definition.
Definition: mbox.h:37
list_node_t writers
list of threads waiting to send
Definition: mbox.h:39
list_node_t readers
list of threads waiting for message
Definition: mbox.h:38
cib_t cib
cib for msg array
Definition: mbox.h:40
msg_t * msg_array
ptr to array of msg queue
Definition: mbox.h:41
Describes a message object which can be sent between threads.
Definition: msg.h:192
Definitions for parsing and composition of DNS messages.