Henrik Boström | 27c2936 | 2019-10-21 13:21:55 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "rtc_base/operations_chain.h" |
| 12 | |
Niels Möller | 105711e | 2022-06-14 13:48:26 | [diff] [blame] | 13 | #include "api/make_ref_counted.h" |
Henrik Boström | 27c2936 | 2019-10-21 13:21:55 | [diff] [blame] | 14 | #include "rtc_base/checks.h" |
| 15 | |
| 16 | namespace rtc { |
| 17 | |
| 18 | OperationsChain::CallbackHandle::CallbackHandle( |
| 19 | scoped_refptr<OperationsChain> operations_chain) |
| 20 | : operations_chain_(std::move(operations_chain)) {} |
| 21 | |
| 22 | OperationsChain::CallbackHandle::~CallbackHandle() { |
Tomas Gunnarsson | 3699236 | 2020-10-05 19:41:36 | [diff] [blame] | 23 | #if RTC_DCHECK_IS_ON |
Henrik Boström | 27c2936 | 2019-10-21 13:21:55 | [diff] [blame] | 24 | RTC_DCHECK(has_run_); |
Tomas Gunnarsson | 3699236 | 2020-10-05 19:41:36 | [diff] [blame] | 25 | #endif |
Henrik Boström | 27c2936 | 2019-10-21 13:21:55 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void OperationsChain::CallbackHandle::OnOperationComplete() { |
Tomas Gunnarsson | 3699236 | 2020-10-05 19:41:36 | [diff] [blame] | 29 | #if RTC_DCHECK_IS_ON |
Henrik Boström | 27c2936 | 2019-10-21 13:21:55 | [diff] [blame] | 30 | RTC_DCHECK(!has_run_); |
Henrik Boström | 27c2936 | 2019-10-21 13:21:55 | [diff] [blame] | 31 | has_run_ = true; |
| 32 | #endif // RTC_DCHECK_IS_ON |
| 33 | operations_chain_->OnOperationComplete(); |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 34 | // We have no reason to keep the `operations_chain_` alive through reference |
Henrik Boström | 27c2936 | 2019-10-21 13:21:55 | [diff] [blame] | 35 | // counting anymore. |
| 36 | operations_chain_ = nullptr; |
| 37 | } |
| 38 | |
| 39 | // static |
| 40 | scoped_refptr<OperationsChain> OperationsChain::Create() { |
Niels Möller | 9512910 | 2022-01-13 10:00:05 | [diff] [blame] | 41 | // Explicit new, to access private constructor. |
| 42 | return rtc::scoped_refptr<OperationsChain>(new OperationsChain()); |
Henrik Boström | 27c2936 | 2019-10-21 13:21:55 | [diff] [blame] | 43 | } |
| 44 | |
Niels Möller | 9512910 | 2022-01-13 10:00:05 | [diff] [blame] | 45 | OperationsChain::OperationsChain() { |
Henrik Boström | 27c2936 | 2019-10-21 13:21:55 | [diff] [blame] | 46 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 47 | } |
| 48 | |
| 49 | OperationsChain::~OperationsChain() { |
| 50 | // Operations keep the chain alive through reference counting so this should |
| 51 | // not be possible. The fact that the chain is empty makes it safe to |
| 52 | // destroy the OperationsChain on any sequence. |
| 53 | RTC_DCHECK(chained_operations_.empty()); |
| 54 | } |
| 55 | |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 56 | void OperationsChain::SetOnChainEmptyCallback( |
| 57 | std::function<void()> on_chain_empty_callback) { |
| 58 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 59 | on_chain_empty_callback_ = std::move(on_chain_empty_callback); |
| 60 | } |
| 61 | |
| 62 | bool OperationsChain::IsEmpty() const { |
| 63 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 64 | return chained_operations_.empty(); |
| 65 | } |
| 66 | |
Henrik Boström | 27c2936 | 2019-10-21 13:21:55 | [diff] [blame] | 67 | std::function<void()> OperationsChain::CreateOperationsChainCallback() { |
Niels Möller | 9512910 | 2022-01-13 10:00:05 | [diff] [blame] | 68 | return [handle = rtc::make_ref_counted<CallbackHandle>( |
| 69 | rtc::scoped_refptr<OperationsChain>(this))]() { |
| 70 | handle->OnOperationComplete(); |
| 71 | }; |
Henrik Boström | 27c2936 | 2019-10-21 13:21:55 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void OperationsChain::OnOperationComplete() { |
| 75 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 76 | // The front element is the operation that just completed, remove it. |
| 77 | RTC_DCHECK(!chained_operations_.empty()); |
| 78 | chained_operations_.pop(); |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 79 | // If there are any other operations chained, execute the next one. Otherwise, |
| 80 | // invoke the "on chain empty" callback if it has been set. |
Henrik Boström | 27c2936 | 2019-10-21 13:21:55 | [diff] [blame] | 81 | if (!chained_operations_.empty()) { |
| 82 | chained_operations_.front()->Run(); |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 83 | } else if (on_chain_empty_callback_.has_value()) { |
| 84 | on_chain_empty_callback_.value()(); |
Henrik Boström | 27c2936 | 2019-10-21 13:21:55 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | } // namespace rtc |