Counting Semaphore


Detailed Description

Copyright (C) 2007-2008 Ola Lundqvist <ola@inguza.com>

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 Documentation

#define initSemaphore ( x,
 )     x = v

Initializes the semaphore

Parameters:
x Semaphore
v Number of shared resources available (1 for mutex).

#define isLockedSemaphore (  )     (x < 0)

Check if semaphore is locked

Parameters:
x Semaphore
Returns:
  • TRUE if locked
  • FALSE if unlocked

#define isUnLockedSemaphore (  )     (x >= 0)

Check if semaphore is unlocked

Parameters:
x Semaphore
Returns:
  • TRUE if unlocked
  • FALSE if locked

#define lockSemaphore (  )     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.

Parameters:
x Semaphore

#define unLockSemaphore (  )     x++

Unlock a semaphore

Parameters:
x Semaphore

#define willLockSemaphore (  )     (x <= 0)

Check if thread will be blocked if trying to lock the semaphore

Parameters:
x Semaphore
Returns:
  • TRUE if it will be blocked
  • FALSE if not


Generated on Mon Apr 7 22:13:48 2008 for SocketDispatcher by  doxygen 1.5.1