A counting semaphore is a system variable of type unsigned char, unsigned int or unsigned long (depending on the OS_CSEM_SIZE constant). To use counting semaphores in your program you must define OS_ENABLE_CSEM in OSAcfg.h
The general difference between counting and binary semaphores is that counting semaphores can take any value in the range from 0 to the maximum value of the type (255, 65535, 4294967295).
Here is an example of defining a counting semaphore variable:
OST_CSEM mycsem;
To use a counting semaphore variable it must be created by the service OS_Csem_Create. After creation, the semaphore is zeroed. The value of a counting semaphore can be changed with any service of the management group (see below). When a task is waiting for a counting semaphore, it will be put in wait mode while the semaphore is zero. Once its value becomes non-zero, the task will be switched to ready mode. After the task gets control, the semaphore's value will be decremented by 1.
Note that there are two groups of services to change a semaphore's value:
In order to call the services from an interrupt, the constant OS_ENABLE_INT should be defined in OSAcfg.h.
A few words about the service OS_Csem_Signal. This service increases a semaphore's value by 1. If the semaphore is already set at its maximum, then its value will remain unchanged and the bEventError flag will be set.
Service | Arguments | Description | Properties |
---|---|---|---|
Creating | |||
OS_Csem_Create | (csem) | Create and zero counting semaphore | |
Management | |||
OS_Csem_Signal | (csem) | Increase counting semaphore's value by 1 (wait if overflow) | |
OS_Csem_Signal_Now | (csem) | Increase counting semaphore's value by 1 (don't wait if overflow) | |
OS_Csem_Set | (csem) | Set counting semaphore's value to 1 | |
OS_Csem_SetValue | (csem, value) | Set counting semaphore's value to a given value | |
OS_Csem_Reset | (csem) | Zero counting semaphore | |
Checking | |||
bool OS_Csem_Check | (csem) | Check if counting semaphore is set (non-zero) | |
OS_Csem_Accept | (csem) | Accept counting semaphore. Decrease its value by 1 | |
Waiting | |||
OS_Csem_Wait | (csem) | Wait for counting semaphore. Then decrease its value by 1 | |
OS_Csem_Wait_TO | (csem, timeout) | Wait for counting semaphore. Then decrease its value by 1. Exit if timeout expired |