1 /* Copyright (c) 2019-2022. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_MC_SIMCALL_OBSERVER_HPP
7 #define SIMGRID_MC_SIMCALL_OBSERVER_HPP
9 #include "simgrid/forward.h"
10 #include "src/mc/transition/Transition.hpp"
11 #include "xbt/asserts.h"
15 namespace simgrid::kernel::actor {
17 class SimcallObserver {
18 ActorImpl* const issuer_;
21 ~SimcallObserver() = default;
24 explicit SimcallObserver(ActorImpl* issuer) : issuer_(issuer) {}
25 ActorImpl* get_issuer() const { return issuer_; }
26 /** Whether this transition can currently be taken without blocking.
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.
31 virtual bool is_enabled() { return true; }
33 /** Returns the amount of time that this transition can be used.
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
40 virtual int get_max_consider() const { return 1; }
42 /** Prepares the simcall to be used.
44 * For most simcalls, this does nothing. Once enabled, there is nothing to do to prepare a send().
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.
49 * The first time a simcall is considered, times_considered is 0, not 1.
51 virtual void prepare(int times_considered)
52 { /* Nothing to do by default */
55 /** Serialize to the given string buffer, to send over the network */
56 virtual void serialize(std::stringstream& stream) const = 0;
58 /** Used to debug (to display the simcall on which each actor is blocked when displaying it */
59 virtual std::string to_string() const = 0;
61 /** Whether the MC should see this simcall.
62 * Simcall that don't have an observer (ie, most of them) are not visible from the MC, but if there is an observer,
63 * they are observable by default. */
64 virtual bool is_visible() const { return true; }
67 template <class T> class ResultingSimcall : public SimcallObserver {
71 ~ResultingSimcall() = default;
74 ResultingSimcall(ActorImpl* actor, T default_result) : SimcallObserver(actor), result_(default_result) {}
75 void set_result(T res) { result_ = res; }
76 T get_result() const { return result_; }
79 class RandomSimcall final : public SimcallObserver {
85 RandomSimcall(ActorImpl* actor, int min, int max) : SimcallObserver(actor), min_(min), max_(max)
87 xbt_assert(min < max);
89 void serialize(std::stringstream& stream) const override;
90 std::string to_string() const override;
91 int get_max_consider() const override;
92 void prepare(int times_considered) override;
93 int get_value() const { return next_value_; }
96 class ConditionWaitSimcall final : public ResultingSimcall<bool> {
97 activity::ConditionVariableImpl* const cond_;
98 activity::MutexImpl* const mutex_;
99 const double timeout_;
102 ConditionWaitSimcall(ActorImpl* actor, activity::ConditionVariableImpl* cond, activity::MutexImpl* mutex,
103 double timeout = -1.0)
104 : ResultingSimcall(actor, false), cond_(cond), mutex_(mutex), timeout_(timeout)
107 void serialize(std::stringstream& stream) const override;
108 std::string to_string() const override;
109 bool is_enabled() override;
110 activity::ConditionVariableImpl* get_cond() const { return cond_; }
111 activity::MutexImpl* get_mutex() const { return mutex_; }
112 double get_timeout() const { return timeout_; }
115 class ActorJoinSimcall final : public SimcallObserver {
116 s4u::ActorPtr const other_; // We need a Ptr to ensure access to the actor after its end, but Ptr requires s4u
117 const double timeout_;
120 ActorJoinSimcall(ActorImpl* actor, ActorImpl* other, double timeout = -1.0);
121 void serialize(std::stringstream& stream) const override;
122 std::string to_string() const override;
123 bool is_enabled() override;
125 s4u::ActorPtr get_other_actor() const { return other_; }
126 double get_timeout() const { return timeout_; }
128 } // namespace simgrid::kernel::actor