Logo AND Algorithmique Numérique Distribuée

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