Logo AND Algorithmique Numérique Distribuée

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