Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics: adjust method names in debug messages.
[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::start(s4u::Host* host, double timeout)
24 {
25   surf_action_ = host->pimpl_cpu->sleep(timeout);
26   surf_action_->set_data(this);
27   return this;
28 }
29
30 RawImpl::~RawImpl()
31 {
32   surf_action_->unref();
33 }
34
35 void RawImpl::suspend()
36 {
37   /* The suspension of raw synchros is delayed to when the process is rescheduled. */
38 }
39
40 void RawImpl::resume()
41 {
42   /* I cannot resume raw synchros directly. This is delayed to when the process is rescheduled at
43    * the end of the synchro. */
44 }
45 void RawImpl::post()
46 {
47   if (surf_action_->get_state() == resource::Action::State::FAILED) {
48     state_ = SIMIX_FAILED;
49   } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
50     state_ = SIMIX_SRC_TIMEOUT;
51   }
52   finish();
53 }
54
55 void RawImpl::finish()
56 {
57   smx_simcall_t simcall = simcalls_.front();
58   simcalls_.pop_front();
59
60   switch (state_) {
61     case SIMIX_DONE:
62       /* do nothing, synchro done */
63       XBT_DEBUG("RawImpl::finish(): execution successful");
64       break;
65
66     case SIMIX_FAILED:
67       XBT_DEBUG("RawImpl::finish(): host '%s' failed", simcall->issuer->get_host()->get_cname());
68       simcall->issuer->context_->iwannadie = true;
69       simcall->issuer->exception_ =
70           std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
71       break;
72     case SIMIX_SRC_TIMEOUT:
73       simcall->issuer->exception_ =
74           std::make_exception_ptr(simgrid::TimeoutError(XBT_THROW_POINT, "Synchronization timeout"));
75       break;
76     default:
77       xbt_die("Internal error in RawImpl::finish() unexpected synchro state %d", static_cast<int>(state_));
78   }
79
80   switch (simcall->call) {
81
82     case SIMCALL_MUTEX_LOCK:
83       simgrid::xbt::intrusive_erase(simcall_mutex_lock__get__mutex(simcall)->sleeping, *simcall->issuer);
84       break;
85
86     case SIMCALL_COND_WAIT:
87       simgrid::xbt::intrusive_erase(simcall_cond_wait__get__cond(simcall)->sleeping_, *simcall->issuer);
88       break;
89
90     case SIMCALL_COND_WAIT_TIMEOUT:
91       simgrid::xbt::intrusive_erase(simcall_cond_wait_timeout__get__cond(simcall)->sleeping_, *simcall->issuer);
92       simcall_cond_wait_timeout__set__result(simcall, 1); // signal a timeout
93       break;
94
95     case SIMCALL_SEM_ACQUIRE:
96       simgrid::xbt::intrusive_erase(simcall_sem_acquire__get__sem(simcall)->sleeping_, *simcall->issuer);
97       break;
98
99     case SIMCALL_SEM_ACQUIRE_TIMEOUT:
100       simgrid::xbt::intrusive_erase(simcall_sem_acquire_timeout__get__sem(simcall)->sleeping_, *simcall->issuer);
101       simcall_sem_acquire_timeout__set__result(simcall, 1); // signal a timeout
102       break;
103
104     default:
105       THROW_IMPOSSIBLE;
106   }
107   simcall->issuer->waiting_synchro = nullptr;
108   SIMIX_simcall_answer(simcall);
109 }
110
111 } // namespace activity
112 } // namespace kernel
113 } // namespace simgrid