Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Separate ConditionVariableImpl into its own files
[simgrid.git] / src / kernel / activity / ConditionVariableImpl.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 "src/kernel/activity/ConditionVariableImpl.hpp"
7 #include "src/kernel/activity/MutexImpl.hpp"
8 #include "src/kernel/activity/SynchroRaw.hpp"
9 #include "src/simix/smx_synchro_private.hpp"
10 #include "xbt/ex.hpp"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ConditionVariable, simix_synchro, "Condition variables");
13
14 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout, smx_actor_t issuer,
15                              smx_simcall_t simcall);
16
17 /********************************* Condition **********************************/
18
19 /**
20  * \brief Initialize a condition.
21  *
22  * Allocates and creates the data for the condition.
23  * It have to be called before the use of the condition.
24  * \return A condition
25  */
26 smx_cond_t SIMIX_cond_init()
27 {
28   XBT_IN("()");
29   smx_cond_t cond = new s_smx_cond_t();
30   XBT_OUT();
31   return cond;
32 }
33
34 /**
35  * \brief Handle a condition waiting simcall without timeouts
36  * \param simcall the simcall
37  */
38 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
39 {
40   XBT_IN("(%p)", simcall);
41   smx_actor_t issuer = simcall->issuer;
42
43   _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
44   XBT_OUT();
45 }
46
47 /**
48  * \brief Handle a condition waiting simcall with timeouts
49  * \param simcall the simcall
50  */
51 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex, double timeout)
52 {
53   XBT_IN("(%p)", simcall);
54   smx_actor_t issuer = simcall->issuer;
55
56   _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
57   XBT_OUT();
58 }
59
60 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout, smx_actor_t issuer,
61                              smx_simcall_t simcall)
62 {
63   XBT_IN("(%p, %p, %f, %p,%p)", cond, mutex, timeout, issuer, simcall);
64   smx_activity_t synchro = nullptr;
65
66   XBT_DEBUG("Wait condition %p", cond);
67
68   /* If there is a mutex unlock it */
69   /* FIXME: what happens if the issuer is not the owner of the mutex? */
70   if (mutex != nullptr) {
71     cond->mutex = mutex;
72     mutex->unlock(issuer);
73   }
74
75   synchro = SIMIX_synchro_wait(issuer->host, timeout);
76   synchro->simcalls.push_front(simcall);
77   issuer->waiting_synchro = synchro;
78   cond->sleeping.push_back(*simcall->issuer);
79   XBT_OUT();
80 }
81
82 /**
83  * \brief Signalizes a condition.
84  *
85  * Signalizes a condition and wakes up a sleeping process.
86  * If there are no process sleeping, no action is done.
87  * \param cond A condition
88  */
89 void SIMIX_cond_signal(smx_cond_t cond)
90 {
91   XBT_IN("(%p)", cond);
92   XBT_DEBUG("Signal condition %p", cond);
93
94   /* If there are processes waiting for the condition choose one and try
95      to make it acquire the mutex */
96   if (not cond->sleeping.empty()) {
97     auto& proc = cond->sleeping.front();
98     cond->sleeping.pop_front();
99
100     /* Destroy waiter's synchronization */
101     proc.waiting_synchro = nullptr;
102
103     /* Now transform the cond wait simcall into a mutex lock one */
104     smx_simcall_t simcall = &proc.simcall;
105     smx_mutex_t mutex;
106     if (simcall->call == SIMCALL_COND_WAIT)
107       mutex = simcall_cond_wait__get__mutex(simcall);
108     else
109       mutex = simcall_cond_wait_timeout__get__mutex(simcall);
110     simcall->call = SIMCALL_MUTEX_LOCK;
111
112     simcall_HANDLER_mutex_lock(simcall, mutex);
113   }
114   XBT_OUT();
115 }
116
117 /**
118  * \brief Broadcasts a condition.
119  *
120  * Signal ALL processes waiting on a condition.
121  * If there are no process waiting, no action is done.
122  * \param cond A condition
123  */
124 void SIMIX_cond_broadcast(smx_cond_t cond)
125 {
126   XBT_IN("(%p)", cond);
127   XBT_DEBUG("Broadcast condition %p", cond);
128
129   /* Signal the condition until nobody is waiting on it */
130   while (not cond->sleeping.empty()) {
131     SIMIX_cond_signal(cond);
132   }
133   XBT_OUT();
134 }
135
136 smx_cond_t SIMIX_cond_ref(smx_cond_t cond)
137 {
138   if (cond != nullptr)
139     intrusive_ptr_add_ref(cond);
140   return cond;
141 }
142
143 void SIMIX_cond_unref(smx_cond_t cond)
144 {
145   XBT_IN("(%p)", cond);
146   XBT_DEBUG("Destroy condition %p", cond);
147   if (cond != nullptr) {
148     intrusive_ptr_release(cond);
149   }
150   XBT_OUT();
151 }
152
153 void intrusive_ptr_add_ref(s_smx_cond_t* cond)
154 {
155   auto previous = cond->refcount_.fetch_add(1);
156   xbt_assert(previous != 0);
157 }
158
159 void intrusive_ptr_release(s_smx_cond_t* cond)
160 {
161   if (cond->refcount_.fetch_sub(1) == 1) {
162     xbt_assert(cond->sleeping.empty(), "Cannot destroy conditional since someone is still using it");
163     delete cond;
164   }
165 }