📄 os_q.lst
字号:
131 if (OSIntNesting > 0) { /* See if called from ISR ... */
132 return ((OS_EVENT *)0); /* ... can't CREATE from an ISR */
133 }
134 OS_ENTER_CRITICAL();
135 pevent = OSEventFreeList; /* Get next free event control block */
136 if (OSEventFreeList != (OS_EVENT *)0) { /* See if pool of free ECB pool was empty */
137 OSEventFreeList = (OS_EVENT *)OSEventFreeList->OSEventPtr;
138 }
139 OS_EXIT_CRITICAL();
140 if (pevent != (OS_EVENT *)0) { /* See if we have an event control block */
141 OS_ENTER_CRITICAL();
142 pq = OSQFreeList; /* Get a free queue control block */
143 if (pq != (OS_Q *)0) { /* Were we able to get a queue control block ? */
144 OSQFreeList = OSQFreeList->OSQPtr; /* Yes, Adjust free list pointer to next free*/
145 OS_EXIT_CRITICAL();
146 pq->OSQStart = start; /* Initialize the queue */
147 pq->OSQEnd = &start[size];
148 pq->OSQIn = start;
149 pq->OSQOut = start;
150 pq->OSQSize = size;
151 pq->OSQEntries = 0;
152 pevent->OSEventType = OS_EVENT_TYPE_Q;
153 pevent->OSEventCnt = 0;
154 pevent->OSEventPtr = pq;
155 #if OS_EVENT_NAME_SIZE > 1
156 pevent->OSEventName[0] = '?'; /* Unknown name */
157 pevent->OSEventName[1] = OS_ASCII_NUL;
158 #endif
159 OS_EventWaitListInit(pevent); /* Initalize the wait list */
160 } else {
161 pevent->OSEventPtr = (void *)OSEventFreeList; /* No, Return event control block on error */
162 OSEventFreeList = pevent;
163 OS_EXIT_CRITICAL();
164 pevent = (OS_EVENT *)0;
165 }
166 }
167 return (pevent);
168 }
169 /*$PAGE*/
170 /*
171 *********************************************************************************************************
172 * DELETE A MESSAGE QUEUE
173 *
174 * Description: This function deletes a message queue and readies all tasks pending on the queue.
175 *
176 * Arguments : pevent is a pointer to the event control block associated with the desired
177 * queue.
178 *
179 * opt determines delete options as follows:
180 * opt == OS_DEL_NO_PEND Delete the queue ONLY if no task pending
181 * opt == OS_DEL_ALWAYS Deletes the queue even if tasks are waiting.
182 * In this case, all the tasks pending will be readied.
183 *
184 * perr is a pointer to an error code that can contain one of the following values:
185 * OS_ERR_NONE The call was successful and the queue was deleted
186 * OS_ERR_DEL_ISR If you tried to delete the queue from an ISR
187 * OS_ERR_INVALID_OPT An invalid option was specified
188 * OS_ERR_TASK_WAITING One or more tasks were waiting on the queue
189 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a queue
190 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
191 *
192 * Returns : pevent upon error
193 * (OS_EVENT *)0 if the queue was successfully deleted.
194 *
195 * Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of
196 * the queue MUST check the return code of OSQPend().
197 * 2) OSQAccept() callers will not know that the intended queue has been deleted unless
198 * they check 'pevent' to see that it's a NULL pointer.
199 * 3) This call can potentially disable interrupts for a long time. The interrupt disable
200 * time is directly proportional to the number of tasks waiting on the queue.
201 * 4) Because ALL tasks pending on the queue will be readied, you MUST be careful in
202 * applications where the queue is used for mutual exclusion because the resource(s)
203 * will no longer be guarded by the queue.
204 * 5) If the storage for the message queue was allocated dynamically (i.e. using a malloc()
205 * type call) then your application MUST release the memory storage by call the counterpart
206 * call of the dynamic allocation scheme used. If the queue storage was created statically
207 * then, the storage can be reused.
208 *********************************************************************************************************
209 */
210
211 #if OS_Q_DEL_EN > 0
212 OS_EVENT *OSQDel (OS_EVENT *pevent, INT8U opt, INT8U *perr)
213 {
214 BOOLEAN tasks_waiting;
215 OS_EVENT *pevent_return;
216 OS_Q *pq;
217 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
218 OS_CPU_SR cpu_sr = 0;
219 #endif
220
221
222
223 #if OS_ARG_CHK_EN > 0
224 if (perr == (INT8U *)0) { /* Validate 'perr' */
225 return (pevent);
226 }
227 if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
228 *perr = OS_ERR_PEVENT_NULL;
229 return (pevent);
230 }
231 #endif
232 if (pevent->OSEventType != OS_EVENT_TYPE_Q) { /* Validate event block type */
233 *perr = OS_ERR_EVENT_TYPE;
234 return (pevent);
235 }
236 if (OSIntNesting > 0) { /* See if called from ISR ... */
237 *perr = OS_ERR_DEL_ISR; /* ... can't DELETE from an ISR */
238 return (pevent);
239 }
240 OS_ENTER_CRITICAL();
241 if (pevent->OSEventGrp != 0) { /* See if any tasks waiting on queue */
242 tasks_waiting = OS_TRUE; /* Yes */
243 } else {
244 tasks_waiting = OS_FALSE; /* No */
245 }
246 switch (opt) {
247 case OS_DEL_NO_PEND: /* Delete queue only if no task waiting */
248 if (tasks_waiting == OS_FALSE) {
249 #if OS_EVENT_NAME_SIZE > 1
250 pevent->OSEventName[0] = '?'; /* Unknown name */
251 pevent->OSEventName[1] = OS_ASCII_NUL;
252 #endif
253 pq = (OS_Q *)pevent->OSEventPtr; /* Return OS_Q to free list */
254 pq->OSQPtr = OSQFreeList;
255 OSQFreeList = pq;
256 pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
257 pevent->OSEventPtr = OSEventFreeList; /* Return Event Control Block to free list */
258 pevent->OSEventCnt = 0;
259 OSEventFreeList = pevent; /* Get next free event control block */
260 OS_EXIT_CRITICAL();
261 *perr = OS_ERR_NONE;
262 pevent_return = (OS_EVENT *)0; /* Queue has been deleted */
263 } else {
264 OS_EXIT_CRITICAL();
265 *perr = OS_ERR_TASK_WAITING;
266 pevent_return = pevent;
267 }
268 break;
269
270 case OS_DEL_ALWAYS: /* Always delete the queue */
271 while (pevent->OSEventGrp != 0) { /* Ready ALL tasks waiting for queue */
272 (void)OS_EventTaskRdy(pevent, (void *)0, OS_STAT_Q, OS_STAT_PEND_OK);
273 }
274 #if OS_EVENT_NAME_SIZE > 1
275 pevent->OSEventName[0] = '?'; /* Unknown name */
276 pevent->OSEventName[1] = OS_ASCII_NUL;
277 #endif
278 pq = (OS_Q *)pevent->OSEventPtr; /* Return OS_Q to free list */
279 pq->OSQPtr = OSQFreeList;
280 OSQFreeList = pq;
281 pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
282 pevent->OSEventPtr = OSEventFreeList; /* Return Event Control Block to free list */
283 pevent->OSEventCnt = 0;
284 OSEventFreeList = pevent; /* Get next free event control block */
285 OS_EXIT_CRITICAL();
286 if (tasks_waiting == OS_TRUE) { /* Reschedule only if task(s) were waiting */
287 OS_Sched(); /* Find highest priority task ready to run */
288 }
289 *perr = OS_ERR_NONE;
290 pevent_return = (OS_EVENT *)0; /* Queue has been deleted */
291 break;
292
293 default:
294 OS_EXIT_CRITICAL();
295 *perr = OS_ERR_INVALID_OPT;
296 pevent_return = pevent;
297 break;
298 }
299 return (pevent_return);
300 }
301 #endif
302
303 /*$PAGE*/
304 /*
305 *********************************************************************************************************
306 * FLUSH QUEUE
307 *
308 * Description : This function is used to flush the contents of the message queue.
309 *
310 * Arguments : none
311 *
312 * Returns : OS_ERR_NONE upon success
313 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a queue
314 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer
315 *
316 * WARNING : You should use this function with great care because, when to flush the queue, you LOOSE
317 * the references to what the queue entries are pointing to and thus, you could cause
318 * 'memory leaks'. In other words, the data you are pointing to that's being referenced
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -