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 {
36 virtual ~Activity() = default;
39 Activity(Activity const&) = delete;
40 Activity& operator=(Activity const&) = delete;
43 enum class State { INITED = 0, STARTING, STARTED, CANCELED, ERRORED, FINISHED };
45 /** Starts a previously created activity.
47 * This function is optional: you can call wait() even if you didn't call start()
49 virtual Activity* start() = 0;
50 /** Blocks until the activity is terminated */
51 virtual Activity* wait() = 0;
52 /** Blocks until the activity is terminated, or until the timeout is elapsed
53 * Raises: timeout exception.*/
54 virtual Activity* wait_for(double timeout) = 0;
55 /** Blocks until the activity is terminated, or until the time limit is reached
56 * Raises: timeout exception. */
57 void wait_until(double time_limit);
59 /** Cancel that activity */
60 virtual Activity* cancel() = 0;
61 /** Retrieve the current state of the activity */
62 Activity::State get_state() { return state_; }
63 void set_state(Activity::State state) { state_ = state; }
64 /** Tests whether the given activity is terminated yet. This is a pure function. */
65 virtual bool test() = 0;
66 virtual const char* get_cname() = 0;
67 virtual const std::string& get_name() = 0;
69 /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
70 virtual double get_remaining();
71 /** Set the [remaining] amount of work that this Activity will entail
73 * It is forbidden to change the amount of work once the Activity is started */
74 Activity* set_remaining(double remains);
76 /** Returns the internal implementation of this Activity */
77 kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
79 void add_successor(ActivityPtr a)
81 successors_.push_back(a);
82 a->add_dependency_on(this);
84 void remove_successor() { successors_.pop_back(); }
85 ActivityPtr get_successor() { return successors_.back(); }
86 bool has_successors() { return not successors_.empty(); }
88 void add_dependency_on(ActivityPtr a) { dependencies_.insert({a}); }
89 void remove_dependency_on(ActivityPtr a) { dependencies_.erase(a); }
90 bool has_dependencies() { return not dependencies_.empty(); }
91 void release_dependencies()
93 while (has_successors()) {
94 ActivityPtr b = get_successor();
95 XBT_CDEBUG(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
96 b->remove_dependency_on(this);
97 if (not b->has_dependencies()) {
104 void vetoable_start()
106 state_ = State::STARTING;
107 if (not has_dependencies()) {
108 state_ = State::STARTED;
109 XBT_CDEBUG(s4u_activity, "All dependencies are solved, let's start '%s'", get_cname());
115 friend void intrusive_ptr_release(Activity* a)
117 if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
118 std::atomic_thread_fence(std::memory_order_acquire);
122 friend void intrusive_ptr_add_ref(Activity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
125 kernel::activity::ActivityImplPtr pimpl_ = nullptr;
126 Activity::State state_ = Activity::State::INITED;
128 std::vector<ActivityPtr> successors_;
129 std::set<ActivityPtr> dependencies_;
130 std::atomic_int_fast32_t refcount_{0};
133 template <class AnyActivity> class Activity_T : public Activity {
134 std::string name_ = "unnamed";
135 std::string tracing_category_ = "";
136 void* user_data_ = nullptr;
139 AnyActivity* set_name(const std::string& name)
141 xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
143 return static_cast<AnyActivity*>(this);
145 const std::string& get_name() { return name_; }
146 const char* get_cname() { return name_.c_str(); }
148 AnyActivity* set_tracing_category(const std::string& category)
150 xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
151 tracing_category_ = category;
152 return static_cast<AnyActivity*>(this);
154 const std::string& get_tracing_category() { return tracing_category_; }
156 AnyActivity* set_user_data(void* data)
159 return static_cast<AnyActivity*>(this);
162 void* get_user_data() { return user_data_; }
164 /* The refcounting is done in the ancestor class, Activity, but we want each of the classes benefiting of the CRTP
165 * (Exec, Comm, etc) to have smart pointers too, so we define these methods here, that forward the ptr_release and
166 * add_ref to the Activity class. Hopefully, the "inline" helps to not hinder the perf here.
168 friend void inline intrusive_ptr_release(AnyActivity* a) { intrusive_ptr_release(static_cast<Activity*>(a)); }
169 friend void inline intrusive_ptr_add_ref(AnyActivity* a) { intrusive_ptr_add_ref(static_cast<Activity*>(a)); }
174 } // namespace simgrid
176 #endif /* SIMGRID_S4U_ACTIVITY_HPP */