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 "xbt/asserts.h"
9 #include "xbt/string.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_trans_synchro, mc_transition, "Logging specific to MC synchronization transitions");
16 namespace simgrid::mc {
18 std::string BarrierTransition::to_string(bool verbose) const
20 return xbt::string_printf("%s(barrier: %u)", Transition::to_c_str(type_), bar_);
22 BarrierTransition::BarrierTransition(aid_t issuer, int times_considered, Type type, std::stringstream& stream)
23 : Transition(type, issuer, times_considered)
25 xbt_assert(stream >> bar_);
27 bool BarrierTransition::depends(const Transition* o) const
30 return o->depends(this);
32 // Actions executed by the same actor are always dependent
36 if (const auto* other = dynamic_cast<const BarrierTransition*>(o)) {
37 if (bar_ != other->bar_)
40 // LOCK indep LOCK: requests are not ordered in a barrier
41 if (type_ == Type::BARRIER_ASYNC_LOCK && other->type_ == Type::BARRIER_ASYNC_LOCK)
44 // WAIT indep WAIT: requests are not ordered
45 if (type_ == Type::BARRIER_WAIT && other->type_ == Type::BARRIER_WAIT)
48 return true; // LOCK/WAIT is dependent because lock may enable wait
51 return false; // barriers are INDEP with non-barrier transitions
54 std::string MutexTransition::to_string(bool verbose) const
56 return xbt::string_printf("%s(mutex: %" PRIxPTR ", owner: %ld)", Transition::to_c_str(type_), mutex_, owner_);
59 MutexTransition::MutexTransition(aid_t issuer, int times_considered, Type type, std::stringstream& stream)
60 : Transition(type, issuer, times_considered)
62 xbt_assert(stream >> mutex_ >> owner_);
65 bool MutexTransition::depends(const Transition* o) const
68 return o->depends(this);
70 // Actions executed by the same actor are always dependent
74 // type_ <= other->type_ in MUTEX_LOCK, MUTEX_TEST, MUTEX_TRYLOCK, MUTEX_UNLOCK, MUTEX_WAIT,
76 if (const auto* other = dynamic_cast<const MutexTransition*>(o)) {
77 // Theorem 4.4.7: Any pair of synchronization actions of distinct actors concerning distinct mutexes are independent
78 if (mutex_ != other->mutex_)
81 // Theorem 4.4.11: LOCK indep TEST/WAIT.
82 // If both enabled, the result does not depend on their order. If WAIT is not enabled, LOCK won't enable it.
83 if (type_ == Type::MUTEX_ASYNC_LOCK && (other->type_ == Type::MUTEX_TEST || other->type_ == Type::MUTEX_WAIT))
86 // Theorem 4.4.8: LOCK indep UNLOCK.
87 // pop_front and push_back are independent.
88 if (type_ == Type::MUTEX_ASYNC_LOCK && other->type_ == Type::MUTEX_UNLOCK)
91 // TEST is a pure function; TEST/WAIT won't change the owner; TRYLOCK will always fail if TEST is enabled (because a
93 if (type_ == Type::MUTEX_TEST &&
94 (other->type_ == Type::MUTEX_TEST || other->type_ == Type::MUTEX_TRYLOCK || other->type_ == Type::MUTEX_WAIT))
97 // TRYLOCK will always fail if TEST is enabled (because a request is queued), and may not overpass the WAITed
98 // request in the queue
99 if (type_ == Type::MUTEX_TRYLOCK && other->type_ == Type::MUTEX_WAIT)
102 // FIXME: UNLOCK indep WAIT/TEST iff wait/test are not first in the waiting queue
106 return false; // mutexes are INDEP with non-mutex transitions
109 std::string SemaphoreTransition::to_string(bool verbose) const
111 if (type_ == Type::SEM_ASYNC_LOCK || type_ == Type::SEM_UNLOCK)
112 return xbt::string_printf("%s(semaphore: %u)", Transition::to_c_str(type_), sem_);
113 if (type_ == Type::SEM_WAIT)
114 return xbt::string_printf("%s(semaphore: %u, granted: %s)", Transition::to_c_str(type_), sem_,
115 granted_ ? "yes" : "no");
118 SemaphoreTransition::SemaphoreTransition(aid_t issuer, int times_considered, Type type, std::stringstream& stream)
119 : Transition(type, issuer, times_considered)
121 xbt_assert(stream >> sem_ >> granted_);
123 bool SemaphoreTransition::depends(const Transition* o) const
125 if (o->type_ < type_)
126 return o->depends(this);
128 // Actions executed by the same actor are always dependent
132 if (const auto* other = dynamic_cast<const SemaphoreTransition*>(o)) {
133 if (sem_ != other->sem_)
136 // LOCK indep UNLOCK: pop_front and push_back are independent.
137 if (type_ == Type::SEM_ASYNC_LOCK && other->type_ == Type::SEM_UNLOCK)
140 // LOCK indep WAIT: If both enabled, ordering has no impact on the result. If WAIT is not enabled, LOCK won't enable
142 if (type_ == Type::SEM_ASYNC_LOCK && other->type_ == Type::SEM_WAIT)
145 // UNLOCK indep UNLOCK: ordering of two pop_front has no impact
146 if (type_ == Type::SEM_UNLOCK && other->type_ == Type::SEM_UNLOCK)
150 // if both enabled (may happen in the initial value is sufficient), the ordering has no impact on the result.
151 // If only one enabled, the other won't be enabled by the first one.
152 // If none enabled, well, nothing will change.
153 if (type_ == Type::SEM_WAIT && other->type_ == Type::SEM_WAIT)
156 return true; // Other semaphore cases are dependent
159 return false; // semaphores are INDEP with non-semaphore transitions
162 } // namespace simgrid::mc