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 
9 #pragma once
10 
24 #include "list.h"
25 #include "cib.h"
26 #include "msg.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
33 #define MBOX_INIT(queue, queue_size) { \
34  { 0 }, { 0 }, CIB_INIT(queue_size), queue \
35 }
36 
40 typedef struct {
45 } mbox_t;
46 
47 enum {
50 };
51 
61 static inline void mbox_init(mbox_t *mbox, msg_t *queue,
62  unsigned int queue_size)
63 {
64  mbox_t m = MBOX_INIT(queue, queue_size);
65 
66  *mbox = m;
67 }
68 
83 int _mbox_put(mbox_t *mbox, msg_t *msg, int blocking);
84 
99 int _mbox_get(mbox_t *mbox, msg_t *msg, int blocking);
100 
110 static inline void mbox_put(mbox_t *mbox, msg_t *msg)
111 {
112  _mbox_put(mbox, msg, BLOCKING);
113 }
114 
126 static inline int mbox_try_put(mbox_t *mbox, msg_t *msg)
127 {
128  return _mbox_put(mbox, msg, NON_BLOCKING);
129 }
130 
140 static inline void mbox_get(mbox_t *mbox, msg_t *msg)
141 {
142  _mbox_get(mbox, msg, BLOCKING);
143 }
144 
156 static inline int mbox_try_get(mbox_t *mbox, msg_t *msg)
157 {
158  return _mbox_get(mbox, msg, NON_BLOCKING);
159 }
160 
168 static inline size_t mbox_size(mbox_t *mbox)
169 {
170  return mbox->cib.mask ? mbox->cib.mask + 1 : 0;
171 }
172 
182 static inline size_t mbox_avail(mbox_t *mbox)
183 {
184  return cib_avail(&mbox->cib);
185 }
186 
192 static inline void mbox_unset(mbox_t *mbox)
193 {
194  mbox->msg_array = NULL;
195  mbox->cib.mask = 0;
196 }
197 
198 #ifdef __cplusplus
199 }
200 #endif
201 
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:101
static void mbox_unset(mbox_t *mbox)
Unset's the mbox, effectively deinitializing and invalidating it.
Definition: mbox.h:192
static void mbox_get(mbox_t *mbox, msg_t *msg)
Get message from mailbox.
Definition: mbox.h:140
static void mbox_init(mbox_t *mbox, msg_t *queue, unsigned int queue_size)
Initialize mbox object.
Definition: mbox.h:61
#define MBOX_INIT(queue, queue_size)
Static initializer for mbox objects.
Definition: mbox.h:33
static size_t mbox_size(mbox_t *mbox)
Get mbox queue size (capacity)
Definition: mbox.h:168
static size_t mbox_avail(mbox_t *mbox)
Get messages available in mbox.
Definition: mbox.h:182
static int mbox_try_put(mbox_t *mbox, msg_t *msg)
Add message to mailbox.
Definition: mbox.h:126
static void mbox_put(mbox_t *mbox, msg_t *msg)
Add message to mailbox.
Definition: mbox.h:110
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:156
@ NON_BLOCKING
non-blocking mode
Definition: mbox.h:48
@ BLOCKING
blocking mode
Definition: mbox.h:49
Intrusive linked list.
circular integer buffer structure
Definition: cib.h:44
unsigned int mask
Size of buffer -1, i.e.
Definition: cib.h:47
List node structure.
Definition: list.h:39
Mailbox struct definition.
Definition: mbox.h:40
list_node_t writers
list of threads waiting to send
Definition: mbox.h:42
list_node_t readers
list of threads waiting for message
Definition: mbox.h:41
cib_t cib
cib for msg array
Definition: mbox.h:43
msg_t * msg_array
ptr to array of msg queue
Definition: mbox.h:44
Describes a message object which can be sent between threads.
Definition: msg.h:195
Definitions for parsing and composition of DNS messages.