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"
12 #include <simgrid/forward.h>
15 #include <xbt/signal.hpp>
17 XBT_LOG_EXTERNAL_CATEGORY(s4u_activity);
24 * This class is the ancestor of every activities that an actor can undertake.
25 * That is, activities are all the things that do take time to the actor in the simulated world.
27 class XBT_PUBLIC Activity {
34 virtual ~Activity() = default;
36 void release_dependencies()
38 while (not successors_.empty()) {
39 ActivityPtr b = successors_.back();
40 XBT_CDEBUG(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
41 b->dependencies_.erase(this);
42 if (b->dependencies_.empty()) {
45 successors_.pop_back();
49 void add_successor(ActivityPtr a)
51 successors_.push_back(a);
52 a->dependencies_.insert({this});
58 state_ = State::STARTING;
59 if (dependencies_.empty()) {
60 XBT_CDEBUG(s4u_activity, "All dependencies are solved, let's start '%s'", get_cname());
66 Activity(Activity const&) = delete;
67 Activity& operator=(Activity const&) = delete;
70 enum class State { INITED = 0, STARTING, STARTED, CANCELED,
71 // ERRORED, // FIXME: state has never been used
74 /** Starts a previously created activity.
76 * This function is optional: you can call wait() even if you didn't call start()
78 virtual Activity* start() = 0;
79 /** Blocks the current actor until the activity is terminated */
80 virtual Activity* wait() = 0;
81 /** Blocks the current actor until the activity is terminated, or until the timeout is elapsed\n
82 * Raises: timeout exception.*/
83 virtual Activity* wait_for(double timeout) = 0;
84 /** Blocks the current actor until the activity is terminated, or until the time limit is reached\n
85 * Raises: timeout exception. */
86 void wait_until(double time_limit);
88 /** Cancel that activity */
89 virtual Activity* cancel() = 0;
90 /** Retrieve the current state of the activity */
91 Activity::State get_state() const { return state_; }
92 void set_state(Activity::State state) { state_ = state; }
93 /** Tests whether the given activity is terminated yet. */
96 /** Blocks the progression of this activity until it gets resumed */
97 virtual Activity* suspend();
98 /** Unblock the progression of this activity if it was suspended previously */
99 virtual Activity* resume();
100 /** Whether or not the progression of this activity is blocked */
101 bool is_suspended() const { return suspended_; }
103 virtual const char* get_cname() const = 0;
104 virtual const std::string& get_name() const = 0;
106 /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
107 virtual double get_remaining() const;
108 /** Set the [remaining] amount of work that this Activity will entail
110 * It is forbidden to change the amount of work once the Activity is started */
111 Activity* set_remaining(double remains);
113 /** Returns the internal implementation of this Activity */
114 kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
117 friend void intrusive_ptr_release(Activity* a)
119 if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
120 std::atomic_thread_fence(std::memory_order_acquire);
124 friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
128 intrusive_ptr_add_ref(this);
131 void unref() { intrusive_ptr_release(this); }
134 kernel::activity::ActivityImplPtr pimpl_ = nullptr;
135 Activity::State state_ = Activity::State::INITED;
137 bool suspended_ = false;
138 std::vector<ActivityPtr> successors_;
139 std::set<ActivityPtr> dependencies_;
140 std::atomic_int_fast32_t refcount_{0};
143 template <class AnyActivity> class Activity_T : public Activity {
144 std::string name_ = "unnamed";
145 std::string tracing_category_ = "";
146 void* user_data_ = nullptr;
149 AnyActivity* add_successor(ActivityPtr a)
151 Activity::add_successor(a);
152 return static_cast<AnyActivity*>(this);
155 AnyActivity* set_name(const std::string& name)
157 xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
159 return static_cast<AnyActivity*>(this);
161 const std::string& get_name() const override { return name_; }
162 const char* get_cname() const override { return name_.c_str(); }
164 AnyActivity* set_tracing_category(const std::string& category)
166 xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
167 tracing_category_ = category;
168 return static_cast<AnyActivity*>(this);
170 const std::string& get_tracing_category() const { return tracing_category_; }
172 AnyActivity* set_user_data(void* data)
175 return static_cast<AnyActivity*>(this);
178 void* get_user_data() const { return user_data_; }
180 /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
181 * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
182 * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
184 friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
185 friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
190 } // namespace simgrid
192 #endif /* SIMGRID_S4U_ACTIVITY_HPP */