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 successors_.push_back(a);
132 a->add_dependency_on(static_cast<AnyActivity*>(this));
134 void remove_successor() { successors_.pop_back(); }
135 AnyActivity* get_successor() { return successors_.back(); }
136 bool has_successors() { return not successors_.empty(); }
138 void add_dependency_on(AnyActivity* a) { dependencies_.insert({a}); }
139 void remove_dependency_on(AnyActivity* a) { dependencies_.erase(a); }
140 bool has_dependencies() { return not dependencies_.empty(); }
141 void release_dependencies()
143 while (has_successors()) {
144 AnyActivity* b = get_successor();
145 b->remove_dependency_on(static_cast<AnyActivity*>(this));
146 if (not b->has_dependencies()) {
147 // XBT_INFO("Activity is done and a successor can start");
154 AnyActivity* vetoable_start()
156 set_state(State::STARTING);
157 if (has_dependencies())
158 return static_cast<AnyActivity*>(this);
159 // XBT_INFO("No veto, Activity can start");
160 set_state(State::STARTED);
161 static_cast<AnyActivity*>(this)->start();
162 return static_cast<AnyActivity*>(this);
165 AnyActivity* set_name(const std::string& name)
167 xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
169 return static_cast<AnyActivity*>(this);
171 const std::string& get_name() { return name_; }
172 const char* get_cname() { return name_.c_str(); }
174 AnyActivity* set_tracing_category(const std::string& category)
176 xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
177 tracing_category_ = category;
178 return static_cast<AnyActivity*>(this);
180 const std::string& get_tracing_category() { return tracing_category_; }
182 AnyActivity* set_user_data(void* data)
185 return static_cast<AnyActivity*>(this);
188 void* get_user_data() { return user_data_; }
192 } // namespace simgrid
194 #endif /* SIMGRID_S4U_ACTIVITY_HPP */