Logo AND Algorithmique Numérique Distribuée

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