Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

rwil::Semaphore Class Reference

Creates a semaphore. More...

#include <Synchronization.h>

List of all members.

Public Methods

 Semaphore (int count)
 Create a semaphore with a given initial value. More...

 Semaphore ()
 Create a semaphore with an initial value of 0. More...

void Wait ()
 Wait functions will return successfully when the count is greater than one. More...

bool TryWait ()
 Non-blocking Wait on semaphore. More...

bool Wait (int millisecondTimeout)
 Blocking wait on semaphore with timeout. More...

void Signal ()
 Signal the semaphore. More...


Private Attributes

Mutex m_mutex
 Mutex used internally to maintain thread safety. More...

int m_count
 The semaphore's value. More...


Detailed Description

Creates a semaphore.

This is a general-purpose cross-platform counting semaphore.

Definition at line 103 of file Synchronization.h.


Constructor & Destructor Documentation

rwil::Semaphore::Semaphore int    count
 

Create a semaphore with a given initial value.

When the count is greater than 0 a Wait call will succeed.

Parameters:
count  The initial value of the semaphore

Definition at line 121 of file Synchronization.cpp.

00121 : m_count(count) {}

rwil::Semaphore::Semaphore  
 

Create a semaphore with an initial value of 0.

The semaphore must be signalled at least once before a wait call will be successful.

Definition at line 122 of file Synchronization.cpp.

00122 : m_count(0) {}


Member Function Documentation

void rwil::Semaphore::Signal  
 

Signal the semaphore.

Increment the semaphore. When it becomes > 0, if a thread is waiting on it, that thread will be released.

Definition at line 192 of file Synchronization.cpp.

References m_count, and m_mutex.

00193   {
00194     m_mutex.Wait();
00195     m_count++;
00196     m_mutex.Release();
00197   }

bool rwil::Semaphore::TryWait  
 

Non-blocking Wait on semaphore.

The semaphore will check to see if its value is > 0. If so, it will be decremented and the semaphore will return true.

Returns:
True if the wait was successsful, False if not.

Definition at line 144 of file Synchronization.cpp.

References m_count, and m_mutex.

00145   {
00146     m_mutex.Wait();
00147     if(m_count == 0)
00148       {
00149         return false;
00150       }
00151     m_count--;
00152     m_mutex.Release();
00153     return true;
00154   }

bool rwil::Semaphore::Wait int    millisecondTimeout
 

Blocking wait on semaphore with timeout.

This semaphore will block on its value for at least millisecondTimeout milliseconds or until the value of semaphore becomes > 0. If it becomes > 0 the semaphore will be decremented and the semaphore will return true.

Parameters:
millisecondTimeout  The number of milliseconds to block before returning false.
Returns:
True if the semaphore was decremented, false if it timed out.

Definition at line 156 of file Synchronization.cpp.

References m_count, and m_mutex.

00157   {
00158 #ifdef _WIN32
00159     int timeout = GetTickCount() + millisecondTimeout;
00160 #endif
00161 #ifdef LINUX
00162     timeb timeout;
00163     ftime(&timeout);
00164     int tempMilli = (int)timeout.millitm + millisecondTimeout;
00165     timeout.time = timeout.time + tempMilli/1000;
00166     timeout.millitm = tempMilli /1000;
00167 #endif
00168     m_mutex.Wait();
00169     while(m_count == 0)
00170       {
00171         m_mutex.Release();
00172 #ifdef _WIN32
00173         Sleep(0);
00174         if(GetTickCount() > timeout) return false;
00175 #endif
00176 #ifdef LINUX
00177         sched_yield();
00178         timeb curtime;
00179         ftime(&curtime);
00180         if((timeout.time <= curtime.time) && (timeout.millitm <= curtime.millitm))
00181           {
00182             return false;
00183           }
00184 #endif
00185         m_mutex.Wait();
00186       }
00187     m_count--;
00188     m_mutex.Release();
00189     return true;
00190   }

void rwil::Semaphore::Wait  
 

Wait functions will return successfully when the count is greater than one.

The semaphore will block until its value is > 0. Then the semaphore value will be decremented and the function will return.

Definition at line 126 of file Synchronization.cpp.

References m_count, and m_mutex.

00127   {
00128     m_mutex.Wait();
00129     while(m_count == 0)
00130       {
00131         m_mutex.Release();
00132 #ifdef _WIN32
00133         Sleep(0);
00134 #endif
00135 #ifdef LINUX
00136         sched_yield();
00137 #endif
00138         m_mutex.Wait();
00139       }
00140     m_count--;
00141     m_mutex.Release();
00142   }


Member Data Documentation

int rwil::Semaphore::m_count [private]
 

The semaphore's value.

Definition at line 153 of file Synchronization.h.

Referenced by Signal(), TryWait(), and Wait().

Mutex rwil::Semaphore::m_mutex [private]
 

Mutex used internally to maintain thread safety.

Definition at line 151 of file Synchronization.h.

Referenced by Signal(), TryWait(), and Wait().


The documentation for this class was generated from the following files:
Generated on Tue Apr 9 13:53:09 2002 for RWIL - Real World Interface Library by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001