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 | |
Sumaid Syed | 22f60eeb | 2021-08-26 05:16:26 | [diff] [blame] | 8 | #include <memory> |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 9 | #include <type_traits> |
Sumaid Syed | 22f60eeb | 2021-08-26 05:16:26 | [diff] [blame] | 10 | #include <utility> |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
David Sanders | 8cfb63a | 2022-04-14 19:36:30 | [diff] [blame] | 13 | #include "base/check.h" |
David Sanders | f84c9f4 | 2022-04-15 13:42:32 | [diff] [blame] | 14 | #include "base/check_op.h" |
Avi Drissman | 63e1f99 | 2023-01-13 18:54:43 | [diff] [blame] | 15 | #include "base/functional/bind.h" |
| 16 | #include "base/functional/callback.h" |
| 17 | #include "base/functional/callback_helpers.h" |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 18 | #include "base/synchronization/lock.h" |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 19 | #include "base/template_util.h" |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 20 | #include "base/thread_annotations.h" |
| 21 | |
| 22 | namespace base { |
| 23 | |
cfredric | 8c677a1 | 2021-08-03 21:48:30 | [diff] [blame] | 24 | namespace internal { |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 25 | |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 26 | template <typename T, typename DoneArg> |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 27 | class BarrierCallbackInfo { |
| 28 | public: |
| 29 | BarrierCallbackInfo(size_t num_callbacks, |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 30 | OnceCallback<void(DoneArg)> done_callback) |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 31 | : 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) { |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 43 | std::vector<base::remove_cvref_t<T>> results = std::move(results_); |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 44 | 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_); |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 52 | std::vector<base::remove_cvref_t<T>> results_ GUARDED_BY(mutex_); |
| 53 | OnceCallback<void(DoneArg)> done_callback_; |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 54 | }; |
| 55 | |
cfredric | eaafc6b | 2021-07-27 21:12:01 | [diff] [blame] | 56 | template <typename T> |
| 57 | void ShouldNeverRun(T t) { |
| 58 | CHECK(false); |
| 59 | } |
| 60 | |
cfredric | 8c677a1 | 2021-08-03 21:48:30 | [diff] [blame] | 61 | } // namespace internal |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 62 | |
| 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 | // |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 69 | // `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 McArdle | a4afa60 | 2021-11-15 21:04:02 | [diff] [blame] | 73 | // 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. |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 77 | // |
cfredric | eaafc6b | 2021-07-27 21:12:01 | [diff] [blame] | 78 | // If `num_callbacks` is 0, `done_callback` is executed immediately. |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 79 | // |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 80 | // `done_callback` may accept a `std::vector<T>`, `const std::vector<T>`, or |
| 81 | // `const std::vector<T>&`. |
| 82 | // |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 83 | // 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 Bock | 6269edb | 2022-01-04 19:34:15 | [diff] [blame] | 88 | // BarrierCallback is copyable. Copies share state. |
| 89 | // |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 90 | // `done_callback` is also cleared on the thread that runs it (by virtue of |
| 91 | // being a OnceCallback). |
Roland Bock | 6269edb | 2022-01-04 19:34:15 | [diff] [blame] | 92 | // |
| 93 | // See also |
| 94 | // https://chromium.googlesource.com/chromium/src/+/HEAD/docs/callback.md |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 95 | template <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>> |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 104 | RepeatingCallback<void(T)> BarrierCallback( |
| 105 | size_t num_callbacks, |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 106 | CallbackType<void(DoneArg)> done_callback) { |
cfredric | eaafc6b | 2021-07-27 21:12:01 | [diff] [blame] | 107 | if (num_callbacks == 0) { |
| 108 | std::move(done_callback).Run({}); |
cfredric | 8c677a1 | 2021-08-03 21:48:30 | [diff] [blame] | 109 | return BindRepeating(&internal::ShouldNeverRun<T>); |
cfredric | eaafc6b | 2021-07-27 21:12:01 | [diff] [blame] | 110 | } |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 111 | |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 112 | return BindRepeating( |
| 113 | &internal::BarrierCallbackInfo<T, DoneArg>::Run, |
| 114 | std::make_unique<internal::BarrierCallbackInfo<T, DoneArg>>( |
| 115 | num_callbacks, std::move(done_callback))); |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | } // namespace base |
| 119 | |
| 120 | #endif // BASE_BARRIER_CALLBACK_H_ |