Logo AND Algorithmique Numérique Distribuée

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