HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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()
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 #endif // __UT_TIMEGATE_H_INCLUDED__
UT_API void startClockThreadIfNeeded()
void acquire()
Definition: UT_TimeGate.h:57
#define UT_API
Definition: UT_API.h:12
bool canAcquire()
Definition: UT_TimeGate.h:49
UT_API volatile int theClockCount
bool tryAcquire()
Definition: UT_TimeGate.h:38