Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Semaphore Class Reference

#include <Sync.h>

Collaboration diagram for Semaphore:

Public Member Functions

 Semaphore (int initialcount=0, int maxcount=0)
 
 ~Semaphore ()
 
bool IsOk () const
 
bool Wait ()
 
bool TryWait ()
 
bool Post ()
 

Private Attributes

Mutex m_mutex
 
Condition m_cond
 
int m_count
 
int m_maxcount
 
bool m_isOk
 

Detailed Description

Definition at line 65 of file Sync.h.

Constructor & Destructor Documentation

Semaphore ( int  initialcount = 0,
int  maxcount = 0 
)

Definition at line 170 of file Sync.C.

References Mutex::IsOk(), Condition::IsOk(), m_cond, m_count, m_isOk, m_maxcount, and m_mutex.

171 : m_cond(m_mutex)
172 {
173  if ((initialcount < 0 || maxcount < 0)
174  || ((maxcount > 0) && (initialcount > maxcount))) {
175  std::cerr << "Semaphore::Semaphore(): invalid initial or maximal count."
176  << std::endl;
177 
178  m_isOk = false;
179  } else {
180  m_maxcount = maxcount;
181  m_count = initialcount;
182  }
183 
184  m_isOk = m_mutex.IsOk() && m_cond.IsOk();
185 }
bool IsOk() const
Definition: Sync.h:53
int m_count
Definition: Sync.h:80
bool IsOk() const
Definition: Sync.h:34
bool m_isOk
Definition: Sync.h:82
Condition m_cond
Definition: Sync.h:79
Mutex m_mutex
Definition: Sync.h:78
int m_maxcount
Definition: Sync.h:81

Here is the call graph for this function:

~Semaphore ( )

Definition at line 187 of file Sync.C.

188 {
189 }

Member Function Documentation

bool IsOk ( ) const
inline

Definition at line 71 of file Sync.h.

References m_isOk.

71 { return m_isOk; }
bool m_isOk
Definition: Sync.h:82
bool Post ( )

Definition at line 223 of file Sync.C.

References Mutex::Lock(), m_cond, m_count, m_maxcount, m_mutex, Condition::Signal(), and Mutex::Unlock().

224 {
225  m_mutex.Lock();
226 
227  if (m_maxcount > 0 && m_count == m_maxcount) {
228  m_mutex.Unlock();
229  return false;
230  }
231 
232  ++m_count;
233 
234  int result = m_cond.Signal();
235  m_mutex.Unlock();
236 
237  return (result == 0);
238 }
int m_count
Definition: Sync.h:80
int Lock()
Definition: Sync.C:50
int Unlock()
Definition: Sync.C:95
Condition m_cond
Definition: Sync.h:79
Mutex m_mutex
Definition: Sync.h:78
int m_maxcount
Definition: Sync.h:81
int Signal()
Definition: Sync.C:150

Here is the call graph for this function:

bool TryWait ( )

Definition at line 208 of file Sync.C.

References Mutex::Lock(), m_count, m_mutex, and Mutex::Unlock().

209 {
210  m_mutex.Lock();
211 
212  if (m_count == 0) {
213  m_mutex.Unlock();
214  return false;
215  }
216 
217  --m_count;
218  m_mutex.Unlock();
219 
220  return true;
221 }
int m_count
Definition: Sync.h:80
int Lock()
Definition: Sync.C:50
int Unlock()
Definition: Sync.C:95
Mutex m_mutex
Definition: Sync.h:78

Here is the call graph for this function:

bool Wait ( )

Definition at line 191 of file Sync.C.

References Mutex::Lock(), m_cond, m_count, m_mutex, Mutex::Unlock(), and Condition::Wait().

192 {
193  m_mutex.Lock();
194 
195  while (m_count == 0) {
196  if (m_cond.Wait() != 0) {
197  m_mutex.Unlock();
198  return false;
199  }
200  }
201 
202  --m_count;
203  m_mutex.Unlock();
204 
205  return true;
206 }
int m_count
Definition: Sync.h:80
int Lock()
Definition: Sync.C:50
int Unlock()
Definition: Sync.C:95
Condition m_cond
Definition: Sync.h:79
Mutex m_mutex
Definition: Sync.h:78
int Wait()
Definition: Sync.C:140

Here is the call graph for this function:

Member Data Documentation

Condition m_cond
private

Definition at line 79 of file Sync.h.

Referenced by Post(), Semaphore(), and Wait().

int m_count
private

Definition at line 80 of file Sync.h.

Referenced by Post(), Semaphore(), TryWait(), and Wait().

bool m_isOk
private

Definition at line 82 of file Sync.h.

Referenced by IsOk(), and Semaphore().

int m_maxcount
private

Definition at line 81 of file Sync.h.

Referenced by Post(), and Semaphore().

Mutex m_mutex
private

Definition at line 78 of file Sync.h.

Referenced by Post(), Semaphore(), TryWait(), and Wait().


The documentation for this class was generated from the following files: