aboutsummaryrefslogtreecommitdiff
path: root/src/atomic_time_point.hh
blob: 13e62aac521b71a66a1f4e3408f859b84f1fffc9 (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
#ifndef ATOMIC_TIME_POINT_HH_
#define ATOMIC_TIME_POINT_HH_

#include <atomic>
#include <chrono>

// The purpose of this class is to provide a simple time point which may be read
// from atomically and without locks.

namespace low_latency {

class AtomicTimePoint final {
  private:
    std::atomic<std::int64_t> count{};
    static_assert(decltype(count)::is_always_lock_free);

  public:
    AtomicTimePoint();
    AtomicTimePoint(const AtomicTimePoint&) = delete;
    AtomicTimePoint(AtomicTimePoint&&) = delete;
    AtomicTimePoint operator=(const AtomicTimePoint&) = delete;
    AtomicTimePoint operator=(AtomicTimePoint&&) = delete;
    ~AtomicTimePoint();

  public:
    bool has_value() const;

    std::chrono::steady_clock::time_point get() const;

    void set(const std::chrono::steady_clock::time_point target);
};

} // namespace low_latency

#endif