Logo AND Algorithmique Numérique Distribuée

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