Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / src / kernel / activity / ActivityImpl.hpp
1 /* Copyright (c) 2007-2023. 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_KERNEL_ACTIVITY_ACTIVITYIMPL_HPP
7 #define SIMGRID_KERNEL_ACTIVITY_ACTIVITYIMPL_HPP
8
9 #include "simgrid/forward.h"
10 #include "simgrid/kernel/resource/Action.hpp"
11 #include "simgrid/simix.hpp"
12 #include "src/kernel/actor/Simcall.hpp"
13 #include "xbt/utility.hpp"
14
15 #include <atomic>
16 #include <list>
17 #include <string>
18 #include <string_view>
19
20 namespace simgrid::kernel::activity {
21
22 XBT_DECLARE_ENUM_CLASS(State, WAITING, READY, RUNNING, DONE, CANCELED, FAILED, SRC_HOST_FAILURE, DST_HOST_FAILURE,
23                        TIMEOUT, SRC_TIMEOUT, DST_TIMEOUT, LINK_FAILURE);
24
25 class XBT_PUBLIC ActivityImpl : public kernel::actor::ObjectAccessSimcallItem {
26   std::atomic_int_fast32_t refcount_{0};
27   std::string name_        = "";
28   actor::ActorImpl* actor_ = nullptr;
29   State state_             = State::WAITING; /* State of the activity */
30   double start_time_       = -1.0;
31   double finish_time_      = -1.0;
32   std::vector<s4u::Host*> hosts_;
33
34 public:
35   virtual ~ActivityImpl();
36   ActivityImpl() = default;
37   std::list<actor::Simcall*> simcalls_; /* List of simcalls waiting for this activity */
38   s4u::Activity* piface_         = nullptr;
39   resource::Action* surf_action_ = nullptr;
40
41 protected:
42   void inline set_name(std::string_view name)
43   {
44     // This is to keep name_ private while allowing ActivityImpl_T<??> to set it and then return a Ptr to qualified
45     // child type
46     name_ = name;
47   }
48   void set_start_time(double start_time) { start_time_ = start_time; }
49   void clear_hosts() { hosts_.clear(); }
50   void add_host(s4u::Host* host) { hosts_.push_back(host); }
51   void set_hosts(const std::vector<s4u::Host*>& hosts) { hosts_ = hosts; }
52
53 public:
54   const std::string& get_name() const { return name_; }
55   const char* get_cname() const { return name_.c_str(); }
56
57   void set_actor(actor::ActorImpl* actor) { actor_ = actor; }
58   actor::ActorImpl* get_actor() const { return actor_; }
59
60   void set_iface(s4u::Activity* iface) { piface_ = iface; }
61   s4u::Activity* get_iface() { return piface_; }
62
63   void set_state(State state) { state_ = state; }
64   const State& get_state() const { return state_; }
65   const char* get_state_str() const;
66
67   double get_start_time() const { return start_time_; }
68   void set_finish_time(double finish_time) { finish_time_ = finish_time; }
69   double get_finish_time() const { return finish_time_; }
70
71   virtual bool test(actor::ActorImpl* issuer);
72   static ssize_t test_any(actor::ActorImpl* issuer, const std::vector<ActivityImpl*>& activities);
73
74   virtual void wait_for(actor::ActorImpl* issuer, double timeout);
75   static void wait_any_for(actor::ActorImpl* issuer, const std::vector<ActivityImpl*>& activities, double timeout);
76   virtual ActivityImpl& set_timeout(double) { THROW_UNIMPLEMENTED; }
77
78   virtual void suspend();
79   virtual void resume();
80   virtual void cancel();
81
82   virtual void post() = 0; // Called by the main loop when the activity is marked as terminated or failed by its model.
83                            // Setups the status, clean things up, and call finish()
84   virtual void set_exception(actor::ActorImpl* issuer) = 0; // Raising exceptions and stuff
85   virtual void finish() = 0; // Unlock all simcalls blocked on that activity, either because it was marked as done by
86                              // the model or because it terminated without waiting for the model
87
88   s4u::Host* get_host() const { return hosts_.front(); }
89   const std::vector<s4u::Host*>& get_hosts() const { return hosts_; };
90
91   void register_simcall(actor::Simcall* simcall);
92   void unregister_simcall(actor::Simcall* simcall);
93   void handle_activity_waitany(actor::Simcall* simcall);
94   void clean_action();
95   virtual double get_remaining() const;
96   // Support for the boost::intrusive_ptr<ActivityImpl> datatype
97   friend XBT_PUBLIC void intrusive_ptr_add_ref(ActivityImpl* activity);
98   friend XBT_PUBLIC void intrusive_ptr_release(ActivityImpl* activity);
99   int get_refcount() const { return static_cast<int>(refcount_); } // For debugging purpose
100 };
101
102 /* This class exists to allow chained setters as in exec->set_name()->set_priority()->set_blah()
103  * The difficulty is that set_name() must return a qualified child class, not the generic ancestor
104  * But the getter is still in the ancestor to be usable on generic activities with no downcast */
105 template <class AnyActivityImpl> class ActivityImpl_T : public ActivityImpl {
106   std::string tracing_category_ = "";
107
108 public:
109   AnyActivityImpl& set_name(std::string_view name) /* Hides the function in the ancestor class */
110   {
111     ActivityImpl::set_name(name);
112     return static_cast<AnyActivityImpl&>(*this);
113   }
114
115   AnyActivityImpl& set_tracing_category(std::string_view category)
116   {
117     tracing_category_ = category;
118     return static_cast<AnyActivityImpl&>(*this);
119   }
120   const std::string& get_tracing_category() const { return tracing_category_; }
121 };
122
123 } // namespace simgrid::kernel::activity
124
125 #endif /* SIMGRID_KERNEL_ACTIVITY_ACTIVITYIMPL_HPP */