1 /* 2 * Copyright 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <functional> 20 #include <memory> 21 #include <mutex> 22 #include <queue> 23 24 #include "common/bind.h" 25 #include "common/callback.h" 26 #include "common/contextual_callback.h" 27 #include "os/thread.h" 28 #include "os/utils.h" 29 30 namespace bluetooth { 31 namespace os { 32 33 // A message-queue style handler for reactor-based thread to handle incoming events from different threads. When it's 34 // constructed, it will register a reactable on the specified thread; when it's destroyed, it will unregister itself 35 // from the thread. 36 class Handler : public common::IPostableContext { 37 public: 38 // Create and register a handler on given thread 39 explicit Handler(Thread* thread); 40 41 Handler(const Handler&) = delete; 42 Handler& operator=(const Handler&) = delete; 43 44 // Unregister this handler from the thread and release resource. Unhandled events will be discarded and not executed. 45 virtual ~Handler(); 46 47 // Enqueue a closure to the queue of this handler 48 virtual void Post(common::OnceClosure closure) override; 49 50 // Remove all pending events from the queue of this handler 51 void Clear(); 52 53 // Die if the current reactable doesn't stop before the timeout. Must be called after Clear() 54 void WaitUntilStopped(std::chrono::milliseconds timeout); 55 56 template <typename Functor, typename... Args> Call(Functor && functor,Args &&...args)57 void Call(Functor&& functor, Args&&... args) { 58 Post(common::BindOnce(std::forward<Functor>(functor), std::forward<Args>(args)...)); 59 } 60 61 template <typename T, typename Functor, typename... Args> CallOn(T * obj,Functor && functor,Args &&...args)62 void CallOn(T* obj, Functor&& functor, Args&&... args) { 63 Post(common::BindOnce(std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...)); 64 } 65 66 template <typename Functor, typename... Args> BindOnce(Functor && functor,Args &&...args)67 common::ContextualOnceCallback<common::MakeUnboundRunType<Functor, Args...>> BindOnce( 68 Functor&& functor, Args&&... args) { 69 return common::ContextualOnceCallback<common::MakeUnboundRunType<Functor, Args...>>( 70 common::BindOnce(std::forward<Functor>(functor), std::forward<Args>(args)...), this); 71 } 72 73 template <typename Functor, typename T, typename... Args> BindOnceOn(T * obj,Functor && functor,Args &&...args)74 common::ContextualOnceCallback<common::MakeUnboundRunType<Functor, T, Args...>> BindOnceOn( 75 T* obj, Functor&& functor, Args&&... args) { 76 return common::ContextualOnceCallback<common::MakeUnboundRunType<Functor, T, Args...>>( 77 common::BindOnce(std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...), this); 78 } 79 80 template <typename Functor, typename... Args> Bind(Functor && functor,Args &&...args)81 common::ContextualCallback<common::MakeUnboundRunType<Functor, Args...>> Bind(Functor&& functor, Args&&... args) { 82 return common::ContextualCallback<common::MakeUnboundRunType<Functor, Args...>>( 83 common::Bind(std::forward<Functor>(functor), std::forward<Args>(args)...), this); 84 } 85 86 template <typename Functor, typename T, typename... Args> BindOn(T * obj,Functor && functor,Args &&...args)87 common::ContextualCallback<common::MakeUnboundRunType<Functor, T, Args...>> BindOn( 88 T* obj, Functor&& functor, Args&&... args) { 89 return common::ContextualCallback<common::MakeUnboundRunType<Functor, T, Args...>>( 90 common::Bind(std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...), this); 91 } 92 93 template <typename T> 94 friend class Queue; 95 96 friend class Alarm; 97 98 friend class RepeatingAlarm; 99 100 private: was_cleared()101 inline bool was_cleared() const { 102 return tasks_ == nullptr; 103 }; 104 std::queue<common::OnceClosure>* tasks_; 105 Thread* thread_; 106 std::unique_ptr<Reactor::Event> event_; 107 Reactor::Reactable* reactable_; 108 mutable std::mutex mutex_; 109 void handle_next_event(); 110 }; 111 112 } // namespace os 113 } // namespace bluetooth 114