Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove unused forward declarations.
[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       break;
46
47     case SIMCALL_SEM_ACQUIRE:
48       simgrid::xbt::intrusive_erase(simcall_sem_acquire__get__sem(simcall)->sleeping, *process);
49       break;
50
51     case SIMCALL_SEM_ACQUIRE_TIMEOUT:
52       simgrid::xbt::intrusive_erase(simcall_sem_acquire_timeout__get__sem(simcall)->sleeping, *process);
53       break;
54
55     default:
56       THROW_IMPOSSIBLE;
57   }
58   XBT_OUT();
59 }
60
61 void SIMIX_synchro_finish(smx_activity_t synchro)
62 {
63   XBT_IN("(%p)", synchro.get());
64   smx_simcall_t simcall = synchro->simcalls.front();
65   synchro->simcalls.pop_front();
66
67   switch (synchro->state) {
68
69     case SIMIX_SRC_TIMEOUT:
70       SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Synchro's wait timeout");
71       break;
72
73     case SIMIX_FAILED:
74         simcall->issuer->context->iwannadie = 1;
75       break;
76
77     default:
78       THROW_IMPOSSIBLE;
79       break;
80   }
81
82   SIMIX_synchro_stop_waiting(simcall->issuer, simcall);
83   simcall->issuer->waiting_synchro = nullptr;
84   SIMIX_simcall_answer(simcall);
85   XBT_OUT();
86 }
87
88 /******************************** Semaphores **********************************/
89 /** @brief Initialize a semaphore */
90 smx_sem_t SIMIX_sem_init(unsigned int value)
91 {
92   XBT_IN("(%u)",value);
93   smx_sem_t sem = new s_smx_sem_t;
94   sem->value = value;
95   XBT_OUT();
96   return sem;
97 }
98
99 /** @brief Destroys a semaphore */
100 void SIMIX_sem_destroy(smx_sem_t sem)
101 {
102   XBT_IN("(%p)",sem);
103   XBT_DEBUG("Destroy semaphore %p", sem);
104   if (sem != nullptr) {
105     xbt_assert(sem->sleeping.empty(), "Cannot destroy semaphore since someone is still using it");
106     delete sem;
107   }
108   XBT_OUT();
109 }
110
111 /** @brief release the semaphore
112  *
113  * Unlock a process waiting on the semaphore.
114  * If no one was blocked, the semaphore capacity is increased by 1.
115  */
116 void SIMIX_sem_release(smx_sem_t sem)
117 {
118   XBT_IN("(%p)",sem);
119   XBT_DEBUG("Sem release semaphore %p", sem);
120   if (not sem->sleeping.empty()) {
121     auto& proc = sem->sleeping.front();
122     sem->sleeping.pop_front();
123     proc.waiting_synchro = nullptr;
124     SIMIX_simcall_answer(&proc.simcall);
125   } else {
126     sem->value++;
127   }
128   XBT_OUT();
129 }
130
131 /** @brief Returns true if acquiring this semaphore would block */
132 int SIMIX_sem_would_block(smx_sem_t sem)
133 {
134   XBT_IN("(%p)",sem);
135   XBT_OUT();
136   return (sem->value <= 0);
137 }
138
139 /** @brief Returns the current capacity of the semaphore */
140 int SIMIX_sem_get_capacity(smx_sem_t sem)
141 {
142   XBT_IN("(%p)",sem);
143   XBT_OUT();
144   return sem->value;
145 }
146
147 static void _SIMIX_sem_wait(smx_sem_t sem, double timeout, smx_actor_t issuer,
148                             smx_simcall_t simcall)
149 {
150   XBT_IN("(%p, %f, %p, %p)",sem,timeout,issuer,simcall);
151   smx_activity_t synchro = nullptr;
152
153   XBT_DEBUG("Wait semaphore %p (timeout:%f)", sem, timeout);
154   if (sem->value <= 0) {
155     synchro = SIMIX_synchro_wait(issuer->host, timeout);
156     synchro->simcalls.push_front(simcall);
157     issuer->waiting_synchro = synchro;
158     sem->sleeping.push_back(*issuer);
159   } else {
160     sem->value--;
161     SIMIX_simcall_answer(simcall);
162   }
163   XBT_OUT();
164 }
165
166 /**
167  * \brief Handles a sem acquire simcall without timeout.
168  */
169 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
170 {
171   XBT_IN("(%p)",simcall);
172   _SIMIX_sem_wait(sem, -1, simcall->issuer, simcall);
173   XBT_OUT();
174 }
175
176 /**
177  * \brief Handles a sem acquire simcall with timeout.
178  */
179 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
180 {
181   XBT_IN("(%p)",simcall);
182   _SIMIX_sem_wait(sem, timeout, simcall->issuer, simcall);
183   XBT_OUT();
184 }