📄 os_sem.lst
字号:
203 3 #endif
204 3 pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
205 3 pevent->OSEventPtr = OSEventFreeList; /* Return Event Control Block to free list */
206 3 pevent->OSEventCnt = 0;
207 3 OSEventFreeList = pevent; /* Get next free event control block */
208 3 OS_EXIT_CRITICAL();
209 3 *perr = OS_ERR_NONE;
210 3 pevent_return = (OS_EVENT *)0; /* Semaphore has been deleted */
211 3 } else {
212 3 OS_EXIT_CRITICAL();
213 3 *perr = OS_ERR_TASK_WAITING;
214 3 pevent_return = pevent;
215 3 }
216 2 break;
217 2
218 2 case OS_DEL_ALWAYS: /* Always delete the semaphore */
219 2 while (pevent->OSEventGrp != 0) { /* Ready ALL tasks waiting for semaphore */
220 3 (void)OS_EventTaskRdy(pevent, (void *)0, OS_STAT_SEM, OS_STAT_PEND_OK);
221 3 }
222 2 #if OS_EVENT_NAME_SIZE > 1
223 2 pevent->OSEventName[0] = '?'; /* Unknown name */
224 2 pevent->OSEventName[1] = OS_ASCII_NUL;
225 2 #endif
226 2 pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
227 2 pevent->OSEventPtr = OSEventFreeList; /* Return Event Control Block to free list */
228 2 pevent->OSEventCnt = 0;
229 2 OSEventFreeList = pevent; /* Get next free event control block */
230 2 OS_EXIT_CRITICAL();
231 2 if (tasks_waiting == OS_TRUE) { /* Reschedule only if task(s) were waiting */
232 3 OS_Sched(); /* Find highest priority task ready to run */
233 3 }
234 2 *perr = OS_ERR_NONE;
235 2 pevent_return = (OS_EVENT *)0; /* Semaphore has been deleted */
236 2 break;
237 2
238 2 default:
239 2 OS_EXIT_CRITICAL();
240 2 *perr = OS_ERR_INVALID_OPT;
C51 COMPILER V7.50 OS_SEM 12/14/2007 08:25:43 PAGE 5
241 2 pevent_return = pevent;
242 2 break;
243 2 }
244 1 return (pevent_return);
245 1 }
246 #endif
247
248 /*$PAGE*/
249 /*
250 *********************************************************************************************************
251 * PEND ON SEMAPHORE
252 *
253 * Description: This function waits for a semaphore.
254 *
255 * Arguments : pevent is a pointer to the event control block associated with the desired
256 * semaphore.
257 *
258 * timeout is an optional timeout period (in clock ticks). If non-zero, your task will
259 * wait for the resource up to the amount of time specified by this argument.
260 * If you specify 0, however, your task will wait forever at the specified
261 * semaphore or, until the resource becomes available (or the event occurs).
262 *
263 * perr is a pointer to where an error message will be deposited. Possible error
264 * messages are:
265 *
266 * OS_ERR_NONE The call was successful and your task owns the resource
267 * or, the event you are waiting for occurred.
268 * OS_ERR_TIMEOUT The semaphore was not received within the specified
269 * 'timeout'.
270 * OS_ERR_PEND_ABORT The wait on the semaphore was aborted.
271 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore.
272 * OS_ERR_PEND_ISR If you called this function from an ISR and the result
273 * would lead to a suspension.
274 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
275 * OS_ERR_PEND_LOCKED If you called this function when the scheduler is locked
276 *
277 * Returns : none
278 *********************************************************************************************************
279 */
280
281 void OSSemPend (OS_EVENT *pevent, INT16U timeout, INT8U *perr) reentrant
282 {
283 1 INT8U pend_stat;
284 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
287 1
288 1
289 1
290 1 #if OS_ARG_CHK_EN > 0
if (perr == (INT8U *)0) { /* Validate 'perr' */
return;
}
if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
*perr = OS_ERR_PEVENT_NULL;
return;
}
#endif
299 1 if (pevent->OSEventType != OS_EVENT_TYPE_SEM) { /* Validate event block type */
300 2 *perr = OS_ERR_EVENT_TYPE;
301 2 return;
302 2 }
C51 COMPILER V7.50 OS_SEM 12/14/2007 08:25:43 PAGE 6
303 1 if (OSIntNesting > 0) { /* See if called from ISR ... */
304 2 *perr = OS_ERR_PEND_ISR; /* ... can't PEND from an ISR */
305 2 return;
306 2 }
307 1 if (OSLockNesting > 0) { /* See if called with scheduler locked ... */
308 2 *perr = OS_ERR_PEND_LOCKED; /* ... can't PEND when locked */
309 2 return;
310 2 }
311 1 OS_ENTER_CRITICAL();
312 1 if (pevent->OSEventCnt > 0) { /* If sem. is positive, resource available ... */
313 2 pevent->OSEventCnt--; /* ... decrement semaphore only if positive. */
314 2 OS_EXIT_CRITICAL();
315 2 *perr = OS_ERR_NONE;
316 2 return;
317 2 }
318 1 /* Otherwise, must wait until event occurs */
319 1 OSTCBCur->OSTCBStat |= OS_STAT_SEM; /* Resource not available, pend on semaphore */
320 1 OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;
321 1 OSTCBCur->OSTCBDly = timeout; /* Store pend timeout in TCB */
322 1 OS_EventTaskWait(pevent); /* Suspend task until event or timeout occurs */
323 1 OS_EXIT_CRITICAL();
324 1 OS_Sched(); /* Find next highest priority task ready */
325 1 OS_ENTER_CRITICAL();
326 1 if (OSTCBCur->OSTCBStatPend != OS_STAT_PEND_OK) { /* See if we timed-out or aborted */
327 2 pend_stat = OSTCBCur->OSTCBStatPend;
328 2 OS_EventTOAbort(pevent);
329 2 OS_EXIT_CRITICAL();
330 2 switch (pend_stat) {
331 3 case OS_STAT_PEND_TO:
332 3 default:
333 3 *perr = OS_ERR_TIMEOUT; /* Indicate that didn't get event within TO */
334 3 break;
335 3
336 3 case OS_STAT_PEND_ABORT:
337 3 *perr = OS_ERR_PEND_ABORT; /* Indicate that we aborted */
338 3 break;
339 3 }
340 2 return;
341 2 }
342 1 OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0;
343 1 OS_EXIT_CRITICAL();
344 1 *perr = OS_ERR_NONE;
345 1 }
346
347 /*$PAGE*/
348 /*
349 *********************************************************************************************************
350 * ABORT WAITING ON A SEMAPHORE
351 *
352 * Description: This function aborts & readies any tasks currently waiting on a semaphore. This function
353 * should be used to fault-abort the wait on the semaphore, rather than to normally signal
354 * the semaphore via OSSemPost().
355 *
356 * Arguments : pevent is a pointer to the event control block associated with the desired
357 * semaphore.
358 *
359 * opt determines the type of ABORT performed:
360 * OS_PEND_OPT_NONE ABORT wait for a single task (HPT) waiting on the
361 * semaphore
362 * OS_PEND_OPT_BROADCAST ABORT wait for ALL tasks that are waiting on the
363 * semaphore
364 *
C51 COMPILER V7.50 OS_SEM 12/14/2007 08:25:43 PAGE 7
365 * perr is a pointer to where an error message will be deposited. Possible error
366 * messages are:
367 *
368 * OS_ERR_NONE No tasks were waiting on the semaphore.
369 * OS_ERR_PEND_ABORT At least one task waiting on the semaphore was readied
370 * and informed of the aborted wait; check return value
371 * for the number of tasks whose wait on the semaphore
372 * was aborted.
373 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore.
374 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
375 *
376 * Returns : == 0 if no tasks were waiting on the semaphore, or upon error.
377 * > 0 if one or more tasks waiting on the semaphore are now readied and informed.
378 *********************************************************************************************************
379 */
380
381 #if OS_SEM_PEND_ABORT_EN > 0
382 INT8U OSSemPendAbort (OS_EVENT *pevent, INT8U opt, INT8U *perr) reentrant
383 {
384 1 INT8U nbr_tasks;
385 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
388 1
389 1
390 1
391 1 #if OS_ARG_CHK_EN > 0
if (perr == (INT8U *)0) { /* Validate 'perr' */
return (0);
}
if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
*perr = OS_ERR_PEVENT_NULL;
return (0);
}
#endif
400 1 if (pevent->OSEventType != OS_EVENT_TYPE_SEM) { /* Validate event block type */
401 2 *perr = OS_ERR_EVENT_TYPE;
402 2 return (0);
403 2 }
404 1 OS_ENTER_CRITICAL();
405 1 if (pevent->OSEventGrp != 0) { /* See if any task waiting on semaphore? */
406 2 nbr_tasks = 0;
407 2 switch (opt) {
408 3 case OS_PEND_OPT_BROADCAST: /* Do we need to abort ALL waiting tasks? */
409 3 while (pevent->OSEventGrp != 0) { /* Yes, ready ALL tasks waiting on semaphore */
410 4 (void)OS_EventTaskRdy(pevent, (void *)0, OS_STAT_SEM, OS_STAT_PEND_ABORT);
411 4 nbr_tasks++;
412 4 }
413 3 break;
414 3
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -