Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modernize simcall process_cleanup.
[simgrid.git] / src / kernel / activity / ConditionVariableImpl.cpp
1 /* Copyright (c) 2007-2018. 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 "src/kernel/activity/MutexImpl.hpp"
8 #include "src/kernel/activity/SynchroRaw.hpp"
9 #include "src/simix/smx_synchro_private.hpp"
10 #include "xbt/ex.hpp"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ConditionVariable, simix_synchro, "Condition variables");
13
14 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout, smx_actor_t issuer,
15                              smx_simcall_t simcall);
16
17 /********************************* Condition **********************************/
18
19 /**
20  * \brief Initialize a condition.
21  *
22  * Allocates and creates the data for the condition.
23  * It have to be called before the use of the condition.
24  * \return A condition
25  */
26 smx_cond_t SIMIX_cond_init()
27 {
28   XBT_IN("()");
29   smx_cond_t cond = new simgrid::kernel::activity::ConditionVariableImpl();
30   XBT_OUT();
31   return cond;
32 }
33
34 /**
35  * \brief Handle a condition waiting simcall without timeouts
36  */
37 void simcall_HANDLER_cond_wait(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex)
38 {
39   XBT_IN("(%p)", simcall);
40   smx_actor_t issuer = simcall->issuer;
41
42   _SIMIX_cond_wait(cond, mutex, -1, issuer, simcall);
43   XBT_OUT();
44 }
45
46 /**
47  * \brief Handle a condition waiting simcall with timeouts
48  */
49 void simcall_HANDLER_cond_wait_timeout(smx_simcall_t simcall, smx_cond_t cond, smx_mutex_t mutex, double timeout)
50 {
51   XBT_IN("(%p)", simcall);
52   smx_actor_t issuer = simcall->issuer;
53
54   _SIMIX_cond_wait(cond, mutex, timeout, issuer, simcall);
55   XBT_OUT();
56 }
57
58 static void _SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex, double timeout, smx_actor_t issuer,
59                              smx_simcall_t simcall)
60 {
61   XBT_IN("(%p, %p, %f, %p,%p)", cond, mutex, timeout, issuer, simcall);
62   smx_activity_t synchro = nullptr;
63
64   XBT_DEBUG("Wait condition %p", cond);
65
66   /* If there is a mutex unlock it */
67   /* FIXME: what happens if the issuer is not the owner of the mutex? */
68   if (mutex != nullptr) {
69     cond->mutex = mutex;
70     mutex->unlock(issuer);
71   }
72
73   synchro = SIMIX_synchro_wait(issuer->host, timeout);
74   synchro->simcalls.push_front(simcall);
75   issuer->waiting_synchro = synchro;
76   cond->sleeping.push_back(*simcall->issuer);
77   XBT_OUT();
78 }
79
80 namespace simgrid {
81 namespace kernel {
82 namespace activity {
83
84 ConditionVariableImpl::ConditionVariableImpl() : cond_(this) {}
85 ConditionVariableImpl::~ConditionVariableImpl() = default;
86
87 /**
88  * \brief Signalizes a condition.
89  *
90  * Signalizes a condition and wakes up a sleeping process.
91  * If there are no process sleeping, no action is done.
92  */
93 void ConditionVariableImpl::signal()
94 {
95   XBT_DEBUG("Signal condition %p", this);
96
97   /* If there are processes waiting for the condition choose one and try
98      to make it acquire the mutex */
99   if (not sleeping.empty()) {
100     auto& proc = sleeping.front();
101     sleeping.pop_front();
102
103     /* Destroy waiter's synchronization */
104     proc.waiting_synchro = nullptr;
105
106     /* Now transform the cond wait simcall into a mutex lock one */
107     smx_simcall_t simcall = &proc.simcall;
108     smx_mutex_t simcall_mutex;
109     if (simcall->call == SIMCALL_COND_WAIT)
110       simcall_mutex = simcall_cond_wait__get__mutex(simcall);
111     else
112       simcall_mutex = simcall_cond_wait_timeout__get__mutex(simcall);
113     simcall->call = SIMCALL_MUTEX_LOCK;
114
115     simcall_HANDLER_mutex_lock(simcall, simcall_mutex);
116   }
117   XBT_OUT();
118 }
119
120 /**
121  * \brief Broadcasts a condition.
122  *
123  * Signal ALL processes waiting on a condition.
124  * If there are no process waiting, no action is done.
125  */
126 void ConditionVariableImpl::broadcast()
127 {
128   XBT_DEBUG("Broadcast condition %p", this);
129
130   /* Signal the condition until nobody is waiting on it */
131   while (not sleeping.empty())
132     signal();
133 }
134
135 // boost::intrusive_ptr<ConditionVariableImpl> support:
136 void intrusive_ptr_add_ref(simgrid::kernel::activity::ConditionVariableImpl* cond)
137 {
138   cond->refcount_.fetch_add(1, std::memory_order_relaxed);
139 }
140
141 void intrusive_ptr_release(simgrid::kernel::activity::ConditionVariableImpl* cond)
142 {
143   if (cond->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
144     std::atomic_thread_fence(std::memory_order_acquire);
145     xbt_assert(cond->sleeping.empty(), "Cannot destroy conditional since someone is still using it");
146     delete cond;
147   }
148 }
149 } // namespace activity
150 } // namespace kernel
151 }