Logo AND Algorithmique Numérique Distribuée

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