1 /* Copyright (c) 2006-2023. 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_ACTIVITYSET_HPP
7 #define SIMGRID_S4U_ACTIVITYSET_HPP
9 #include <simgrid/forward.h>
10 #include <simgrid/s4u/Activity.hpp>
16 extern template class XBT_PUBLIC xbt::Extendable<s4u::ActivitySet>;
19 /** @brief ActivitiesSet
21 * This class is a container of activities, allowing to wait for the completion of any or all activities in the set.
22 * This is somehow similar to the select(2) system call under UNIX, allowing you to wait for the next event about these
25 class XBT_PUBLIC ActivitySet : public xbt::Extendable<ActivitySet> {
26 std::vector<ActivityPtr>
27 activities_; // We use a vector instead of a set to improve reproductibility accross architectures
28 std::vector<ActivityPtr> failed_activities_;
31 ActivitySet() = default;
32 ActivitySet(const std::vector<ActivityPtr> init) : activities_(init) {}
33 ~ActivitySet() = default;
35 /** Add an activity to the set */
36 void push(ActivityPtr a) { activities_.push_back(a); }
37 /** Remove that activity from the set (no-op if the activity is not in the set) */
38 void erase(ActivityPtr a);
40 /** Get the amount of activities in the set. Failed activities (if any) are not counted */
41 int size() { return activities_.size(); }
42 /** Return whether the set is empty. Failed activities (if any) are not counted */
43 int empty() { return activities_.empty(); }
45 /** Wait for the completion of all activities in the set, but not longer than the provided timeout
47 * On timeout, an exception is raised.
49 * In any case, the completed activities remain in the set. Use test_any() to retrieve them.
51 void wait_all_for(double timeout);
52 /** Wait for the completion of all activities in the set. The set is NOT emptied afterward. */
53 void wait_all() { wait_all_for(-1); }
54 /** Returns the first terminated activity if any, or ActivityPtr(nullptr) if no activity is terminated */
55 ActivityPtr test_any();
57 /** Wait for the completion of one activity from the set, but not longer than the provided timeout.
59 * See wait_any() for details.
61 * @return the first terminated activity, which is automatically removed from the set.
64 ActivityPtr wait_any_for(double timeout);
65 /** Wait for the completion of one activity from the set.
67 * If an activity fails during that time, an exception is raised, and the failed exception is marked as failed in the
68 * set. Use get_failed_activity() to retrieve it.
70 * If more than one activity failed, the other ones are also removed from the set. Use get_failed_activity() several
71 * time to retrieve them all.
73 * @return the first terminated activity, which is automatically removed from the set. If more than one activity
74 * terminated at the same timestamp, then the other ones are still in the set. Use either test_any() or wait_any() to
75 * retrieve the other ones.
77 ActivityPtr wait_any() { return wait_any_for(-1); }
79 ActivityPtr get_failed_activity();
80 bool has_failed_activities() { return not failed_activities_.empty(); }
84 } // namespace simgrid