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  __declspec(dllimport) void __stdcall
125  WakeConditionVariable(CONDITION_VARIABLE *ConditionVariable);
126  __declspec(dllimport) void __stdcall
127  WakeAllConditionVariable(CONDITION_VARIABLE *ConditionVariable);
128 }
129 
130 inline
132  : myCond{nullptr}
133 {
134 }
135 
136 inline
138 {
139  // You should not destroy a condition upon which other threads are
140  // currently blocked. I suppose we trigger them here to try and detect
141  // errors more easily.
142  triggerAll();
143 }
144 
145 // inline for speed
146 inline void
148 {
149  lock.sleep(this);
150 }
151 
152 // inline for speed
153 inline void
155 {
156  WakeAllConditionVariable((CONDITION_VARIABLE*) &myCond);
157 }
158 
159 // inline for speed
160 inline void
162 {
163  WakeConditionVariable((CONDITION_VARIABLE*) &myCond);
164 }
165 
166 #endif // _WIN32
167 
168 #endif // __UT_Condition_h__
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)
void triggerAll()
#define UT_VERIFY(expr)
Definition: UT_Assert.h:202