123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #ifndef RTC_BASE_OPERATIONS_CHAIN_H_
- #define RTC_BASE_OPERATIONS_CHAIN_H_
- #include <functional>
- #include <memory>
- #include <queue>
- #include <set>
- #include <type_traits>
- #include <utility>
- #include "api/scoped_refptr.h"
- #include "rtc_base/checks.h"
- #include "rtc_base/constructor_magic.h"
- #include "rtc_base/ref_count.h"
- #include "rtc_base/ref_counted_object.h"
- #include "rtc_base/synchronization/sequence_checker.h"
- namespace rtc {
- namespace rtc_operations_chain_internal {
- class Operation {
- public:
- virtual ~Operation() {}
- virtual void Run() = 0;
- };
- template <typename FunctorT>
- class OperationWithFunctor final : public Operation {
- public:
- OperationWithFunctor(FunctorT&& functor, std::function<void()> callback)
- : functor_(std::forward<FunctorT>(functor)),
- callback_(std::move(callback)) {}
- ~OperationWithFunctor() override { RTC_DCHECK(has_run_); }
- void Run() override {
- RTC_DCHECK(!has_run_);
- #ifdef RTC_DCHECK_IS_ON
- has_run_ = true;
- #endif
-
-
-
-
-
- auto functor = std::move(functor_);
- functor(std::move(callback_));
-
- }
- private:
- typename std::remove_reference<FunctorT>::type functor_;
- std::function<void()> callback_;
- #ifdef RTC_DCHECK_IS_ON
- bool has_run_ = false;
- #endif
- };
- }
- class OperationsChain final : public RefCountedObject<RefCountInterface> {
- public:
- static scoped_refptr<OperationsChain> Create();
- ~OperationsChain();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <typename FunctorT>
- void ChainOperation(FunctorT&& functor) {
- RTC_DCHECK_RUN_ON(&sequence_checker_);
- chained_operations_.push(
- std::make_unique<
- rtc_operations_chain_internal::OperationWithFunctor<FunctorT>>(
- std::forward<FunctorT>(functor), CreateOperationsChainCallback()));
-
-
-
- if (chained_operations_.size() == 1) {
- chained_operations_.front()->Run();
- }
- }
- private:
- friend class CallbackHandle;
-
-
-
-
-
- class CallbackHandle final : public RefCountedObject<RefCountInterface> {
- public:
- explicit CallbackHandle(scoped_refptr<OperationsChain> operations_chain);
- ~CallbackHandle();
- void OnOperationComplete();
- private:
- scoped_refptr<OperationsChain> operations_chain_;
- #ifdef RTC_DCHECK_IS_ON
- bool has_run_ = false;
- #endif
- RTC_DISALLOW_COPY_AND_ASSIGN(CallbackHandle);
- };
- OperationsChain();
- std::function<void()> CreateOperationsChainCallback();
- void OnOperationComplete();
- webrtc::SequenceChecker sequence_checker_;
-
-
-
- std::queue<std::unique_ptr<rtc_operations_chain_internal::Operation>>
- chained_operations_ RTC_GUARDED_BY(sequence_checker_);
- RTC_DISALLOW_COPY_AND_ASSIGN(OperationsChain);
- };
- }
- #endif
|