Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid into CRTP
[simgrid.git] / include / simgrid / s4u / Activity.hpp
1 /* Copyright (c) 2006-2019. 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_S4U_ACTIVITY_HPP
7 #define SIMGRID_S4U_ACTIVITY_HPP
8
9 #include "xbt/asserts.h"
10 #include <simgrid/forward.h>
11 #include <xbt/signal.hpp>
12
13 namespace simgrid {
14 namespace s4u {
15
16 /** @brief Activities
17  *
18  * This class is the ancestor of every activities that an actor can undertake.
19  * That is, activities are all the things that do take time to the actor in the simulated world.
20  *
21  * They are somewhat linked but not identical to simgrid::kernel::resource::Action,
22  * that are stuff occurring on a resource:
23  *
24  * - A sequential execution activity encompasses 2 actions: one for the exec itself,
25  *   and a time-limited sleep used as timeout detector.
26  * - A point-to-point communication activity encompasses 3 actions: one for the comm itself
27  *   (which spans on all links of the path), and one infinite sleep used as failure detector
28  *   on both sender and receiver hosts.
29  * - Synchronization activities may possibly be connected to no action.
30  */
31 class XBT_PUBLIC Activity {
32   friend Comm;
33   friend XBT_PUBLIC void intrusive_ptr_release(Comm * c);
34   friend XBT_PUBLIC void intrusive_ptr_add_ref(Comm * c);
35
36   friend Exec;
37   friend ExecSeq;
38   friend ExecPar;
39   friend XBT_PUBLIC void intrusive_ptr_release(Exec * e);
40   friend XBT_PUBLIC void intrusive_ptr_add_ref(Exec * e);
41
42   friend Io;
43   friend XBT_PUBLIC void intrusive_ptr_release(Io* i);
44   friend XBT_PUBLIC void intrusive_ptr_add_ref(Io* i);
45
46 protected:
47   Activity()  = default;
48   virtual ~Activity() = default;
49
50 public:
51 #ifndef DOXYGEN
52   Activity(Activity const&) = delete;
53   Activity& operator=(Activity const&) = delete;
54 #endif
55
56   enum class State { INITED = 0, STARTED, CANCELED, ERRORED, FINISHED };
57
58   /** Starts a previously created activity.
59    *
60    * This function is optional: you can call wait() even if you didn't call start()
61    */
62   virtual Activity* start() = 0;
63   /** Blocks until the activity is terminated */
64   virtual Activity* wait() = 0;
65   /** Blocks until the activity is terminated, or until the timeout is elapsed
66    *  Raises: timeout exception.*/
67   virtual Activity* wait_for(double timeout) = 0;
68   /** Blocks until the activity is terminated, or until the time limit is reached
69    * Raises: timeout exception. */
70   void wait_until(double time_limit);
71
72   /** Cancel that activity */
73   virtual Activity* cancel() = 0;
74   /** Retrieve the current state of the activity */
75   Activity::State get_state() { return state_; }
76   /** Tests whether the given activity is terminated yet. This is a pure function. */
77   virtual bool test() = 0;
78
79   /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
80   virtual double get_remaining();
81
82   /** Set the [remaining] amount of work that this Activity will entail
83    *
84    * It is forbidden to change the amount of work once the Activity is started */
85   Activity* set_remaining(double remains);
86
87   /** Put some user data onto the Activity */
88
89   kernel::activity::ActivityImplPtr get_impl() { return pimpl_; }
90
91 #ifndef DOXYGEN
92   XBT_ATTRIB_DEPRECATED_v324("Please use Activity::wait_for()") virtual void wait(double timeout) = 0;
93   XBT_ATTRIB_DEPRECATED_v323("Please use Activity::get_state()") Activity::State getState() { return state_; }
94   XBT_ATTRIB_DEPRECATED_v323("Please use Activity::get_remaining()") double getRemains() { return get_remaining(); }
95   XBT_ATTRIB_DEPRECATED_v323("Please use Activity::set_remaining()") Activity* setRemains(double remains)
96   {
97     return set_remaining(remains);
98   }
99 #endif
100
101 private:
102   kernel::activity::ActivityImplPtr pimpl_ = nullptr;
103   Activity::State state_                   = Activity::State::INITED;
104   double remains_                          = 0;
105 };
106
107 template <class AnyActivity> class Activity_T : public Activity {
108 private:
109   std::string name_             = "";
110   std::string tracing_category_ = "";
111   void* user_data_              = nullptr;
112
113 public:
114   AnyActivity* set_name(const std::string& name)
115   {
116     xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
117     name_ = name;
118     return static_cast<AnyActivity*>(this);
119   }
120   const std::string& get_name() { return name_; }
121   const char* get_cname() { return name_.c_str(); }
122
123   AnyActivity* set_tracing_category(const std::string& category)
124   {
125     xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
126     tracing_category_ = category;
127     return static_cast<AnyActivity*>(this);
128   }
129   const std::string& get_tracing_category() { return tracing_category_; }
130
131   AnyActivity* set_user_data(void* data)
132   {
133     user_data_ = data;
134     return static_cast<AnyActivity*>(this);
135   }
136
137   void* get_user_data() { return user_data_; }
138   XBT_ATTRIB_DEPRECATED_v323("Please use Activity::set_user_data()") AnyActivity* setUserData(void* data)
139   {
140     return set_user_data(data);
141   }
142   XBT_ATTRIB_DEPRECATED_v323("Please use Activity::get_user_data()") void* getUserData() { return user_data_; }
143 };
144
145 } // namespace s4u
146 } // namespace simgrid
147
148 #endif /* SIMGRID_S4U_ACTIVITY_HPP */