Logo AND Algorithmique Numérique Distribuée

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