forked from Haivision/srt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_cxx11.cpp
More file actions
139 lines (114 loc) · 3.44 KB
/
sync_cxx11.cpp
File metadata and controls
139 lines (114 loc) · 3.44 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
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* SRT - Secure, Reliable, Transport
* Copyright (c) 2020 Haivision Systems Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#include "platform_sys.h"
#include <iomanip>
#include <math.h>
#include <stdexcept>
#include "sync.h"
#include "common.h"
////////////////////////////////////////////////////////////////////////////////
//
// Clock frequency helpers
//
////////////////////////////////////////////////////////////////////////////////
namespace {
template <int val>
int pow10();
template <>
int pow10<10>()
{
return 1;
}
template <int val>
int pow10()
{
return 1 + pow10<val / 10>();
}
}
namespace srt
{
namespace sync
{
int clockSubsecondPrecision()
{
const int64_t ticks_per_sec = (steady_clock::period::den / steady_clock::period::num);
const int decimals = pow10<ticks_per_sec>();
return decimals;
}
////////////////////////////////////////////////////////////////////////////////
//
// SyncCond (based on stl chrono C++11)
//
////////////////////////////////////////////////////////////////////////////////
Condition::Condition() {}
Condition::~Condition() {}
void Condition::init() {}
void Condition::destroy() {}
void Condition::reset()
{
// SRT attempts to safely handle `fork()` in multithreaded environments,
// even though using `fork()` in such contexts is strongly discouraged.
// This is because `fork()` only duplicates the calling thread, leaving
// synchronization primitives (like condition variables) in an
// undefined or inconsistent state in the child process.
//
// To mitigate this, SRT forcefully reinitializes these synchronization
// primitives post-fork. In POSIX, this is done by overwriting the object
// with its default-initialized state. In C++11, we achieve the same effect
// using *placement new* to reconstruct the object in place. This ensures
// the condition variable is returned to a fresh, "neutral" state,
// as if it was just created.
new (&m_cv) std::condition_variable;
}
void Condition::wait(UniqueLock& lock)
{
assert_thisthread_not_waiting();
ScopedWaiter w(*this);
m_cv.wait(lock);
}
bool Condition::wait_for(UniqueLock& lock, const steady_clock::duration& rel_time)
{
assert_thisthread_not_waiting();
ScopedWaiter w(*this);
// Another possible implementation is wait_until(steady_clock::now() + timeout);
return m_cv.wait_for(lock, rel_time) != std::cv_status::timeout;
}
bool Condition::wait_until(UniqueLock& lock, const steady_clock::time_point& timeout_time)
{
assert_thisthread_not_waiting();
ScopedWaiter w(*this);
return m_cv.wait_until(lock, timeout_time) != std::cv_status::timeout;
}
void Condition::notify_one()
{
m_cv.notify_one();
}
void Condition::notify_all()
{
m_cv.notify_all();
}
////////////////////////////////////////////////////////////////////////////////
//
// CThreadError class - thread local storage error wrapper
//
////////////////////////////////////////////////////////////////////////////////
// Threal local error will be used by CUDTUnited
// with a static scope, therefore static thread_local
static thread_local srt::CUDTException s_thErr;
void SetThreadLocalError(const srt::CUDTException& e)
{
s_thErr = e;
}
srt::CUDTException& GetThreadLocalError()
{
return s_thErr;
}
} // ns sync
} // ns srt