Logo AND Algorithmique Numérique Distribuée

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