Logo AND Algorithmique Numérique Distribuée

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