Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement ActivitySet
[simgrid.git] / include / simgrid / s4u / ActivitySet.hpp
diff --git a/include/simgrid/s4u/ActivitySet.hpp b/include/simgrid/s4u/ActivitySet.hpp
new file mode 100644 (file)
index 0000000..4add822
--- /dev/null
@@ -0,0 +1,79 @@
+/* Copyright (c) 2006-2023. The SimGrid Team. All rights reserved.          */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#ifndef SIMGRID_S4U_ACTIVITYSET_HPP
+#define SIMGRID_S4U_ACTIVITYSET_HPP
+
+#include <simgrid/forward.h>
+#include <simgrid/s4u/Activity.hpp>
+
+#include <vector>
+
+namespace simgrid::s4u {
+/** @brief ActivitiesSet
+ *
+ * This class is a container of activities, allowing to wait for the completion of any or all activities in the set.
+ * This is somehow similar to the select(2) system call under UNIX, allowing you to wait for the next event about these
+ * activities.
+ */
+class XBT_PUBLIC ActivitySet : public xbt::Extendable<ActivitySet> {
+  std::vector<ActivityPtr>
+      activities_; // We use a vector instead of a set to improve reproductibility accross architectures
+  std::vector<ActivityPtr> failed_activities_;
+
+public:
+  ActivitySet()  = default;
+  ~ActivitySet() = default;
+
+  /** Add an activity to the set */
+  void push(ActivityPtr a) { activities_.push_back(a); }
+  /** Remove that activity from the set (no-op if the activity is not in the set) */
+  void erase(ActivityPtr a);
+
+  /** Get the amount of activities in the set. Failed activities (if any) are not counted */
+  int size() { return activities_.size(); }
+  /** Return whether the set is empty. Failed activities (if any) are not counted */
+  int empty() { return activities_.empty(); }
+
+  /** Wait for the completion of all activities in the set, but not longer than the provided timeout
+   *
+   * On timeout, an exception is raised, and the completed activities remain in the set. Use test_any() to retrieve
+   * them.
+   */
+  void wait_all_for(double timeout);
+  /** Wait for the completion of all activities in the set */
+  void wait_all() { wait_all_for(-1); }
+  /** Returns the first terminated activity if any, or ActivityPtr(nullptr) if no activity is terminated */
+  ActivityPtr test_any();
+
+  /** Wait for the completion of one activity from the set, but not longer than the provided timeout.
+   *
+   *  See wait_any() for details.
+   *
+   * @return the first terminated activity, which is automatically removed from the set.
+   */
+
+  ActivityPtr wait_any_for(double timeout);
+  /** Wait for the completion of one activity from the set.
+   *
+   * If an activity fails during that time, an exception is raised, and the failed exception is marked as failed in the
+   * set. Use get_failed_activity() to retrieve it.
+   *
+   * If more than one activity failed, the other ones are also removed from the set. Use get_failed_activity() several
+   * time to retrieve them all.
+   *
+   * @return the first terminated activity, which is automatically removed from the set. If more than one activity
+   * terminated at the same timestamp, then the other ones are still in the set. Use either test_any() or wait_any() to
+   * retrieve the other ones.
+   */
+  ActivityPtr wait_any() { return wait_any_for(-1); }
+
+  ActivityPtr get_failed_activity();
+  bool has_failed_activities() { return not failed_activities_.empty(); }
+};
+
+}; // namespace simgrid::s4u
+
+#endif