Blame view

RIOT/core/include/mbox.h 3.92 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  /*
   * Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @defgroup    core_mbox Mailboxes
   * @ingroup     core
   * @brief       Mailbox implementation
   *
   * @{
   *
   * @file
   * @brief       Mailbox API
   *
   * @author      Kaspar Schleiser <kaspar@schleiser.de>
   */
  
  #ifndef MBOX_H
  #define MBOX_H
  
  #include "list.h"
  #include "cib.h"
  #include "msg.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /** Static initializer for mbox objects */
  #define MBOX_INIT(queue, queue_size) {{0}, {0}, CIB_INIT(queue_size), queue}
  
  /**
   * @brief Mailbox struct definition
   */
  typedef struct {
      list_node_t readers;    /**< list of threads waiting for message    */
      list_node_t writers;    /**< list of threads waiting to send        */
      cib_t cib;              /**< cib for msg array                      */
      msg_t *msg_array;       /**< ptr to array of msg queue              */
  } mbox_t;
  
  enum {
      NON_BLOCKING = 0,       /**< non-blocking mode */
      BLOCKING,               /**< blocking mode */
  };
  
  /**
   * @brief Initialize mbox object
   *
   * @note The message queue size must be a power of two!
   *
   * @param[in]   mbox        ptr to mailbox to initialize
   * @param[in]   queue       array of msg_t used as queue
   * @param[in]   queue_size  number of msg_t objects in queue
   */
  static inline void mbox_init(mbox_t *mbox, msg_t *queue, unsigned int queue_size)
  {
      mbox_t m = MBOX_INIT(queue, queue_size);
      *mbox = m;
  }
  
  /**
   * @brief Add message to mailbox
   *
   * If the mailbox is full, this fuction will return right away.
   *
   * @internal
   *
   * @param[in] mbox      ptr to mailbox to operate on
   * @param[in] msg       ptr to message that will be copied into mailbox
   * @param[in] blocking  block if 1, don't block if 0
   *
   * @return  1   if msg could be delivered
   * @return  0   otherwise
   */
  int _mbox_put(mbox_t *mbox, msg_t *msg, int blocking);
  
  /**
   * @brief Get message from mailbox
   *
   * If the mailbox is empty, this fuction will return right away.
   *
   * @internal
   *
   * @param[in] mbox  ptr to mailbox to operate on
   * @param[in] msg   ptr to storage for retrieved message
   * @param[in] blocking  block if 1, don't block if 0
   *
   * @return  1   if msg could be retrieved
   * @return  0   otherwise
   */
  int _mbox_get(mbox_t *mbox, msg_t *msg, int blocking);
  
  /**
   * @brief Add message to mailbox
   *
   * If the mailbox is full, this fuction will block until space becomes
   * available.
   *
   * @param[in] mbox  ptr to mailbox to operate on
   * @param[in] msg   ptr to message that will be copied into mailbox
   */
  static inline void mbox_put(mbox_t *mbox, msg_t *msg)
  {
      _mbox_put(mbox, msg, BLOCKING);
  }
  
  /**
   * @brief Add message to mailbox
   *
   * If the mailbox is full, this fuction will return right away.
   *
   * @param[in] mbox  ptr to mailbox to operate on
   * @param[in] msg   ptr to message that will be copied into mailbox
   *
   * @return  1   if msg could be delivered
   * @return  0   otherwise
   */
  static inline int mbox_try_put(mbox_t *mbox, msg_t *msg)
  {
      return _mbox_put(mbox, msg, NON_BLOCKING);
  }
  
  /**
   * @brief Get message from mailbox
   *
   * If the mailbox is empty, this fuction will block until a message becomes
   * available.
   *
   * @param[in] mbox  ptr to mailbox to operate on
   * @param[in] msg   ptr to storage for retrieved message
   */
  static inline void mbox_get(mbox_t *mbox, msg_t *msg)
  {
      _mbox_get(mbox, msg, BLOCKING);
  }
  
  /**
   * @brief Get message from mailbox
   *
   * If the mailbox is empty, this fuction will return right away.
   *
   * @param[in] mbox  ptr to mailbox to operate on
   * @param[in] msg   ptr to storage for retrieved message
   *
   * @return  1   if msg could be retrieved
   * @return  0   otherwise
   */
  static inline int mbox_try_get(mbox_t *mbox, msg_t *msg)
  {
      return _mbox_get(mbox, msg, NON_BLOCKING);
  }
  
  #ifdef __cplusplus
  }
  #endif
  
  /** @} */
  #endif /* MBOX_H */