Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MC: Support Mutexes in DPOR
[simgrid.git] / src / kernel / actor / MutexObserver.cpp
1 /* Copyright (c) 2019-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/kernel/actor/MutexObserver.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "src/kernel/activity/MutexImpl.hpp"
9 #include "src/kernel/actor/ActorImpl.hpp"
10 #include "src/mc/mc_config.hpp"
11
12 #include <sstream>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(obs_mutex, mc_observer, "Logging specific to mutex simcalls observation");
15
16 namespace simgrid {
17 namespace kernel {
18 namespace actor {
19
20 #if 0
21 bool MutexSimcall::depends(SimcallObserver* other)
22 {
23   if (dynamic_cast<RandomSimcall*>(other) != nullptr)
24     return other->depends(this); /* Other is random, that is very permissive. Use that relation instead. */
25
26 #if 0 /* This code is currently broken and shouldn't be used. We must implement asynchronous locks before */
27   MutexSimcall* that = dynamic_cast<MutexSimcall*>(other);
28   if (that == nullptr)
29     return true; // Depends on anything we don't know
30
31   /* Theorem 4.4.7: Any pair of synchronization actions of distinct actors concerning distinct mutexes are independent */
32   if (this->get_issuer() != that->get_issuer() && this->get_mutex() != that->get_mutex())
33     return false;
34
35   /* Theorem 4.4.8 An AsyncMutexLock is independent with a MutexUnlock of another actor */
36   if (((dynamic_cast<MutexLockSimcall*>(this) != nullptr && dynamic_cast<MutexUnlockSimcall*>(that)) ||
37        (dynamic_cast<MutexLockSimcall*>(that) != nullptr && dynamic_cast<MutexUnlockSimcall*>(this))) &&
38       get_issuer() != other->get_issuer())
39     return false;
40 #endif
41   return true; // Depend on things we don't know for sure that they are independent
42 }
43 #endif
44
45 MutexObserver::MutexObserver(ActorImpl* actor, mc::Transition::Type type, activity::MutexImpl* mutex)
46     : SimcallObserver(actor), type_(type), mutex_(mutex)
47 {
48 }
49
50 void MutexObserver::serialize(std::stringstream& stream) const
51 {
52   auto* owner = get_mutex()->get_owner();
53   stream << (short)type_ << ' ' << (uintptr_t)get_mutex() << ' ' << (owner != nullptr ? owner->get_pid() : -1);
54 }
55
56 bool MutexObserver::is_enabled()
57 {
58   // Only wait can be disabled
59   return type_ != mc::Transition::Type::MUTEX_WAIT || mutex_->get_owner() == get_issuer();
60 }
61
62 } // namespace actor
63 } // namespace kernel
64 } // namespace simgrid