blob: 773878216f8f33adbf9f29567e2b35d8219c6fcd [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2021 The Chromium Authors
cfredrica0464ebf2021-07-24 01:26:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_BARRIER_CALLBACK_H_
6#define BASE_BARRIER_CALLBACK_H_
7
Sumaid Syed22f60eeb2021-08-26 05:16:268#include <memory>
cfredrica0464ebf2021-07-24 01:26:399#include <type_traits>
Sumaid Syed22f60eeb2021-08-26 05:16:2610#include <utility>
cfredrica0464ebf2021-07-24 01:26:3911#include <vector>
12
David Sanders8cfb63a2022-04-14 19:36:3013#include "base/check.h"
David Sandersf84c9f42022-04-15 13:42:3214#include "base/check_op.h"
Avi Drissman63e1f992023-01-13 18:54:4315#include "base/functional/bind.h"
16#include "base/functional/callback.h"
17#include "base/functional/callback_helpers.h"
cfredrica0464ebf2021-07-24 01:26:3918#include "base/synchronization/lock.h"
cfredricc8cdd1d2021-08-11 00:01:4819#include "base/template_util.h"
cfredrica0464ebf2021-07-24 01:26:3920#include "base/thread_annotations.h"
21
22namespace base {
23
cfredric8c677a12021-08-03 21:48:3024namespace internal {
cfredrica0464ebf2021-07-24 01:26:3925
cfredricc8cdd1d2021-08-11 00:01:4826template <typename T, typename DoneArg>
cfredrica0464ebf2021-07-24 01:26:3927class BarrierCallbackInfo {
28 public:
29 BarrierCallbackInfo(size_t num_callbacks,
cfredricc8cdd1d2021-08-11 00:01:4830 OnceCallback<void(DoneArg)> done_callback)
cfredrica0464ebf2021-07-24 01:26:3931 : num_callbacks_left_(num_callbacks),
32 done_callback_(std::move(done_callback)) {
33 results_.reserve(num_callbacks);
34 }
35
36 void Run(T t) LOCKS_EXCLUDED(mutex_) {
37 base::ReleasableAutoLock lock(&mutex_);
38 DCHECK_NE(num_callbacks_left_, 0U);
39 results_.push_back(std::move(t));
40 --num_callbacks_left_;
41
42 if (num_callbacks_left_ == 0) {
cfredricc8cdd1d2021-08-11 00:01:4843 std::vector<base::remove_cvref_t<T>> results = std::move(results_);
cfredrica0464ebf2021-07-24 01:26:3944 lock.Release();
45 std::move(done_callback_).Run(std::move(results));
46 }
47 }
48
49 private:
50 Lock mutex_;
51 size_t num_callbacks_left_ GUARDED_BY(mutex_);
cfredricc8cdd1d2021-08-11 00:01:4852 std::vector<base::remove_cvref_t<T>> results_ GUARDED_BY(mutex_);
53 OnceCallback<void(DoneArg)> done_callback_;
cfredrica0464ebf2021-07-24 01:26:3954};
55
cfredriceaafc6b2021-07-27 21:12:0156template <typename T>
57void ShouldNeverRun(T t) {
58 CHECK(false);
59}
60
cfredric8c677a12021-08-03 21:48:3061} // namespace internal
cfredrica0464ebf2021-07-24 01:26:3962
63// BarrierCallback<T> is an analog of BarrierClosure for which each `Run()`
64// invocation takes a `T` as an argument. After `num_callbacks` such
65// invocations, BarrierCallback invokes `Run()` on its `done_callback`, passing
66// the vector of `T`s as an argument. (The ordering of the vector is
67// unspecified.)
68//
cfredricc8cdd1d2021-08-11 00:01:4869// `T`s that are movable are moved into the callback's storage; otherwise the T
70// is copied. (BarrierCallback does not support `T`s that are neither movable
71// nor copyable.) If T is a reference, the reference is removed, and the
72// callback moves or copies the underlying value per the previously stated rule.
Dan McArdlea4afa602021-11-15 21:04:0273// Beware of creating dangling references. Types that contain references but are
74// not references themselves are not modified for callback storage, e.g.
75// `std::pair<int, const Foo&>`. Dangling references will be passed to
76// `done_callback` if the referenced `Foo` objects have already been deleted.
cfredricc8cdd1d2021-08-11 00:01:4877//
cfredriceaafc6b2021-07-27 21:12:0178// If `num_callbacks` is 0, `done_callback` is executed immediately.
cfredrica0464ebf2021-07-24 01:26:3979//
cfredricc8cdd1d2021-08-11 00:01:4880// `done_callback` may accept a `std::vector<T>`, `const std::vector<T>`, or
81// `const std::vector<T>&`.
82//
cfredrica0464ebf2021-07-24 01:26:3983// BarrierCallback is thread-safe - the internals are protected by a
84// `base::Lock`. `done_callback` will be run on the thread that calls the final
85// Run() on the returned callbacks, or the thread that constructed the
86// BarrierCallback (in the case where `num_callbacks` is 0).
87//
Roland Bock6269edb2022-01-04 19:34:1588// BarrierCallback is copyable. Copies share state.
89//
cfredrica0464ebf2021-07-24 01:26:3990// `done_callback` is also cleared on the thread that runs it (by virtue of
91// being a OnceCallback).
Roland Bock6269edb2022-01-04 19:34:1592//
93// See also
94// https://chromium.googlesource.com/chromium/src/+/HEAD/docs/callback.md
cfredricc8cdd1d2021-08-11 00:01:4895template <typename T,
96 typename RawArg = base::remove_cvref_t<T>,
97 typename DoneArg = std::vector<RawArg>,
98 template <typename>
99 class CallbackType,
100 typename std::enable_if<std::is_same<
101 std::vector<RawArg>,
102 base::remove_cvref_t<DoneArg>>::value>::type* = nullptr,
103 typename = base::EnableIfIsBaseCallback<CallbackType>>
cfredrica0464ebf2021-07-24 01:26:39104RepeatingCallback<void(T)> BarrierCallback(
105 size_t num_callbacks,
cfredricc8cdd1d2021-08-11 00:01:48106 CallbackType<void(DoneArg)> done_callback) {
cfredriceaafc6b2021-07-27 21:12:01107 if (num_callbacks == 0) {
108 std::move(done_callback).Run({});
cfredric8c677a12021-08-03 21:48:30109 return BindRepeating(&internal::ShouldNeverRun<T>);
cfredriceaafc6b2021-07-27 21:12:01110 }
cfredrica0464ebf2021-07-24 01:26:39111
cfredricc8cdd1d2021-08-11 00:01:48112 return BindRepeating(
113 &internal::BarrierCallbackInfo<T, DoneArg>::Run,
114 std::make_unique<internal::BarrierCallbackInfo<T, DoneArg>>(
115 num_callbacks, std::move(done_callback)));
cfredrica0464ebf2021-07-24 01:26:39116}
117
118} // namespace base
119
120#endif // BASE_BARRIER_CALLBACK_H_