1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#ifndef ARROW_UTIL_TASK_GROUP_H
19#define ARROW_UTIL_TASK_GROUP_H
20
21#include <functional>
22#include <memory>
23#include <utility>
24
25#include "arrow/status.h"
26#include "arrow/util/macros.h"
27#include "arrow/util/visibility.h"
28
29namespace arrow {
30namespace internal {
31
32class ThreadPool;
33
34// TODO Simplify this. Subgroups don't seem necessary.
35
36/// \brief A group of related tasks
37///
38/// A TaskGroup executes tasks with the signature `Status()`.
39/// Execution can be serial or parallel, depending on the TaskGroup
40/// implementation. When Finish() returns, it is guaranteed that all
41/// tasks have finished, or at least one has errored.
42///
43class ARROW_EXPORT TaskGroup {
44 public:
45 /// Add a Status-returning function to execute. Execution order is
46 /// undefined. The function may be executed immediately or later.
47 template <typename Function>
48 void Append(Function&& func) {
49 return AppendReal(std::forward<Function>(func));
50 }
51
52 /// Wait for execution of all tasks (and subgroups) to be finished,
53 /// or for at least one task (or subgroup) to error out.
54 /// The returned Status propagates the error status of the first failing
55 /// task (or subgroup).
56 virtual Status Finish() = 0;
57
58 /// The current agregate error Status. Non-blocking, useful for stopping early.
59 virtual Status current_status() = 0;
60
61 /// Whether some tasks have already failed. Non-blocking , useful for stopping early.
62 virtual bool ok() = 0;
63
64 /// How many tasks can typically be executed in parallel.
65 /// This is only a hint, useful for testing or debugging.
66 virtual int parallelism() = 0;
67
68 /// Create a subgroup of this group. This group can only finish
69 /// when all subgroups have finished (this means you must be
70 /// be careful to call Finish() on subgroups before calling it
71 /// on the main group).
72 // XXX if a subgroup errors out, should it propagate immediately to the parent
73 // and to children?
74 virtual std::shared_ptr<TaskGroup> MakeSubGroup() = 0;
75
76 static std::shared_ptr<TaskGroup> MakeSerial();
77 static std::shared_ptr<TaskGroup> MakeThreaded(internal::ThreadPool*);
78
79 virtual ~TaskGroup() = default;
80
81 protected:
82 TaskGroup() = default;
83 ARROW_DISALLOW_COPY_AND_ASSIGN(TaskGroup);
84
85 virtual void AppendReal(std::function<Status()> task) = 0;
86};
87
88} // namespace internal
89} // namespace arrow
90
91#endif // ARROW_UTIL_TASK_GROUP_H
92