Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Partially snake_case ActorImpl
[simgrid.git] / src / simix / smx_synchro.cpp
1 /* Copyright (c) 2007-2018. 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 "smx_private.hpp"
7 #include "src/kernel/activity/ConditionVariableImpl.hpp"
8 #include "src/kernel/activity/MutexImpl.hpp"
9 #include "src/kernel/activity/SynchroRaw.hpp"
10 #include "src/simix/smx_synchro_private.hpp"
11 #include "src/surf/cpu_interface.hpp"
12 #include <xbt/ex.hpp>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix, "SIMIX Synchronization (mutex, semaphores and conditions)");
15
16 /***************************** Raw synchronization *********************************/
17
18 smx_activity_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
19 {
20   XBT_IN("(%p, %f)",smx_host,timeout);
21
22   simgrid::kernel::activity::RawImplPtr sync =
23       simgrid::kernel::activity::RawImplPtr(new simgrid::kernel::activity::RawImpl());
24   sync->sleep                          = smx_host->pimpl_cpu->sleep(timeout);
25   sync->sleep->set_data(sync.get());
26   XBT_OUT();
27   return sync;
28 }
29
30 void SIMIX_synchro_stop_waiting(smx_actor_t process, smx_simcall_t simcall)
31 {
32   XBT_IN("(%p, %p)",process,simcall);
33   switch (simcall->call) {
34
35     case SIMCALL_MUTEX_LOCK:
36       simgrid::xbt::intrusive_erase(simcall_mutex_lock__get__mutex(simcall)->sleeping, *process);
37       break;
38
39     case SIMCALL_COND_WAIT:
40       simgrid::xbt::intrusive_erase(simcall_cond_wait__get__cond(simcall)->sleeping, *process);
41       break;
42
43     case SIMCALL_COND_WAIT_TIMEOUT:
44       simgrid::xbt::intrusive_erase(simcall_cond_wait_timeout__get__cond(simcall)->sleeping, *process);
45       simcall_cond_wait_timeout__set__result(simcall, 1); // signal a timeout
46       break;
47
48     case SIMCALL_SEM_ACQUIRE:
49       simgrid::xbt::intrusive_erase(simcall_sem_acquire__get__sem(simcall)->sleeping, *process);
50       break;
51
52     case SIMCALL_SEM_ACQUIRE_TIMEOUT:
53       simgrid::xbt::intrusive_erase(simcall_sem_acquire_timeout__get__sem(simcall)->sleeping, *process);
54       simcall_sem_acquire_timeout__set__result(simcall, 1); // signal a timeout
55       break;
56
57     default:
58       THROW_IMPOSSIBLE;
59   }
60   XBT_OUT();
61 }
62
63 void SIMIX_synchro_finish(smx_activity_t synchro)
64 {
65   XBT_IN("(%p)", synchro.get());
66   smx_simcall_t simcall = synchro->simcalls_.front();
67   synchro->simcalls_.pop_front();
68
69   if (synchro->state_ != SIMIX_SRC_TIMEOUT) {
70     if (synchro->state_ == SIMIX_FAILED)
71       simcall->issuer->context_->iwannadie = 1;
72     else
73       THROW_IMPOSSIBLE;
74   }
75
76   SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
77   simcall->issuer->waiting_synchro = nullptr;
78   SIMIX_simcall_answer(simcall);
79   XBT_OUT();
80 }
81
82 /******************************** Semaphores **********************************/
83 /** @brief Initialize a semaphore */
84 smx_sem_t SIMIX_sem_init(unsigned int value)
85 {
86   XBT_IN("(%u)",value);
87   smx_sem_t sem = new s_smx_sem_t;
88   sem->value = value;
89   XBT_OUT();
90   return sem;
91 }
92
93 /** @brief Destroys a semaphore */
94 void SIMIX_sem_destroy(smx_sem_t sem)
95 {
96   XBT_IN("(%p)",sem);
97   XBT_DEBUG("Destroy semaphore %p", sem);
98   if (sem != nullptr) {
99     xbt_assert(sem->sleeping.empty(), "Cannot destroy semaphore since someone is still using it");
100     delete sem;
101   }
102   XBT_OUT();
103 }
104
105 /** @brief release the semaphore
106  *
107  * Unlock a process waiting on the semaphore.
108  * If no one was blocked, the semaphore capacity is increased by 1.
109  */
110 void SIMIX_sem_release(smx_sem_t sem)
111 {
112   XBT_IN("(%p)",sem);
113   XBT_DEBUG("Sem release semaphore %p", sem);
114   if (not sem->sleeping.empty()) {
115     auto& proc = sem->sleeping.front();
116     sem->sleeping.pop_front();
117     proc.waiting_synchro = nullptr;
118     SIMIX_simcall_answer(&proc.simcall);
119   } else {
120     sem->value++;
121   }
122   XBT_OUT();
123 }
124
125 /** @brief Returns true if acquiring this semaphore would block */
126 int SIMIX_sem_would_block(smx_sem_t sem)
127 {
128   XBT_IN("(%p)",sem);
129   XBT_OUT();
130   return (sem->value <= 0);
131 }
132
133 /** @brief Returns the current capacity of the semaphore */
134 int SIMIX_sem_get_capacity(smx_sem_t sem)
135 {
136   XBT_IN("(%p)",sem);
137   XBT_OUT();
138   return sem->value;
139 }
140
141 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
142                             smx_simcall_t simcall)
143 {
144   XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
145   smx_activity_t synchro = nullptr;
146
147   XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
148   if (sem->value <= 0) {
149     synchro = SIMIX_synchro_wait(issuer->host_, timeout);
150     synchro->simcalls_.push_front(simcall);
151     issuer->waiting_synchro = synchro;
152     sem->sleeping.push_back(*issuer);
153   } else {
154     sem->value--;
155     SIMIX_simcall_answer(simcall);
156   }
157   XBT_OUT();
158 }
159
160 /**
161  * \brief Handles a sem acquire simcall without timeout.
162  */
163 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
164 {
165   XBT_IN("(%p)",simcall);
166   _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
167   XBT_OUT();
168 }
169
170 /**
171  * \brief Handles a sem acquire simcall with timeout.
172  */
173 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
174 {
175   XBT_IN("(%p)",simcall);
176   simcall_sem_acquire_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
177   _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
178   XBT_OUT();
179 }