forked from Haivision/srt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_timer.cpp
More file actions
126 lines (106 loc) · 2.95 KB
/
sync_timer.cpp
File metadata and controls
126 lines (106 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "sync_timer.h"
namespace srt
{
namespace sync
{
////////////////////////////////////////////////////////////////////////////////
//
// Timer
//
////////////////////////////////////////////////////////////////////////////////
CTimer::CTimer()
{
}
CTimer::~CTimer()
{
}
// This function sleeps up to the given time, then exits.
// Meanwhile it can be influenced from another thread by calling:
// - tick(): exit waiting, but re-check the end time and fall back to sleep if not reached
// - interrupt(): exit waiting with setting wait time to now() so that it exits immediately
//
// This function returns true if it has exit on the originally set time.
// If the time was changed due to being interrupted and it did really exit before
// that time, false is returned.
bool CTimer::sleep_until(TimePoint<steady_clock> tp)
{
// The class member m_sched_time can be used to interrupt the sleep.
// Refer to Timer::interrupt().
enterCS(m_event.mutex());
m_tsSchedTime = tp;
leaveCS(m_event.mutex());
#if SRT_BUSY_WAITING
wait_busy();
#else
wait_stalled();
#endif
// Returning false means that sleep was early interrupted
return m_tsSchedTime.load() >= tp;
}
void CTimer::wait_stalled()
{
TimePoint<steady_clock> cur_tp = steady_clock::now();
{
UniqueLock elk (m_event.mutex());
while (cur_tp < m_tsSchedTime.load())
{
m_event.wait_until(elk, m_tsSchedTime);
cur_tp = steady_clock::now();
}
}
}
void srt::sync::CTimer::wait_busy()
{
#if defined(_WIN32)
// 10 ms on Windows: bad accuracy of timers
const steady_clock::duration
td_threshold = milliseconds_from(10);
#else
// 1 ms on non-Windows platforms
const steady_clock::duration
td_threshold = milliseconds_from(1);
#endif
TimePoint<steady_clock> cur_tp = steady_clock::now();
{
UniqueLock elk (m_event.mutex());
while (cur_tp < m_tsSchedTime.load())
{
steady_clock::duration td_wait = m_tsSchedTime.load() - cur_tp;
if (td_wait <= 2 * td_threshold)
break;
td_wait -= td_threshold;
m_event.wait_for(elk, td_wait);
cur_tp = steady_clock::now();
}
while (cur_tp < m_tsSchedTime.load())
{
InvertedLock ulk (m_event.mutex());
#ifdef IA32
__asm__ volatile ("pause; rep; nop; nop; nop; nop; nop;");
#elif IA64
__asm__ volatile ("nop 0; nop 0; nop 0; nop 0; nop 0;");
#elif AMD64
__asm__ volatile ("nop; nop; nop; nop; nop;");
#elif defined(_WIN32) && !defined(__MINGW32__)
__nop();
__nop();
__nop();
__nop();
__nop();
#endif
cur_tp = steady_clock::now();
}
}
}
void CTimer::interrupt()
{
UniqueLock lck(m_event.mutex());
m_tsSchedTime = steady_clock::now();
m_event.notify_all();
}
void CTimer::tick()
{
m_event.notify_one();
}
}
}