blob: 8abc4d18b9fd549a9dd9af9e41271d8615ccf6ed [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/*************************************************************
2Description:
3 mbtk_queue.c
4 Used to implement mobiletek standard queue interfaces
5Author:
6 YangDagang
7Date:
8 2019-7-13
9*************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include "mbtk_type.h"
14#include "mbtk_queue.h"
15
16/*************************************************************
17 Constants and Macros
18*************************************************************/
19
20/*************************************************************
21 Definitions:enum,struct,union
22*************************************************************/
23
24
25/*************************************************************
26 Variables:local,extern
27*************************************************************/
28
29/*************************************************************
30 Local Function Declaration
31*************************************************************/
32
33/*************************************************************
34 Extern Function Declaration
35*************************************************************/
36
37/*************************************************************
38 Function Definitions
39*************************************************************/
40/*=============================================
41FUNCTION
42 mbtk_tcpip_ptr
43
44DESCRIPTION
45 get tcpip information pointer
46
47DEPENDENCIES
48 None
49
50PARAMETERS
51 None
52
53RETURN VALUE
54 tcpip information pointer
55
56SIDE EFFECTS
57 None
58=============================================*/
59void mbtk_queue_init(mbtk_queue_node_t *queue)
60{
61 if(queue == NULL)
62 {
63 return;
64 }
65 queue->count = 0;
66 queue->payload = NULL;
67 queue->front = NULL;
68 queue->rear = NULL;
69 mbtk_mutex_init(&queue->mutex);
70}
71
72int mbtk_queue_put(mbtk_queue_node_t *queue,void *payload)
73{
74 mbtk_queue_node_t *curr = NULL;
75 mbtk_queue_node_t *p = NULL;
76
77 if(payload == NULL || queue == NULL)
78 {
79 return -1;
80 }
81 mbtk_mutex_lock(&queue->mutex);
82 curr = (mbtk_queue_node_t*)malloc(sizeof(mbtk_queue_node_t));
83 if(curr == NULL)
84 {
85 mbtk_mutex_unlock(&queue->mutex);
86 return -1;
87 }
88 curr->count = 0;
89 curr->rear = NULL;
90 curr->payload = payload;
91 if(queue->front != NULL)
92 {
93 p = queue->front;
94 p->rear = curr;
95 curr->front = p;
96 queue->front = curr;
97 }
98 else
99 {
100 //fisrt add node
101 queue->front = curr;
102 queue->rear = curr;
103 curr->front = queue;
104 }
105 queue->count++;
106 mbtk_mutex_unlock(&queue->mutex);
107 return 0;
108}
109
110int mbtk_queue_insert(mbtk_queue_node_t *queue,void *payload)
111{
112 mbtk_queue_node_t *curr = NULL;
113 mbtk_queue_node_t *p = NULL;
114
115 if(payload == NULL || queue == NULL)
116 {
117 return -1;
118 }
119 mbtk_mutex_lock(&queue->mutex);
120 curr = (mbtk_queue_node_t*)malloc(sizeof(mbtk_queue_node_t));
121 if(curr == NULL)
122 {
123 mbtk_mutex_unlock(&queue->mutex);
124 return -1;
125 }
126 curr->rear = NULL;
127 curr->count = 0;
128 curr->payload = payload;
129 if(queue->front != NULL)
130 {
131 p = queue->rear;
132 queue->rear = curr;
133 curr->front = queue;
134 curr->rear = p;
135 p->front = curr;
136 }
137 else
138 {
139 //fisrt add node
140 queue->front = curr;
141 queue->rear = curr;
142 curr->front = queue;
143 }
144 queue->count++;
145 mbtk_mutex_unlock(&queue->mutex);
146 return 0;
147}
148
149void* mbtk_queue_get(mbtk_queue_node_t *queue)
150{
151 mbtk_queue_node_t *curr = NULL;
152 void *payload = NULL;
153
154 if((queue == NULL) || mbtk_queue_empty(queue))
155 {
156 return NULL;
157 }
158 mbtk_mutex_lock(&queue->mutex);
159 curr = queue->rear;
160 payload = curr->payload;
161 if(curr->rear != NULL)
162 {
163 queue->rear = curr->rear;
164 curr->rear->front = queue;
165 }
166 else
167 {
168 queue->front = NULL;
169 queue->rear = NULL;
170 }
171 free(curr);
172 curr = NULL;
173 queue->count--;
174 mbtk_mutex_unlock(&queue->mutex);
175 return payload;
176}
177
178bool mbtk_queue_empty(mbtk_queue_node_t *queue)
179{
180 bool is_empty = FALSE;
181
182 mbtk_mutex_lock(&queue->mutex);
183 if(queue != NULL)
184 {
185 if((queue->front == NULL) && (queue->rear == NULL))
186 {
187 is_empty = TRUE;
188 }
189 }
190 mbtk_mutex_unlock(&queue->mutex);
191 return is_empty;
192}
193
194bool mbtk_queue_is_full(mbtk_queue_node_t *queue)
195{
196 bool is_full = FALSE;
197
198 mbtk_mutex_lock(&queue->mutex);
199 if((queue != NULL) && (queue->count == MBTK_QUEUE_MAX_NUM))
200 {
201 is_full = TRUE;
202 }
203 mbtk_mutex_unlock(&queue->mutex);
204 return is_full;
205}
206
207void mbtk_queue_deinit(mbtk_queue_node_t *queue)
208{
209 if(queue == NULL)
210 {
211 return;
212 }
213 queue->count = 0;
214 queue->payload = NULL;
215 queue->front = NULL;
216 queue->rear = NULL;
217 mbtk_mutex_deinit(&queue->mutex);
218}