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 {
29 friend XBT_PUBLIC void intrusive_ptr_release(Comm * c);
30 friend XBT_PUBLIC void intrusive_ptr_add_ref(Comm * c);
35 friend XBT_PUBLIC void intrusive_ptr_release(Exec * e);
36 friend XBT_PUBLIC void intrusive_ptr_add_ref(Exec * e);
39 friend XBT_PUBLIC void intrusive_ptr_release(Io* i);
40 friend XBT_PUBLIC void intrusive_ptr_add_ref(Io* i);
44 virtual ~Activity() = default;
47 Activity(Activity const&) = delete;
48 Activity& operator=(Activity const&) = delete;
51 enum class State { INITED = 0, STARTING, STARTED, CANCELED, ERRORED, FINISHED };
53 /** Starts a previously created activity.
55 * This function is optional: you can call wait() even if you didn't call start()
57 virtual Activity* start() = 0;
58 /** Blocks until the activity is terminated */
59 virtual Activity* wait() = 0;
60 /** Blocks until the activity is terminated, or until the timeout is elapsed
61 * Raises: timeout exception.*/
62 virtual Activity* wait_for(double timeout) = 0;
63 /** Blocks until the activity is terminated, or until the time limit is reached
64 * Raises: timeout exception. */
65 void wait_until(double time_limit);
67 /** Cancel that activity */
68 virtual Activity* cancel() = 0;
69 /** Retrieve the current state of the activity */
70 Activity::State get_state() { return state_; }
71 void set_state(Activity::State state) { state_ = state; }
72 /** Tests whether the given activity is terminated yet. This is a pure function. */
73 virtual bool test() = 0;
75 /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
76 virtual double get_remaining();
78 /** Set the [remaining] amount of work that this Activity will entail
80 * It is forbidden to change the amount of work once the Activity is started */
81 Activity* set_remaining(double remains);
83 /** Returns the internal implementation of this Activity */
84 kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
87 kernel::activity::ActivityImplPtr pimpl_ = nullptr;
88 Activity::State state_ = Activity::State::INITED;
92 template <class AnyActivity> class Activity_T : public Activity {
94 std::string name_ = "unnamed";
95 std::string tracing_category_ = "";
96 void* user_data_ = nullptr;
97 std::atomic_int_fast32_t refcount_{0};
98 std::vector<Activity*> successors_;
99 std::set<Activity*> dependencies_;
103 friend void intrusive_ptr_release(AnyActivity* a)
105 if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
106 std::atomic_thread_fence(std::memory_order_acquire);
110 friend void intrusive_ptr_add_ref(AnyActivity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
113 void add_successor(Activity* a)
115 successors_.push_back(a);
116 static_cast<AnyActivity*>(a)->add_dependency_on(static_cast<Activity*>(this));
118 void remove_successor() { successors_.pop_back(); }
119 Activity* get_successor() { return successors_.back(); }
120 bool has_successors() { return not successors_.empty(); }
122 void add_dependency_on(Activity* a) { dependencies_.insert({a}); }
123 void remove_dependency_on(Activity* a) { dependencies_.erase(a); }
124 bool has_dependencies() { return not dependencies_.empty(); }
125 void release_dependencies()
127 while (has_successors()) {
128 AnyActivity* b = static_cast<AnyActivity*>(get_successor());
129 XBT_CDEBUG(s4u_activity, "Remove a dependency from '%s' on '%s'", static_cast<AnyActivity*>(this)->get_cname(),
131 b->remove_dependency_on(static_cast<Activity*>(this));
132 if (not b->has_dependencies()) {
139 AnyActivity* vetoable_start()
141 set_state(State::STARTING);
142 if (has_dependencies())
143 return static_cast<AnyActivity*>(this);
144 set_state(State::STARTED);
145 XBT_CDEBUG(s4u_activity, "All dependencies are solved, let's start '%s'",
146 static_cast<AnyActivity*>(this)->get_cname());
147 static_cast<AnyActivity*>(this)->start();
148 return static_cast<AnyActivity*>(this);
151 AnyActivity* set_name(const std::string& name)
153 xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
155 return static_cast<AnyActivity*>(this);
157 const std::string& get_name() { return name_; }
158 const char* get_cname() { return name_.c_str(); }
160 AnyActivity* set_tracing_category(const std::string& category)
162 xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
163 tracing_category_ = category;
164 return static_cast<AnyActivity*>(this);
166 const std::string& get_tracing_category() { return tracing_category_; }
168 AnyActivity* set_user_data(void* data)
171 return static_cast<AnyActivity*>(this);
174 void* get_user_data() { return user_data_; }
178 } // namespace simgrid
180 #endif /* SIMGRID_S4U_ACTIVITY_HPP */