HDK
Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
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>
27
class
UT_TimeGate
28
{
29
public
:
30
inline
UT_TimeGate
()
31
: myTickCount(0)
32
{
33
UT_TimeGateImpl::startClockThreadIfNeeded
();
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_TimeGate
Definition:
UT_TimeGate.h:27
UT_TimeGateImpl::startClockThreadIfNeeded
UT_API void startClockThreadIfNeeded()
UT_TimeGate::acquire
void acquire()
Definition:
UT_TimeGate.h:57
UT_API.h
UT_API
#define UT_API
Definition:
UT_API.h:13
UT_TimeGate::UT_TimeGate
UT_TimeGate()
Definition:
UT_TimeGate.h:30
UT_TimeGate::canAcquire
bool canAcquire()
Definition:
UT_TimeGate.h:49
UT_TimeGateImpl::theClockCount
UT_API volatile int theClockCount
UT_TimeGate::tryAcquire
bool tryAcquire()
Definition:
UT_TimeGate.h:38
UT
UT_TimeGate.h
Generated on Sat Feb 27 2021 05:26:56 for HDK by
1.8.6