Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Factor Activity::cancel() through CRTP.
[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 XBT_LOG_EXTERNAL_CATEGORY(s4u);
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_activity, s4u, "S4U activities");
14
15 namespace simgrid {
16 namespace s4u {
17
18 void Activity::wait_until(double time_limit)
19 {
20   double now = Engine::get_clock();
21   if (time_limit > now)
22     wait_for(time_limit - now);
23 }
24
25 bool Activity::test()
26 {
27   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
28              state_ == State::CANCELED || state_ == State::FINISHED);
29
30   if (state_ == State::CANCELED || state_ == State::FINISHED)
31     return true;
32
33   if (state_ == State::INITED || state_ == State::STARTING)
34     this->vetoable_start();
35
36   if (kernel::actor::simcall([this] { return this->get_impl()->test(); })) {
37     complete(State::FINISHED);
38     return true;
39   }
40
41   return false;
42 }
43
44 Activity* Activity::cancel()
45 {
46   kernel::actor::simcall([this] { pimpl_->cancel(); });
47   complete(State::CANCELED);
48   return this;
49 }
50
51 Activity* Activity::suspend()
52 {
53   if (suspended_)
54     return this; // Already suspended
55   suspended_ = true;
56
57   if (state_ == State::STARTED)
58     pimpl_->suspend();
59
60   return this;
61 }
62
63 Activity* Activity::resume()
64 {
65   if (not suspended_)
66     return this; // nothing to restore when it's not suspended
67
68   if (state_ == State::STARTED)
69     pimpl_->resume();
70
71   return this;
72 }
73
74 const char* Activity::get_state_str() const
75 {
76   return to_c_str(state_);
77 }
78
79 double Activity::get_remaining() const
80 {
81   if (state_ == State::INITED || state_ == State::STARTING)
82     return remains_;
83   else
84     return pimpl_->get_remaining();
85 }
86
87 Activity* Activity::set_remaining(double remains)
88 {
89   xbt_assert(state_ == State::INITED, "Cannot change the remaining amount of work once the Activity is started");
90   remains_ = remains;
91   return this;
92 }
93
94 } // namespace s4u
95 } // namespace simgrid