1 /* Copyright (c) 2015-2023. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/mc/transition/TransitionSynchro.hpp"
7 #include "src/mc/mc_forward.hpp"
8 #include "src/mc/transition/TransitionObjectAccess.hpp"
9 #include "xbt/asserts.h"
11 #include "xbt/string.hpp"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_trans_synchro, mc_transition, "Logging specific to MC synchronization transitions");
18 namespace simgrid::mc {
20 std::string BarrierTransition::to_string(bool verbose) const
22 return xbt::string_printf("%s(barrier: %u)", Transition::to_c_str(type_), bar_);
24 BarrierTransition::BarrierTransition(aid_t issuer, int times_considered, Type type, std::stringstream& stream)
25 : Transition(type, issuer, times_considered)
27 xbt_assert(stream >> bar_);
29 bool BarrierTransition::depends(const Transition* o) const
32 return o->depends(this);
34 // Actions executed by the same actor are always dependent
38 if (const auto* other = dynamic_cast<const BarrierTransition*>(o)) {
39 if (bar_ != other->bar_)
42 // LOCK indep LOCK: requests are not ordered in a barrier
43 if (type_ == Type::BARRIER_ASYNC_LOCK && other->type_ == Type::BARRIER_ASYNC_LOCK)
46 // WAIT indep WAIT: requests are not ordered
47 if (type_ == Type::BARRIER_WAIT && other->type_ == Type::BARRIER_WAIT)
50 return true; // LOCK/WAIT is dependent because lock may enable wait
53 return false; // barriers are INDEP with non-barrier transitions
55 bool BarrierTransition::reversible_race(const Transition* other) const
58 case Type::BARRIER_ASYNC_LOCK:
59 return true; // BarrierAsyncLock is always enabled
60 case Type::BARRIER_WAIT:
61 // If the other event is a barrier lock event, then we are not reversible;
62 // otherwise we are reversible.
63 return other->type_ != Transition::Type::BARRIER_ASYNC_LOCK;
65 xbt_die("Unexpected transition type %s", to_c_str(type_));
69 std::string MutexTransition::to_string(bool verbose) const
71 return xbt::string_printf("%s(mutex: %" PRIxPTR ", owner: %ld)", Transition::to_c_str(type_), mutex_, owner_);
74 MutexTransition::MutexTransition(aid_t issuer, int times_considered, Type type, std::stringstream& stream)
75 : Transition(type, issuer, times_considered)
77 xbt_assert(stream >> mutex_ >> owner_);
80 bool MutexTransition::depends(const Transition* o) const
83 return o->depends(this);
85 // Actions executed by the same actor are always dependent
89 // type_ <= other->type_ in MUTEX_LOCK, MUTEX_TEST, MUTEX_TRYLOCK, MUTEX_UNLOCK, MUTEX_WAIT,
91 if (const auto* other = dynamic_cast<const MutexTransition*>(o)) {
92 // Theorem 4.4.7: Any pair of synchronization actions of distinct actors concerning distinct mutexes are independent
93 if (mutex_ != other->mutex_)
96 // Theorem 4.4.11: LOCK indep TEST/WAIT.
97 // If both enabled, the result does not depend on their order. If WAIT is not enabled, LOCK won't enable it.
98 if (type_ == Type::MUTEX_ASYNC_LOCK && (other->type_ == Type::MUTEX_TEST || other->type_ == Type::MUTEX_WAIT))
101 // Theorem 4.4.8: LOCK indep UNLOCK.
102 // pop_front and push_back are independent.
103 if (type_ == Type::MUTEX_ASYNC_LOCK && other->type_ == Type::MUTEX_UNLOCK)
106 // Theorem 4.4.9: LOCK indep UNLOCK.
107 // any combination of wait and test is indenpendent.
108 if ((type_ == Type::MUTEX_WAIT || type_ == Type::MUTEX_TEST) &&
109 (other->type_ == Type::MUTEX_WAIT || other->type_ == Type::MUTEX_TEST))
112 // TEST is a pure function; TEST/WAIT won't change the owner; TRYLOCK will always fail if TEST is enabled (because a
113 // request is queued)
114 if (type_ == Type::MUTEX_TEST &&
115 (other->type_ == Type::MUTEX_TEST || other->type_ == Type::MUTEX_TRYLOCK || other->type_ == Type::MUTEX_WAIT))
118 // TRYLOCK will always fail if TEST is enabled (because a request is queued), and may not overpass the WAITed
119 // request in the queue
120 if (type_ == Type::MUTEX_TRYLOCK && other->type_ == Type::MUTEX_WAIT)
123 // FIXME: UNLOCK indep WAIT/TEST iff wait/test are not first in the waiting queue
127 return false; // mutexes are INDEP with non-mutex transitions
130 bool SemaphoreTransition::reversible_race(const Transition* other) const
133 case Type::SEM_ASYNC_LOCK:
134 return true; // SemAsyncLock is always enabled
135 case Type::SEM_UNLOCK:
136 return true; // SemUnlock is always enabled
138 if (other->type_ == Transition::Type::SEM_UNLOCK &&
139 static_cast<const SemaphoreTransition*>(other)->get_capacity() <= 1) {
142 xbt_die("SEM_WAIT that is dependent with a SEM_UNLOCK should not be reversible. FixMe");
145 xbt_die("Unexpected transition type %s", to_c_str(type_));
149 std::string SemaphoreTransition::to_string(bool verbose) const
151 if (type_ == Type::SEM_ASYNC_LOCK || type_ == Type::SEM_UNLOCK)
152 return xbt::string_printf("%s(semaphore: %u, capacity: %u)", Transition::to_c_str(type_), sem_, capacity_);
153 if (type_ == Type::SEM_WAIT)
154 return xbt::string_printf("%s(semaphore: %u, capacity: %u, granted: %s)", Transition::to_c_str(type_), sem_,
155 capacity_, granted_ ? "yes" : "no");
158 SemaphoreTransition::SemaphoreTransition(aid_t issuer, int times_considered, Type type, std::stringstream& stream)
159 : Transition(type, issuer, times_considered)
161 xbt_assert(stream >> sem_ >> granted_ >> capacity_);
163 bool SemaphoreTransition::depends(const Transition* o) const
165 if (o->type_ < type_)
166 return o->depends(this);
168 // Actions executed by the same actor are always dependent
172 if (const auto* other = dynamic_cast<const SemaphoreTransition*>(o)) {
173 if (sem_ != other->sem_)
176 // LOCK indep UNLOCK: pop_front and push_back are independent.
177 if (type_ == Type::SEM_ASYNC_LOCK && other->type_ == Type::SEM_UNLOCK)
180 // LOCK indep WAIT: If both enabled, ordering has no impact on the result. If WAIT is not enabled, LOCK won't enable
182 if (type_ == Type::SEM_ASYNC_LOCK && other->type_ == Type::SEM_WAIT)
185 // UNLOCK indep UNLOCK: ordering of two pop_front has no impact
186 if (type_ == Type::SEM_UNLOCK && other->type_ == Type::SEM_UNLOCK)
189 // UNLOCK indep with a WAIT if the semaphore had enought capacity anyway
190 if (type_ == Type::SEM_UNLOCK && capacity_ > 1 && other->type_ == Type::SEM_WAIT)
194 // if both enabled (may happen in the initial value is sufficient), the ordering has no impact on the result.
195 // If only one enabled, the other won't be enabled by the first one.
196 // If none enabled, well, nothing will change.
197 if (type_ == Type::SEM_WAIT && other->type_ == Type::SEM_WAIT)
200 return true; // Other semaphore cases are dependent
203 return false; // semaphores are INDEP with non-semaphore transitions
206 bool MutexTransition::reversible_race(const Transition* other) const
209 case Type::MUTEX_ASYNC_LOCK:
210 return true; // MutexAsyncLock is always enabled
211 case Type::MUTEX_TEST:
212 return true; // MutexTest is always enabled
213 case Type::MUTEX_TRYLOCK:
214 return true; // MutexTrylock is always enabled
215 case Type::MUTEX_UNLOCK:
216 return true; // MutexUnlock is always enabled
218 case Type::MUTEX_WAIT:
219 // Only an Unlock can be dependent with a Wait
220 // and in this case, that Unlock enabled the wait
224 xbt_die("Unexpected transition type %s", to_c_str(type_));
228 } // namespace simgrid::mc