1 /* Copyright (c) 2006-2020. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_S4U_ACTIVITY_HPP
7 #define SIMGRID_S4U_ACTIVITY_HPP
9 #include "xbt/asserts.h"
13 #include <simgrid/forward.h>
16 #include <xbt/signal.hpp>
23 * This class is the ancestor of every activities that an actor can undertake.
24 * That is, activities are all the things that do take time to the actor in the simulated world.
26 class XBT_PUBLIC Activity {
28 friend XBT_PUBLIC void intrusive_ptr_release(Comm * c);
29 friend XBT_PUBLIC void intrusive_ptr_add_ref(Comm * c);
34 friend XBT_PUBLIC void intrusive_ptr_release(Exec * e);
35 friend XBT_PUBLIC void intrusive_ptr_add_ref(Exec * e);
38 friend XBT_PUBLIC void intrusive_ptr_release(Io* i);
39 friend XBT_PUBLIC void intrusive_ptr_add_ref(Io* i);
43 virtual ~Activity() = default;
46 Activity(Activity const&) = delete;
47 Activity& operator=(Activity const&) = delete;
50 enum class State { INITED = 0, STARTING, STARTED, CANCELED, ERRORED, FINISHED };
52 /** Starts a previously created activity.
54 * This function is optional: you can call wait() even if you didn't call start()
56 virtual Activity* start() = 0;
57 /** Blocks until the activity is terminated */
58 // virtual Activity* wait() = 0;
59 /** Blocks until the activity is terminated, or until the timeout is elapsed
60 * Raises: timeout exception.*/
61 virtual Activity* wait_for(double timeout) = 0;
62 /** Blocks until the activity is terminated, or until the time limit is reached
63 * Raises: timeout exception. */
64 void wait_until(double time_limit);
66 /** Cancel that activity */
67 virtual Activity* cancel() = 0;
68 /** Retrieve the current state of the activity */
69 Activity::State get_state() { return state_; }
70 void set_state(Activity::State state) { state_ = state; }
71 /** Tests whether the given activity is terminated yet. This is a pure function. */
72 virtual bool test() = 0;
74 /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
75 virtual double get_remaining();
77 /** Set the [remaining] amount of work that this Activity will entail
79 * It is forbidden to change the amount of work once the Activity is started */
80 Activity* set_remaining(double remains);
82 /** Returns the internal implementation of this Activity */
83 kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
86 kernel::activity::ActivityImplPtr pimpl_ = nullptr;
87 Activity::State state_ = Activity::State::INITED;
91 // template <class AnyActivity> class DependencyGuard {
93 // static bool activity_start_vetoer(AnyActivity* a) { return not a->has_dependencies(); }
94 // static void on_activity_done(AnyActivity* a);
96 //// while (a->has_successors()) {
97 //// AnyActivity* b = a->get_successor();
98 //// b->remove_dependency_on(a);
99 //// if (not b->has_dependencies()) {
100 //// XBT_INFO("Activity is done and a successor can start");
101 //// b->vetoable_start();
103 //// a->remove_successor();
108 template <class AnyActivity> class Activity_T : public Activity {
110 std::string name_ = "";
111 std::string tracing_category_ = "";
112 void* user_data_ = nullptr;
113 std::atomic_int_fast32_t refcount_{0};
114 std::vector<AnyActivity*> successors_;
115 std::set<AnyActivity*> dependencies_;
119 friend void intrusive_ptr_release(AnyActivity* a)
121 if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
122 std::atomic_thread_fence(std::memory_order_acquire);
126 friend void intrusive_ptr_add_ref(AnyActivity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
129 void add_successor(AnyActivity* a)
131 // XBT_INFO("Adding %s as a successor of %s", get_name(), a->get_name());
132 successors_.push_back(a);
133 a->add_dependency_on(static_cast<AnyActivity*>(this));
135 void remove_successor() { successors_.pop_back(); }
136 AnyActivity* get_successor() { return successors_.back(); }
137 bool has_successors() { return not successors_.empty(); }
139 void add_dependency_on(AnyActivity* a) { dependencies_.insert({a}); }
140 void remove_dependency_on(AnyActivity* a) { dependencies_.erase(a); }
141 bool has_dependencies() { return not dependencies_.empty(); }
142 void on_activity_done()
144 while (has_successors()) {
145 AnyActivity* b = get_successor();
146 b->remove_dependency_on(static_cast<AnyActivity*>(this));
147 if (not b->has_dependencies()) {
148 // XBT_INFO("Activity is done and a successor can start");
155 AnyActivity* vetoable_start()
157 set_state(State::STARTING);
158 if (has_dependencies())
159 return static_cast<AnyActivity*>(this);
160 // XBT_INFO("No veto, Activity can start");
161 set_state(State::STARTED);
162 static_cast<AnyActivity*>(this)->start();
163 return static_cast<AnyActivity*>(this);
166 virtual AnyActivity* wait()
168 static_cast<AnyActivity*>(this)->wait();
170 return static_cast<AnyActivity*>(this);
173 AnyActivity* set_name(const std::string& name)
175 xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
177 return static_cast<AnyActivity*>(this);
179 const std::string& get_name() { return name_; }
180 const char* get_cname() { return name_.c_str(); }
182 AnyActivity* set_tracing_category(const std::string& category)
184 xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
185 tracing_category_ = category;
186 return static_cast<AnyActivity*>(this);
188 const std::string& get_tracing_category() { return tracing_category_; }
190 AnyActivity* set_user_data(void* data)
193 return static_cast<AnyActivity*>(this);
196 void* get_user_data() { return user_data_; }
200 } // namespace simgrid
202 #endif /* SIMGRID_S4U_ACTIVITY_HPP */