Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics: define function before use.
[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 /********************************* Condition **********************************/
15
16 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout, smx_actor_t issuer,
17                              smx_simcall_t simcall)
18 {
19   XBT_IN("(%p, %p, %f, %p,%p)", cond, mutex, timeout, issuer, simcall);
20   smx_activity_t synchro = nullptr;
21
22   XBT_DEBUG("Wait condition %p", cond);
23
24   /* If there is a mutex unlock it */
25   /* FIXME: what happens if the issuer is not the owner of the mutex? */
26   if (mutex != nullptr) {
27     cond->mutex = mutex;
28     mutex->unlock(issuer);
29   }
30
31   synchro = SIMIX_synchro_wait(issuer->host, timeout);
32   synchro->simcalls.push_front(simcall);
33   issuer->waiting_synchro = synchro;
34   cond->sleeping.push_back(*simcall->issuer);
35   XBT_OUT();
36 }
37
38 /**
39  * \brief Handle a condition waiting simcall without timeouts
40  */
41 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
42 {
43   XBT_IN("(%p)", simcall);
44   smx_actor_t issuer = simcall->issuer;
45
46   _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
47   XBT_OUT();
48 }
49
50 /**
51  * \brief Handle a condition waiting simcall with timeouts
52  */
53 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex, double timeout)
54 {
55   XBT_IN("(%p)", simcall);
56   smx_actor_t issuer = simcall->issuer;
57
58   _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
59   XBT_OUT();
60 }
61
62 namespace simgrid {
63 namespace kernel {
64 namespace activity {
65
66 ConditionVariableImpl::ConditionVariableImpl() : cond_(this) {}
67 ConditionVariableImpl::~ConditionVariableImpl() = default;
68
69 /**
70  * \brief Signalizes a condition.
71  *
72  * Signalizes a condition and wakes up a sleeping process.
73  * If there are no process sleeping, no action is done.
74  */
75 void ConditionVariableImpl::signal()
76 {
77   XBT_DEBUG("Signal condition %p", this);
78
79   /* If there are processes waiting for the condition choose one and try
80      to make it acquire the mutex */
81   if (not sleeping.empty()) {
82     auto& proc = sleeping.front();
83     sleeping.pop_front();
84
85     /* Destroy waiter's synchronization */
86     proc.waiting_synchro = nullptr;
87
88     /* Now transform the cond wait simcall into a mutex lock one */
89     smx_simcall_t simcall = &proc.simcall;
90     smx_mutex_t simcall_mutex;
91     if (simcall->call == SIMCALL_COND_WAIT)
92       simcall_mutex = simcall_cond_wait__get__mutex(simcall);
93     else
94       simcall_mutex = simcall_cond_wait_timeout__get__mutex(simcall);
95     simcall->call = SIMCALL_MUTEX_LOCK;
96
97     simcall_HANDLER_mutex_lock(simcall, simcall_mutex);
98   }
99   XBT_OUT();
100 }
101
102 /**
103  * \brief Broadcasts a condition.
104  *
105  * Signal ALL processes waiting on a condition.
106  * If there are no process waiting, no action is done.
107  */
108 void ConditionVariableImpl::broadcast()
109 {
110   XBT_DEBUG("Broadcast condition %p", this);
111
112   /* Signal the condition until nobody is waiting on it */
113   while (not sleeping.empty())
114     signal();
115 }
116
117 // boost::intrusive_ptr<ConditionVariableImpl> support:
118 void intrusive_ptr_add_ref(simgrid::kernel::activity::ConditionVariableImpl* cond)
119 {
120   cond->refcount_.fetch_add(1, std::memory_order_relaxed);
121 }
122
123 void intrusive_ptr_release(simgrid::kernel::activity::ConditionVariableImpl* cond)
124 {
125   if (cond->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
126     std::atomic_thread_fence(std::memory_order_acquire);
127     xbt_assert(cond->sleeping.empty(), "Cannot destroy conditional since someone is still using it");
128     delete cond;
129   }
130 }
131 } // namespace activity
132 } // namespace kernel
133 }