Logo AND Algorithmique Numérique Distribuée

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