summaryrefslogtreecommitdiff
path: root/indra/llcommon/chrono.h
blob: d121b9adf66020a65d145dbe971bb59ef715cf3a (plain)
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
/**
 * @file   chrono.h
 * @author Nat Goodspeed
 * @date   2021-10-05
 * @brief  supplement <chrono> with utility functions
 *
 * $LicenseInfo:firstyear=2021&license=viewerlgpl$
 * Copyright (c) 2021, Linden Research, Inc.
 * $/LicenseInfo$
 */

#if ! defined(LL_CHRONO_H)
#define LL_CHRONO_H

#include <chrono>
#include <type_traits>              // std::enable_if

namespace LL
{

// time_point_cast() is derived from https://stackoverflow.com/a/35293183
// without the iteration: we think errors in the ~1 microsecond range are
// probably acceptable.

// This variant is for the optimal case when the source and dest use the same
// clock: that case is handled by std::chrono.
template <typename DestTimePoint, typename SrcTimePoint,
          typename std::enable_if<std::is_same<typename DestTimePoint::clock,
                                               typename SrcTimePoint::clock>::value,
                                  bool>::type = true>
DestTimePoint time_point_cast(const SrcTimePoint& time)
{
    return std::chrono::time_point_cast<typename DestTimePoint::duration>(time);
}

// This variant is for when the source and dest use different clocks -- see
// the linked StackOverflow answer, also Howard Hinnant's, for more context.
template <typename DestTimePoint, typename SrcTimePoint,
          typename std::enable_if<! std::is_same<typename DestTimePoint::clock,
                                                 typename SrcTimePoint::clock>::value,
                                  bool>::type = true>
DestTimePoint time_point_cast(const SrcTimePoint& time)
{
    // The basic idea is that we must adjust the passed time_point by the
    // difference between the clocks' epochs. But since time_point doesn't
    // expose its epoch, we fall back on what each of them thinks is now().
    // However, since we necessarily make sequential calls to those now()
    // functions, the answers differ not only by the cycles spent executing
    // those calls, but by potential OS interruptions between them. Try to
    // reduce that error by capturing the source clock time both before and
    // after the dest clock, and splitting the difference. Of course an
    // interruption between two of these now() calls without a comparable
    // interruption between the other two will skew the result, but better is
    // more expensive.
    const auto src_before = typename SrcTimePoint::clock::now();
    const auto dest_now   = typename DestTimePoint::clock::now();
    const auto src_after  = typename SrcTimePoint::clock::now();
    const auto src_diff   = src_after - src_before;
    const auto src_now    = src_before + src_diff / 2;
    return dest_now + (time - src_now);
}

} // namespace LL

#endif /* ! defined(LL_CHRONO_H) */