Skip to content

Commit

Permalink
[core] Minor code reorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsharabayko committed May 5, 2020
1 parent c6fc002 commit aea0dc1
Showing 1 changed file with 81 additions and 42 deletions.
123 changes: 81 additions & 42 deletions srtcore/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,50 +39,8 @@ using namespace std;

#if USE_STDCXX_CHRONO

template <class Clock, class Duration = typename Clock::duration>
using time_point = chrono::time_point<Clock, Duration>;
using steady_clock = chrono::steady_clock;
template <class Clock>
using Duration = chrono::duration<Clock>;
template <class Clock>
using TimePoint = chrono::time_point<Clock>;

inline long long count_microseconds(const steady_clock::duration &t)
{
return std::chrono::duration_cast<std::chrono::microseconds>(t).count();
}

inline long long count_microseconds(const steady_clock::time_point tp)
{
return std::chrono::duration_cast<std::chrono::microseconds>(tp.time_since_epoch()).count();
}

inline long long count_milliseconds(const steady_clock::duration &t)
{
return std::chrono::duration_cast<std::chrono::milliseconds>(t).count();
}

inline long long count_seconds(const steady_clock::duration &t)
{
return std::chrono::duration_cast<std::chrono::seconds>(t).count();
}

inline steady_clock::duration microseconds_from(long t_us) { return std::chrono::microseconds(t_us); }

inline steady_clock::duration milliseconds_from(long t_ms) { return std::chrono::milliseconds(t_ms); }

inline steady_clock::duration seconds_from(long t_s) { return std::chrono::seconds(t_s); }

template <class Clock, class Duration = typename Clock::duration>
inline bool is_zero(const time_point<Clock, Duration> &tp)
{
return tp.time_since_epoch() == Clock::duration::zero();
}

inline bool is_zero(const steady_clock::time_point& t)
{
return t == steady_clock::time_point();
}

#else

Expand Down Expand Up @@ -130,12 +88,36 @@ class Duration
int64_t m_duration;
};

#endif // USE_STDCXX_CHRONO

///////////////////////////////////////////////////////////////////////////////
//
// TimePoint and steadt_clock classes
//
///////////////////////////////////////////////////////////////////////////////

#if USE_STDCXX_CHRONO

using steady_clock = chrono::steady_clock;

template <class Clock, class Duration = typename Clock::duration>
using time_point = chrono::time_point<Clock, Duration>;

template <class Clock>
using TimePoint = chrono::time_point<Clock>;

template <class Clock, class Duration = typename Clock::duration>
inline bool is_zero(const time_point<Clock, Duration> &tp)
{
return tp.time_since_epoch() == Clock::duration::zero();
}

inline bool is_zero(const steady_clock::time_point& t)
{
return t == steady_clock::time_point();
}

#else
template <class _Clock>
class TimePoint;

Expand Down Expand Up @@ -216,6 +198,9 @@ class TimePoint
uint64_t m_timestamp;
};

template <>
uint64_t srt::sync::TimePoint<srt::sync::steady_clock>::us_since_epoch() const;

template <>
srt::sync::Duration<srt::sync::steady_clock> srt::sync::TimePoint<srt::sync::steady_clock>::time_since_epoch() const;

Expand All @@ -224,6 +209,58 @@ inline Duration<steady_clock> operator*(const int& lhs, const Duration<steady_cl
return rhs * lhs;
}

#endif // USE_STDCXX_CHRONO

///////////////////////////////////////////////////////////////////////////////
//
// Duration and timepoint conversions
//
///////////////////////////////////////////////////////////////////////////////

#if USE_STDCXX_CHRONO

inline long long count_microseconds(const steady_clock::duration &t)
{
return std::chrono::duration_cast<std::chrono::microseconds>(t).count();
}

inline long long count_microseconds(const steady_clock::time_point tp)
{
return std::chrono::duration_cast<std::chrono::microseconds>(tp.time_since_epoch()).count();
}

inline long long count_milliseconds(const steady_clock::duration &t)
{
return std::chrono::duration_cast<std::chrono::milliseconds>(t).count();
}

inline long long count_seconds(const steady_clock::duration &t)
{
return std::chrono::duration_cast<std::chrono::seconds>(t).count();
}

inline steady_clock::duration microseconds_from(long t_us)
{
return std::chrono::microseconds(t_us);
}

inline steady_clock::duration milliseconds_from(long t_ms)
{
return std::chrono::milliseconds(t_ms);
}

inline steady_clock::duration seconds_from(long t_s)
{
return std::chrono::seconds(t_s);
}

#else

inline int64_t count_microseconds(const TimePoint<steady_clock> tp)
{
return static_cast<int64_t>(tp.us_since_epoch());
}

int64_t count_microseconds(const steady_clock::duration& t);
int64_t count_milliseconds(const steady_clock::duration& t);
int64_t count_seconds(const steady_clock::duration& t);
Expand All @@ -237,6 +274,8 @@ inline bool is_zero(const TimePoint<steady_clock>& t)
return t == TimePoint<steady_clock>();
}

#endif // USE_STDCXX_CHRONO


///////////////////////////////////////////////////////////////////////////////
//
Expand Down

0 comments on commit aea0dc1

Please sign in to comment.