Logo AND Algorithmique Numérique Distribuée

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