| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2020 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
#include "platform_sys.h" |
| 11 |
|
| 12 |
#include <iomanip> |
| 13 |
#include <math.h> |
| 14 |
#include <stdexcept> |
| 15 |
#include "sync.h" |
| 16 |
#include "srt_compat.h" |
| 17 |
#include "common.h" |
| 18 |
|
| 19 |
//////////////////////////////////////////////////////////////////////////////// |
| 20 |
// |
| 21 |
// Clock frequency helpers |
| 22 |
// |
| 23 |
//////////////////////////////////////////////////////////////////////////////// |
| 24 |
|
| 25 |
namespace { |
| 26 |
template <int val> |
| 27 |
int pow10(); |
| 28 |
|
| 29 |
template <> |
| 30 |
int pow10<10>() |
| 31 |
{ |
| 32 |
return 1; |
| 33 |
} |
| 34 |
|
| 35 |
template <int val> |
| 36 |
int pow10() |
| 37 |
{ |
| 38 |
return 1 + pow10<val / 10>(); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
int srt::sync::clockSubsecondPrecision() |
| 43 |
{ |
| 44 |
const int64_t ticks_per_sec = (srt::sync::steady_clock::period::den / srt::sync::steady_clock::period::num); |
| 45 |
const int decimals = pow10<ticks_per_sec>(); |
| 46 |
return decimals; |
| 47 |
} |
| 48 |
|
| 49 |
//////////////////////////////////////////////////////////////////////////////// |
| 50 |
// |
| 51 |
// SyncCond (based on stl chrono C++11) |
| 52 |
// |
| 53 |
//////////////////////////////////////////////////////////////////////////////// |
| 54 |
|
| 55 |
srt::sync::Condition::Condition() {} |
| 56 |
|
| 57 |
srt::sync::Condition::~Condition() {} |
| 58 |
|
| 59 |
void srt::sync::Condition::init() {} |
| 60 |
|
| 61 |
void srt::sync::Condition::reset() |
| 62 |
{ |
| 63 |
// SRT attempts to safely handle `fork()` in multithreaded environments, |
| 64 |
// even though using `fork()` in such contexts is strongly discouraged. |
| 65 |
// This is because `fork()` only duplicates the calling thread, leaving |
| 66 |
// synchronization primitives (like condition variables) in an |
| 67 |
// undefined or inconsistent state in the child process. |
| 68 |
// |
| 69 |
// To mitigate this, SRT forcefully reinitializes these synchronization |
| 70 |
// primitives post-fork. In POSIX, this is done by overwriting the object |
| 71 |
// with its default-initialized state. In C++11, we achieve the same effect |
| 72 |
// using *placement new* to reconstruct the object in place. This ensures |
| 73 |
// the condition variable is returned to a fresh, "neutral" state, |
| 74 |
// as if it was just created. |
| 75 |
|
| 76 |
new (&m_cv) std::condition_variable; |
| 77 |
} |
| 78 |
|
| 79 |
void srt::sync::Condition::destroy() {} |
| 80 |
|
| 81 |
void srt::sync::Condition::wait(UniqueLock& lock) |
| 82 |
{ |
| 83 |
m_cv.wait(lock); |
| 84 |
} |
| 85 |
|
| 86 |
bool srt::sync::Condition::wait_for(UniqueLock& lock, const steady_clock::duration& rel_time) |
| 87 |
{ |
| 88 |
// Another possible implementation is wait_until(steady_clock::now() + timeout); |
| 89 |
return m_cv.wait_for(lock, rel_time) != std::cv_status::timeout; |
| 90 |
} |
| 91 |
|
| 92 |
bool srt::sync::Condition::wait_until(UniqueLock& lock, const steady_clock::time_point& timeout_time) |
| 93 |
{ |
| 94 |
return m_cv.wait_until(lock, timeout_time) != std::cv_status::timeout; |
| 95 |
} |
| 96 |
|
| 97 |
void srt::sync::Condition::notify_one() |
| 98 |
{ |
| 99 |
m_cv.notify_one(); |
| 100 |
} |
| 101 |
|
| 102 |
void srt::sync::Condition::notify_all() |
| 103 |
{ |
| 104 |
m_cv.notify_all(); |
| 105 |
} |
| 106 |
|
| 107 |
//////////////////////////////////////////////////////////////////////////////// |
| 108 |
// |
| 109 |
// CThreadError class - thread local storage error wrapper |
| 110 |
// |
| 111 |
//////////////////////////////////////////////////////////////////////////////// |
| 112 |
|
| 113 |
// Threal local error will be used by CUDTUnited |
| 114 |
// with a static scope, therefore static thread_local |
| 115 |
static thread_local srt::CUDTException s_thErr; |
| 116 |
|
| 117 |
void srt::sync::SetThreadLocalError(const srt::CUDTException& e) |
| 118 |
{ |
| 119 |
s_thErr = e; |
| 120 |
} |
| 121 |
|
| 122 |
srt::CUDTException& srt::sync::GetThreadLocalError() |
| 123 |
{ |
| 124 |
return s_thErr; |
| 125 |
} |
| 126 |
|