Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Polymorphic base class destructor should be either public virtual or protected non...
[simgrid.git] / src / kernel / actor / SimcallObserver.hpp
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 #ifndef SIMGRID_MC_SIMCALL_OBSERVER_HPP
7 #define SIMGRID_MC_SIMCALL_OBSERVER_HPP
8
9 #include "simgrid/forward.h"
10 #include "src/mc/transition/Transition.hpp"
11 #include "xbt/asserts.h"
12
13 #include <string>
14
15 namespace simgrid {
16 namespace kernel {
17 namespace actor {
18
19 class SimcallObserver {
20   ActorImpl* const issuer_;
21
22 protected:
23   ~SimcallObserver() = default;
24
25 public:
26   explicit SimcallObserver(ActorImpl* issuer) : issuer_(issuer) {}
27   ActorImpl* get_issuer() const { return issuer_; }
28   /** Whether this transition can currently be taken without blocking.
29    *
30    * For example, a mutex_lock is not enabled when the mutex is not free.
31    * A comm_receive is not enabled before the corresponding send has been issued.
32    */
33   virtual bool is_enabled() { return true; }
34
35   /** Returns the amount of time that this transition can be used.
36    *
37    * If it's 0, the transition is not enabled.
38    * If it's 1 (as with send/wait), there is no need to fork the state space exploration on this point.
39    * If it's more than one (as with mc_random or waitany), we need to consider this transition several times to start
40    * differing branches
41    */
42   virtual int get_max_consider() { return 1; }
43
44   /** Prepares the simcall to be used.
45    *
46    * For most simcalls, this does nothing. Once enabled, there is nothing to do to prepare a send().
47    *
48    * It is useful only for the simcalls that can be used several times, such as waitany() or random().
49    * For them, prepare() selects the right outcome for the time being considered.
50    *
51    * The first time a simcall is considered, times_considered is 0, not 1.
52    */
53   virtual void prepare(int times_considered)
54   { /* Nothing to do by default */
55   }
56
57   /** Serialize to the given string buffer */
58   virtual void serialize(std::stringstream& stream) const;
59
60   /** Some simcalls may only be observable under some conditions.
61    * Most simcalls are not visible from the MC because they don't have an observer at all. */
62   virtual bool is_visible() const { return true; }
63 };
64
65 template <class T> class ResultingSimcall : public SimcallObserver {
66   T result_;
67
68 protected:
69   ~ResultingSimcall() = default;
70
71 public:
72   ResultingSimcall() = default;
73   ResultingSimcall(ActorImpl* actor, T default_result) : SimcallObserver(actor), result_(default_result) {}
74   void set_result(T res) { result_ = res; }
75   T get_result() const { return result_; }
76 };
77
78 class RandomSimcall final : public SimcallObserver {
79   const int min_;
80   const int max_;
81   int next_value_ = 0;
82
83 public:
84   RandomSimcall(ActorImpl* actor, int min, int max) : SimcallObserver(actor), min_(min), max_(max)
85   {
86     xbt_assert(min < max);
87   }
88   void serialize(std::stringstream& stream) const override;
89   int get_max_consider() override;
90   void prepare(int times_considered) override;
91   int get_value() const { return next_value_; }
92 };
93
94 class ConditionWaitSimcall final : public ResultingSimcall<bool> {
95   activity::ConditionVariableImpl* const cond_;
96   activity::MutexImpl* const mutex_;
97   const double timeout_;
98
99 public:
100   ConditionWaitSimcall(ActorImpl* actor, activity::ConditionVariableImpl* cond, activity::MutexImpl* mutex,
101                        double timeout = -1.0)
102       : ResultingSimcall(actor, false), cond_(cond), mutex_(mutex), timeout_(timeout)
103   {
104   }
105   bool is_enabled() override;
106   bool is_visible() const override { return false; }
107   activity::ConditionVariableImpl* get_cond() const { return cond_; }
108   activity::MutexImpl* get_mutex() const { return mutex_; }
109   double get_timeout() const { return timeout_; }
110 };
111
112 } // namespace actor
113 } // namespace kernel
114 } // namespace simgrid
115
116 #endif