Logo AND Algorithmique Numérique Distribuée

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