Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'add_wait_for_to_py_comm_binding' into 'master'
[simgrid.git] / src / kernel / actor / SynchroObserver.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/SynchroObserver.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "src/kernel/activity/BarrierImpl.hpp"
9 #include "src/kernel/activity/MutexImpl.hpp"
10 #include "src/kernel/activity/SemaphoreImpl.hpp"
11 #include "src/kernel/actor/ActorImpl.hpp"
12 #include "src/mc/mc_config.hpp"
13
14 #include <sstream>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(obs_mutex, mc_observer, "Logging specific to mutex simcalls observation");
17
18 namespace simgrid {
19 namespace kernel {
20 namespace actor {
21
22 MutexObserver::MutexObserver(ActorImpl* actor, mc::Transition::Type type, activity::MutexImpl* mutex)
23     : SimcallObserver(actor), type_(type), mutex_(mutex)
24 {
25   xbt_assert(mutex_);
26 }
27
28 void MutexObserver::serialize(std::stringstream& stream) const
29 {
30   const auto* owner = get_mutex()->get_owner();
31   stream << (short)type_ << ' ' << get_mutex()->get_id() << ' ' << (owner != nullptr ? owner->get_pid() : -1);
32 }
33
34 bool MutexObserver::is_enabled()
35 {
36   // Only wait can be disabled
37   return type_ != mc::Transition::Type::MUTEX_WAIT || mutex_->get_owner() == get_issuer();
38 }
39
40 SemaphoreObserver::SemaphoreObserver(ActorImpl* actor, mc::Transition::Type type, activity::SemaphoreImpl* sem)
41     : SimcallObserver(actor), type_(type), sem_(sem)
42 {
43   xbt_assert(sem_);
44 }
45
46 void SemaphoreObserver::serialize(std::stringstream& stream) const
47 {
48   stream << (short)type_ << ' ' << get_sem()->get_id() << ' ' << false /* Granted is ignored for LOCK/UNLOCK */;
49 }
50
51 SemaphoreAcquisitionObserver::SemaphoreAcquisitionObserver(ActorImpl* actor, mc::Transition::Type type,
52                                                            activity::SemAcquisitionImpl* acqui, double timeout)
53     : ResultingSimcall(actor, false), type_(type), acquisition_(acqui), timeout_(timeout)
54 {
55 }
56 bool SemaphoreAcquisitionObserver::is_enabled()
57 {
58   return acquisition_->granted_;
59 }
60 void SemaphoreAcquisitionObserver::serialize(std::stringstream& stream) const
61 {
62   stream << (short)type_ << ' ' << acquisition_->semaphore_->get_id() << ' ' << acquisition_->granted_;
63 }
64
65 BarrierObserver::BarrierObserver(ActorImpl* actor, mc::Transition::Type type, activity::BarrierImpl* bar)
66     : ResultingSimcall(actor, false), type_(type), barrier_(bar), timeout_(-1)
67 {
68   xbt_assert(type_ == mc::Transition::Type::BARRIER_LOCK);
69 }
70 BarrierObserver::BarrierObserver(ActorImpl* actor, mc::Transition::Type type, activity::BarrierAcquisitionImpl* acqui,
71                                  double timeout)
72     : ResultingSimcall(actor, false), type_(type), acquisition_(acqui), timeout_(timeout)
73 {
74   xbt_assert(type_ == mc::Transition::Type::BARRIER_WAIT);
75 }
76 void BarrierObserver::serialize(std::stringstream& stream) const
77 {
78   xbt_assert(barrier_ != nullptr || (acquisition_ != nullptr && acquisition_->barrier_ != nullptr));
79   stream << (short)type_ << ' ' << (barrier_ != nullptr ? barrier_->get_id() : acquisition_->barrier_->get_id());
80 }
81 bool BarrierObserver::is_enabled()
82 {
83   return type_ == mc::Transition::Type::BARRIER_LOCK ||
84          (type_ == mc::Transition::Type::BARRIER_WAIT && acquisition_ != nullptr && acquisition_->granted_);
85 }
86
87 } // namespace actor
88 } // namespace kernel
89 } // namespace simgrid