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