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"
19 class SimcallObserver {
20 ActorImpl* const issuer_;
23 explicit SimcallObserver(ActorImpl* issuer) : issuer_(issuer) {}
24 ActorImpl* get_issuer() const { return issuer_; }
25 /** Whether this transition can currently be taken without blocking.
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.
30 virtual bool is_enabled() { return true; }
32 /** Returns the amount of time that this transition can be used.
34 * If it's 1 (as with send/wait), there is no need to fork the state space exploration on this point.
35 * If it's more than one (as with mc_random or waitany), we need to consider this transition several times to start
38 virtual int get_max_consider() { return 1; }
40 /** Prepares the simcall to be used.
42 * For most simcalls, this does nothing. Once enabled, there is nothing to do to prepare a send().
44 * It is useful only for the simcalls that can be used several times, such as waitany() or random().
45 * For them, prepare() selects the right outcome for the time being considered.
47 * The first time a simcall is considered, times_considered is 0, not 1.
49 virtual void prepare(int times_considered)
50 { /* Nothing to do by default */
53 /** Computes the dependency relation */
54 virtual bool depends(SimcallObserver* other);
56 /** Serialize to the given string buffer */
57 virtual void serialize(std::stringstream& stream) const;
59 /** Some simcalls may only be observable under some conditions.
60 * Most simcalls are not visible from the MC because they don't have an observer at all. */
61 virtual bool is_visible() const { return true; }
64 template <class T> class ResultingSimcall : public SimcallObserver {
68 ResultingSimcall() = default;
69 ResultingSimcall(ActorImpl* actor, T default_result) : SimcallObserver(actor), result_(default_result) {}
70 void set_result(T res) { result_ = res; }
71 T get_result() const { return result_; }
74 class RandomSimcall : public SimcallObserver {
80 RandomSimcall(ActorImpl* actor, int min, int max) : SimcallObserver(actor), min_(min), max_(max)
82 xbt_assert(min < max);
84 void serialize(std::stringstream& stream) const override;
85 int get_max_consider() override;
86 void prepare(int times_considered) override;
87 int get_value() const { return next_value_; }
88 bool depends(SimcallObserver* other) override;
91 class MutexSimcall : public SimcallObserver {
92 activity::MutexImpl* const mutex_;
95 MutexSimcall(ActorImpl* actor, activity::MutexImpl* mutex) : SimcallObserver(actor), mutex_(mutex) {}
96 activity::MutexImpl* get_mutex() const { return mutex_; }
97 bool depends(SimcallObserver* other) override;
100 class MutexUnlockSimcall : public MutexSimcall {
101 using MutexSimcall::MutexSimcall;
104 class MutexLockSimcall : public MutexSimcall {
105 const bool blocking_;
108 MutexLockSimcall(ActorImpl* actor, activity::MutexImpl* mutex, bool blocking = true)
109 : MutexSimcall(actor, mutex), blocking_(blocking)
112 bool is_enabled() override;
115 class ConditionWaitSimcall : public ResultingSimcall<bool> {
116 activity::ConditionVariableImpl* const cond_;
117 activity::MutexImpl* const mutex_;
118 const double timeout_;
121 ConditionWaitSimcall(ActorImpl* actor, activity::ConditionVariableImpl* cond, activity::MutexImpl* mutex,
122 double timeout = -1.0)
123 : ResultingSimcall(actor, false), cond_(cond), mutex_(mutex), timeout_(timeout)
126 bool is_enabled() override;
127 bool is_visible() const override { return false; }
128 activity::ConditionVariableImpl* get_cond() const { return cond_; }
129 activity::MutexImpl* get_mutex() const { return mutex_; }
130 double get_timeout() const { return timeout_; }
133 class SemAcquireSimcall : public ResultingSimcall<bool> {
134 activity::SemaphoreImpl* const sem_;
135 const double timeout_;
138 SemAcquireSimcall(ActorImpl* actor, activity::SemaphoreImpl* sem, double timeout = -1.0)
139 : ResultingSimcall(actor, false), sem_(sem), timeout_(timeout)
142 bool is_enabled() override;
143 bool is_visible() const override { return false; }
144 activity::SemaphoreImpl* get_sem() const { return sem_; }
145 double get_timeout() const { return timeout_; }
149 } // namespace kernel
150 } // namespace simgrid