Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fa4b9cf03db7bb1d0fb2c224badc3e5d26567f4e
[simgrid.git] / src / kernel / activity / ConditionVariableImpl.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 "simgrid/Exception.hpp"
8 #include "src/kernel/activity/MutexImpl.hpp"
9 #include "src/kernel/activity/SynchroRaw.hpp"
10 #include "src/simix/smx_synchro_private.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   simgrid::kernel::activity::RawImplPtr 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 = simgrid::kernel::activity::RawImplPtr(new simgrid::kernel::activity::RawImpl())
32                 ->start(issuer->get_host(), timeout);
33   synchro->simcalls_.push_front(simcall);
34   issuer->waiting_synchro = synchro;
35   cond->sleeping.push_back(*simcall->issuer);
36   XBT_OUT();
37 }
38
39 /**
40  * @brief Handle a condition waiting simcall without timeouts
41  */
42 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
43 {
44   XBT_IN("(%p)", simcall);
45   smx_actor_t issuer = simcall->issuer;
46
47   _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
48   XBT_OUT();
49 }
50
51 /**
52  * @brief Handle a condition waiting simcall with timeouts
53  */
54 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex, double timeout)
55 {
56   XBT_IN("(%p)", simcall);
57   smx_actor_t issuer = simcall->issuer;
58   simcall_cond_wait_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
59   _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
60   XBT_OUT();
61 }
62
63 namespace simgrid {
64 namespace kernel {
65 namespace activity {
66
67 ConditionVariableImpl::ConditionVariableImpl() : cond_(this) {}
68 ConditionVariableImpl::~ConditionVariableImpl() = default;
69
70 /**
71  * @brief Signalizes a condition.
72  *
73  * Signalizes a condition and wakes up a sleeping process.
74  * If there are no process sleeping, no action is done.
75  */
76 void ConditionVariableImpl::signal()
77 {
78   XBT_DEBUG("Signal condition %p", this);
79
80   /* If there are processes waiting for the condition choose one and try
81      to make it acquire the mutex */
82   if (not sleeping.empty()) {
83     auto& proc = sleeping.front();
84     sleeping.pop_front();
85
86     /* Destroy waiter's synchronization */
87     proc.waiting_synchro = nullptr;
88
89     /* Now transform the cond wait simcall into a mutex lock one */
90     smx_simcall_t simcall = &proc.simcall;
91     smx_mutex_t simcall_mutex;
92     if (simcall->call == SIMCALL_COND_WAIT)
93       simcall_mutex = simcall_cond_wait__get__mutex(simcall);
94     else
95       simcall_mutex = simcall_cond_wait_timeout__get__mutex(simcall);
96     simcall->call = SIMCALL_MUTEX_LOCK;
97
98     simcall_HANDLER_mutex_lock(simcall, simcall_mutex);
99   }
100   XBT_OUT();
101 }
102
103 /**
104  * @brief Broadcasts a condition.
105  *
106  * Signal ALL processes waiting on a condition.
107  * If there are no process waiting, no action is done.
108  */
109 void ConditionVariableImpl::broadcast()
110 {
111   XBT_DEBUG("Broadcast condition %p", this);
112
113   /* Signal the condition until nobody is waiting on it */
114   while (not sleeping.empty())
115     signal();
116 }
117
118 // boost::intrusive_ptr<ConditionVariableImpl> support:
119 void intrusive_ptr_add_ref(simgrid::kernel::activity::ConditionVariableImpl* cond)
120 {
121   cond->refcount_.fetch_add(1, std::memory_order_relaxed);
122 }
123
124 void intrusive_ptr_release(simgrid::kernel::activity::ConditionVariableImpl* cond)
125 {
126   if (cond->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
127     std::atomic_thread_fence(std::memory_order_acquire);
128     xbt_assert(cond->sleeping.empty(), "Cannot destroy conditional since someone is still using it");
129     delete cond;
130   }
131 }
132 } // namespace activity
133 } // namespace kernel
134 }