1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/3rdparty/srt-1-fit/srtcore/atomic_clock.h
john fe086dfc31
SRT: Upgrade libsrt from 1.4.1 to 1.5.1. v6.0.12 (#3362)
Co-authored-by: winlin <winlin@vip.126.com>
2023-01-04 19:56:33 +08:00

91 lines
1.8 KiB
C++

/*
* SRT - Secure, Reliable, Transport
* Copyright (c) 2021 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/.
*
*/
#ifndef INC_SRT_SYNC_ATOMIC_CLOCK_H
#define INC_SRT_SYNC_ATOMIC_CLOCK_H
#include "sync.h"
#include "atomic.h"
namespace srt
{
namespace sync
{
template <class Clock>
class AtomicDuration
{
atomic<int64_t> dur;
typedef typename Clock::duration duration_type;
typedef typename Clock::time_point time_point_type;
public:
AtomicDuration() ATR_NOEXCEPT : dur(0) {}
duration_type load()
{
int64_t val = dur.load();
return duration_type(val);
}
void store(const duration_type& d)
{
dur.store(d.count());
}
AtomicDuration<Clock>& operator=(const duration_type& s)
{
dur = s.count();
return *this;
}
operator duration_type() const
{
return duration_type(dur);
}
};
template <class Clock>
class AtomicClock
{
atomic<uint64_t> dur;
typedef typename Clock::duration duration_type;
typedef typename Clock::time_point time_point_type;
public:
AtomicClock() ATR_NOEXCEPT : dur(0) {}
time_point_type load() const
{
int64_t val = dur.load();
return time_point_type(duration_type(val));
}
void store(const time_point_type& d)
{
dur.store(uint64_t(d.time_since_epoch().count()));
}
AtomicClock& operator=(const time_point_type& s)
{
dur = s.time_since_epoch().count();
return *this;
}
operator time_point_type() const
{
return time_point_type(duration_type(dur.load()));
}
};
} // namespace sync
} // namespace srt
#endif // INC_SRT_SYNC_ATOMIC_CLOCK_H