OSSem Pend

void OSSemPend(OS_EVENT *pevent, INT16U timeout, XNT80 *err);

OSSemPend () is used when a task desires to get exclusive access to a resource, synchronize its activities with an ISR, a task, or until an event occurs. If a task calls OSSemPend () and the value of the semaphore is greater than 0, then OSSemPend () will decrement the semaphore and return to its caller. However, if the value of the semaphore is equal to zero, OSSemPend () places the calling task in the waiting list for the semaphore. The task will thus wait until a task or an ISR signals the semaphore or, the specified timeout expires. If the semaphore is signaled before the timeout expires, jiC/OS-H will resume the highest priority task that is waiting for the semaphore. A pended task that has been suspended with OSTaskSuspend () can obtain the semaphore. The task will, however, remain suspended until the task is resumed by calling OSTaskResume ().

Arguments pevent is a pointer to the semaphore. This pointer is returned to your application when the semaphore is created (see OSSemCreate () on page 538).

timeout is used to allow the task to resume execution if a message is not received from the mailbox within the specified number of clock ticks. A timeout value of 0 indicates that the task desires to wait forever for the message. The maximum timeout is 65535 clock ticks. The timeout value is not synchronized with the clock tick. The timeout count starts being decremented on the next clock tick which could potentially occur immediately.

err is a pointer to a variable which will be used to hold an error code. OSSemPend () sets *err to either:

1. OS_NO_ERR, the semaphore is available

2. OS__TIMEOUT, the semaphore was not signaled within the specified timeout

3. OS_ERR_PEND_ISR, you called this function from an ISR and (iC/OS-II would have to suspend the ISR. In general, you should not call OSMboxPend (). jiC/OS-H checks for this situation in case you do anyway.

Return Value

None

Notes/Warnings

Semaphores must be created before they are used.

540 — Embedded Systems Building Blocks, Second Edition Example

OS_EVENT *DispSem;

void DispTask(void *pdata) {

INT8U err;

OSSemPend(DispSem, 0, &err);

/* The only way this task continues is if _ */ /* _ the semaphore is signaled! */

Was this article helpful?

0 0

Post a comment