1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #ifndef K_QUEUE_H
6 #define K_QUEUE_H
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 /** @addtogroup aos_rhino queue
13  *  Queue can be used for synchronous communication between tasks.
14  *
15  *  @{
16  */
17 
18 #define WAKE_ONE_TASK 0u
19 #define WAKE_ALL_TASK 1u
20 
21 /**
22  * queue statistcs
23  */
24 typedef struct {
25     void   **queue_start;
26     size_t   size;
27     size_t   cur_num;
28     size_t   peak_num;
29 } msg_q_t;
30 
31 /**
32  * queue information
33  */
34 typedef struct {
35     msg_q_t  msg_q;
36     klist_t *pend_entry;    /**< first pending task which is waiting for msg */
37 } msg_info_t;
38 
39 /**
40  * mutex object
41  */
42 typedef struct queue_s {
43     blk_obj_t   blk_obj;    /**< Manage blocked tasks */
44     k_ringbuf_t ringbuf;    /**< Msg passing by ringbuf */
45     msg_q_t     msg_q;
46 #if (RHINO_CONFIG_KOBJ_LIST > 0)
47     klist_t     queue_item; /**< kobj list for statistics */
48 #endif
49     uint8_t     mm_alloc_flag;  /**< buffer from internal malloc or caller input */
50 } kqueue_t;
51 
52 /**
53  * Create a queue.
54  *
55  * @param[in]  queue    pointer to the queue (the space is provided outside, by user)
56  * @param[in]  name     name of the queue
57  * @param[in]  start    start address of the queue internal space
58  * @param[in]  msg_num  num of the msg
59  *
60  * @return  the operation status, RHINO_SUCCESS is OK, others is error
61  */
62 kstat_t krhino_queue_create(kqueue_t *queue, const name_t *name, void **start, size_t msg_num);
63 
64 /**
65  * Delete a queue.
66  *
67  * @param[in]  queue  pointer to the queue
68  *
69  * @return  the operation status, RHINO_SUCCESS is OK, others is error
70  */
71 kstat_t krhino_queue_del(kqueue_t *queue);
72 
73 #if (RHINO_CONFIG_KOBJ_DYN_ALLOC > 0)
74 /**
75  * Malloc and create a queue.
76  *
77  * @param[out] queue    pointer to the queue (the space is provided inside, from heap)
78  * @param[in]  name     name of the queue
79  * @param[in]  msg_num  num of the msg
80  *
81  * @return  the operation status, RHINO_SUCCESS is OK, others is error
82  */
83 kstat_t krhino_queue_dyn_create(kqueue_t **queue, const name_t *name, size_t msg_num);
84 
85 /**
86  * Delete and free a queue.
87  *
88  * @param[in]  queue    pointer to the queue
89  *
90  * @return  the operation status, RHINO_SUCCESS is OK, others is error
91  */
92 kstat_t krhino_queue_dyn_del(kqueue_t *queue);
93 #endif
94 
95 /**
96  * Send a message to the tail of a queue.
97  *
98  * @param[in]  queue  pointer to the queue
99  * @param[in]  msg    msg to send
100  *
101  * @return  the operation status, RHINO_SUCCESS is OK, others is error
102  */
103 kstat_t krhino_queue_back_send(kqueue_t *queue, void *msg);
104 
105 /**
106  * Send a message to the tail of a queue and wake all pending tasks.
107  *
108  * @param[in]  queue    pointer to the queue
109  * @param[in]  msg      msg to send
110  *
111  * @return  the operation status, RHINO_SUCCESS is OK, others is error
112  */
113 kstat_t krhino_queue_all_send(kqueue_t *queue, void *msg);
114 
115 /**
116  * Receive message from a queue, task may be blocked.
117  *
118  * @param[in]   queue   pointer to the queue
119  * @param[in]   ticks   ticks to wait before receive
120  * @param[out]  msg     buf to save msg
121  *
122  * @return  the operation status, RHINO_SUCCESS is OK, others is error
123  */
124 kstat_t krhino_queue_recv(kqueue_t *queue, tick_t ticks, void **msg);
125 
126 /**
127  * Check a queue full or not.
128  *
129  * @param[in]  queue    pointer to the queue
130  *
131  * @return  the operation status, RHINO_QUEUE_FULL/RHINO_QUEUE_NOT_FULL
132  */
133 kstat_t krhino_queue_is_full(kqueue_t *queue);
134 
135 /**
136  * Reset a queue and discard all unreceived messages.
137  *
138  * @param[in]  queue    pointer to the queue
139  *
140  * @return  the operation status, RHINO_SUCCESS is OK, others is error
141  */
142 kstat_t krhino_queue_flush(kqueue_t *queue);
143 
144 /**
145  * Get information of a queue.
146  *
147  * @param[in]   queue   pointer to the queue
148  * @param[out]  info    buf to save msg-info
149  *
150  * @return  the operation status, RHINO_SUCCESS is OK, others is error
151  */
152 kstat_t krhino_queue_info_get(kqueue_t *queue, msg_info_t *info);
153 
154 /** @} */
155 
156 #ifdef __cplusplus
157 }
158 #endif
159 
160 #endif /* K_QUEUE_H */
161 
162