Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a174119b82fc36491400ca3a00309176aed67119
[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/Exception.hpp"
9 #include "simgrid/s4u/Activity.hpp"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "src/kernel/activity/ActivityImpl.hpp"
12 #include "src/kernel/actor/ActorImpl.hpp"
13 #include "src/kernel/actor/SimcallObserver.hpp"
14
15 XBT_LOG_EXTERNAL_CATEGORY(s4u);
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_activity, s4u, "S4U activities");
17
18 namespace simgrid {
19 namespace s4u {
20
21 void Activity::wait_until(double time_limit)
22 {
23   double now = Engine::get_clock();
24   if (time_limit > now)
25     wait_for(time_limit - now);
26 }
27
28 Activity* Activity::wait_for(double timeout)
29 {
30   if (state_ == State::INITED)
31     vetoable_start();
32
33   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
34   kernel::actor::ActivityWaitSimcall observer{issuer, pimpl_.get(), timeout};
35   if (kernel::actor::simcall_blocking(
36           [&observer] { observer.get_activity()->wait_for(observer.get_issuer(), observer.get_timeout()); }, &observer))
37     throw TimeoutException(XBT_THROW_POINT, "Timeouted");
38   complete(State::FINISHED);
39   return this;
40 }
41
42 bool Activity::test()
43 {
44   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
45              state_ == State::CANCELED || state_ == State::FINISHED);
46
47   if (state_ == State::CANCELED || state_ == State::FINISHED)
48     return true;
49
50   if (state_ == State::INITED || state_ == State::STARTING)
51     this->vetoable_start();
52
53   if (kernel::actor::simcall([this] { return this->get_impl()->test(); })) {
54     complete(State::FINISHED);
55     return true;
56   }
57
58   return false;
59 }
60
61 Activity* Activity::cancel()
62 {
63   kernel::actor::simcall([this] {
64     XBT_HERE();
65     pimpl_->cancel();
66   });
67   complete(State::CANCELED);
68   return this;
69 }
70
71 Activity* Activity::suspend()
72 {
73   if (suspended_)
74     return this; // Already suspended
75   suspended_ = true;
76
77   if (state_ == State::STARTED)
78     pimpl_->suspend();
79
80   return this;
81 }
82
83 Activity* Activity::resume()
84 {
85   if (not suspended_)
86     return this; // nothing to restore when it's not suspended
87
88   if (state_ == State::STARTED)
89     pimpl_->resume();
90
91   return this;
92 }
93
94 const char* Activity::get_state_str() const
95 {
96   return to_c_str(state_);
97 }
98
99 double Activity::get_remaining() const
100 {
101   if (state_ == State::INITED || state_ == State::STARTING)
102     return remains_;
103   else
104     return pimpl_->get_remaining();
105 }
106
107 Activity* Activity::set_remaining(double remains)
108 {
109   xbt_assert(state_ == State::INITED, "Cannot change the remaining amount of work once the Activity is started");
110   remains_ = remains;
111   return this;
112 }
113
114 } // namespace s4u
115 } // namespace simgrid