From: Gabriel Corona Date: Wed, 20 Jul 2016 12:56:40 +0000 (+0200) Subject: [s4u] More support for C++-style time and durations X-Git-Tag: v3_14~737^2~8 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/e79c652187a77f7383b81afb91a4adc8d38986ef [s4u] More support for C++-style time and durations --- diff --git a/include/simgrid/chrono.hpp b/include/simgrid/chrono.hpp new file mode 100644 index 0000000000..4395b41851 --- /dev/null +++ b/include/simgrid/chrono.hpp @@ -0,0 +1,55 @@ +/* Copyright (c) 2016. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef SIMGRID_CHRONO_HPP +#define SIMGRID_CHRONO_HPP + +/** @file chrono.hpp Time support + * + * Define clock, duration types, time point types compatible with the standard + * C++ library API. + */ + +#include +#include + +#include + +namespace simgrid { + +/** A C++ compatible TrivialClock working with simulated-time */ +struct SimulationClock { + using rep = double; + using period = std::ratio<1>; + using duration = std::chrono::duration; + using time_point = std::chrono::time_point; + static constexpr bool is_steady = true; + static time_point now() + { + return time_point(duration(SIMIX_get_clock())); + } +}; + +/** Default duration for simulated time */ +using SimulationClockDuration = SimulationClock::duration; + +/** Default time point for simulated time */ +using SimulationClockTimePoint = SimulationClock::time_point; + +// Durations based on doubles: +using nanoseconds = std::chrono::duration; +using microseconds = std::chrono::duration; +using milliseconds = std::chrono::duration; +using seconds = std::chrono::duration; +using minutes = std::chrono::duration>; +using hours = std::chrono::duration>; + +/** A time point in the simulated time */ +template +using SimulationTimePoint = std::chrono::time_point; + +} + +#endif diff --git a/include/simgrid/s4u/actor.hpp b/include/simgrid/s4u/actor.hpp index 8e1b70395a..5039f84983 100644 --- a/include/simgrid/s4u/actor.hpp +++ b/include/simgrid/s4u/actor.hpp @@ -7,6 +7,7 @@ #define SIMGRID_S4U_ACTOR_HPP #include +#include #include #include #include @@ -20,6 +21,7 @@ #include #include +#include #include #include @@ -258,6 +260,13 @@ namespace this_actor { /** Block the actor sleeping for that amount of seconds (may throws hostFailure) */ XBT_PUBLIC(void) sleep(double duration); + template + inline void sleep(std::chrono::duration duration) + { + auto seconds = std::chrono::duration_cast(duration); + sleep(seconds.count()); + } + /** Block the actor, computing the given amount of flops */ XBT_PUBLIC(e_smx_state_t) execute(double flop); diff --git a/include/simgrid/s4u/conditionVariable.hpp b/include/simgrid/s4u/conditionVariable.hpp index 2db6fb8f9d..3b03bca7ff 100644 --- a/include/simgrid/s4u/conditionVariable.hpp +++ b/include/simgrid/s4u/conditionVariable.hpp @@ -17,6 +17,7 @@ #include #include +#include #include namespace simgrid { @@ -46,41 +47,20 @@ public: static Ptr createConditionVariable(); - // Wait functions: + // Wait functions without time: void wait(std::unique_lock& lock); - std::cv_status wait_until(std::unique_lock& lock, double timeout_time); - std::cv_status wait_for(std::unique_lock& lock, double duration); - - /** Wait for a given duraiton - * - * This version gives us the ability to do (in C++): - * - * - * using namespace std::literals::chrono_literals; - * - * cond->wait_for(lock, 1ms); - * cond->wait_for(lock, 1s); - * cond->wait_for(lock, 1min); - * cond->wait_for(lock, 1h); - * - */ - template - std::cv_status wait_for(std::unique_lock& lock, std::chrono::duration duration) - { - typedef std::chrono::duration SecondsDouble; - auto seconds = std::chrono::duration_cast(duration); - return this->wait_for(lock, duration.count()); - } - - // Variants which takes a predicate: - template void wait(std::unique_lock& lock, P pred) { while (!pred()) wait(lock); } + + // Wait function taking a plain double as time: + + std::cv_status wait_until(std::unique_lock& lock, double timeout_time); + std::cv_status wait_for(std::unique_lock& lock, double duration); template bool wait_until(std::unique_lock& lock, double timeout_time, P pred) { @@ -94,13 +74,39 @@ public: { return this->wait_until(lock, SIMIX_get_clock() + duration, std::move(pred)); } + + // Wait function taking a C++ style time: + template - bool wait_for(std::unique_lock& lock, std::chrono::duration duration, P pred) + bool wait_for( + std::unique_lock& lock, std::chrono::duration duration, + P pred) { - typedef std::chrono::duration SecondsDouble; - auto seconds = std::chrono::duration_cast(duration); + auto seconds = std::chrono::duration_cast(duration); return this->wait_for(lock, seconds.count(), pred); } + template + std::cv_status wait_for( + std::unique_lock& lock, std::chrono::duration duration) + { + auto seconds = std::chrono::duration_cast(duration); + return this->wait_for(lock, seconds.count()); + } + template + std::cv_status wait_until(std::unique_lock& lock, + const SimulationTimePoint& timeout_time) + { + auto timeout_native = std::chrono::time_point_cast(timeout_time); + return this->wait_until(lock, timeout_native.time_since_epoch().count()); + } + template + bool wait_until(std::unique_lock& lock, + const SimulationTimePoint& timeout_time, P pred) + { + auto timeout_native = std::chrono::time_point_cast(timeout_time); + return this->wait_until(lock, timeout_native.time_since_epoch().count(), + std::move(pred)); + } // Notify functions diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index 28348f0086..87bdcd2cf7 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -627,7 +627,7 @@ set(headers_to_install include/msg/datatypes.h include/simdag/simdag.h include/simdag/datatypes.h - + include/simgrid/chrono.hpp include/simgrid/plugins/energy.h include/simgrid/instr.h include/simgrid/msg.h