HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_Condition.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Condition.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  * A condition class the implements pthread-like condition variables.
10  */
11 
12 #ifndef __UT_Condition_h__
13 #define __UT_Condition_h__
14 
15 #include "UT_API.h"
16 #include "UT_Lock.h"
17 #include <SYS/SYS_Types.h>
18 #ifdef USE_PTHREADS
19  #include <pthread.h>
20 #endif
21 #if defined(WIN32)
22  #include <SYS/SYS_AtomicInt.h>
23  #include <SYS/SYS_Handle.h>
24 #endif
25 
27 {
28 public:
29  UT_Condition();
30  ~UT_Condition();
31 
32  // Block until we're triggered. The lock must be held when this method is
33  // called it will be released when waiting for the trigger and re-acquired
34  // when this method returns.
35  void waitForTrigger(UT_Lock &lock)
36  #ifdef USE_PTHREADS
37  {
38  // We inline this method for speed.
39  #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
40  int r = pthread_cond_wait(&myCond, &lock.myLock);
41  UT_ASSERT(r == 0);
42  #else
43  pthread_cond_wait(&myCond, &lock.myLock);
44  #endif
45  }
46  #else
47  ;
48  #endif
49 
50  // Trigger all waiting threads. The caller is responsible for holding
51  // the same lock that was passed to waitForTrigger(), otherwise lost
52  // wakeups are possible.
53  void triggerAll()
54  #ifdef USE_PTHREADS
55  {
56  // We inline this method for speed.
57  #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
58  int r = pthread_cond_broadcast(&myCond);
59  UT_ASSERT(r == 0);
60  #else
61  pthread_cond_broadcast(&myCond);
62  #endif
63  }
64  #else
65  ;
66  #endif
67 
68  // Trigger one waiting thread. The caller is responsible for holding
69  // the same lock that was passed to waitForTrigger(), otherwise lost
70  // wakeups are possible.
71  void triggerOne()
72  #ifdef USE_PTHREADS
73  {
74  // We inline this method for speed.
75  #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
76  int r = pthread_cond_signal(&myCond);
77  UT_ASSERT(r == 0);
78  #else
79  pthread_cond_signal(&myCond);
80  #endif
81  }
82  #else
83  ;
84  #endif
85 
86 private:
87  // Prevent users from calling the copy constructor or assignment operator
88  // by declaring them as private. They are not implemented.
89  UT_Condition(const UT_Condition &copy);
90  UT_Condition &operator=(const UT_Condition &copy);
91 
92 #ifdef USE_PTHREADS
93  pthread_cond_t myCond;
94 #elif defined(WIN32)
95  UT_Lock myCriticalSectionLock;
96  SYS_AtomicInt32 myNumWaitingThreads;
97  SYS_WinHandle myEvents[2];
98 #endif
99 };
100 
101 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102 
103 #if defined(WIN32)
104  #define USE_SPINLOCK_FOR_CONDITIONLOCK
105 #endif
106 
107 #ifdef USE_SPINLOCK_FOR_CONDITIONLOCK
108  #include "UT_SpinLock.h"
109 #endif
110 
111 // A condition class that doesn't require an external lock
113 {
114 public:
116  #ifdef USE_SPINLOCK_FOR_CONDITIONLOCK
117  : myLock(false)
118  #else
119  : myLock(*this)
120  #endif
121  {}
122 
123  // Block until we're triggered
125  {
126  #ifdef USE_SPINLOCK_FOR_CONDITIONLOCK
127  myLock.waitForTrigger();
128  #else
129  myLock.lock();
130  myCond.waitForTrigger(myLock);
131  myLock.unlock();
132  #endif
133  }
134 
135  // Trigger one waiting thread
136  void triggerOne()
137  #ifdef USE_SPINLOCK_FOR_CONDITIONLOCK
138  { myLock.trigger(); }
139  #else
140  { myCond.triggerOne(); }
141  #endif
142 
143 private:
144  UT_ConditionLock(const UT_ConditionLock &copy); // not allowed
145  UT_ConditionLock &operator=(const UT_ConditionLock &copy); // not allowed
146 
147 #ifdef USE_SPINLOCK_FOR_CONDITIONLOCK
148  UT_SpinLock myLock;
149 #else
150  UT_Lock myLock;
151  UT_Condition myCond;
152 #endif
153 };
154 
155 #endif
#define UT_API
Definition: UT_API.h:12
void * SYS_WinHandle
Definition: SYS_Handle.h:19
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
GLboolean r
Definition: glcorearb.h:1221