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   // Unregister this handler from the thread and release resource. Unhandled events will be discarded and not executed.
42   virtual ~Handler();
43 
44   DISALLOW_COPY_AND_ASSIGN(Handler);
45 
46   // Enqueue a closure to the queue of this handler
47   virtual void Post(common::OnceClosure closure) override;
48 
49   // Remove all pending events from the queue of this handler
50   void Clear();
51 
52   // Die if the current reactable doesn't stop before the timeout.  Must be called after Clear()
53   void WaitUntilStopped(std::chrono::milliseconds timeout);
54 
55   template <typename Functor, typename... Args>
Call(Functor && functor,Args &&...args)56   void Call(Functor&& functor, Args&&... args) {
57     Post(common::BindOnce(std::forward<Functor>(functor), std::forward<Args>(args)...));
58   }
59 
60   template <typename T, typename Functor, typename... Args>
CallOn(T * obj,Functor && functor,Args &&...args)61   void CallOn(T* obj, Functor&& functor, Args&&... args) {
62     Post(common::BindOnce(std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...));
63   }
64 
65   template <typename Functor, typename... Args>
BindOnce(Functor && functor,Args &&...args)66   common::ContextualOnceCallback<common::MakeUnboundRunType<Functor, Args...>> BindOnce(
67       Functor&& functor, Args&&... args) {
68     return common::ContextualOnceCallback<common::MakeUnboundRunType<Functor, Args...>>(
69         common::BindOnce(std::forward<Functor>(functor), std::forward<Args>(args)...), this);
70   }
71 
72   template <typename Functor, typename T, typename... Args>
BindOnceOn(T * obj,Functor && functor,Args &&...args)73   common::ContextualOnceCallback<common::MakeUnboundRunType<Functor, T, Args...>> BindOnceOn(
74       T* obj, Functor&& functor, Args&&... args) {
75     return common::ContextualOnceCallback<common::MakeUnboundRunType<Functor, T, Args...>>(
76         common::BindOnce(std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...), this);
77   }
78 
79   template <typename Functor, typename... Args>
Bind(Functor && functor,Args &&...args)80   common::ContextualCallback<common::MakeUnboundRunType<Functor, Args...>> Bind(Functor&& functor, Args&&... args) {
81     return common::ContextualCallback<common::MakeUnboundRunType<Functor, Args...>>(
82         common::Bind(std::forward<Functor>(functor), std::forward<Args>(args)...), this);
83   }
84 
85   template <typename Functor, typename T, typename... Args>
BindOn(T * obj,Functor && functor,Args &&...args)86   common::ContextualCallback<common::MakeUnboundRunType<Functor, T, Args...>> BindOn(
87       T* obj, Functor&& functor, Args&&... args) {
88     return common::ContextualCallback<common::MakeUnboundRunType<Functor, T, Args...>>(
89         common::Bind(std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...), this);
90   }
91 
92   template <typename T>
93   friend class Queue;
94 
95   friend class Alarm;
96 
97   friend class RepeatingAlarm;
98 
99  private:
was_cleared()100   inline bool was_cleared() const {
101     return tasks_ == nullptr;
102   };
103   std::queue<common::OnceClosure>* tasks_;
104   Thread* thread_;
105   int fd_;
106   Reactor::Reactable* reactable_;
107   mutable std::mutex mutex_;
108   void handle_next_event();
109 };
110 
111 }  // namespace os
112 }  // namespace bluetooth
113