cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors. All rights reserved. |
| 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 | |
| 13 | #include "base/bind.h" |
| 14 | #include "base/callback.h" |
| 15 | #include "base/callback_helpers.h" |
| 16 | #include "base/synchronization/lock.h" |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 17 | #include "base/template_util.h" |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 18 | #include "base/thread_annotations.h" |
| 19 | |
| 20 | namespace base { |
| 21 | |
cfredric | 8c677a1 | 2021-08-03 21:48:30 | [diff] [blame] | 22 | namespace internal { |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 23 | |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 24 | template <typename T, typename DoneArg> |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 25 | class BarrierCallbackInfo { |
| 26 | public: |
| 27 | BarrierCallbackInfo(size_t num_callbacks, |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 28 | OnceCallback<void(DoneArg)> done_callback) |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 29 | : num_callbacks_left_(num_callbacks), |
| 30 | done_callback_(std::move(done_callback)) { |
| 31 | results_.reserve(num_callbacks); |
| 32 | } |
| 33 | |
| 34 | void Run(T t) LOCKS_EXCLUDED(mutex_) { |
| 35 | base::ReleasableAutoLock lock(&mutex_); |
| 36 | DCHECK_NE(num_callbacks_left_, 0U); |
| 37 | results_.push_back(std::move(t)); |
| 38 | --num_callbacks_left_; |
| 39 | |
| 40 | if (num_callbacks_left_ == 0) { |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 41 | std::vector<base::remove_cvref_t<T>> results = std::move(results_); |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 42 | lock.Release(); |
| 43 | std::move(done_callback_).Run(std::move(results)); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | Lock mutex_; |
| 49 | size_t num_callbacks_left_ GUARDED_BY(mutex_); |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 50 | std::vector<base::remove_cvref_t<T>> results_ GUARDED_BY(mutex_); |
| 51 | OnceCallback<void(DoneArg)> done_callback_; |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 52 | }; |
| 53 | |
cfredric | eaafc6b | 2021-07-27 21:12:01 | [diff] [blame] | 54 | template <typename T> |
| 55 | void ShouldNeverRun(T t) { |
| 56 | CHECK(false); |
| 57 | } |
| 58 | |
cfredric | 8c677a1 | 2021-08-03 21:48:30 | [diff] [blame] | 59 | } // namespace internal |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 60 | |
| 61 | // BarrierCallback<T> is an analog of BarrierClosure for which each `Run()` |
| 62 | // invocation takes a `T` as an argument. After `num_callbacks` such |
| 63 | // invocations, BarrierCallback invokes `Run()` on its `done_callback`, passing |
| 64 | // the vector of `T`s as an argument. (The ordering of the vector is |
| 65 | // unspecified.) |
| 66 | // |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 67 | // `T`s that are movable are moved into the callback's storage; otherwise the T |
| 68 | // is copied. (BarrierCallback does not support `T`s that are neither movable |
| 69 | // nor copyable.) If T is a reference, the reference is removed, and the |
| 70 | // callback moves or copies the underlying value per the previously stated rule. |
| 71 | // |
cfredric | eaafc6b | 2021-07-27 21:12:01 | [diff] [blame] | 72 | // If `num_callbacks` is 0, `done_callback` is executed immediately. |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 73 | // |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 74 | // `done_callback` may accept a `std::vector<T>`, `const std::vector<T>`, or |
| 75 | // `const std::vector<T>&`. |
| 76 | // |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 77 | // BarrierCallback is thread-safe - the internals are protected by a |
| 78 | // `base::Lock`. `done_callback` will be run on the thread that calls the final |
| 79 | // Run() on the returned callbacks, or the thread that constructed the |
| 80 | // BarrierCallback (in the case where `num_callbacks` is 0). |
| 81 | // |
| 82 | // `done_callback` is also cleared on the thread that runs it (by virtue of |
| 83 | // being a OnceCallback). |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 84 | template <typename T, |
| 85 | typename RawArg = base::remove_cvref_t<T>, |
| 86 | typename DoneArg = std::vector<RawArg>, |
| 87 | template <typename> |
| 88 | class CallbackType, |
| 89 | typename std::enable_if<std::is_same< |
| 90 | std::vector<RawArg>, |
| 91 | base::remove_cvref_t<DoneArg>>::value>::type* = nullptr, |
| 92 | typename = base::EnableIfIsBaseCallback<CallbackType>> |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 93 | RepeatingCallback<void(T)> BarrierCallback( |
| 94 | size_t num_callbacks, |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 95 | CallbackType<void(DoneArg)> done_callback) { |
cfredric | eaafc6b | 2021-07-27 21:12:01 | [diff] [blame] | 96 | if (num_callbacks == 0) { |
| 97 | std::move(done_callback).Run({}); |
cfredric | 8c677a1 | 2021-08-03 21:48:30 | [diff] [blame] | 98 | return BindRepeating(&internal::ShouldNeverRun<T>); |
cfredric | eaafc6b | 2021-07-27 21:12:01 | [diff] [blame] | 99 | } |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 100 | |
cfredric | c8cdd1d | 2021-08-11 00:01:48 | [diff] [blame] | 101 | return BindRepeating( |
| 102 | &internal::BarrierCallbackInfo<T, DoneArg>::Run, |
| 103 | std::make_unique<internal::BarrierCallbackInfo<T, DoneArg>>( |
| 104 | num_callbacks, std::move(done_callback))); |
cfredric | a0464ebf | 2021-07-24 01:26:39 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } // namespace base |
| 108 | |
| 109 | #endif // BASE_BARRIER_CALLBACK_H_ |