Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Declare methods 'const'.
[simgrid.git] / src / kernel / activity / ActivityImpl.cpp
1 /* Copyright (c) 2007-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 "src/kernel/activity/ActivityImpl.hpp"
7 #include "simgrid/modelchecker.h"
8 #include "src/mc/mc_replay.hpp"
9 #include "src/simix/smx_private.hpp"
10 #include <cmath> // isfinite()
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
13
14 namespace simgrid {
15 namespace kernel {
16 namespace activity {
17
18 ActivityImpl::~ActivityImpl()
19 {
20   clean_action();
21   XBT_DEBUG("Destroy activity %p", this);
22 }
23
24 void ActivityImpl::register_simcall(smx_simcall_t simcall)
25 {
26   simcalls_.push_back(simcall);
27   simcall->issuer_->waiting_synchro_ = this;
28 }
29
30 void ActivityImpl::clean_action()
31 {
32   if (surf_action_) {
33     surf_action_->unref();
34     surf_action_ = nullptr;
35   }
36 }
37
38 double ActivityImpl::get_remaining() const
39 {
40   return surf_action_ ? surf_action_->get_remains() : 0;
41 }
42
43 const char* ActivityImpl::get_state_str() const
44 {
45   switch (state_) {
46     case State::WAITING:
47       return "WAITING";
48
49     case State::READY:
50       return "READY";
51
52     case State::RUNNING:
53       return "RUNNING";
54
55     case State::CANCELED:
56       return "CANCELED";
57
58     case State::FAILED:
59       return "FAILED";
60
61     case State::DONE:
62       return "DONE";
63
64     case State::SRC_HOST_FAILURE:
65       return "SRC_HOST_FAILURE";
66
67     case State::DST_HOST_FAILURE:
68       return "DST_HOST_FAILURE";
69
70     case State::TIMEOUT:
71       return "TIMEOUT";
72
73     case State::SRC_TIMEOUT:
74       return "SRC_TIMEOUT";
75     case State::DST_TIMEOUT:
76       return "DST_TIMEOUT";
77
78     case State::LINK_FAILURE:
79       return "LINK_FAILURE";
80   }
81   THROW_IMPOSSIBLE;
82 }
83
84 bool ActivityImpl::test()
85 {
86   if (state_ != State::WAITING && state_ != State::RUNNING) {
87     finish();
88     return true;
89   }
90   return false;
91 }
92
93 void ActivityImpl::wait_for(actor::ActorImpl* issuer, double timeout)
94 {
95   XBT_DEBUG("Wait for execution of synchro %p, state %d", this, (int)state_);
96   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
97
98   /* Associate this simcall to the synchro */
99   register_simcall(&issuer->simcall_);
100
101   if (MC_is_active() || MC_record_replay_is_active()) {
102     int idx = SIMCALL_GET_MC_VALUE(issuer->simcall_);
103     if (idx == 0) {
104       state_ = simgrid::kernel::activity::State::DONE;
105     } else {
106       /* If we reached this point, the wait simcall must have a timeout */
107       /* Otherwise it shouldn't be enabled and executed by the MC */
108       if (timeout < 0.0)
109         THROW_IMPOSSIBLE;
110       state_ = simgrid::kernel::activity::State::TIMEOUT;
111     }
112     finish();
113     return;
114   }
115
116   /* If the synchro is already finished then perform the error handling */
117   if (state_ != simgrid::kernel::activity::State::RUNNING)
118     finish();
119   else {
120     /* we need a sleep action (even when there is no timeout) to be notified of host failures */
121     set_timeout(timeout);
122   }
123 }
124
125 void ActivityImpl::suspend()
126 {
127   if (surf_action_ == nullptr)
128     return;
129   XBT_VERB("This activity is suspended (remain: %f)", surf_action_->get_remains());
130   surf_action_->suspend();
131   on_suspended(*this);
132 }
133
134 void ActivityImpl::resume()
135 {
136   if (surf_action_ == nullptr)
137     return;
138   XBT_VERB("This activity is resumed (remain: %f)", surf_action_->get_remains());
139   surf_action_->resume();
140   on_resumed(*this);
141 }
142
143 void ActivityImpl::cancel()
144 {
145   XBT_VERB("Activity %p is canceled", this);
146   if (surf_action_ != nullptr)
147     surf_action_->cancel();
148   state_ = State::CANCELED;
149 }
150
151 // boost::intrusive_ptr<Activity> support:
152 void intrusive_ptr_add_ref(simgrid::kernel::activity::ActivityImpl* activity)
153 {
154   activity->refcount_.fetch_add(1, std::memory_order_relaxed);
155 }
156
157 void intrusive_ptr_release(simgrid::kernel::activity::ActivityImpl* activity)
158 {
159   if (activity->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
160     std::atomic_thread_fence(std::memory_order_acquire);
161     delete activity;
162   }
163 }
164 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_resumed;
165 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_suspended;
166 }
167 }
168 } // namespace simgrid::kernel::activity::