All rights reserved.
Counting Sempaphore is a simple function to handle simple counting semaphores for a number of shared resources.
For more informaton about semaphores read: http://en.wikipedia.org/wiki/Semaphore_(programming)
Defines | |
| #define | initSemaphore(x, v) x = v |
| #define | lockSemaphore(x) while (willLockSemaphore(x)) { usleep(1); } x-- |
| #define | willLockSemaphore(x) (x <= 0) |
| #define | isLockedSemaphore(x) (x < 0) |
| #define | isUnLockedSemaphore(x) (x >= 0) |
| #define | unLockSemaphore(x) x++ |
| #define initSemaphore | ( | x, | |||
| v | ) | x = v |
Initializes the semaphore
| x | Semaphore | |
| v | Number of shared resources available (1 for mutex). |
| #define isLockedSemaphore | ( | x | ) | (x < 0) |
Check if semaphore is locked
| x | Semaphore |
| #define isUnLockedSemaphore | ( | x | ) | (x >= 0) |
Check if semaphore is unlocked
| x | Semaphore |
| #define lockSemaphore | ( | x | ) | while (willLockSemaphore(x)) { usleep(1); } x-- |
Lock a semaphore. Usleep for 1 microsecond and then check again if semaphore is locked by some other thread. Do this until it is unlocked.
| x | Semaphore |
| #define unLockSemaphore | ( | x | ) | x++ |
Unlock a semaphore
| x | Semaphore |
| #define willLockSemaphore | ( | x | ) | (x <= 0) |
Check if thread will be blocked if trying to lock the semaphore
| x | Semaphore |
1.5.1