HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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_Assert.h"
17 #include "UT_Lock.h"
18 
19 #ifdef USE_PTHREADS
20  #include <pthread.h>
21 #endif
22 
23 
24 /// Condition synchronization primitive
26 {
27 public:
28  UT_Condition();
29  ~UT_Condition();
30 
31  /// Block until we're triggered.
32  ///
33  /// The lock must be held when this method is called it will be released
34  /// when waiting for the trigger and re-acquired when this method returns.
35  void waitForTrigger(UT_Lock &lock);
36 
37  /// Wait with a timeout, returns true if the lock was re-acquired.
38  /// False means a timeout occurred.
39  bool waitForTriggerMS(UT_Lock &lock, int64 timeout_msec);
40 
41  /// Trigger all waiting threads.
42  ///
43  /// @note The caller should hold the same lock that was passed to
44  /// waitForTrigger() if precise scheduling of events is desired.
45  void triggerAll();
46 
47  /// Trigger one waiting thread.
48  ///
49  /// @note The caller should hold the same lock that was passed to
50  /// waitForTrigger() if precise scheduling of events is desired.
51  void triggerOne();
52 
53  // Prevent users from calling the copy constructor or assignment operator
54  // by declaring them as private. They are not implemented.
55  UT_Condition(const UT_Condition &copy) = delete;
56  UT_Condition &operator=(const UT_Condition &copy) = delete;
57 
58 private:
59 
60 #if defined(USE_PTHREADS)
61  pthread_cond_t myCond;
62 #elif defined(_WIN32)
63  void *myCond;
64 #else
65  #error Missing implementation
66 #endif
67 };
68 
69 //////////////////////////////////////////////////////////////////////////////
70 //
71 // PThreads Implementation
72 //
73 
74 #ifdef USE_PTHREADS
75 
76 inline
78 {
79  UT_VERIFY(pthread_cond_init(&myCond, 0) == 0);
80 }
81 
82 inline
84 {
85  // You should not destroy a condition upon which other threads are
86  // currently blocked. I suppose we trigger them here to try and detect
87  // errors more easily.
88  triggerAll();
89 
90  UT_VERIFY(pthread_cond_destroy(&myCond) == 0);
91 }
92 
93 // inline for speed
94 inline void
96 {
97  UT_VERIFY(pthread_cond_wait(&myCond, &lock.myLock) == 0);
98 }
99 
100 // inline for speed
101 inline void
103 {
104  UT_VERIFY(pthread_cond_broadcast(&myCond) == 0);
105 }
106 
107 // inline for speed
108 inline void
110 {
111  UT_VERIFY(pthread_cond_signal(&myCond) == 0);
112 }
113 
114 #endif // USE_PTHREADS
115 
116 //////////////////////////////////////////////////////////////////////////////
117 //
118 // Windows Implementation
119 //
120 
121 #ifdef _WIN32
122 
123 extern "C"
124 {
125  struct _RTL_CONDITION_VARIABLE;
126  using CONDITION_VARIABLE = _RTL_CONDITION_VARIABLE;
127 
128  __declspec(dllimport) void __stdcall
129  WakeConditionVariable(CONDITION_VARIABLE *ConditionVariable);
130  __declspec(dllimport) void __stdcall
131  WakeAllConditionVariable(CONDITION_VARIABLE *ConditionVariable);
132 }
133 
134 inline
136  : myCond{nullptr}
137 {
138 }
139 
140 inline
142 {
143  // You should not destroy a condition upon which other threads are
144  // currently blocked. I suppose we trigger them here to try and detect
145  // errors more easily.
146  triggerAll();
147 }
148 
149 // inline for speed
150 inline void
152 {
153  lock.sleep(this);
154 }
155 
156 inline bool
158 {
159  return lock.sleepMS(this, timeout_ms);
160 }
161 
162 // inline for speed
163 inline void
165 {
166  WakeAllConditionVariable((CONDITION_VARIABLE*) &myCond);
167 }
168 
169 // inline for speed
170 inline void
172 {
173  WakeConditionVariable((CONDITION_VARIABLE*) &myCond);
174 }
175 
176 #endif // _WIN32
177 
178 #endif // __UT_Condition_h__
bool waitForTriggerMS(UT_Lock &lock, int64 timeout_msec)
void
Definition: png.h:1083
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
#define UT_API
Definition: UT_API.h:14
Condition synchronization primitive.
Definition: UT_Condition.h:25
void triggerOne()
void waitForTrigger(UT_Lock &lock)
long long int64
Definition: SYS_Types.h:116
void triggerAll()
#define UT_VERIFY(expr)
Definition: UT_Assert.h:202
LeafData & operator=(const LeafData &)=delete