Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
start modernizing ConditionVariableImpl
[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 simgrid::kernel::activity::ConditionVariableImpl();
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 namespace simgrid {
83 namespace kernel {
84 namespace activity {
85
86 ConditionVariableImpl::ConditionVariableImpl() : cond_(this) {}
87 ConditionVariableImpl::~ConditionVariableImpl() = default;
88
89 /**
90  * \brief Signalizes a condition.
91  *
92  * Signalizes a condition and wakes up a sleeping process.
93  * If there are no process sleeping, no action is done.
94  * \param cond A condition
95  */
96 void ConditionVariableImpl::signal()
97 {
98   XBT_DEBUG("Signal condition %p", this);
99
100   /* If there are processes waiting for the condition choose one and try
101      to make it acquire the mutex */
102   if (not sleeping.empty()) {
103     auto& proc = sleeping.front();
104     sleeping.pop_front();
105
106     /* Destroy waiter's synchronization */
107     proc.waiting_synchro = nullptr;
108
109     /* Now transform the cond wait simcall into a mutex lock one */
110     smx_simcall_t simcall = &proc.simcall;
111     smx_mutex_t mutex;
112     if (simcall->call == SIMCALL_COND_WAIT)
113       mutex = simcall_cond_wait__get__mutex(simcall);
114     else
115       mutex = simcall_cond_wait_timeout__get__mutex(simcall);
116     simcall->call = SIMCALL_MUTEX_LOCK;
117
118     simcall_HANDLER_mutex_lock(simcall, mutex);
119   }
120   XBT_OUT();
121 }
122
123 /**
124  * \brief Broadcasts a condition.
125  *
126  * Signal ALL processes waiting on a condition.
127  * If there are no process waiting, no action is done.
128  */
129 void ConditionVariableImpl::broadcast()
130 {
131   XBT_DEBUG("Broadcast condition %p", this);
132
133   /* Signal the condition until nobody is waiting on it */
134   while (not sleeping.empty())
135     signal();
136 }
137
138 // boost::intrusive_ptr<ConditionVariableImpl> support:
139 void intrusive_ptr_add_ref(simgrid::kernel::activity::ConditionVariableImpl* cond)
140 {
141   cond->refcount_.fetch_add(1, std::memory_order_relaxed);
142 }
143
144 void intrusive_ptr_release(simgrid::kernel::activity::ConditionVariableImpl* cond)
145 {
146   if (cond->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
147     std::atomic_thread_fence(std::memory_order_acquire);
148     xbt_assert(cond->sleeping.empty(), "Cannot destroy conditional since someone is still using it");
149     delete cond;
150   }
151 }
152 } // namespace activity
153 } // namespace kernel
154 }
155
156 XBT_PRIVATE void simcall_HANDLER_cond_signal(smx_simcall_t simcall, smx_cond_t cond)
157 {
158   cond->signal();
159 }
160
161 XBT_PRIVATE void simcall_HANDLER_cond_broadcast(smx_simcall_t simcall, smx_cond_t cond)
162 {
163   cond->broadcast();
164 }