Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[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   return to_c_str(state_);
46 }
47
48 bool ActivityImpl::test()
49 {
50   if (state_ != State::WAITING && state_ != State::RUNNING) {
51     finish();
52     return true;
53   }
54   return false;
55 }
56
57 void ActivityImpl::wait_for(actor::ActorImpl* issuer, double timeout)
58 {
59   XBT_DEBUG("Wait for execution of synchro %p, state %d", this, (int)state_);
60   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
61
62   /* Associate this simcall to the synchro */
63   register_simcall(&issuer->simcall_);
64
65   if (MC_is_active() || MC_record_replay_is_active()) {
66     int idx = SIMCALL_GET_MC_VALUE(issuer->simcall_);
67     if (idx == 0) {
68       state_ = simgrid::kernel::activity::State::DONE;
69     } else {
70       /* If we reached this point, the wait simcall must have a timeout */
71       /* Otherwise it shouldn't be enabled and executed by the MC */
72       if (timeout < 0.0)
73         THROW_IMPOSSIBLE;
74       state_ = simgrid::kernel::activity::State::TIMEOUT;
75     }
76     finish();
77     return;
78   }
79
80   /* If the synchro is already finished then perform the error handling */
81   if (state_ != simgrid::kernel::activity::State::RUNNING)
82     finish();
83   else {
84     /* we need a sleep action (even when there is no timeout) to be notified of host failures */
85     set_timeout(timeout);
86   }
87 }
88
89 void ActivityImpl::suspend()
90 {
91   if (surf_action_ == nullptr)
92     return;
93   XBT_VERB("This activity is suspended (remain: %f)", surf_action_->get_remains());
94   surf_action_->suspend();
95   on_suspended(*this);
96 }
97
98 void ActivityImpl::resume()
99 {
100   if (surf_action_ == nullptr)
101     return;
102   XBT_VERB("This activity is resumed (remain: %f)", surf_action_->get_remains());
103   surf_action_->resume();
104   on_resumed(*this);
105 }
106
107 void ActivityImpl::cancel()
108 {
109   XBT_VERB("Activity %p is canceled", this);
110   if (surf_action_ != nullptr)
111     surf_action_->cancel();
112   state_ = State::CANCELED;
113 }
114
115 // boost::intrusive_ptr<Activity> support:
116 void intrusive_ptr_add_ref(simgrid::kernel::activity::ActivityImpl* activity)
117 {
118   activity->refcount_.fetch_add(1, std::memory_order_relaxed);
119 }
120
121 void intrusive_ptr_release(simgrid::kernel::activity::ActivityImpl* activity)
122 {
123   if (activity->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
124     std::atomic_thread_fence(std::memory_order_acquire);
125     delete activity;
126   }
127 }
128 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_resumed;
129 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_suspended;
130 }
131 }
132 } // namespace simgrid::kernel::activity::