Loading...
Searching...
No Matches
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
20
21#include "cib.h"
22#include "compiler_hints.h"
23#include "list.h"
24#include "msg.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
31#define MBOX_INIT(queue, queue_size) { \
32 { 0 }, { 0 }, CIB_INIT(queue_size), queue \
33}
34
44
45enum {
48};
49
59ACCESS(write_only, 2, 3)
60static inline void mbox_init(mbox_t *mbox, msg_t *queue,
61 unsigned int queue_size)
62{
63 mbox_t m = MBOX_INIT(queue, queue_size);
64
65 *mbox = m;
66}
67
82int _mbox_put(mbox_t *mbox, msg_t *msg, int blocking);
83
98int _mbox_get(mbox_t *mbox, msg_t *msg, int blocking);
99
109static inline void mbox_put(mbox_t *mbox, msg_t *msg)
110{
111 _mbox_put(mbox, msg, BLOCKING);
112}
113
125static inline int mbox_try_put(mbox_t *mbox, msg_t *msg)
126{
127 return _mbox_put(mbox, msg, NON_BLOCKING);
128}
129
139static inline void mbox_get(mbox_t *mbox, msg_t *msg)
140{
141 _mbox_get(mbox, msg, BLOCKING);
142}
143
155static inline int mbox_try_get(mbox_t *mbox, msg_t *msg)
156{
157 return _mbox_get(mbox, msg, NON_BLOCKING);
158}
159
167static inline size_t mbox_size(mbox_t *mbox)
168{
169 return mbox->cib.mask ? mbox->cib.mask + 1 : 0;
170}
171
181static inline size_t mbox_avail(mbox_t *mbox)
182{
183 return cib_avail(&mbox->cib);
184}
185
191static inline void mbox_unset(mbox_t *mbox)
192{
193 mbox->msg_array = NULL;
194 mbox->cib.mask = 0;
195}
196
197#ifdef __cplusplus
198}
199#endif
200
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
Common macros and compiler attributes/pragmas configuration.
#define ACCESS(mode, ptr_idx, size_idx)
Emit an attribute (if supported by the compiler) that declares how a function will access its paramet...
Messaging API for inter process communication.
static void mbox_unset(mbox_t *mbox)
Unset's the mbox, effectively deinitializing and invalidating it.
Definition mbox.h:191
static void mbox_get(mbox_t *mbox, msg_t *msg)
Get message from mailbox.
Definition mbox.h:139
static void mbox_init(mbox_t *mbox, msg_t *queue, unsigned int queue_size)
Initialize mbox object.
Definition mbox.h:60
#define MBOX_INIT(queue, queue_size)
Static initializer for mbox objects.
Definition mbox.h:31
static size_t mbox_size(mbox_t *mbox)
Get mbox queue size (capacity)
Definition mbox.h:167
static size_t mbox_avail(mbox_t *mbox)
Get messages available in mbox.
Definition mbox.h:181
static int mbox_try_put(mbox_t *mbox, msg_t *msg)
Add message to mailbox.
Definition mbox.h:125
static void mbox_put(mbox_t *mbox, msg_t *msg)
Add message to mailbox.
Definition mbox.h:109
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:155
@ NON_BLOCKING
non-blocking mode
Definition mbox.h:46
@ BLOCKING
blocking mode
Definition mbox.h:47
Intrusive linked list.
struct list_node list_node_t
List node structure.
circular integer buffer structure
Definition cib.h:41
unsigned int mask
Size of buffer -1, i.e.
Definition cib.h:44
Mailbox struct definition.
Definition mbox.h:38
list_node_t writers
list of threads waiting to send
Definition mbox.h:40
list_node_t readers
list of threads waiting for message
Definition mbox.h:39
cib_t cib
cib for msg array
Definition mbox.h:41
msg_t * msg_array
ptr to array of msg queue
Definition mbox.h:42
Describes a message object which can be sent between threads.
Definition msg.h:193