7
7
#include <sys/task.h>
8
8
9
9
#include "private/error.h"
10
+ #include "private/utils.h"
10
11
11
12
mq_t * mo_mq_create (uint16_t max_items )
12
13
{
13
14
mq_t * mq = malloc (sizeof * mq );
14
- if (!mq )
15
+ if (unlikely ( !mq ) )
15
16
return NULL ;
16
17
17
18
mq -> q = queue_create (max_items );
18
- if (!mq -> q ) {
19
+ if (unlikely ( !mq -> q ) ) {
19
20
free (mq );
20
21
return NULL ;
21
22
}
@@ -24,22 +25,33 @@ mq_t *mo_mq_create(uint16_t max_items)
24
25
25
26
int32_t mo_mq_destroy (mq_t * mq )
26
27
{
28
+ if (unlikely (!mq ))
29
+ return ERR_OK ; /* Destroying NULL is no-op */
30
+
31
+ if (unlikely (!mq -> q ))
32
+ return ERR_FAIL ; /* Invalid mqueue state */
33
+
27
34
CRITICAL_ENTER ();
28
35
29
- if (queue_count (mq -> q ) != 0 ) { /* refuse to destroy non-empty q */
36
+ if (unlikely ( queue_count (mq -> q ) != 0 ) ) { /* refuse to destroy non-empty q */
30
37
CRITICAL_LEAVE ();
31
38
return ERR_MQ_NOTEMPTY ;
32
39
}
33
40
41
+ /* Safe to destroy now - no need to hold critical section */
42
+ CRITICAL_LEAVE ();
43
+
34
44
queue_destroy (mq -> q );
35
45
free (mq );
36
46
37
- CRITICAL_LEAVE ();
38
47
return ERR_OK ;
39
48
}
40
49
41
50
int32_t mo_mq_enqueue (mq_t * mq , message_t * msg )
42
51
{
52
+ if (unlikely (!mq || !mq -> q || !msg ))
53
+ return ERR_FAIL ;
54
+
43
55
int32_t rc ;
44
56
45
57
CRITICAL_ENTER ();
@@ -52,6 +64,9 @@ int32_t mo_mq_enqueue(mq_t *mq, message_t *msg)
52
64
/* remove oldest message (FIFO) */
53
65
message_t * mo_mq_dequeue (mq_t * mq )
54
66
{
67
+ if (unlikely (!mq || !mq -> q ))
68
+ return NULL ;
69
+
55
70
message_t * msg ;
56
71
57
72
CRITICAL_ENTER ();
@@ -64,6 +79,9 @@ message_t *mo_mq_dequeue(mq_t *mq)
64
79
/* inspect head without removing */
65
80
message_t * mo_mq_peek (mq_t * mq )
66
81
{
82
+ if (unlikely (!mq || !mq -> q ))
83
+ return NULL ;
84
+
67
85
message_t * msg ;
68
86
69
87
CRITICAL_ENTER ();
0 commit comments