blob: 4398bb16c1fc7a3ba9019b211b7e8e313c9b9af0 [file] [log] [blame]
Henrik Boström27c29362019-10-21 13:21:551/*
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öller105711e2022-06-14 13:48:2613#include "api/make_ref_counted.h"
Henrik Boström27c29362019-10-21 13:21:5514#include "rtc_base/checks.h"
15
16namespace rtc {
17
18OperationsChain::CallbackHandle::CallbackHandle(
19 scoped_refptr<OperationsChain> operations_chain)
20 : operations_chain_(std::move(operations_chain)) {}
21
22OperationsChain::CallbackHandle::~CallbackHandle() {
Tomas Gunnarsson36992362020-10-05 19:41:3623#if RTC_DCHECK_IS_ON
Henrik Boström27c29362019-10-21 13:21:5524 RTC_DCHECK(has_run_);
Tomas Gunnarsson36992362020-10-05 19:41:3625#endif
Henrik Boström27c29362019-10-21 13:21:5526}
27
28void OperationsChain::CallbackHandle::OnOperationComplete() {
Tomas Gunnarsson36992362020-10-05 19:41:3629#if RTC_DCHECK_IS_ON
Henrik Boström27c29362019-10-21 13:21:5530 RTC_DCHECK(!has_run_);
Henrik Boström27c29362019-10-21 13:21:5531 has_run_ = true;
32#endif // RTC_DCHECK_IS_ON
33 operations_chain_->OnOperationComplete();
Artem Titov96e3b992021-07-26 14:03:1434 // We have no reason to keep the `operations_chain_` alive through reference
Henrik Boström27c29362019-10-21 13:21:5535 // counting anymore.
36 operations_chain_ = nullptr;
37}
38
39// static
40scoped_refptr<OperationsChain> OperationsChain::Create() {
Niels Möller95129102022-01-13 10:00:0541 // Explicit new, to access private constructor.
42 return rtc::scoped_refptr<OperationsChain>(new OperationsChain());
Henrik Boström27c29362019-10-21 13:21:5543}
44
Niels Möller95129102022-01-13 10:00:0545OperationsChain::OperationsChain() {
Henrik Boström27c29362019-10-21 13:21:5546 RTC_DCHECK_RUN_ON(&sequence_checker_);
47}
48
49OperationsChain::~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öme574a312020-08-25 08:20:1156void 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
62bool OperationsChain::IsEmpty() const {
63 RTC_DCHECK_RUN_ON(&sequence_checker_);
64 return chained_operations_.empty();
65}
66
Henrik Boström27c29362019-10-21 13:21:5567std::function<void()> OperationsChain::CreateOperationsChainCallback() {
Niels Möller95129102022-01-13 10:00:0568 return [handle = rtc::make_ref_counted<CallbackHandle>(
69 rtc::scoped_refptr<OperationsChain>(this))]() {
70 handle->OnOperationComplete();
71 };
Henrik Boström27c29362019-10-21 13:21:5572}
73
74void 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öme574a312020-08-25 08:20:1179 // 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öm27c29362019-10-21 13:21:5581 if (!chained_operations_.empty()) {
82 chained_operations_.front()->Run();
Henrik Boströme574a312020-08-25 08:20:1183 } else if (on_chain_empty_callback_.has_value()) {
84 on_chain_empty_callback_.value()();
Henrik Boström27c29362019-10-21 13:21:5585 }
86}
87
88} // namespace rtc