ion 0.12.0
Atomic concurrency support library
Loading...
Searching...
No Matches
chrono.hpp
1/**
2 * @defgroup ion_chrono Time utilities
3 * Time utilities
4 *
5 * @warning Depending on the clock type the and whether VDSO is used, querying clock may involve a
6 * system call. Linking statically to glibc disables VDSO, for example.
7 *
8 * @ingroup ion
9 * @file
10 * @copyright
11 * SPDX-FileCopyrightText: 2025-2025 European Southern Observatory (ESO)
12
13 * SPDX-License-Identifier: LGPL-3.0-only
14 */
15#ifndef ION_CHRONO_HPP
16#define ION_CHRONO_HPP
17#include <chrono>
18#include <type_traits>
19
20#include <ion/detail/macros.hpp>
21
22namespace ion {
23
24/**
25 * Blocks execution of current thread, without yielding, until specified @a time has been reached.
26 *
27 * @param time Time point to wait for.
28 * @tparam Clock Clock type satisfying C++11 `Clock` requirements.
29 */
30template <class Clock, class Duration>
31void WaitUntil(std::chrono::time_point<Clock, Duration> const& time) noexcept(
32 std::is_nothrow_invocable_v<decltype(&Clock::now), Clock>);
33
34/**
35 * Blocks execution of current thread, without yielding, for specified @a duration, using a
36 * steady clock.
37 *
38 * @param duration time duration to wait.
39 */
40template <class Rep, class Period>
41void WaitFor(std::chrono::duration<Rep, Period> const& duration) noexcept;
42
43template <class Clock, class Duration>
44void WaitUntil(std::chrono::time_point<Clock, Duration> const& time) noexcept(
45 std::is_nothrow_invocable_v<decltype(&Clock::now), Clock>) {
46 while (Clock::now() < time) {
47 ION_PAUSE();
48 }
49}
50
51template <class Rep, class Period>
52void WaitFor(std::chrono::duration<Rep, Period> const& duration) noexcept {
53 WaitUntil(std::chrono::steady_clock::now() +
54 std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration));
55}
56
57} // namespace ion
58#endif // ION_CHRONO_HPP