HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_TimeGate.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_TimeGate.h (UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_TIMEGATE_H_INCLUDED__
12 #define __UT_TIMEGATE_H_INCLUDED__
13 
14 #include "UT_API.h"
15 
16 namespace UT_TimeGateImpl
17 {
18  UT_API extern volatile int theClockCount;
19  UT_API extern void startClockThreadIfNeeded();
20 }
21 
22 /// Used to reduce the frequency of checks gated by a time out (in
23 /// milliseconds at resolution of ~100ms). Note that the frequency is not
24 /// guaranteed, esp. if you try to call tryAcquire() from multiple threads on
25 /// the same UT_TimeGate object.
26 template <int TIMEOUT_MS>
28 {
29 public:
30  inline UT_TimeGate()
31  : myTickCount(0)
32  {
34  }
35 
36  /// Return true if at least TIMEOUT_MS has elapsed from the last time this
37  /// method returned true. Resolution is ~100ms.
38  inline bool tryAcquire()
39  {
40  const int tick = UT_TimeGateImpl::theClockCount;
41  bool ok = (tick - myTickCount) > ((TIMEOUT_MS + 99) / 100);
42  if (ok)
43  myTickCount = tick;
44  return ok;
45  }
46 
47  /// Return true if at least TIMEOUT_MS has elapsed from the last call to
48  /// acquire(), without actually acquiring.
49  inline bool canAcquire() const
50  {
51  const int tick = UT_TimeGateImpl::theClockCount;
52  return (tick - myTickCount) > ((TIMEOUT_MS + 99) / 100);
53  }
54 
55  /// Resets the gate so that canAcquire()/tryAcquire() will return false
56  /// until at least TIMEOUT_MS has elapsed.
57  inline void acquire()
58  {
59  myTickCount = UT_TimeGateImpl::theClockCount;
60  }
61 
62 private:
63  // Volatile because multiple threads use the same instance
64  volatile int myTickCount;
65 };
66 
67 /// Dynamic version of timegate where the tick threshold isn't
68 /// hardcoded by the template specification.
70 {
71 public:
72  inline UT_TimeGateD()
73  : myTickCount(0)
74  , myTickThresholdInTenths(1)
75  {
77  }
78 
79  inline void setThreshold(int threshold_ms)
80  {
81  myTickThresholdInTenths = (threshold_ms + 99) / 100;
82  }
83 
84  /// Return true if at least TIMEOUT_MS has elapsed from the last time this
85  /// method returned true. Resolution is ~100ms.
86  inline bool tryAcquire()
87  {
88  const int tick = UT_TimeGateImpl::theClockCount;
89  bool ok = (tick - myTickCount) > myTickThresholdInTenths;
90  if (ok)
91  myTickCount = tick;
92  return ok;
93  }
94 
95  /// Return true if at least TIMEOUT_MS has elapsed from the last call to
96  /// acquire(), without actually acquiring.
97  inline bool canAcquire() const
98  {
99  const int tick = UT_TimeGateImpl::theClockCount;
100  return (tick - myTickCount) > myTickThresholdInTenths;
101  }
102 
103  /// Resets the gate so that canAcquire()/tryAcquire() will return false
104  /// until at least TIMEOUT_MS has elapsed.
105  inline void acquire()
106  {
107  myTickCount = UT_TimeGateImpl::theClockCount;
108  }
109 
110 private:
111  // Volatile because multiple threads use the same instance
112  volatile int myTickCount;
113  int myTickThresholdInTenths;
114 };
115 
116 #endif // __UT_TIMEGATE_H_INCLUDED__
bool tryAcquire()
Definition: UT_TimeGate.h:86
UT_API void startClockThreadIfNeeded()
void acquire()
Definition: UT_TimeGate.h:57
bool canAcquire() const
Definition: UT_TimeGate.h:49
bool canAcquire() const
Definition: UT_TimeGate.h:97
#define UT_API
Definition: UT_API.h:14
void acquire()
Definition: UT_TimeGate.h:105
UT_API volatile int theClockCount
bool tryAcquire()
Definition: UT_TimeGate.h:38
void setThreshold(int threshold_ms)
Definition: UT_TimeGate.h:79