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  /// Trigger all waiting threads.
38  ///
39  /// @note The caller should hold the same lock that was passed to
40  /// waitForTrigger() if precise scheduling of events is desired.
41  void triggerAll();
42 
43  /// Trigger one waiting thread.
44  ///
45  /// @note The caller should hold the same lock that was passed to
46  /// waitForTrigger() if precise scheduling of events is desired.
47  void triggerOne();
48 
49  // Prevent users from calling the copy constructor or assignment operator
50  // by declaring them as private. They are not implemented.
51  UT_Condition(const UT_Condition &copy) = delete;
52  UT_Condition &operator=(const UT_Condition &copy) = delete;
53 
54 private:
55 
56 #if defined(USE_PTHREADS)
57  pthread_cond_t myCond;
58 #elif defined(_WIN32)
59  void *myCond;
60 #else
61  #error Missing implementation
62 #endif
63 };
64 
65 //////////////////////////////////////////////////////////////////////////////
66 //
67 // PThreads Implementation
68 //
69 
70 #ifdef USE_PTHREADS
71 
72 inline
74 {
75  UT_VERIFY(pthread_cond_init(&myCond, 0) == 0);
76 }
77 
78 inline
80 {
81  // You should not destroy a condition upon which other threads are
82  // currently blocked. I suppose we trigger them here to try and detect
83  // errors more easily.
84  triggerAll();
85 
86  UT_VERIFY(pthread_cond_destroy(&myCond) == 0);
87 }
88 
89 // inline for speed
90 inline void
92 {
93  UT_VERIFY(pthread_cond_wait(&myCond, &lock.myLock) == 0);
94 }
95 
96 // inline for speed
97 inline void
99 {
100  UT_VERIFY(pthread_cond_broadcast(&myCond) == 0);
101 }
102 
103 // inline for speed
104 inline void
106 {
107  UT_VERIFY(pthread_cond_signal(&myCond) == 0);
108 }
109 
110 #endif // USE_PTHREADS
111 
112 //////////////////////////////////////////////////////////////////////////////
113 //
114 // Windows Implementation
115 //
116 
117 #ifdef _WIN32
118 
119 extern "C"
120 {
121  struct _RTL_CONDITION_VARIABLE;
122  using CONDITION_VARIABLE = _RTL_CONDITION_VARIABLE;
123 
124  struct _RTL_CRITICAL_SECTION;
125  using CRITICAL_SECTION = _RTL_CRITICAL_SECTION;
126 
127  __declspec(dllimport) void __stdcall
128  WakeConditionVariable(CONDITION_VARIABLE *ConditionVariable);
129  __declspec(dllimport) void __stdcall
130  WakeAllConditionVariable(CONDITION_VARIABLE *ConditionVariable);
131  __declspec(dllimport) int __stdcall
132  SleepConditionVariableCS(CONDITION_VARIABLE *ConditionVariable,
133  CRITICAL_SECTION *CriticalSection,
134  unsigned long dwMilliseconds);
135 }
136 
137 inline
139  : myCond{nullptr}
140 {
141 }
142 
143 inline
145 {
146  // You should not destroy a condition upon which other threads are
147  // currently blocked. I suppose we trigger them here to try and detect
148  // errors more easily.
149  triggerAll();
150 }
151 
152 // inline for speed
153 inline void
155 {
156  UT_VERIFY(SleepConditionVariableCS((CONDITION_VARIABLE*) &myCond,
157  (CRITICAL_SECTION*) &lock.myLock,
158  (unsigned long) -1));
159 }
160 
161 // inline for speed
162 inline void
164 {
165  WakeAllConditionVariable((CONDITION_VARIABLE*) &myCond);
166 }
167 
168 // inline for speed
169 inline void
171 {
172  WakeConditionVariable((CONDITION_VARIABLE*) &myCond);
173 }
174 
175 #endif // _WIN32
176 
177 #endif // __UT_Condition_h__
#define UT_API
Definition: UT_API.h:13
Condition synchronization primitive.
Definition: UT_Condition.h:25
void triggerOne()
void waitForTrigger(UT_Lock &lock)
void
Definition: png.h:1083
void triggerAll()
#define UT_VERIFY(expr)
Definition: UT_Assert.h:181
OIIO_API bool copy(string_view from, string_view to, std::string &err)