Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b7071612de63ee28094ee64d34bb274ee1cca24d
[simgrid.git] / src / kernel / actor / SynchroObserver.hpp
1 /* Copyright (c) 2019-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 #ifndef SIMGRID_MC_MUTEX_OBSERVER_HPP
7 #define SIMGRID_MC_MUTEX_OBSERVER_HPP
8
9 #include "simgrid/forward.h"
10 #include "src/kernel/activity/MutexImpl.hpp"
11 #include "src/kernel/actor/ActorImpl.hpp"
12 #include "src/kernel/actor/SimcallObserver.hpp"
13
14 #include <string>
15
16 namespace simgrid::kernel::actor {
17
18 /* All the observers of Mutex transitions are very similar, so implement them all together in this class */
19 class MutexObserver final : public SimcallObserver {
20   mc::Transition::Type type_;
21   activity::MutexImpl* const mutex_;
22
23 public:
24   MutexObserver(ActorImpl* actor, mc::Transition::Type type, activity::MutexImpl* mutex);
25
26   void serialize(std::stringstream& stream) const override;
27   std::string to_string() const override;
28   bool is_enabled() override;
29
30   activity::MutexImpl* get_mutex() const { return mutex_; }
31 };
32
33 /* This observer is used for SEM_LOCK and SEM_UNLOCK (only) */
34 class SemaphoreObserver final : public SimcallObserver {
35   mc::Transition::Type type_;
36   activity::SemaphoreImpl* const sem_;
37
38 public:
39   SemaphoreObserver(ActorImpl* actor, mc::Transition::Type type, activity::SemaphoreImpl* sem);
40
41   void serialize(std::stringstream& stream) const override;
42   std::string to_string() const override;
43
44   activity::SemaphoreImpl* get_sem() const { return sem_; }
45 };
46
47 /* This observer is ued for SEM_WAIT, that is returning and needs the acquisition (in MC mode) */
48 class SemaphoreAcquisitionObserver final : public ResultingSimcall<bool> {
49   mc::Transition::Type type_;
50   activity::SemAcquisitionImpl* const acquisition_;
51   const double timeout_;
52
53 public:
54   SemaphoreAcquisitionObserver(ActorImpl* actor, mc::Transition::Type type, activity::SemAcquisitionImpl* acqui,
55                                double timeout = -1.0);
56
57   void serialize(std::stringstream& stream) const override;
58   std::string to_string() const override;
59   bool is_enabled() override;
60
61   double get_timeout() const { return timeout_; }
62 };
63
64 /* This observer is used for BARRIER_LOCK and BARRIER_WAIT. WAIT is returning and needs the acquisition */
65 class BarrierObserver final : public ResultingSimcall<bool> {
66   mc::Transition::Type type_;
67   activity::BarrierImpl* const barrier_                = nullptr;
68   activity::BarrierAcquisitionImpl* const acquisition_ = nullptr;
69   const double timeout_;
70
71 public:
72   BarrierObserver(ActorImpl* actor, mc::Transition::Type type, activity::BarrierImpl* bar);
73   BarrierObserver(ActorImpl* actor, mc::Transition::Type type, activity::BarrierAcquisitionImpl* acqui,
74                   double timeout = -1.0);
75
76   void serialize(std::stringstream& stream) const override;
77   std::string to_string() const override;
78   bool is_enabled() override;
79
80   double get_timeout() const { return timeout_; }
81 };
82
83 } // namespace simgrid::kernel::actor
84
85 #endif