1 /* Copyright (c) 2006-2019. 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>
22 * This class is the ancestor of every activities that an actor can undertake.
23 * That is, activities are all the things that do take time to the actor in the simulated world.
25 class XBT_PUBLIC Activity {
27 friend XBT_PUBLIC void intrusive_ptr_release(Comm * c);
28 friend XBT_PUBLIC void intrusive_ptr_add_ref(Comm * c);
33 friend XBT_PUBLIC void intrusive_ptr_release(Exec * e);
34 friend XBT_PUBLIC void intrusive_ptr_add_ref(Exec * e);
37 friend XBT_PUBLIC void intrusive_ptr_release(Io* i);
38 friend XBT_PUBLIC void intrusive_ptr_add_ref(Io* i);
42 virtual ~Activity() = default;
45 Activity(Activity const&) = delete;
46 Activity& operator=(Activity const&) = delete;
49 enum class State { INITED = 0, STARTING, STARTED, CANCELED, ERRORED, FINISHED };
51 /** Starts a previously created activity.
53 * This function is optional: you can call wait() even if you didn't call start()
55 virtual Activity* start() = 0;
56 /** Blocks until the activity is terminated */
57 virtual Activity* wait() = 0;
58 /** Blocks until the activity is terminated, or until the timeout is elapsed
59 * Raises: timeout exception.*/
60 virtual Activity* wait_for(double timeout) = 0;
61 /** Blocks until the activity is terminated, or until the time limit is reached
62 * Raises: timeout exception. */
63 void wait_until(double time_limit);
65 /** Cancel that activity */
66 virtual Activity* cancel() = 0;
67 /** Retrieve the current state of the activity */
68 Activity::State get_state() { return state_; }
69 void set_state(Activity::State state) { state_ = state; }
70 /** Tests whether the given activity is terminated yet. This is a pure function. */
71 virtual bool test() = 0;
73 /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
74 virtual double get_remaining();
76 /** Set the [remaining] amount of work that this Activity will entail
78 * It is forbidden to change the amount of work once the Activity is started */
79 Activity* set_remaining(double remains);
81 /** Returns the internal implementation of this Activity */
82 kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
85 kernel::activity::ActivityImplPtr pimpl_ = nullptr;
86 Activity::State state_ = Activity::State::INITED;
90 template <class AnyActivity> class Activity_T : public Activity {
92 std::string name_ = "";
93 std::string tracing_category_ = "";
94 void* user_data_ = nullptr;
95 std::atomic_int_fast32_t refcount_{0};
96 std::vector<AnyActivity*> successors_;
97 std::set<AnyActivity*> dependencies_;
101 friend void intrusive_ptr_release(AnyActivity* a)
103 if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
104 std::atomic_thread_fence(std::memory_order_acquire);
108 friend void intrusive_ptr_add_ref(AnyActivity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
111 void add_successor(AnyActivity* a)
113 successors_.push_back(a);
114 a->add_dependency_on(static_cast<AnyActivity*>(this));
116 void remove_successor() { successors_.pop_back(); }
117 AnyActivity* get_successor() { return successors_.back(); }
118 bool has_successors() { return not successors_.empty(); }
120 void add_dependency_on(AnyActivity* a) { dependencies_.insert({a}); }
121 void remove_dependency_on(AnyActivity* a) { dependencies_.erase(a); }
122 bool has_dependencies() { return not dependencies_.empty(); }
123 void release_dependencies()
125 while (has_successors()) {
126 AnyActivity* b = get_successor();
127 b->remove_dependency_on(static_cast<AnyActivity*>(this));
128 if (not b->has_dependencies()) {
135 AnyActivity* vetoable_start()
137 set_state(State::STARTING);
138 if (has_dependencies())
139 return static_cast<AnyActivity*>(this);
140 set_state(State::STARTED);
141 static_cast<AnyActivity*>(this)->start();
142 return static_cast<AnyActivity*>(this);
145 AnyActivity* set_name(const std::string& name)
147 xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
149 return static_cast<AnyActivity*>(this);
151 const std::string& get_name() { return name_; }
152 const char* get_cname() { return name_.c_str(); }
154 AnyActivity* set_tracing_category(const std::string& category)
156 xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
157 tracing_category_ = category;
158 return static_cast<AnyActivity*>(this);
160 const std::string& get_tracing_category() { return tracing_category_; }
162 AnyActivity* set_user_data(void* data)
165 return static_cast<AnyActivity*>(this);
168 void* get_user_data() { return user_data_; }
172 } // namespace simgrid
174 #endif /* SIMGRID_S4U_ACTIVITY_HPP */