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"
11 #include <simgrid/forward.h>
13 #include <xbt/signal.hpp>
20 * This class is the ancestor of every activities that an actor can undertake.
21 * That is, activities are all the things that do take time to the actor in the simulated world.
23 class XBT_PUBLIC Activity {
32 virtual ~Activity() = default;
36 Activity(Activity const&) = delete;
37 Activity& operator=(Activity const&) = delete;
40 enum class State { INITED = 0, STARTED, CANCELED, ERRORED, FINISHED };
42 /** Starts a previously created activity.
44 * This function is optional: you can call wait() even if you didn't call start()
46 virtual Activity* start() = 0;
47 /** Blocks until the activity is terminated */
48 virtual Activity* wait() = 0;
49 /** Blocks until the activity is terminated, or until the timeout is elapsed
50 * Raises: timeout exception.*/
51 virtual Activity* wait_for(double timeout) = 0;
52 /** Blocks until the activity is terminated, or until the time limit is reached
53 * Raises: timeout exception. */
54 void wait_until(double time_limit);
56 /** Cancel that activity */
57 virtual Activity* cancel() = 0;
58 /** Retrieve the current state of the activity */
59 Activity::State get_state() { return state_; }
60 /** Tests whether the given activity is terminated yet. This is a pure function. */
61 virtual bool test() = 0;
63 /** Get the remaining amount of work that this Activity entails. When it's 0, it's done. */
64 virtual double get_remaining();
66 /** Set the [remaining] amount of work that this Activity will entail
68 * It is forbidden to change the amount of work once the Activity is started */
69 Activity* set_remaining(double remains);
71 /** Returns the internal implementation of this Activity */
72 kernel::activity::ActivityImpl* get_impl() const { return pimpl_.get(); }
75 kernel::activity::ActivityImplPtr pimpl_ = nullptr;
76 Activity::State state_ = Activity::State::INITED;
80 template <class AnyActivity> class Activity_T : public Activity {
82 std::string name_ = "";
83 std::string tracing_category_ = "";
84 void* user_data_ = nullptr;
85 std::atomic_int_fast32_t refcount_{0};
89 friend void intrusive_ptr_release(AnyActivity* a)
91 if (a->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
92 std::atomic_thread_fence(std::memory_order_acquire);
96 friend void intrusive_ptr_add_ref(AnyActivity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
98 AnyActivity* set_name(const std::string& name)
100 xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
102 return static_cast<AnyActivity*>(this);
104 const std::string& get_name() { return name_; }
105 const char* get_cname() { return name_.c_str(); }
107 AnyActivity* set_tracing_category(const std::string& category)
109 xbt_assert(get_state() == State::INITED, "Cannot change the tracing category of an activity after its start");
110 tracing_category_ = category;
111 return static_cast<AnyActivity*>(this);
113 const std::string& get_tracing_category() { return tracing_category_; }
115 AnyActivity* set_user_data(void* data)
118 return static_cast<AnyActivity*>(this);
121 void* get_user_data() { return user_data_; }
125 } // namespace simgrid
127 #endif /* SIMGRID_S4U_ACTIVITY_HPP */