Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
implement generic ActivityPtr
[simgrid.git] / include / simgrid / s4u / Activity.hpp
1 /* Copyright (c) 2006-2020. 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 class XBT_PUBLIC Activity {
24   friend Comm;
25   friend Exec;
26   friend ExecSeq;
27   friend ExecPar;
28   friend Io;
29
30 protected:
31   Activity()  = default;
32   virtual ~Activity() = default;
33
34 public:
35 #ifndef DOXYGEN
36   Activity(Activity const&) = delete;
37   Activity& operator=(Activity const&) = delete;
38 #endif
39
40   enum class State { INITED = 0, STARTED, CANCELED, ERRORED, FINISHED };
41
42   /** Starts a previously created activity.
43    *
44    * This function is optional: you can call wait() even if you didn't call start()
45    */
46   virtual Activity* start() = 0;
47   /** Blocks until the activity is terminated */
48   virtual Activity* wait() = 0;
49   /** Blocks until the activity is terminated, or until the timeout is elapsed
50    *  Raises: timeout exception.*/
51   virtual Activity* wait_for(double timeout) = 0;
52   /** Blocks until the activity is terminated, or until the time limit is reached
53    * Raises: timeout exception. */
54   void wait_until(double time_limit);
55
56   /** Cancel that activity */
57   virtual Activity* cancel() = 0;
58   /** Retrieve the current state of the activity */
59   Activity::State get_state() { return state_; }
60   /** Tests whether the given activity is terminated yet. This is a pure function. */
61   virtual bool test() = 0;
62
63   /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
64   virtual double get_remaining();
65
66   /** Set the [remaining] amount of work that this Activity will entail
67    *
68    * It is forbidden to change the amount of work once the Activity is started */
69   Activity* set_remaining(double remains);
70
71   /** Returns the internal implementation of this Activity */
72   kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
73
74 #ifndef DOXYGEN
75   friend void intrusive_ptr_release(Activity* a)
76   {
77     if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
78       std::atomic_thread_fence(std::memory_order_acquire);
79       delete a;
80     }
81   }
82   friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
83 #endif
84 private:
85   kernel::activity::ActivityImplPtr pimpl_ = nullptr;
86   Activity::State state_                   = Activity::State::INITED;
87   double remains_                          = 0;
88   std::atomic_int_fast32_t refcount_{0};
89 };
90
91 template <class AnyActivity> class Activity_T : public Activity {
92 private:
93   std::string name_             = "";
94   std::string tracing_category_ = "";
95   void* user_data_              = nullptr;
96
97 public:
98   AnyActivity* set_name(const std::string& name)
99   {
100     xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
101     name_ = name;
102     return static_cast<AnyActivity*>(this);
103   }
104   const std::string& get_name() { return name_; }
105   const char* get_cname() { return name_.c_str(); }
106
107   AnyActivity* set_tracing_category(const std::string& category)
108   {
109     xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
110     tracing_category_ = category;
111     return static_cast<AnyActivity*>(this);
112   }
113   const std::string& get_tracing_category() { return tracing_category_; }
114
115   AnyActivity* set_user_data(void* data)
116   {
117     user_data_ = data;
118     return static_cast<AnyActivity*>(this);
119   }
120
121   void* get_user_data() { return user_data_; }
122 #ifndef DOXYGEN
123   /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
124    * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
125    * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
126    */
127   friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
128   friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
129 #endif
130 };
131
132 } // namespace s4u
133 } // namespace simgrid
134
135 #endif /* SIMGRID_S4U_ACTIVITY_HPP */