Logo AND Algorithmique Numérique Distribuée

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