1 /* Copyright (c) 2006-2021. 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>
16 #include <xbt/utility.hpp>
18 XBT_LOG_EXTERNAL_CATEGORY(s4u_activity);
25 * This class is the ancestor of every activities that an actor can undertake.
26 * That is, activities are all the things that do take time to the actor in the simulated world.
28 class XBT_PUBLIC Activity {
35 virtual ~Activity() = default;
37 virtual bool is_assigned() const = 0;
39 void release_dependencies()
41 while (not successors_.empty()) {
42 ActivityPtr b = successors_.back();
43 XBT_CVERB(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
44 b->dependencies_.erase(this);
45 if (b->dependencies_.empty()) {
48 successors_.pop_back();
52 void add_successor(ActivityPtr a)
54 successors_.push_back(a);
55 a->dependencies_.insert({this});
61 state_ = State::STARTING;
62 if (dependencies_.empty() && is_assigned()) {
63 XBT_CVERB(s4u_activity, "'%s' is assigned to a resource and all dependencies are solved. Let's start", get_cname());
69 Activity(Activity const&) = delete;
70 Activity& operator=(Activity const&) = delete;
73 // enum class State { ... }
74 XBT_DECLARE_ENUM_CLASS(State, INITED, STARTING, STARTED, CANCELED, FINISHED);
76 /** Starts a previously created activity.
78 * This function is optional: you can call wait() even if you didn't call start()
80 virtual Activity* start() = 0;
81 /** Blocks the current actor until the activity is terminated */
82 virtual Activity* wait() = 0;
83 /** Blocks the current actor until the activity is terminated, or until the timeout is elapsed\n
84 * Raises: timeout exception.*/
85 virtual Activity* wait_for(double timeout) = 0;
86 /** Blocks the current actor until the activity is terminated, or until the time limit is reached\n
87 * Raises: timeout exception. */
88 void wait_until(double time_limit);
90 /** Cancel that activity */
91 virtual Activity* cancel() = 0;
92 /** Retrieve the current state of the activity */
93 Activity::State get_state() const { return state_; }
94 /** Return a string representation of the activity's state (one of INITED, STARTING, STARTED, CANCELED, FINISHED) */
95 const char* get_state_str() const;
96 void set_state(Activity::State state) { state_ = state; }
97 /** Tests whether the given activity is terminated yet. */
100 /** Blocks the progression of this activity until it gets resumed */
101 virtual Activity* suspend();
102 /** Unblock the progression of this activity if it was suspended previously */
103 virtual Activity* resume();
104 /** Whether or not the progression of this activity is blocked */
105 bool is_suspended() const { return suspended_; }
107 virtual const char* get_cname() const = 0;
108 virtual const std::string& get_name() const = 0;
110 /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
111 virtual double get_remaining() const;
112 /** Set the [remaining] amount of work that this Activity will entail
114 * It is forbidden to change the amount of work once the Activity is started */
115 Activity* set_remaining(double remains);
117 /** Returns the internal implementation of this Activity */
118 kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
121 friend void intrusive_ptr_release(Activity* a)
123 if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
124 std::atomic_thread_fence(std::memory_order_acquire);
128 friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
132 intrusive_ptr_add_ref(this);
135 void unref() { intrusive_ptr_release(this); }
138 kernel::activity::ActivityImplPtr pimpl_ = nullptr;
139 Activity::State state_ = Activity::State::INITED;
141 bool suspended_ = false;
142 std::vector<ActivityPtr> successors_;
143 std::set<ActivityPtr> dependencies_;
144 std::atomic_int_fast32_t refcount_{0};
147 template <class AnyActivity> class Activity_T : public Activity {
148 std::string name_ = "unnamed";
149 std::string tracing_category_ = "";
150 void* user_data_ = nullptr;
153 AnyActivity* add_successor(ActivityPtr a)
155 Activity::add_successor(a);
156 return static_cast<AnyActivity*>(this);
159 AnyActivity* set_name(const std::string& name)
161 xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
163 return static_cast<AnyActivity*>(this);
165 const std::string& get_name() const override { return name_; }
166 const char* get_cname() const override { return name_.c_str(); }
168 AnyActivity* set_tracing_category(const std::string& category)
170 xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
171 tracing_category_ = category;
172 return static_cast<AnyActivity*>(this);
174 const std::string& get_tracing_category() const { return tracing_category_; }
176 AnyActivity* set_user_data(void* data)
179 return static_cast<AnyActivity*>(this);
182 void* get_user_data() const { return user_data_; }
184 AnyActivity* vetoable_start()
186 Activity::vetoable_start();
187 return static_cast<AnyActivity*>(this);
190 /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
191 * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
192 * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
194 friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
195 friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
200 } // namespace simgrid
202 #endif /* SIMGRID_S4U_ACTIVITY_HPP */