Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
small simplifications around simcalls
[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_DEBUG("Wait condition %p", this);
93
94   /* If there is a mutex unlock it */
95   /* FIXME: what happens if the issuer is not the owner of the mutex? */
96   if (mutex != nullptr) {
97     mutex_ = mutex;
98     mutex->unlock(issuer);
99   }
100
101   RawImplPtr synchro(new RawImpl());
102   synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
103   synchro->register_simcall(simcall);
104   sleeping_.push_back(*simcall->issuer_);
105 }
106
107 // boost::intrusive_ptr<ConditionVariableImpl> support:
108 void intrusive_ptr_add_ref(simgrid::kernel::activity::ConditionVariableImpl* cond)
109 {
110   cond->refcount_.fetch_add(1, std::memory_order_relaxed);
111 }
112
113 void intrusive_ptr_release(simgrid::kernel::activity::ConditionVariableImpl* cond)
114 {
115   if (cond->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
116     std::atomic_thread_fence(std::memory_order_acquire);
117     xbt_assert(cond->sleeping_.empty(), "Cannot destroy conditional since someone is still using it");
118     delete cond;
119   }
120 }
121 } // namespace activity
122 } // namespace kernel
123 } // namespace simgrid