2 *********************************************************************************************************
7 * (c) Copyright 1992-2007, Micrium, Weston, FL
11 * By : Jean J. Labrosse
16 * uC/OS-II is provided in source form for FREE evaluation, for educational use or for peaceful research.
17 * If you plan on using uC/OS-II in a commercial product you need to contact Micriµm to properly license
18 * its use in your product. We provide ALL the source code for your convenience and to help you experience
19 * uC/OS-II. The fact that the source is provided does NOT mean that you can use it without paying a
21 *********************************************************************************************************
24 #ifndef OS_MASTER_FILE
31 *********************************************************************************************************
34 * Description: This function checks the semaphore to see if a resource is available or, if an event
35 * occurred. Unlike OSSemPend(), OSSemAccept() does not suspend the calling task if the
36 * resource is not available or the event did not occur.
38 * Arguments : pevent is a pointer to the event control block
40 * Returns : > 0 if the resource is available or the event did not occur the semaphore is
41 * decremented to obtain the resource.
42 * == 0 if the resource is not available or the event did not occur or,
43 * if 'pevent' is a NULL pointer or,
44 * if you didn't pass a pointer to a semaphore
45 *********************************************************************************************************
48 #if OS_SEM_ACCEPT_EN > 0
49 INT16U
OSSemAccept (OS_EVENT
*pevent
)
52 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
59 if (pevent
== (OS_EVENT
*)0) { /* Validate 'pevent' */
63 if (pevent
->OSEventType
!= OS_EVENT_TYPE_SEM
) { /* Validate event block type */
67 cnt
= pevent
->OSEventCnt
;
68 if (cnt
> 0) { /* See if resource is available */
69 pevent
->OSEventCnt
--; /* Yes, decrement semaphore and notify caller */
72 return (cnt
); /* Return semaphore count */
78 *********************************************************************************************************
81 * Description: This function creates a semaphore.
83 * Arguments : cnt is the initial value for the semaphore. If the value is 0, no resource is
84 * available (or no event has occurred). You initialize the semaphore to a
85 * non-zero value to specify how many resources are available (e.g. if you have
86 * 10 resources, you would initialize the semaphore to 10).
88 * Returns : != (void *)0 is a pointer to the event control block (OS_EVENT) associated with the
90 * == (void *)0 if no event control blocks were available
91 *********************************************************************************************************
94 OS_EVENT
*OSSemCreate (INT16U cnt
)
97 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
103 if (OSIntNesting
> 0) { /* See if called from ISR ... */
104 return ((OS_EVENT
*)0); /* ... can't CREATE from an ISR */
107 pevent
= OSEventFreeList
; /* Get next free event control block */
108 if (OSEventFreeList
!= (OS_EVENT
*)0) { /* See if pool of free ECB pool was empty */
109 OSEventFreeList
= (OS_EVENT
*)OSEventFreeList
->OSEventPtr
;
112 if (pevent
!= (OS_EVENT
*)0) { /* Get an event control block */
113 pevent
->OSEventType
= OS_EVENT_TYPE_SEM
;
114 pevent
->OSEventCnt
= cnt
; /* Set semaphore value */
115 pevent
->OSEventPtr
= (void *)0; /* Unlink from ECB free list */
116 #if OS_EVENT_NAME_SIZE > 1
117 pevent
->OSEventName
[0] = '?'; /* Unknown name */
118 pevent
->OSEventName
[1] = OS_ASCII_NUL
;
120 OS_EventWaitListInit(pevent
); /* Initialize to 'nobody waiting' on sem. */
127 *********************************************************************************************************
130 * Description: This function deletes a semaphore and readies all tasks pending on the semaphore.
132 * Arguments : pevent is a pointer to the event control block associated with the desired
135 * opt determines delete options as follows:
136 * opt == OS_DEL_NO_PEND Delete semaphore ONLY if no task pending
137 * opt == OS_DEL_ALWAYS Deletes the semaphore even if tasks are waiting.
138 * In this case, all the tasks pending will be readied.
140 * perr is a pointer to an error code that can contain one of the following values:
141 * OS_ERR_NONE The call was successful and the semaphore was deleted
142 * OS_ERR_DEL_ISR If you attempted to delete the semaphore from an ISR
143 * OS_ERR_INVALID_OPT An invalid option was specified
144 * OS_ERR_TASK_WAITING One or more tasks were waiting on the semaphore
145 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore
146 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
148 * Returns : pevent upon error
149 * (OS_EVENT *)0 if the semaphore was successfully deleted.
151 * Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of
152 * the semaphore MUST check the return code of OSSemPend().
153 * 2) OSSemAccept() callers will not know that the intended semaphore has been deleted unless
154 * they check 'pevent' to see that it's a NULL pointer.
155 * 3) This call can potentially disable interrupts for a long time. The interrupt disable
156 * time is directly proportional to the number of tasks waiting on the semaphore.
157 * 4) Because ALL tasks pending on the semaphore will be readied, you MUST be careful in
158 * applications where the semaphore is used for mutual exclusion because the resource(s)
159 * will no longer be guarded by the semaphore.
160 *********************************************************************************************************
163 #if OS_SEM_DEL_EN > 0
164 OS_EVENT
*OSSemDel (OS_EVENT
*pevent
, INT8U opt
, INT8U
*perr
)
166 BOOLEAN tasks_waiting
;
167 OS_EVENT
*pevent_return
;
168 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
169 OS_CPU_SR cpu_sr
= 0;
174 #if OS_ARG_CHK_EN > 0
175 if (perr
== (INT8U
*)0) { /* Validate 'perr' */
178 if (pevent
== (OS_EVENT
*)0) { /* Validate 'pevent' */
179 *perr
= OS_ERR_PEVENT_NULL
;
183 if (pevent
->OSEventType
!= OS_EVENT_TYPE_SEM
) { /* Validate event block type */
184 *perr
= OS_ERR_EVENT_TYPE
;
187 if (OSIntNesting
> 0) { /* See if called from ISR ... */
188 *perr
= OS_ERR_DEL_ISR
; /* ... can't DELETE from an ISR */
192 if (pevent
->OSEventGrp
!= 0) { /* See if any tasks waiting on semaphore */
193 tasks_waiting
= OS_TRUE
; /* Yes */
195 tasks_waiting
= OS_FALSE
; /* No */
198 case OS_DEL_NO_PEND
: /* Delete semaphore only if no task waiting */
199 if (tasks_waiting
== OS_FALSE
) {
200 #if OS_EVENT_NAME_SIZE > 1
201 pevent
->OSEventName
[0] = '?'; /* Unknown name */
202 pevent
->OSEventName
[1] = OS_ASCII_NUL
;
204 pevent
->OSEventType
= OS_EVENT_TYPE_UNUSED
;
205 pevent
->OSEventPtr
= OSEventFreeList
; /* Return Event Control Block to free list */
206 pevent
->OSEventCnt
= 0;
207 OSEventFreeList
= pevent
; /* Get next free event control block */
210 pevent_return
= (OS_EVENT
*)0; /* Semaphore has been deleted */
213 *perr
= OS_ERR_TASK_WAITING
;
214 pevent_return
= pevent
;
218 case OS_DEL_ALWAYS
: /* Always delete the semaphore */
219 while (pevent
->OSEventGrp
!= 0) { /* Ready ALL tasks waiting for semaphore */
220 (void)OS_EventTaskRdy(pevent
, (void *)0, OS_STAT_SEM
, OS_STAT_PEND_OK
);
222 #if OS_EVENT_NAME_SIZE > 1
223 pevent
->OSEventName
[0] = '?'; /* Unknown name */
224 pevent
->OSEventName
[1] = OS_ASCII_NUL
;
226 pevent
->OSEventType
= OS_EVENT_TYPE_UNUSED
;
227 pevent
->OSEventPtr
= OSEventFreeList
; /* Return Event Control Block to free list */
228 pevent
->OSEventCnt
= 0;
229 OSEventFreeList
= pevent
; /* Get next free event control block */
231 if (tasks_waiting
== OS_TRUE
) { /* Reschedule only if task(s) were waiting */
232 OS_Sched(); /* Find highest priority task ready to run */
235 pevent_return
= (OS_EVENT
*)0; /* Semaphore has been deleted */
240 *perr
= OS_ERR_INVALID_OPT
;
241 pevent_return
= pevent
;
244 return (pevent_return
);
250 *********************************************************************************************************
253 * Description: This function waits for a semaphore.
255 * Arguments : pevent is a pointer to the event control block associated with the desired
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).
263 * perr is a pointer to where an error message will be deposited. Possible error
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
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
278 *********************************************************************************************************
281 void OSSemPend (OS_EVENT
*pevent
, INT16U timeout
, INT8U
*perr
)
283 INT8U y
; // add by wakeup
284 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
285 OS_CPU_SR cpu_sr
= 0;
288 timeout
= timeout
+ OSTime
; // timeout is the right time to wakeup
290 #if OS_ARG_CHK_EN > 0
291 if (perr
== (INT8U
*)0) { /* Validate 'perr' */
294 if (pevent
== (OS_EVENT
*)0) { /* Validate 'pevent' */
295 *perr
= OS_ERR_PEVENT_NULL
;
299 if (pevent
->OSEventType
!= OS_EVENT_TYPE_SEM
) { /* Validate event block type */
300 *perr
= OS_ERR_EVENT_TYPE
;
303 if (OSIntNesting
> 0) { /* See if called from ISR ... */
304 *perr
= OS_ERR_PEND_ISR
; /* ... can't PEND from an ISR */
307 if (OSLockNesting
> 0) { /* See if called with scheduler locked ... */
308 *perr
= OS_ERR_PEND_LOCKED
; /* ... can't PEND when locked */
312 if (pevent
->OSEventCnt
> 0) { /* If sem. is positive, resource available ... */
313 pevent
->OSEventCnt
--; /* ... decrement semaphore only if positive. */
318 /* Otherwise, must wait until event occurs */
319 OSTCBCur
->OSTCBStat
|= OS_STAT_SEM
; /* Resource not available, pend on semaphore */
320 OSTCBCur
->OSTCBStatPend
= OS_STAT_PEND_OK
;
321 OSTCBCur
->OSTCBDly
= timeout
; /* Store pend timeout in TCB */
322 OS_EventTaskWait(pevent
); /* Suspend task until event or timeout occurs */
324 OS_Sched(); /* Find next highest priority task ready */
326 switch (OSTCBCur
->OSTCBStatPend
) { /* See if we timed-out or aborted */
327 case OS_STAT_PEND_OK
:
331 case OS_STAT_PEND_ABORT
:
332 *perr
= OS_ERR_PEND_ABORT
; /* Indicate that we aborted */
335 case OS_STAT_PEND_TO
:
337 OS_EventTaskRemove(OSTCBCur
, pevent
);
338 *perr
= OS_ERR_TIMEOUT
; /* Indicate that we didn't get event within TO */
341 OSTCBCur
->OSTCBStat
= OS_STAT_RDY
; /* Set task status to ready */
342 OSTCBCur
->OSTCBStatPend
= OS_STAT_PEND_OK
; /* Clear pend status */
343 OSTCBCur
->OSTCBEventPtr
= (OS_EVENT
*)0; /* Clear event pointers */
344 #if (OS_EVENT_MULTI_EN > 0)
345 OSTCBCur
->OSTCBEventMultiPtr
= (OS_EVENT
**)0;
347 if(timeout
< OSNextUp
) OSNextUp
= timeout
;
353 *********************************************************************************************************
354 * ABORT WAITING ON A SEMAPHORE
356 * Description: This function aborts & readies any tasks currently waiting on a semaphore. This function
357 * should be used to fault-abort the wait on the semaphore, rather than to normally signal
358 * the semaphore via OSSemPost().
360 * Arguments : pevent is a pointer to the event control block associated with the desired
363 * opt determines the type of ABORT performed:
364 * OS_PEND_OPT_NONE ABORT wait for a single task (HPT) waiting on the
366 * OS_PEND_OPT_BROADCAST ABORT wait for ALL tasks that are waiting on the
369 * perr is a pointer to where an error message will be deposited. Possible error
372 * OS_ERR_NONE No tasks were waiting on the semaphore.
373 * OS_ERR_PEND_ABORT At least one task waiting on the semaphore was readied
374 * and informed of the aborted wait; check return value
375 * for the number of tasks whose wait on the semaphore
377 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore.
378 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
380 * Returns : == 0 if no tasks were waiting on the semaphore, or upon error.
381 * > 0 if one or more tasks waiting on the semaphore are now readied and informed.
382 *********************************************************************************************************
385 #if OS_SEM_PEND_ABORT_EN > 0
386 INT8U
OSSemPendAbort (OS_EVENT
*pevent
, INT8U opt
, INT8U
*perr
)
389 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
390 OS_CPU_SR cpu_sr
= 0;
395 #if OS_ARG_CHK_EN > 0
396 if (perr
== (INT8U
*)0) { /* Validate 'perr' */
399 if (pevent
== (OS_EVENT
*)0) { /* Validate 'pevent' */
400 *perr
= OS_ERR_PEVENT_NULL
;
404 if (pevent
->OSEventType
!= OS_EVENT_TYPE_SEM
) { /* Validate event block type */
405 *perr
= OS_ERR_EVENT_TYPE
;
409 if (pevent
->OSEventGrp
!= 0) { /* See if any task waiting on semaphore? */
412 case OS_PEND_OPT_BROADCAST
: /* Do we need to abort ALL waiting tasks? */
413 while (pevent
->OSEventGrp
!= 0) { /* Yes, ready ALL tasks waiting on semaphore */
414 (void)OS_EventTaskRdy(pevent
, (void *)0, OS_STAT_SEM
, OS_STAT_PEND_ABORT
);
419 case OS_PEND_OPT_NONE
:
420 default: /* No, ready HPT waiting on semaphore */
421 (void)OS_EventTaskRdy(pevent
, (void *)0, OS_STAT_SEM
, OS_STAT_PEND_ABORT
);
426 OS_Sched(); /* Find HPT ready to run */
427 *perr
= OS_ERR_PEND_ABORT
;
432 return (0); /* No tasks waiting on semaphore */
438 *********************************************************************************************************
439 * POST TO A SEMAPHORE
441 * Description: This function signals a semaphore
443 * Arguments : pevent is a pointer to the event control block associated with the desired
446 * Returns : OS_ERR_NONE The call was successful and the semaphore was signaled.
447 * OS_ERR_SEM_OVF If the semaphore count exceeded its limit. In other words, you have
448 * signalled the semaphore more often than you waited on it with either
449 * OSSemAccept() or OSSemPend().
450 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore
451 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
452 *********************************************************************************************************
455 INT8U
OSSemPost (OS_EVENT
*pevent
)
457 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
458 OS_CPU_SR cpu_sr
= 0;
463 #if OS_ARG_CHK_EN > 0
464 if (pevent
== (OS_EVENT
*)0) { /* Validate 'pevent' */
465 return (OS_ERR_PEVENT_NULL
);
468 if (pevent
->OSEventType
!= OS_EVENT_TYPE_SEM
) { /* Validate event block type */
469 return (OS_ERR_EVENT_TYPE
);
472 if (pevent
->OSEventGrp
!= 0) { /* See if any task waiting for semaphore */
473 /* Ready HPT waiting on event */
474 (void)OS_EventTaskRdy(pevent
, (void *)0, OS_STAT_SEM
, OS_STAT_PEND_OK
);
476 OS_Sched(); /* Find HPT ready to run */
477 return (OS_ERR_NONE
);
479 if (pevent
->OSEventCnt
< 65535u) { /* Make sure semaphore will not overflow */
480 pevent
->OSEventCnt
++; /* Increment semaphore count to register event */
482 return (OS_ERR_NONE
);
484 OS_EXIT_CRITICAL(); /* Semaphore value has reached its maximum */
485 return (OS_ERR_SEM_OVF
);
490 *********************************************************************************************************
493 * Description: This function obtains information about a semaphore
495 * Arguments : pevent is a pointer to the event control block associated with the desired
498 * p_sem_data is a pointer to a structure that will contain information about the
501 * Returns : OS_ERR_NONE The call was successful and the message was sent
502 * OS_ERR_EVENT_TYPE If you are attempting to obtain data from a non semaphore.
503 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
504 * OS_ERR_PDATA_NULL If 'p_sem_data' is a NULL pointer
505 *********************************************************************************************************
508 #if OS_SEM_QUERY_EN > 0
509 INT8U
OSSemQuery (OS_EVENT
*pevent
, OS_SEM_DATA
*p_sem_data
)
511 #if OS_LOWEST_PRIO <= 63
519 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
520 OS_CPU_SR cpu_sr
= 0;
525 #if OS_ARG_CHK_EN > 0
526 if (pevent
== (OS_EVENT
*)0) { /* Validate 'pevent' */
527 return (OS_ERR_PEVENT_NULL
);
529 if (p_sem_data
== (OS_SEM_DATA
*)0) { /* Validate 'p_sem_data' */
530 return (OS_ERR_PDATA_NULL
);
533 if (pevent
->OSEventType
!= OS_EVENT_TYPE_SEM
) { /* Validate event block type */
534 return (OS_ERR_EVENT_TYPE
);
537 p_sem_data
->OSEventGrp
= pevent
->OSEventGrp
; /* Copy message mailbox wait list */
538 psrc
= &pevent
->OSEventTbl
[0];
539 pdest
= &p_sem_data
->OSEventTbl
[0];
540 for (i
= 0; i
< OS_EVENT_TBL_SIZE
; i
++) {
543 p_sem_data
->OSCnt
= pevent
->OSEventCnt
; /* Get semaphore count */
545 return (OS_ERR_NONE
);
547 #endif /* OS_SEM_QUERY_EN */
551 *********************************************************************************************************
554 * Description: This function sets the semaphore count to the value specified as an argument. Typically,
555 * this value would be 0.
557 * You would typically use this function when a semaphore is used as a signaling mechanism
558 * and, you want to reset the count value.
560 * Arguments : pevent is a pointer to the event control block
562 * cnt is the new value for the semaphore count. You would pass 0 to reset the
565 * perr is a pointer to an error code returned by the function as follows:
567 * OS_ERR_NONE The call was successful and the semaphore value was set.
568 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore.
569 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
570 * OS_ERR_TASK_WAITING If tasks are waiting on the semaphore.
571 *********************************************************************************************************
574 #if OS_SEM_SET_EN > 0
575 void OSSemSet (OS_EVENT
*pevent
, INT16U cnt
, INT8U
*perr
)
577 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
578 OS_CPU_SR cpu_sr
= 0;
583 #if OS_ARG_CHK_EN > 0
584 if (perr
== (INT8U
*)0) { /* Validate 'perr' */
587 if (pevent
== (OS_EVENT
*)0) { /* Validate 'pevent' */
588 *perr
= OS_ERR_PEVENT_NULL
;
592 if (pevent
->OSEventType
!= OS_EVENT_TYPE_SEM
) { /* Validate event block type */
593 *perr
= OS_ERR_EVENT_TYPE
;
598 if (pevent
->OSEventCnt
> 0) { /* See if semaphore already has a count */
599 pevent
->OSEventCnt
= cnt
; /* Yes, set it to the new value specified. */
601 if (pevent
->OSEventGrp
== 0) { /* See if task(s) waiting? */
602 pevent
->OSEventCnt
= cnt
; /* No, OK to set the value */
604 *perr
= OS_ERR_TASK_WAITING
;
611 #endif /* OS_SEM_EN */