1 /* Copyright (c) 2021. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include <simgrid/kernel/Timer.hpp>
8 #include <simgrid/s4u/Engine.hpp>
14 std::shared_ptr<Timer> Timer::set(double date, xbt::Task<void()>&& callback)
16 auto timer = std::make_shared<Timer>(date, std::move(callback));
17 timer->handle_ = kernel_timers().emplace(std::make_pair(date, timer));
21 /** @brief cancels a timer that was added earlier */
24 kernel_timers().erase(handle_);
27 /** Handle any pending timer. Returns if something was actually run. */
28 bool Timer::execute_all()
31 while (not kernel_timers().empty() && s4u::Engine::get_clock() >= kernel_timers().top().first) {
33 // FIXME: make the timers being real callbacks (i.e. provide dispatchers that read and expand the args)
34 std::shared_ptr<Timer> timer = kernel_timers().top().second;
35 kernel_timers().pop();
43 } // namespace simgrid