Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
66f332c15eb4f5951b870251117af966616d9bc9
[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.h"
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
13 #include "src/kernel/activity/SynchroRaw.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_synchro, simix, "SIMIX Synchronization (mutex, semaphores and conditions)");
16
17 static smx_activity_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout);
18 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
19                              smx_actor_t issuer, smx_simcall_t simcall);
20 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
21                             smx_simcall_t simcall);
22
23 /***************************** Raw synchronization *********************************/
24
25 static smx_activity_t SIMIX_synchro_wait(sg_host_t smx_host, double timeout)
26 {
27   XBT_IN("(%p, %f)",smx_host,timeout);
28
29   simgrid::kernel::activity::RawImplPtr sync =
30       simgrid::kernel::activity::RawImplPtr(new simgrid::kernel::activity::RawImpl());
31   sync->sleep                          = smx_host->pimpl_cpu->sleep(timeout);
32   sync->sleep->setData(&*sync);
33   XBT_OUT();
34   return sync;
35 }
36
37 void SIMIX_synchro_stop_waiting(smx_actor_t process, smx_simcall_t simcall)
38 {
39   XBT_IN("(%p, %p)",process,simcall);
40   switch (simcall->call) {
41
42     case SIMCALL_MUTEX_LOCK:
43       xbt_swag_remove(process, simcall_mutex_lock__get__mutex(simcall)->sleeping);
44       break;
45
46     case SIMCALL_COND_WAIT:
47       xbt_swag_remove(process, simcall_cond_wait__get__cond(simcall)->sleeping);
48       break;
49
50     case SIMCALL_COND_WAIT_TIMEOUT:
51       xbt_swag_remove(process, simcall_cond_wait_timeout__get__cond(simcall)->sleeping);
52       break;
53
54     case SIMCALL_SEM_ACQUIRE:
55       xbt_swag_remove(process, simcall_sem_acquire__get__sem(simcall)->sleeping);
56       break;
57
58     case SIMCALL_SEM_ACQUIRE_TIMEOUT:
59       xbt_swag_remove(process, simcall_sem_acquire_timeout__get__sem(simcall)->sleeping);
60       break;
61
62     default:
63       THROW_IMPOSSIBLE;
64   }
65   XBT_OUT();
66 }
67
68 void SIMIX_synchro_finish(smx_activity_t synchro)
69 {
70   XBT_IN("(%p)", synchro.get());
71   smx_simcall_t simcall = synchro->simcalls.front();
72   synchro->simcalls.pop_front();
73
74   switch (synchro->state) {
75
76     case SIMIX_SRC_TIMEOUT:
77       SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
78       break;
79
80     case SIMIX_FAILED:
81         simcall->issuer->context->iwannadie = 1;
82 //      SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
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 /*********************************** Mutex ************************************/
96
97 namespace simgrid {
98 namespace simix {
99
100 MutexImpl::MutexImpl() : mutex_(this)
101 {
102   XBT_IN("(%p)", this);
103   // Useful to initialize sleeping swag:
104   simgrid::simix::ActorImpl p;
105   this->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
106   XBT_OUT();
107 }
108
109 MutexImpl::~MutexImpl()
110 {
111   XBT_IN("(%p)", this);
112   xbt_swag_free(this->sleeping);
113   XBT_OUT();
114 }
115
116 void MutexImpl::lock(smx_actor_t issuer)
117 {
118   XBT_IN("(%p; %p)", this, issuer);
119   /* FIXME: check where to validate the arguments */
120   smx_activity_t synchro = nullptr;
121
122   if (this->locked) {
123     /* FIXME: check if the host is active ? */
124     /* Somebody using the mutex, use a synchronization to get host failures */
125     synchro = SIMIX_synchro_wait(issuer->host, -1);
126     synchro->simcalls.push_back(&issuer->simcall);
127     issuer->waiting_synchro = synchro;
128     xbt_swag_insert(issuer, this->sleeping);
129   } else {
130     /* mutex free */
131     this->locked = true;
132     this->owner = issuer;
133     SIMIX_simcall_answer(&issuer->simcall);
134   }
135   XBT_OUT();
136 }
137
138 /** Tries to lock the mutex for a process
139  *
140  * \param  issuer  the process that tries to acquire the mutex
141  * \return whether we managed to lock the mutex
142  */
143 bool MutexImpl::try_lock(smx_actor_t issuer)
144 {
145   XBT_IN("(%p, %p)", this, issuer);
146   if (this->locked) {
147     XBT_OUT();
148     return false;
149   }
150
151   this->locked = true;
152   this->owner = issuer;
153   XBT_OUT();
154   return true;
155 }
156
157 /** Unlock a mutex for a process
158  *
159  * Unlocks the mutex and gives it to a process waiting for it.
160  * If the unlocker is not the owner of the mutex nothing happens.
161  * If there are no process waiting, it sets the mutex as free.
162  */
163 void MutexImpl::unlock(smx_actor_t issuer)
164 {
165   XBT_IN("(%p, %p)", this, issuer);
166   if (not this->locked)
167     THROWF(mismatch_error, 0, "Cannot release that mutex: it was not locked.");
168
169   /* If the mutex is not owned by the issuer, that's not good */
170   if (issuer != this->owner)
171     THROWF(mismatch_error, 0, "Cannot release that mutex: it was locked by %s (pid:%lu), not by you.",
172            this->owner->cname(), this->owner->pid);
173
174   if (xbt_swag_size(this->sleeping) > 0) {
175     /*process to wake up */
176     smx_actor_t p = (smx_actor_t) xbt_swag_extract(this->sleeping);
177     p->waiting_synchro = nullptr;
178     this->owner = p;
179     SIMIX_simcall_answer(&p->simcall);
180   } else {
181     /* nobody to wake up */
182     this->locked = false;
183     this->owner = nullptr;
184   }
185   XBT_OUT();
186 }
187
188 }
189 }
190
191 /** Increase the refcount for this mutex */
192 smx_mutex_t SIMIX_mutex_ref(smx_mutex_t mutex)
193 {
194   if (mutex != nullptr)
195     intrusive_ptr_add_ref(mutex);
196   return mutex;
197 }
198
199 /** Decrease the refcount for this mutex */
200 void SIMIX_mutex_unref(smx_mutex_t mutex)
201 {
202   if (mutex != nullptr)
203     intrusive_ptr_release(mutex);
204 }
205
206 // Simcall handlers:
207
208 void simcall_HANDLER_mutex_lock(smx_simcall_t simcall, smx_mutex_t mutex)
209 {
210   mutex->lock(simcall->issuer);
211 }
212
213 int simcall_HANDLER_mutex_trylock(smx_simcall_t simcall, smx_mutex_t mutex)
214 {
215   return mutex->try_lock(simcall->issuer);
216 }
217
218 void simcall_HANDLER_mutex_unlock(smx_simcall_t simcall, smx_mutex_t mutex)
219 {
220   mutex->unlock(simcall->issuer);
221 }
222
223 /********************************* Condition **********************************/
224
225 /**
226  * \brief Initialize a condition.
227  *
228  * Allocates and creates the data for the condition.
229  * It have to be called before the use of the condition.
230  * \return A condition
231  */
232 smx_cond_t SIMIX_cond_init()
233 {
234   XBT_IN("()");
235   simgrid::simix::ActorImpl p;
236   smx_cond_t cond = new s_smx_cond();
237   cond->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
238   cond->refcount_ = 1;
239   XBT_OUT();
240   return cond;
241 }
242
243 /**
244  * \brief Handle a condition waiting simcall without timeouts
245  * \param simcall the simcall
246  */
247 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
248 {
249   XBT_IN("(%p)",simcall);
250   smx_actor_t issuer = simcall->issuer;
251
252   _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
253   XBT_OUT();
254 }
255
256 /**
257  * \brief Handle a condition waiting simcall with timeouts
258  * \param simcall the simcall
259  */
260 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond,
261                      smx_mutex_t mutex, double timeout)
262 {
263   XBT_IN("(%p)",simcall);
264   smx_actor_t issuer = simcall->issuer;
265
266   _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
267   XBT_OUT();
268 }
269
270
271 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout,
272                              smx_actor_t issuer, smx_simcall_t simcall)
273 {
274   XBT_IN("(%p, %p, %f, %p,%p)",cond,mutex,timeout,issuer,simcall);
275   smx_activity_t synchro = nullptr;
276
277   XBT_DEBUG("Wait condition %p", cond);
278
279   /* If there is a mutex unlock it */
280   /* FIXME: what happens if the issuer is not the owner of the mutex? */
281   if (mutex != nullptr) {
282     cond->mutex = mutex;
283     mutex->unlock(issuer);
284   }
285
286   synchro = SIMIX_synchro_wait(issuer->host, timeout);
287   synchro->simcalls.push_front(simcall);
288   issuer->waiting_synchro = synchro;
289   xbt_swag_insert(simcall->issuer, cond->sleeping);
290   XBT_OUT();
291 }
292
293 /**
294  * \brief Signalizes a condition.
295  *
296  * Signalizes a condition and wakes up a sleeping process.
297  * If there are no process sleeping, no action is done.
298  * \param cond A condition
299  */
300 void SIMIX_cond_signal(smx_cond_t cond)
301 {
302   XBT_IN("(%p)",cond);
303   smx_actor_t proc = nullptr;
304   smx_mutex_t mutex = nullptr;
305   smx_simcall_t simcall = nullptr;
306
307   XBT_DEBUG("Signal condition %p", cond);
308
309   /* If there are processes waiting for the condition choose one and try
310      to make it acquire the mutex */
311   if ((proc = (smx_actor_t) xbt_swag_extract(cond->sleeping))) {
312
313     /* Destroy waiter's synchronization */
314     proc->waiting_synchro = nullptr;
315
316     /* Now transform the cond wait simcall into a mutex lock one */
317     simcall = &proc->simcall;
318     if(simcall->call == SIMCALL_COND_WAIT)
319       mutex = simcall_cond_wait__get__mutex(simcall);
320     else
321       mutex = simcall_cond_wait_timeout__get__mutex(simcall);
322     simcall->call = SIMCALL_MUTEX_LOCK;
323
324     simcall_HANDLER_mutex_lock(simcall, mutex);
325   }
326   XBT_OUT();
327 }
328
329 /**
330  * \brief Broadcasts a condition.
331  *
332  * Signal ALL processes waiting on a condition.
333  * If there are no process waiting, no action is done.
334  * \param cond A condition
335  */
336 void SIMIX_cond_broadcast(smx_cond_t cond)
337 {
338   XBT_IN("(%p)",cond);
339   XBT_DEBUG("Broadcast condition %p", cond);
340
341   /* Signal the condition until nobody is waiting on it */
342   while (xbt_swag_size(cond->sleeping)) {
343     SIMIX_cond_signal(cond);
344   }
345   XBT_OUT();
346 }
347
348 smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
349 {
350   if (cond != nullptr)
351     intrusive_ptr_add_ref(cond);
352   return cond;
353 }
354
355 void SIMIX_cond_unref(smx_cond_t cond)
356 {
357   XBT_IN("(%p)",cond);
358   XBT_DEBUG("Destroy condition %p", cond);
359   if (cond != nullptr) {
360     intrusive_ptr_release(cond);
361   }
362   XBT_OUT();
363 }
364
365
366 void intrusive_ptr_add_ref(s_smx_cond_t *cond)
367 {
368   auto previous = (cond->refcount_)++;
369   xbt_assert(previous != 0);
370 }
371
372 void intrusive_ptr_release(s_smx_cond_t *cond)
373 {
374   auto count = --(cond->refcount_);
375   if (count == 0) {
376     xbt_assert(xbt_swag_size(cond->sleeping) == 0,
377                 "Cannot destroy conditional since someone is still using it");
378     xbt_swag_free(cond->sleeping);
379     delete cond;
380   }
381 }
382
383 /******************************** Semaphores **********************************/
384 /** @brief Initialize a semaphore */
385 smx_sem_t SIMIX_sem_init(unsigned int value)
386 {
387   XBT_IN("(%u)",value);
388   simgrid::simix::ActorImpl p;
389
390   smx_sem_t sem = xbt_new0(s_smx_sem_t, 1);
391   sem->sleeping = xbt_swag_new(xbt_swag_offset(p, synchro_hookup));
392   sem->value = value;
393   XBT_OUT();
394   return sem;
395 }
396
397 /** @brief Destroys a semaphore */
398 void SIMIX_sem_destroy(smx_sem_t sem)
399 {
400   XBT_IN("(%p)",sem);
401   XBT_DEBUG("Destroy semaphore %p", sem);
402   if (sem != nullptr) {
403     xbt_assert(xbt_swag_size(sem->sleeping) == 0,
404                 "Cannot destroy semaphore since someone is still using it");
405     xbt_swag_free(sem->sleeping);
406     xbt_free(sem);
407   }
408   XBT_OUT();
409 }
410
411 /** @brief release the semaphore
412  *
413  * Unlock a process waiting on the semaphore.
414  * If no one was blocked, the semaphore capacity is increased by 1.
415  */
416 void SIMIX_sem_release(smx_sem_t sem)
417 {
418   XBT_IN("(%p)",sem);
419   smx_actor_t proc;
420
421   XBT_DEBUG("Sem release semaphore %p", sem);
422   if ((proc = (smx_actor_t) xbt_swag_extract(sem->sleeping))) {
423     proc->waiting_synchro = nullptr;
424     SIMIX_simcall_answer(&proc->simcall);
425   } else {
426     sem->value++;
427   }
428   XBT_OUT();
429 }
430
431 /** @brief Returns true if acquiring this semaphore would block */
432 int SIMIX_sem_would_block(smx_sem_t sem)
433 {
434   XBT_IN("(%p)",sem);
435   XBT_OUT();
436   return (sem->value <= 0);
437 }
438
439 /** @brief Returns the current capacity of the semaphore */
440 int SIMIX_sem_get_capacity(smx_sem_t sem)
441 {
442   XBT_IN("(%p)",sem);
443   XBT_OUT();
444   return sem->value;
445 }
446
447 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
448                             smx_simcall_t simcall)
449 {
450   XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
451   smx_activity_t synchro = nullptr;
452
453   XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
454   if (sem->value <= 0) {
455     synchro = SIMIX_synchro_wait(issuer->host, timeout);
456     synchro->simcalls.push_front(simcall);
457     issuer->waiting_synchro = synchro;
458     xbt_swag_insert(issuer, sem->sleeping);
459   } else {
460     sem->value--;
461     SIMIX_simcall_answer(simcall);
462   }
463   XBT_OUT();
464 }
465
466 /**
467  * \brief Handles a sem acquire simcall without timeout.
468  * \param simcall the simcall
469  */
470 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
471 {
472   XBT_IN("(%p)",simcall);
473   _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
474   XBT_OUT();
475 }
476
477 /**
478  * \brief Handles a sem acquire simcall with timeout.
479  * \param simcall the simcall
480  */
481 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
482 {
483   XBT_IN("(%p)",simcall);
484   _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
485   XBT_OUT();
486 }