Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
factor cancel across activities
[simgrid.git] / src / kernel / activity / SynchroRaw.cpp
1 /* Copyright (c) 2007-2019. 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/SynchroRaw.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/kernel/resource/Action.hpp"
9 #include "src/kernel/activity/ConditionVariableImpl.hpp"
10 #include "src/kernel/activity/MutexImpl.hpp"
11 #include "src/kernel/activity/SemaphoreImpl.hpp"
12 #include "src/kernel/context/Context.hpp"
13 #include "src/surf/cpu_interface.hpp"
14 #include "src/surf/surf_interface.hpp"
15 #include <simgrid/s4u/Host.hpp>
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix, "SIMIX Synchronization (mutex, semaphores and conditions)");
18
19 namespace simgrid {
20 namespace kernel {
21 namespace activity {
22
23 RawImpl& RawImpl::set_host(s4u::Host* host)
24 {
25   host_ = host;
26   return *this;
27 }
28 RawImpl& RawImpl::set_timeout(double timeout)
29 {
30   timeout_ = timeout;
31   return *this;
32 }
33
34 RawImpl* RawImpl::start()
35 {
36   surf_action_ = host_->pimpl_cpu->sleep(timeout_);
37   surf_action_->set_data(this);
38   return this;
39 }
40
41 void RawImpl::suspend()
42 {
43   /* The suspension of raw synchros is delayed to when the process is rescheduled. */
44 }
45
46 void RawImpl::resume()
47 {
48   /* I cannot resume raw synchros directly. This is delayed to when the process is rescheduled at
49    * the end of the synchro. */
50 }
51
52 void RawImpl::cancel()
53 {
54   /* I cannot cancel raw synchros directly. */
55 }
56
57 void RawImpl::post()
58 {
59   if (surf_action_->get_state() == resource::Action::State::FAILED) {
60     state_ = SIMIX_FAILED;
61   } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
62     state_ = SIMIX_SRC_TIMEOUT;
63   }
64   finish();
65 }
66
67 void RawImpl::finish()
68 {
69   smx_simcall_t simcall = simcalls_.front();
70   simcalls_.pop_front();
71
72   switch (state_) {
73     case SIMIX_DONE:
74       /* do nothing, synchro done */
75       XBT_DEBUG("RawImpl::finish(): execution successful");
76       break;
77
78     case SIMIX_FAILED:
79       XBT_DEBUG("RawImpl::finish(): host '%s' failed", simcall->issuer->get_host()->get_cname());
80       simcall->issuer->context_->iwannadie = true;
81       simcall->issuer->exception_ =
82           std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
83       break;
84     case SIMIX_SRC_TIMEOUT:
85       simcall->issuer->exception_ =
86           std::make_exception_ptr(simgrid::TimeoutError(XBT_THROW_POINT, "Synchronization timeout"));
87       break;
88     default:
89       xbt_die("Internal error in RawImpl::finish() unexpected synchro state %d", static_cast<int>(state_));
90   }
91
92   switch (simcall->call) {
93
94     case SIMCALL_MUTEX_LOCK:
95       simgrid::xbt::intrusive_erase(simcall_mutex_lock__get__mutex(simcall)->sleeping_, *simcall->issuer);
96       break;
97
98     case SIMCALL_COND_WAIT:
99       simgrid::xbt::intrusive_erase(simcall_cond_wait__get__cond(simcall)->sleeping_, *simcall->issuer);
100       break;
101
102     case SIMCALL_COND_WAIT_TIMEOUT:
103       simgrid::xbt::intrusive_erase(simcall_cond_wait_timeout__get__cond(simcall)->sleeping_, *simcall->issuer);
104       simcall_cond_wait_timeout__set__result(simcall, 1); // signal a timeout
105       break;
106
107     case SIMCALL_SEM_ACQUIRE:
108       simgrid::xbt::intrusive_erase(simcall_sem_acquire__get__sem(simcall)->sleeping_, *simcall->issuer);
109       break;
110
111     case SIMCALL_SEM_ACQUIRE_TIMEOUT:
112       simgrid::xbt::intrusive_erase(simcall_sem_acquire_timeout__get__sem(simcall)->sleeping_, *simcall->issuer);
113       simcall_sem_acquire_timeout__set__result(simcall, 1); // signal a timeout
114       break;
115
116     default:
117       THROW_IMPOSSIBLE;
118   }
119   simcall->issuer->waiting_synchro = nullptr;
120   SIMIX_simcall_answer(simcall);
121 }
122
123 } // namespace activity
124 } // namespace kernel
125 } // namespace simgrid