123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- #ifndef RTC_BASE_ASYNC_INVOKER_H_
- #define RTC_BASE_ASYNC_INVOKER_H_
- #include <atomic>
- #include <memory>
- #include <utility>
- #include "api/scoped_refptr.h"
- #include "rtc_base/async_invoker_inl.h"
- #include "rtc_base/bind.h"
- #include "rtc_base/constructor_magic.h"
- #include "rtc_base/event.h"
- #include "rtc_base/ref_counted_object.h"
- #include "rtc_base/third_party/sigslot/sigslot.h"
- #include "rtc_base/thread.h"
- namespace rtc {
- class AsyncInvoker : public MessageHandler {
- public:
- AsyncInvoker();
- ~AsyncInvoker() override;
-
-
- template <class ReturnT, class FunctorT>
- void AsyncInvoke(const Location& posted_from,
- Thread* thread,
- FunctorT&& functor,
- uint32_t id = 0) {
- std::unique_ptr<AsyncClosure> closure(
- new FireAndForgetAsyncClosure<FunctorT>(
- this, std::forward<FunctorT>(functor)));
- DoInvoke(posted_from, thread, std::move(closure), id);
- }
-
-
- template <class ReturnT, class FunctorT>
- void AsyncInvokeDelayed(const Location& posted_from,
- Thread* thread,
- FunctorT&& functor,
- uint32_t delay_ms,
- uint32_t id = 0) {
- std::unique_ptr<AsyncClosure> closure(
- new FireAndForgetAsyncClosure<FunctorT>(
- this, std::forward<FunctorT>(functor)));
- DoInvokeDelayed(posted_from, thread, std::move(closure), delay_ms, id);
- }
-
-
-
-
-
- void Flush(Thread* thread, uint32_t id = MQID_ANY);
-
-
-
- void Clear();
- private:
- void OnMessage(Message* msg) override;
- void DoInvoke(const Location& posted_from,
- Thread* thread,
- std::unique_ptr<AsyncClosure> closure,
- uint32_t id);
- void DoInvokeDelayed(const Location& posted_from,
- Thread* thread,
- std::unique_ptr<AsyncClosure> closure,
- uint32_t delay_ms,
- uint32_t id);
-
-
-
-
-
-
-
-
- std::atomic<int> pending_invocations_;
-
-
-
-
- scoped_refptr<RefCountedObject<Event>> invocation_complete_;
-
-
-
- std::atomic<bool> destroying_;
- friend class AsyncClosure;
- RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker);
- };
- class GuardedAsyncInvoker : public sigslot::has_slots<> {
- public:
- GuardedAsyncInvoker();
- ~GuardedAsyncInvoker() override;
-
-
-
-
- bool Flush(uint32_t id = MQID_ANY);
-
-
- template <class ReturnT, class FunctorT>
- bool AsyncInvoke(const Location& posted_from,
- FunctorT&& functor,
- uint32_t id = 0) {
- CritScope cs(&crit_);
- if (thread_ == nullptr)
- return false;
- invoker_.AsyncInvoke<ReturnT, FunctorT>(
- posted_from, thread_, std::forward<FunctorT>(functor), id);
- return true;
- }
-
-
- template <class ReturnT, class FunctorT>
- bool AsyncInvokeDelayed(const Location& posted_from,
- FunctorT&& functor,
- uint32_t delay_ms,
- uint32_t id = 0) {
- CritScope cs(&crit_);
- if (thread_ == nullptr)
- return false;
- invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(
- posted_from, thread_, std::forward<FunctorT>(functor), delay_ms, id);
- return true;
- }
-
-
- template <class ReturnT, class FunctorT, class HostT>
- bool AsyncInvoke(const Location& posted_from,
- const Location& callback_posted_from,
- FunctorT&& functor,
- void (HostT::*callback)(ReturnT),
- HostT* callback_host,
- uint32_t id = 0) {
- CritScope cs(&crit_);
- if (thread_ == nullptr)
- return false;
- invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
- posted_from, callback_posted_from, thread_,
- std::forward<FunctorT>(functor), callback, callback_host, id);
- return true;
- }
-
-
- template <class ReturnT, class FunctorT, class HostT>
- bool AsyncInvoke(const Location& posted_from,
- const Location& callback_posted_from,
- FunctorT&& functor,
- void (HostT::*callback)(),
- HostT* callback_host,
- uint32_t id = 0) {
- CritScope cs(&crit_);
- if (thread_ == nullptr)
- return false;
- invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
- posted_from, callback_posted_from, thread_,
- std::forward<FunctorT>(functor), callback, callback_host, id);
- return true;
- }
- private:
-
- void ThreadDestroyed();
- CriticalSection crit_;
- Thread* thread_ RTC_GUARDED_BY(crit_);
- AsyncInvoker invoker_ RTC_GUARDED_BY(crit_);
- };
- }
- #endif
|