Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1d78318d7cf4128c6a9fe6e618c3d8ce57b93888
[simgrid.git] / src / simix / smx_synchro.cpp
1 /* Copyright (c) 2007-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "smx_private.hpp"
8 #include "src/surf/cpu_interface.hpp"
9 #include "src/surf/surf_interface.hpp"
10 #include <xbt/ex.hpp>
11 #include <xbt/log.h>
12 #include <xbt/utility.hpp>
13
14 #include "src/kernel/activity/SynchroRaw.hpp"
15 #include "src/simix/MutexImpl.hpp"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix, "SIMIX Synchronization (mutex, semaphores and conditions)");
18
19 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
20                              smx_actor_t issuer, smx_simcall_t simcall);
21 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
22                             smx_simcall_t simcall);
23
24 /***************************** Raw synchronization *********************************/
25
26 smx_activity_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
27 {
28   XBT_IN("(%p, %f)",smx_host,timeout);
29
30   simgrid::kernel::activity::RawImplPtr sync =
31       simgrid::kernel::activity::RawImplPtr(new simgrid::kernel::activity::RawImpl());
32   sync->sleep                          = smx_host->pimpl_cpu->sleep(timeout);
33   sync->sleep->setData(sync.get());
34   XBT_OUT();
35   return sync;
36 }
37
38 void SIMIX_synchro_stop_waiting(smx_actor_t process, smx_simcall_t simcall)
39 {
40   XBT_IN("(%p, %p)",process,simcall);
41   switch (simcall->call) {
42
43     case SIMCALL_MUTEX_LOCK:
44       simgrid::xbt::intrusive_erase(simcall_mutex_lock__get__mutex(simcall)->sleeping, *process);
45       break;
46
47     case SIMCALL_COND_WAIT:
48       simgrid::xbt::intrusive_erase(simcall_cond_wait__get__cond(simcall)->sleeping, *process);
49       break;
50
51     case SIMCALL_COND_WAIT_TIMEOUT:
52       simgrid::xbt::intrusive_erase(simcall_cond_wait_timeout__get__cond(simcall)->sleeping, *process);
53       break;
54
55     case SIMCALL_SEM_ACQUIRE:
56       simgrid::xbt::intrusive_erase(simcall_sem_acquire__get__sem(simcall)->sleeping, *process);
57       break;
58
59     case SIMCALL_SEM_ACQUIRE_TIMEOUT:
60       simgrid::xbt::intrusive_erase(simcall_sem_acquire_timeout__get__sem(simcall)->sleeping, *process);
61       break;
62
63     default:
64       THROW_IMPOSSIBLE;
65   }
66   XBT_OUT();
67 }
68
69 void SIMIX_synchro_finish(smx_activity_t synchro)
70 {
71   XBT_IN("(%p)", synchro.get());
72   smx_simcall_t simcall = synchro->simcalls.front();
73   synchro->simcalls.pop_front();
74
75   switch (synchro->state) {
76
77     case SIMIX_SRC_TIMEOUT:
78       SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
79       break;
80
81     case SIMIX_FAILED:
82         simcall->issuer->context->iwannadie = 1;
83       break;
84
85     default:
86       THROW_IMPOSSIBLE;
87       break;
88   }
89
90   SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
91   simcall->issuer->waiting_synchro = nullptr;
92   SIMIX_simcall_answer(simcall);
93   XBT_OUT();
94 }
95
96 /********************************* Condition **********************************/
97
98 /**
99  * \brief Initialize a condition.
100  *
101  * Allocates and creates the data for the condition.
102  * It have to be called before the use of the condition.
103  * \return A condition
104  */
105 smx_cond_t SIMIX_cond_init()
106 {
107   XBT_IN("()");
108   smx_cond_t cond = new s_smx_cond_t();
109   cond->refcount_ = 1;
110   XBT_OUT();
111   return cond;
112 }
113
114 /**
115  * \brief Handle a condition waiting simcall without timeouts
116  * \param simcall the simcall
117  */
118 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
119 {
120   XBT_IN("(%p)",simcall);
121   smx_actor_t issuer = simcall->issuer;
122
123   _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
124   XBT_OUT();
125 }
126
127 /**
128  * \brief Handle a condition waiting simcall with timeouts
129  * \param simcall the simcall
130  */
131 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
132                      smx_mutex_t mutex, double timeout)
133 {
134   XBT_IN("(%p)",simcall);
135   smx_actor_t issuer = simcall->issuer;
136
137   _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
138   XBT_OUT();
139 }
140
141
142 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
143                              smx_actor_t issuer, smx_simcall_t simcall)
144 {
145   XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
146   smx_activity_t synchro = nullptr;
147
148   XBT_DEBUG("Wait condition %p", cond);
149
150   /* If there is a mutex unlock it */
151   /* FIXME: what happens if the issuer is not the owner of the mutex? */
152   if (mutex != nullptr) {
153     cond->mutex = mutex;
154     mutex->unlock(issuer);
155   }
156
157   synchro = SIMIX_synchro_wait(issuer->host, timeout);
158   synchro->simcalls.push_front(simcall);
159   issuer->waiting_synchro = synchro;
160   cond->sleeping.push_back(*simcall->issuer);
161   XBT_OUT();
162 }
163
164 /**
165  * \brief Signalizes a condition.
166  *
167  * Signalizes a condition and wakes up a sleeping process.
168  * If there are no process sleeping, no action is done.
169  * \param cond A condition
170  */
171 void SIMIX_cond_signal(smx_cond_t cond)
172 {
173   XBT_IN("(%p)",cond);
174   XBT_DEBUG("Signal condition %p", cond);
175
176   /* If there are processes waiting for the condition choose one and try
177      to make it acquire the mutex */
178   if (not cond->sleeping.empty()) {
179     auto& proc = cond->sleeping.front();
180     cond->sleeping.pop_front();
181
182     /* Destroy waiter's synchronization */
183     proc.waiting_synchro = nullptr;
184
185     /* Now transform the cond wait simcall into a mutex lock one */
186     smx_simcall_t simcall = &proc.simcall;
187     smx_mutex_t mutex;
188     if(simcall->call == SIMCALL_COND_WAIT)
189       mutex = simcall_cond_wait__get__mutex(simcall);
190     else
191       mutex = simcall_cond_wait_timeout__get__mutex(simcall);
192     simcall->call = SIMCALL_MUTEX_LOCK;
193
194     simcall_HANDLER_mutex_lock(simcall, mutex);
195   }
196   XBT_OUT();
197 }
198
199 /**
200  * \brief Broadcasts a condition.
201  *
202  * Signal ALL processes waiting on a condition.
203  * If there are no process waiting, no action is done.
204  * \param cond A condition
205  */
206 void SIMIX_cond_broadcast(smx_cond_t cond)
207 {
208   XBT_IN("(%p)",cond);
209   XBT_DEBUG("Broadcast condition %p", cond);
210
211   /* Signal the condition until nobody is waiting on it */
212   while (not cond->sleeping.empty()) {
213     SIMIX_cond_signal(cond);
214   }
215   XBT_OUT();
216 }
217
218 smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
219 {
220   if (cond != nullptr)
221     intrusive_ptr_add_ref(cond);
222   return cond;
223 }
224
225 void SIMIX_cond_unref(smx_cond_t cond)
226 {
227   XBT_IN("(%p)",cond);
228   XBT_DEBUG("Destroy condition %p", cond);
229   if (cond != nullptr) {
230     intrusive_ptr_release(cond);
231   }
232   XBT_OUT();
233 }
234
235
236 void intrusive_ptr_add_ref(s_smx_cond_t *cond)
237 {
238   auto previous = cond->refcount_.fetch_add(1);
239   xbt_assert(previous != 0);
240 }
241
242 void intrusive_ptr_release(s_smx_cond_t *cond)
243 {
244   if (cond->refcount_.fetch_sub(1) == 1) {
245     xbt_assert(cond->sleeping.empty(), "Cannot destroy conditional since someone is still using it");
246     delete cond;
247   }
248 }
249
250 /******************************** Semaphores **********************************/
251 /** @brief Initialize a semaphore */
252 smx_sem_t SIMIX_sem_init(unsigned int value)
253 {
254   XBT_IN("(%u)",value);
255   smx_sem_t sem = new s_smx_sem_t;
256   sem->value = value;
257   XBT_OUT();
258   return sem;
259 }
260
261 /** @brief Destroys a semaphore */
262 void SIMIX_sem_destroy(smx_sem_t sem)
263 {
264   XBT_IN("(%p)",sem);
265   XBT_DEBUG("Destroy semaphore %p", sem);
266   if (sem != nullptr) {
267     xbt_assert(sem->sleeping.empty(), "Cannot destroy semaphore since someone is still using it");
268     delete sem;
269   }
270   XBT_OUT();
271 }
272
273 /** @brief release the semaphore
274  *
275  * Unlock a process waiting on the semaphore.
276  * If no one was blocked, the semaphore capacity is increased by 1.
277  */
278 void SIMIX_sem_release(smx_sem_t sem)
279 {
280   XBT_IN("(%p)",sem);
281   XBT_DEBUG("Sem release semaphore %p", sem);
282   if (not sem->sleeping.empty()) {
283     auto& proc = sem->sleeping.front();
284     sem->sleeping.pop_front();
285     proc.waiting_synchro = nullptr;
286     SIMIX_simcall_answer(&proc.simcall);
287   } else {
288     sem->value++;
289   }
290   XBT_OUT();
291 }
292
293 /** @brief Returns true if acquiring this semaphore would block */
294 int SIMIX_sem_would_block(smx_sem_t sem)
295 {
296   XBT_IN("(%p)",sem);
297   XBT_OUT();
298   return (sem->value <= 0);
299 }
300
301 /** @brief Returns the current capacity of the semaphore */
302 int SIMIX_sem_get_capacity(smx_sem_t sem)
303 {
304   XBT_IN("(%p)",sem);
305   XBT_OUT();
306   return sem->value;
307 }
308
309 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
310                             smx_simcall_t simcall)
311 {
312   XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
313   smx_activity_t synchro = nullptr;
314
315   XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
316   if (sem->value <= 0) {
317     synchro = SIMIX_synchro_wait(issuer->host, timeout);
318     synchro->simcalls.push_front(simcall);
319     issuer->waiting_synchro = synchro;
320     sem->sleeping.push_back(*issuer);
321   } else {
322     sem->value--;
323     SIMIX_simcall_answer(simcall);
324   }
325   XBT_OUT();
326 }
327
328 /**
329  * \brief Handles a sem acquire simcall without timeout.
330  * \param simcall the simcall
331  */
332 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
333 {
334   XBT_IN("(%p)",simcall);
335   _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
336   XBT_OUT();
337 }
338
339 /**
340  * \brief Handles a sem acquire simcall with timeout.
341  * \param simcall the simcall
342  */
343 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
344 {
345   XBT_IN("(%p)",simcall);
346   _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
347   XBT_OUT();
348 }