Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sync.h
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
23 #if !defined(_SYNC_H)
24 # define _SYNC_H
25 
26 # include <pthread.h>
27 
28 class Mutex
29 {
30  public:
31  Mutex();
32  ~Mutex();
33 
34  inline bool IsOk() const { return m_isOk; }
35 
36  int Lock();
37  int TryLock();
38  int Unlock();
39 
40  friend class Condition;
41 
42  private:
43  pthread_mutex_t m_mutex;
44  bool m_isOk;
45 };
46 
47 class Condition
48 {
49  public:
50  Condition(Mutex& mutex);
51  ~Condition();
52 
53  inline bool IsOk() const { return (m_isOk && m_mutex.IsOk()); }
54 
55  int Wait();
56  int Signal();
57  int Broadcast();
58 
59  private:
61  pthread_cond_t m_cond;
62  bool m_isOk;
63 };
64 
65 class Semaphore
66 {
67  public:
68  Semaphore(int initialcount = 0, int maxcount = 0);
69  ~Semaphore();
70 
71  inline bool IsOk() const { return m_isOk; }
72 
73  bool Wait();
74  bool TryWait();
75  bool Post();
76 
77  private:
80  int m_count;
82  bool m_isOk;
83 };
84 
85 #endif // !defined(_SYNC_H)
86 
87 
88 
89 
90 
91 
Condition(Mutex &mutex)
Definition: Sync.C:116
bool IsOk() const
Definition: Sync.h:53
int m_count
Definition: Sync.h:80
bool IsOk() const
Definition: Sync.h:34
int Lock()
Definition: Sync.C:50
~Mutex()
Definition: Sync.C:39
Definition: Sync.h:65
bool m_isOk
Definition: Sync.h:44
Definition: Sync.h:28
int Unlock()
Definition: Sync.C:95
bool m_isOk
Definition: Sync.h:82
~Condition()
Definition: Sync.C:129
Condition m_cond
Definition: Sync.h:79
Mutex & m_mutex
Definition: Sync.h:60
pthread_cond_t m_cond
Definition: Sync.h:61
pthread_mutex_t m_mutex
Definition: Sync.h:43
int TryLock()
Definition: Sync.C:74
Definition: Sync.h:47
Mutex m_mutex
Definition: Sync.h:78
bool Wait()
Definition: Sync.C:191
bool TryWait()
Definition: Sync.C:208
Semaphore(int initialcount=0, int maxcount=0)
Definition: Sync.C:170
int Wait()
Definition: Sync.C:140
bool Post()
Definition: Sync.C:223
int Broadcast()
Definition: Sync.C:160
Mutex()
Definition: Sync.C:29
int m_maxcount
Definition: Sync.h:81
int Signal()
Definition: Sync.C:150
bool IsOk() const
Definition: Sync.h:71
~Semaphore()
Definition: Sync.C:187
bool m_isOk
Definition: Sync.h:62