Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dbd39a5de76f6b0280b3a2ce638af490079d0354
[simgrid.git] / src / mc / transition / TransitionSynchro.cpp
1 /* Copyright (c) 2015-2023. 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/mc/transition/TransitionSynchro.hpp"
7 #include "xbt/asserts.h"
8 #include "xbt/ex.h"
9 #include "xbt/string.hpp"
10
11 #include <inttypes.h>
12 #include <sstream>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_trans_synchro, mc_transition, "Logging specific to MC synchronization transitions");
15
16 namespace simgrid::mc {
17
18 std::string BarrierTransition::to_string(bool verbose) const
19 {
20   return xbt::string_printf("%s(barrier: %u)", Transition::to_c_str(type_), bar_);
21 }
22 BarrierTransition::BarrierTransition(aid_t issuer, int times_considered, Type type, std::stringstream& stream)
23     : Transition(type, issuer, times_considered)
24 {
25   xbt_assert(stream >> bar_);
26 }
27 bool BarrierTransition::depends(const Transition* o) const
28 {
29   if (o->type_ < type_)
30     return o->depends(this);
31
32   if (auto* other = dynamic_cast<const BarrierTransition*>(o)) {
33     if (bar_ != other->bar_)
34       return false;
35
36     // LOCK indep LOCK: requests are not ordered in a barrier
37     if (type_ == Type::BARRIER_ASYNC_LOCK && other->type_ == Type::BARRIER_ASYNC_LOCK)
38       return false;
39
40     // WAIT indep WAIT: requests are not ordered
41     if (type_ == Type::BARRIER_WAIT && other->type_ == Type::BARRIER_WAIT)
42       return false;
43
44     return true; // LOCK/WAIT is dependent because lock may enable wait
45   }
46
47   return false; // barriers are INDEP with non-barrier transitions
48 }
49
50 std::string MutexTransition::to_string(bool verbose) const
51 {
52   return xbt::string_printf("%s(mutex: %" PRIxPTR ", owner: %ld)", Transition::to_c_str(type_), mutex_, owner_);
53 }
54
55 MutexTransition::MutexTransition(aid_t issuer, int times_considered, Type type, std::stringstream& stream)
56     : Transition(type, issuer, times_considered)
57 {
58   xbt_assert(stream >> mutex_ >> owner_);
59 }
60
61 bool MutexTransition::depends(const Transition* o) const
62 {
63   if (o->type_ < type_)
64     return o->depends(this);
65
66   // Actions executed by the same actor are always dependent
67   if (o->aid_ == aid_)
68     return true;
69
70   // type_ <= other->type_ in  MUTEX_LOCK, MUTEX_TEST, MUTEX_TRYLOCK, MUTEX_UNLOCK, MUTEX_WAIT,
71
72   if (auto* other = dynamic_cast<const MutexTransition*>(o)) {
73     // Theorem 4.4.7: Any pair of synchronization actions of distinct actors concerning distinct mutexes are independent
74     if (mutex_ != other->mutex_)
75       return false;
76
77     // Theorem 4.4.11: LOCK indep TEST/WAIT.
78     //  If both enabled, the result does not depend on their order. If WAIT is not enabled, LOCK won't enable it.
79     if (type_ == Type::MUTEX_ASYNC_LOCK && (other->type_ == Type::MUTEX_TEST || other->type_ == Type::MUTEX_WAIT))
80       return false;
81
82     // Theorem 4.4.8: LOCK indep UNLOCK.
83     //  pop_front and push_back are independent.
84     if (type_ == Type::MUTEX_ASYNC_LOCK && other->type_ == Type::MUTEX_UNLOCK)
85       return false;
86
87     // TEST is a pure function; TEST/WAIT won't change the owner; TRYLOCK will always fail if TEST is enabled (because a
88     // request is queued)
89     if (type_ == Type::MUTEX_TEST &&
90         (other->type_ == Type::MUTEX_TEST || other->type_ == Type::MUTEX_TRYLOCK || other->type_ == Type::MUTEX_WAIT))
91       return false;
92
93     // TRYLOCK will always fail if TEST is enabled (because a request is queued), and may not overpass the WAITed
94     // request in the queue
95     if (type_ == Type::MUTEX_TRYLOCK && other->type_ == Type::MUTEX_WAIT)
96       return false;
97
98     // FIXME: UNLOCK indep WAIT/TEST iff wait/test are not first in the waiting queue
99     return true;
100   }
101
102   return false; // mutexes are INDEP with non-mutex transitions
103 }
104
105 std::string SemaphoreTransition::to_string(bool verbose) const
106 {
107   if (type_ == Type::SEM_ASYNC_LOCK || type_ == Type::SEM_UNLOCK)
108     return xbt::string_printf("%s(semaphore: %" PRIxPTR ")", Transition::to_c_str(type_), sem_);
109   if (type_ == Type::SEM_WAIT)
110     return xbt::string_printf("%s(semaphore: %" PRIxPTR ", granted: %s)", Transition::to_c_str(type_), sem_,
111                               granted_ ? "yes" : "no");
112   THROW_IMPOSSIBLE;
113 }
114 SemaphoreTransition::SemaphoreTransition(aid_t issuer, int times_considered, Type type, std::stringstream& stream)
115     : Transition(type, issuer, times_considered)
116 {
117   xbt_assert(stream >> sem_ >> granted_);
118 }
119 bool SemaphoreTransition::depends(const Transition* o) const
120 {
121   if (o->type_ < type_)
122     return o->depends(this);
123
124   if (auto* other = dynamic_cast<const SemaphoreTransition*>(o)) {
125     if (sem_ != other->sem_)
126       return false;
127
128     // LOCK indep UNLOCK: pop_front and push_back are independent.
129     if (type_ == Type::SEM_ASYNC_LOCK && other->type_ == Type::SEM_UNLOCK)
130       return false;
131
132     // LOCK indep WAIT: If both enabled, ordering has no impact on the result. If WAIT is not enabled, LOCK won't enable
133     // it.
134     if (type_ == Type::SEM_ASYNC_LOCK && other->type_ == Type::SEM_WAIT)
135       return false;
136
137     // UNLOCK indep UNLOCK: ordering of two pop_front has no impact
138     if (type_ == Type::SEM_UNLOCK && other->type_ == Type::SEM_UNLOCK)
139       return false;
140
141     // WAIT indep WAIT:
142     // if both enabled (may happen in the initial value is sufficient), the ordering has no impact on the result.
143     // If only one enabled, the other won't be enabled by the first one.
144     // If none enabled, well, nothing will change.
145     if (type_ == Type::SEM_WAIT && other->type_ == Type::SEM_WAIT)
146       return false;
147
148     return true; // Other semaphore cases are dependent
149   }
150
151   return false; // semaphores are INDEP with non-semaphore transitions
152 }
153
154 } // namespace simgrid::mc