Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Prefer std::array to map enum to string.
[simgrid.git] / src / s4u / s4u_Activity.cpp
1 /* Copyright (c) 2006-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "xbt/log.h"
7
8 #include "simgrid/s4u/Activity.hpp"
9 #include "simgrid/s4u/Engine.hpp"
10 #include "src/kernel/activity/ActivityImpl.hpp"
11
12 #include <array>
13
14 XBT_LOG_EXTERNAL_CATEGORY(s4u);
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_activity, s4u, "S4U activities");
16
17 namespace simgrid {
18 namespace s4u {
19
20 void Activity::wait_until(double time_limit)
21 {
22   double now = Engine::get_clock();
23   if (time_limit > now)
24     wait_for(time_limit - now);
25 }
26
27 bool Activity::test()
28 {
29   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
30              state_ == State::CANCELED || state_ == State::FINISHED);
31
32   if (state_ == State::CANCELED || state_ == State::FINISHED)
33     return true;
34
35   if (state_ == State::INITED || state_ == State::STARTING)
36     this->vetoable_start();
37
38   if (kernel::actor::simcall([this] { return this->get_impl()->test(); })) {
39     state_ = State::FINISHED;
40     this->release_dependencies();
41     return true;
42   }
43
44   return false;
45 }
46
47 Activity* Activity::suspend()
48 {
49   if (suspended_)
50     return this; // Already suspended
51   suspended_ = true;
52
53   if (state_ == State::STARTED)
54     pimpl_->suspend();
55
56   return this;
57 }
58
59 Activity* Activity::resume()
60 {
61   if (not suspended_)
62     return this; // nothing to restore when it's not suspended
63
64   if (state_ == State::STARTED)
65     pimpl_->resume();
66
67   return this;
68 }
69
70 const char* Activity::get_state_str() const
71 {
72   constexpr std::array<const char*, 5> names{{"INITED", "STARTING", "STARTED", "CANCELED", "FINISHED"}};
73   return names[static_cast<int>(state_)];
74 }
75
76 double Activity::get_remaining() const
77 {
78   if (state_ == State::INITED || state_ == State::STARTING)
79     return remains_;
80   else
81     return pimpl_->get_remaining();
82 }
83
84 Activity* Activity::set_remaining(double remains)
85 {
86   xbt_assert(state_ == State::INITED, "Cannot change the remaining amount of work once the Activity is started");
87   remains_ = remains;
88   return this;
89 }
90
91 } // namespace s4u
92 } // namespace simgrid