1 /* Copyright (c) 2016-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_CHRONO_HPP
7 #define SIMGRID_CHRONO_HPP
9 /** @file chrono.hpp Time support
11 * Define clock, duration types, time point types compatible with the standard
18 #include <simgrid/engine.h>
22 /** @brief A C++ compatible [TrivialClock](http://en.cppreference.com/w/cpp/concept/TrivialClock) working with simulated-time.
24 * SimGrid uses `double` for representing the simulated time, where *durations* are expressed in seconds
25 * (with *infinite duration* expressed as a negative value) and
26 * *timepoints* are expressed as seconds from the beginning of the simulation.
27 * In contrast, all the C++ APIs use the much more sensible `std::chrono::duration` and`std::chrono::time_point`.
29 * This class can be used to build `std::chrono` objects that use simulated time,
30 * using #SimulationClockDuration and #SimulationClockTimePoint.
32 * This means it is possible to use (since C++14):
35 * using namespace std::chrono_literals;
36 * simgrid::s4u::actor::sleep_for(42s);
40 class SimulationClock {
43 using period = std::ratio<1>;
44 using duration = std::chrono::duration<rep, period>;
45 using time_point = std::chrono::time_point<SimulationClock, duration>;
46 static constexpr bool is_steady = true;
47 static time_point now() { return time_point(duration(simgrid_get_clock())); }
50 /** Default duration for simulated time */
51 using SimulationClockDuration = SimulationClock::duration;
53 /** Default time point for simulated time */
54 using SimulationClockTimePoint = SimulationClock::time_point;
56 // Durations based on doubles:
57 using nanoseconds = std::chrono::duration<double, std::nano>;
58 using microseconds = std::chrono::duration<double, std::micro>;
59 using milliseconds = std::chrono::duration<double, std::milli>;
60 using seconds = std::chrono::duration<double>;
61 using minutes = std::chrono::duration<double, std::ratio<60>>;
62 using hours = std::chrono::duration<double, std::ratio<3600>>;
64 /** A time point in the simulated time */
65 template<class Duration>
66 using SimulationTimePoint = std::chrono::time_point<SimulationClock, Duration>;