Logo AND Algorithmique Numérique Distribuée

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