Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Minor sonar smells, and other cosmetics.
[simgrid.git] / src / mc / transition / TransitionSynchro.cpp
1 /* Copyright (c) 2015-2022. 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/string.hpp"
9
10 #include <inttypes.h>
11 #include <sstream>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_trans_synchro, mc_transition, "Logging specific to MC synchronization transitions");
14
15 namespace simgrid {
16 namespace mc {
17 std::string MutexTransition::to_string(bool verbose) const
18 {
19   return xbt::string_printf("%s(mutex: %" PRIxPTR ", owner:%ld)", Transition::to_c_str(type_), mutex_, owner_);
20 }
21
22 MutexTransition::MutexTransition(aid_t issuer, int times_considered, Type type, std::stringstream& stream)
23     : Transition(type, issuer, times_considered)
24 {
25   xbt_assert(stream >> mutex_ >> owner_);
26 }
27
28 bool MutexTransition::depends(const Transition* o) const
29 {
30   if (o->type_ < type_)
31     return o->depends(this);
32
33   // type_ <= other->type_ in  MUTEX_LOCK, MUTEX_TEST, MUTEX_TRYLOCK, MUTEX_UNLOCK, MUTEX_WAIT,
34
35   if (auto* other = dynamic_cast<const MutexTransition*>(o)) {
36     // Theorem 4.4.7: Any pair of synchronization actions of distinct actors concerning distinct mutexes are independent
37     if (mutex_ != other->mutex_)
38       return false;
39
40     // Theorem 4.4.11: LOCK indep TEST/WAIT.
41     //  If both enabled, the result does not depend on their order. If WAIT is not enabled, LOCK won't enable it.
42     if (type_ == Type::MUTEX_LOCK && (other->type_ == Type::MUTEX_TEST || other->type_ == Type::MUTEX_WAIT))
43       return false;
44
45     // Theorem 4.4.8: LOCK indep UNLOCK.
46     //  pop_front and push_back are independent.
47     if (type_ == Type::MUTEX_LOCK && other->type_ == Type::MUTEX_UNLOCK)
48       return false;
49
50     // TEST is a pure function; TEST/WAIT won't change the owner; TRYLOCK will always fail if TEST is enabled (because a
51     // request is queued)
52     if (type_ == Type::MUTEX_TEST &&
53         (other->type_ == Type::MUTEX_TEST || other->type_ == Type::MUTEX_TRYLOCK || other->type_ == Type::MUTEX_WAIT))
54       return false;
55
56     // TRYLOCK will always fail if TEST is enabled (because a request is queued), and may not overpass the WAITed
57     // request in the queue
58     if (type_ == Type::MUTEX_TRYLOCK && other->type_ == Type::MUTEX_WAIT)
59       return false;
60
61     // FIXME: UNLOCK indep WAIT/TEST iff wait/test are not first in the waiting queue
62   }
63
64   return true; // FIXME: TODO
65 }
66
67 } // namespace mc
68 } // namespace simgrid