Logo AND Algorithmique Numérique Distribuée

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