Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 2 | // 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 | |
Daniel Cheng | 915d255 | 2023-11-13 22:25:13 | [diff] [blame] | 8 | #include <concepts> |
Sumaid Syed | 22f60eeb | 2021-08-26 05:16:26 | [diff] [blame] | 9 | #include <memory> |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 10 | #include <type_traits> |
Sumaid Syed | 22f60eeb | 2021-08-26 05:16:26 | [diff] [blame] | 11 | #include <utility> |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
David Sanders | 8cfb63a | 2022-04-14 19:36:30 | [diff] [blame] | 14 | #include "base/check.h" |
David Sanders | f84c9f4 | 2022-04-15 13:42:32 | [diff] [blame] | 15 | #include "base/check_op.h" |
Avi Drissman | 63e1f99 | 2023-01-13 18:54:43 | [diff] [blame] | 16 | #include "base/functional/bind.h" |
| 17 | #include "base/functional/callback.h" |
| 18 | #include "base/functional/callback_helpers.h" |
Peter Boström | 5411965 | 2024-11-14 00:16:38 | [diff] [blame] | 19 | #include "base/notreached.h" |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 20 | #include "base/synchronization/lock.h" |
| 21 | #include "base/thread_annotations.h" |
| 22 | |
| 23 | namespace base { |
| 24 | |
cfredric | 8c677a1 | 2021-08-03 21:48:30 | [diff] [blame] | 25 | namespace internal { |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 26 | |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 27 | template <typename T, typename DoneArg> |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 28 | class BarrierCallbackInfo { |
| 29 | public: |
| 30 | BarrierCallbackInfo(size_t num_callbacks, |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 31 | OnceCallback<void(DoneArg)> done_callback) |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 32 | : num_callbacks_left_(num_callbacks), |
| 33 | done_callback_(std::move(done_callback)) { |
| 34 | results_.reserve(num_callbacks); |
| 35 | } |
| 36 | |
| 37 | void Run(T t) LOCKS_EXCLUDED(mutex_) { |
| 38 | base::ReleasableAutoLock lock(&mutex_); |
| 39 | DCHECK_NE(num_callbacks_left_, 0U); |
| 40 | results_.push_back(std::move(t)); |
| 41 | --num_callbacks_left_; |
| 42 | |
| 43 | if (num_callbacks_left_ == 0) { |
punithnayak | 35a22f1 | 2023-12-11 20:24:42 | [diff] [blame] | 44 | std::vector<std::remove_cvref_t<T>> results = std::move(results_); |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 45 | lock.Release(); |
| 46 | std::move(done_callback_).Run(std::move(results)); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | Lock mutex_; |
| 52 | size_t num_callbacks_left_ GUARDED_BY(mutex_); |
punithnayak | 35a22f1 | 2023-12-11 20:24:42 | [diff] [blame] | 53 | std::vector<std::remove_cvref_t<T>> results_ GUARDED_BY(mutex_); |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 54 | OnceCallback<void(DoneArg)> done_callback_; |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 55 | }; |
| 56 | |
cfredric | eaafc6b | 2021-07-27 21:12:01 | [diff] [blame] | 57 | template <typename T> |
| 58 | void ShouldNeverRun(T t) { |
Peter Boström | 5411965 | 2024-11-14 00:16:38 | [diff] [blame] | 59 | NOTREACHED(); |
cfredric | eaafc6b | 2021-07-27 21:12:01 | [diff] [blame] | 60 | } |
| 61 | |
cfredric | 8c677a1 | 2021-08-03 21:48:30 | [diff] [blame] | 62 | } // namespace internal |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 63 | |
| 64 | // BarrierCallback<T> is an analog of BarrierClosure for which each `Run()` |
| 65 | // invocation takes a `T` as an argument. After `num_callbacks` such |
| 66 | // invocations, BarrierCallback invokes `Run()` on its `done_callback`, passing |
| 67 | // the vector of `T`s as an argument. (The ordering of the vector is |
| 68 | // unspecified.) |
| 69 | // |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 70 | // `T`s that are movable are moved into the callback's storage; otherwise the T |
| 71 | // is copied. (BarrierCallback does not support `T`s that are neither movable |
| 72 | // nor copyable.) If T is a reference, the reference is removed, and the |
| 73 | // callback moves or copies the underlying value per the previously stated rule. |
Dan McArdle | a4afa60 | 2021-11-15 21:04:02 | [diff] [blame] | 74 | // Beware of creating dangling references. Types that contain references but are |
| 75 | // not references themselves are not modified for callback storage, e.g. |
| 76 | // `std::pair<int, const Foo&>`. Dangling references will be passed to |
| 77 | // `done_callback` if the referenced `Foo` objects have already been deleted. |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 78 | // |
cfredric | eaafc6b | 2021-07-27 21:12:01 | [diff] [blame] | 79 | // If `num_callbacks` is 0, `done_callback` is executed immediately. |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 80 | // |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 81 | // `done_callback` may accept a `std::vector<T>`, `const std::vector<T>`, or |
| 82 | // `const std::vector<T>&`. |
| 83 | // |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 84 | // BarrierCallback is thread-safe - the internals are protected by a |
| 85 | // `base::Lock`. `done_callback` will be run on the thread that calls the final |
| 86 | // Run() on the returned callbacks, or the thread that constructed the |
| 87 | // BarrierCallback (in the case where `num_callbacks` is 0). |
| 88 | // |
Roland Bock | 6269edb | 2022-01-04 19:34:15 | [diff] [blame] | 89 | // BarrierCallback is copyable. Copies share state. |
| 90 | // |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 91 | // `done_callback` is also cleared on the thread that runs it (by virtue of |
| 92 | // being a OnceCallback). |
Roland Bock | 6269edb | 2022-01-04 19:34:15 | [diff] [blame] | 93 | // |
| 94 | // See also |
| 95 | // https://chromium.googlesource.com/chromium/src/+/HEAD/docs/callback.md |
Daniel Cheng | 915d255 | 2023-11-13 22:25:13 | [diff] [blame] | 96 | template <typename T, |
punithnayak | 35a22f1 | 2023-12-11 20:24:42 | [diff] [blame] | 97 | typename RawArg = std::remove_cvref_t<T>, |
Daniel Cheng | 915d255 | 2023-11-13 22:25:13 | [diff] [blame] | 98 | typename DoneArg = std::vector<RawArg>, |
| 99 | template <typename> |
| 100 | class CallbackType> |
| 101 | requires(std::same_as<std::vector<RawArg>, std::remove_cvref_t<DoneArg>> && |
| 102 | IsBaseCallback<CallbackType<void()>>) |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 103 | RepeatingCallback<void(T)> BarrierCallback( |
| 104 | size_t num_callbacks, |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 105 | CallbackType<void(DoneArg)> done_callback) { |
cfredric | eaafc6b | 2021-07-27 21:12:01 | [diff] [blame] | 106 | if (num_callbacks == 0) { |
| 107 | std::move(done_callback).Run({}); |
cfredric | 8c677a1 | 2021-08-03 21:48:30 | [diff] [blame] | 108 | return BindRepeating(&internal::ShouldNeverRun<T>); |
cfredric | eaafc6b | 2021-07-27 21:12:01 | [diff] [blame] | 109 | } |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 110 | |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 111 | return BindRepeating( |
| 112 | &internal::BarrierCallbackInfo<T, DoneArg>::Run, |
| 113 | std::make_unique<internal::BarrierCallbackInfo<T, DoneArg>>( |
| 114 | num_callbacks, std::move(done_callback))); |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | } // namespace base |
| 118 | |
| 119 | #endif // BASE_BARRIER_CALLBACK_H_ |