pico]OS
1.0.4
|
Functions | |
POSEXTERN POSSEMA_t POSCALL | posSemaCreate (INT_t initcount) |
POSEXTERN void POSCALL | posSemaDestroy (POSSEMA_t sema) |
POSEXTERN VAR_t POSCALL | posSemaGet (POSSEMA_t sema) |
POSEXTERN VAR_t POSCALL | posSemaSignal (POSSEMA_t sema) |
POSEXTERN VAR_t POSCALL | posSemaWait (POSSEMA_t sema, UINT_t timeoutticks) |
Semaphores are basically used for task synchronization. Task synchronization means that only a defined number of tasks can execute a piece of code. Usually, a semaphore is initialized with the value 1, so only one task can hold the semaphore at a time (Please read the chapter about the mutex functions also if you are interested in task synchronization).
The second thing semaphores can be used for is sending signals to waiting tasks. Imagine you have an interrupt service routine that is triggered every time when a big chunk of data is available on a device. The data is to big to process them directly in the ISR. The ISR will only trigger a semaphore (it will signalize the semaphore), and a high priorized task waiting for the semaphore will be set to running state and will process the data from the device. In this case, the semaphore would be initialized with zero when it is created. The first task requesting the semaphore would block immediately, and can only proceed its work when the semaphore is triggered from outside.
Semaphores are implemented as counters. A task requesting a semaphore (via posSemaGet or posSemaWait) will decrement the counter. If the counter is zero, the task willing to decrement the counter is blocked. When a semaphore is signaled (via posSemaSignal), the counter is incremented. If the counter reaches a positive, nonzero value, the highest priorized task pending on the semaphore is set to running state and can decrement the counter by itself.
Semaphore function. Allocates a new semaphore object.
initcount | Initial semaphore count (see detailed semaphore description). |
POSEXTERN void POSCALL posSemaDestroy | ( | POSSEMA_t | sema | ) |
Semaphore function. Frees a no more needed semaphore object.
sema | handle to the semaphore object. |
Semaphore function. This function tries to get the semaphore object. If the semaphore is in nonsignalized state (that means its counter is zero or less), this function blocks the task execution until the semaphore gets signaled.
sema | handle to the semaphore object. |
Semaphore function. This function signalizes a semaphore object, that means it increments the semaphore counter and sets tasks pending on the semaphore to running state, when the counter reaches a positive, nonzero value.
sema | handle to the semaphore object. |
Semaphore function. This function tries to get the semaphore object. If the semaphore is in nonsignalized state (that means its counter is zero or less), this function blocks the task execution until the semaphore gets signaled or a timeout happens.
sema | handle to the semaphore object. |
timeoutticks | timeout in timer ticks (see HZ define and MS macro). If this parameter is set to zero, the function immediately returns. If this parameter is set to INFINITE, the function will never time out. |