Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cf3cd4cbdc02884652f7531bc09695df9fff73df
[simgrid.git] / include / simgrid / s4u / ActivitySet.hpp
1 /* Copyright (c) 2006-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_S4U_ACTIVITYSET_HPP
7 #define SIMGRID_S4U_ACTIVITYSET_HPP
8
9 #include <simgrid/forward.h>
10 #include <simgrid/s4u/Activity.hpp>
11
12 #include <vector>
13
14 namespace simgrid::s4u {
15 /** @brief ActivitiesSet
16  *
17  * This class is a container of activities, allowing to wait for the completion of any or all activities in the set.
18  * This is somehow similar to the select(2) system call under UNIX, allowing you to wait for the next event about these
19  * activities.
20  */
21 class XBT_PUBLIC ActivitySet : public xbt::Extendable<ActivitySet> {
22   std::vector<ActivityPtr>
23       activities_; // We use a vector instead of a set to improve reproductibility accross architectures
24   std::vector<ActivityPtr> failed_activities_;
25
26 public:
27   ActivitySet()  = default;
28   ActivitySet(const std::vector<ActivityPtr> init) : activities_(init) {}
29   ~ActivitySet() = default;
30
31   /** Add an activity to the set */
32   void push(ActivityPtr a) { activities_.push_back(a); }
33   /** Remove that activity from the set (no-op if the activity is not in the set) */
34   void erase(ActivityPtr a);
35
36   /** Get the amount of activities in the set. Failed activities (if any) are not counted */
37   int size() { return activities_.size(); }
38   /** Return whether the set is empty. Failed activities (if any) are not counted */
39   int empty() { return activities_.empty(); }
40
41   /** Wait for the completion of all activities in the set, but not longer than the provided timeout
42    *
43    * On timeout, an exception is raised, and the completed activities remain in the set. Use test_any() to retrieve
44    * them.
45    */
46   void wait_all_for(double timeout);
47   /** Wait for the completion of all activities in the set */
48   void wait_all() { wait_all_for(-1); }
49   /** Returns the first terminated activity if any, or ActivityPtr(nullptr) if no activity is terminated */
50   ActivityPtr test_any();
51
52   /** Wait for the completion of one activity from the set, but not longer than the provided timeout.
53    *
54    *  See wait_any() for details.
55    *
56    * @return the first terminated activity, which is automatically removed from the set.
57    */
58
59   ActivityPtr wait_any_for(double timeout);
60   /** Wait for the completion of one activity from the set.
61    *
62    * If an activity fails during that time, an exception is raised, and the failed exception is marked as failed in the
63    * set. Use get_failed_activity() to retrieve it.
64    *
65    * If more than one activity failed, the other ones are also removed from the set. Use get_failed_activity() several
66    * time to retrieve them all.
67    *
68    * @return the first terminated activity, which is automatically removed from the set. If more than one activity
69    * terminated at the same timestamp, then the other ones are still in the set. Use either test_any() or wait_any() to
70    * retrieve the other ones.
71    */
72   ActivityPtr wait_any() { return wait_any_for(-1); }
73
74   ActivityPtr get_failed_activity();
75   bool has_failed_activities() { return not failed_activities_.empty(); }
76 };
77
78 }; // namespace simgrid::s4u
79
80 #endif