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