Logo AND Algorithmique Numérique Distribuée

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