diff options
author | 2024-03-19 18:36:45 -0700 | |
---|---|---|
committer | 2024-03-21 20:44:24 +0000 | |
commit | a229634d83e639a2a1d743267e94af752a3cecd3 (patch) | |
tree | 7d2b0e9fea6d7599927bec98e831a432e59ab08c | |
parent | 9fb7ecca2828aa50145419c505ce7b11e840cfbc (diff) |
Extract bindable context from postable model
Bug: 330419766
Test: m .
Flag: EXEMPT, Mechanical Refactor
Change-Id: I2ebd19c1950b16fb9e3a87912e5682d2ed5b5fc2
-rw-r--r-- | system/btif/include/btif_jni_task.h | 3 | ||||
-rw-r--r-- | system/btif/src/btif_jni_task.cc | 3 | ||||
-rw-r--r-- | system/common/message_loop_thread.cc | 4 | ||||
-rw-r--r-- | system/common/message_loop_thread.h | 40 | ||||
-rw-r--r-- | system/gd/common/postable_context.h | 58 | ||||
-rw-r--r-- | system/gd/os/handler.cc | 3 | ||||
-rw-r--r-- | system/gd/os/handler.h | 30 | ||||
-rw-r--r-- | system/stack/btu/main_thread.cc | 3 | ||||
-rw-r--r-- | system/stack/include/main_thread.h | 4 |
9 files changed, 82 insertions, 66 deletions
diff --git a/system/btif/include/btif_jni_task.h b/system/btif/include/btif_jni_task.h index e2771db2a4..3b3f17db3e 100644 --- a/system/btif/include/btif_jni_task.h +++ b/system/btif/include/btif_jni_task.h @@ -17,6 +17,7 @@ #pragma once #include "btif/include/btif_common.h" +#include "common/postable_context.h" #include "include/hardware/bluetooth.h" void jni_thread_startup(); @@ -55,3 +56,5 @@ bt_status_t do_in_jni_thread(base::OnceClosure task); bool is_on_jni_thread(); void post_on_bt_jni(BtJniClosure closure); + +bluetooth::common::PostableContext* get_jni(); diff --git a/system/btif/src/btif_jni_task.cc b/system/btif/src/btif_jni_task.cc index 083515c872..9be6e18525 100644 --- a/system/btif/src/btif_jni_task.cc +++ b/system/btif/src/btif_jni_task.cc @@ -26,6 +26,7 @@ #include <utility> #include "common/message_loop_thread.h" +#include "common/postable_context.h" #include "include/hardware/bluetooth.h" #include "osi/include/allocator.h" #include "stack/include/bt_types.h" @@ -124,3 +125,5 @@ void post_on_bt_jni(BtJniClosure closure) { std::move(closure))) == BT_STATUS_SUCCESS); } + +bluetooth::common::PostableContext* get_jni() { return jni_thread.Postable(); } diff --git a/system/common/message_loop_thread.cc b/system/common/message_loop_thread.cc index faca7f0754..9eb305f5c2 100644 --- a/system/common/message_loop_thread.cc +++ b/system/common/message_loop_thread.cc @@ -29,6 +29,8 @@ #include <string> #include <thread> +#include "common/postable_context.h" + namespace bluetooth { namespace common { @@ -215,5 +217,7 @@ void MessageLoopThread::Post(base::OnceClosure closure) { DoInThread(FROM_HERE, std::move(closure)); } +PostableContext* MessageLoopThread::Postable() { return this; } + } // namespace common } // namespace bluetooth diff --git a/system/common/message_loop_thread.h b/system/common/message_loop_thread.h index 525fcf2ba7..5c8b00efa8 100644 --- a/system/common/message_loop_thread.h +++ b/system/common/message_loop_thread.h @@ -27,8 +27,7 @@ #include <thread> #include "abstract_message_loop.h" -#include "common/contextual_callback.h" -#include "common/i_postable_context.h" +#include "common/postable_context.h" namespace bluetooth { @@ -37,7 +36,7 @@ namespace common { /** * An interface to various thread related functionality */ -class MessageLoopThread final : public IPostableContext { +class MessageLoopThread final : public PostableContext { public: /** * Create a message loop thread with name. Thread won't be running until @@ -173,37 +172,10 @@ class MessageLoopThread final : public IPostableContext { */ void Post(base::OnceClosure closure) override; - template <typename Functor, typename... Args> - auto BindOnce(Functor&& functor, Args&&... args) { - return common::ContextualOnceCallback( - common::BindOnce(std::forward<Functor>(functor), - std::forward<Args>(args)...), - this); - } - - template <typename Functor, typename T, typename... Args> - auto BindOnceOn(T* obj, Functor&& functor, Args&&... args) { - return common::ContextualOnceCallback( - common::BindOnce(std::forward<Functor>(functor), - common::Unretained(obj), std::forward<Args>(args)...), - this); - } - - template <typename Functor, typename... Args> - auto Bind(Functor&& functor, Args&&... args) { - return common::ContextualCallback( - common::Bind(std::forward<Functor>(functor), - std::forward<Args>(args)...), - this); - } - - template <typename Functor, typename T, typename... Args> - auto BindOn(T* obj, Functor&& functor, Args&&... args) { - return common::ContextualCallback( - common::Bind(std::forward<Functor>(functor), common::Unretained(obj), - std::forward<Args>(args)...), - this); - } + /** + * Returns a postable object + */ + PostableContext* Postable(); private: /** diff --git a/system/gd/common/postable_context.h b/system/gd/common/postable_context.h new file mode 100644 index 0000000000..e44850cd85 --- /dev/null +++ b/system/gd/common/postable_context.h @@ -0,0 +1,58 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "common/bind.h" +#include "common/contextual_callback.h" +#include "common/i_postable_context.h" + +namespace bluetooth::common { + +class PostableContext : public IPostableContext { + public: + virtual ~PostableContext() = default; + + template <typename Functor, typename... Args> + auto BindOnce(Functor&& functor, Args&&... args) { + return common::ContextualOnceCallback( + common::BindOnce(std::forward<Functor>(functor), std::forward<Args>(args)...), this); + } + + template <typename Functor, typename T, typename... Args> + auto BindOnceOn(T* obj, Functor&& functor, Args&&... args) { + return common::ContextualOnceCallback( + common::BindOnce( + std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...), + this); + } + + template <typename Functor, typename... Args> + auto Bind(Functor&& functor, Args&&... args) { + return common::ContextualCallback( + common::Bind(std::forward<Functor>(functor), std::forward<Args>(args)...), this); + } + + template <typename Functor, typename T, typename... Args> + auto BindOn(T* obj, Functor&& functor, Args&&... args) { + return common::ContextualCallback( + common::Bind( + std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...), + this); + } +}; + +} // namespace bluetooth::common diff --git a/system/gd/os/handler.cc b/system/gd/os/handler.cc index 1443df6ced..bbb00058ec 100644 --- a/system/gd/os/handler.cc +++ b/system/gd/os/handler.cc @@ -18,13 +18,10 @@ #include <bluetooth/log.h> -#include <cstring> - #include "common/bind.h" #include "common/callback.h" #include "os/log.h" #include "os/reactor.h" -#include "os/utils.h" namespace bluetooth { namespace os { diff --git a/system/gd/os/handler.h b/system/gd/os/handler.h index 2609ea8486..03f65e5d37 100644 --- a/system/gd/os/handler.h +++ b/system/gd/os/handler.h @@ -16,16 +16,14 @@ #pragma once -#include <functional> #include <memory> #include <mutex> #include <queue> #include "common/bind.h" #include "common/callback.h" -#include "common/contextual_callback.h" +#include "common/postable_context.h" #include "os/thread.h" -#include "os/utils.h" namespace bluetooth { namespace os { @@ -33,7 +31,7 @@ namespace os { // A message-queue style handler for reactor-based thread to handle incoming events from different threads. When it's // constructed, it will register a reactable on the specified thread; when it's destroyed, it will unregister itself // from the thread. -class Handler : public common::IPostableContext { +class Handler : public common::PostableContext { public: // Create and register a handler on given thread explicit Handler(Thread* thread); @@ -63,30 +61,6 @@ class Handler : public common::IPostableContext { Post(common::BindOnce(std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...)); } - template <typename Functor, typename... Args> - auto BindOnce(Functor&& functor, Args&&... args) { - return common::ContextualOnceCallback( - common::BindOnce(std::forward<Functor>(functor), std::forward<Args>(args)...), this); - } - - template <typename Functor, typename T, typename... Args> - auto BindOnceOn(T* obj, Functor&& functor, Args&&... args) { - return common::ContextualOnceCallback( - common::BindOnce(std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...), this); - } - - template <typename Functor, typename... Args> - auto Bind(Functor&& functor, Args&&... args) { - return common::ContextualCallback( - common::Bind(std::forward<Functor>(functor), std::forward<Args>(args)...), this); - } - - template <typename Functor, typename T, typename... Args> - auto BindOn(T* obj, Functor&& functor, Args&&... args) { - return common::ContextualCallback( - common::Bind(std::forward<Functor>(functor), common::Unretained(obj), std::forward<Args>(args)...), this); - } - template <typename T> friend class Queue; diff --git a/system/stack/btu/main_thread.cc b/system/stack/btu/main_thread.cc index a2cdd7bac6..b53ffd33fd 100644 --- a/system/stack/btu/main_thread.cc +++ b/system/stack/btu/main_thread.cc @@ -36,6 +36,9 @@ using namespace bluetooth; static MessageLoopThread main_thread("bt_main_thread"); bluetooth::common::MessageLoopThread* get_main_thread() { return &main_thread; } +bluetooth::common::PostableContext* get_main() { + return main_thread.Postable(); +} bt_status_t do_in_main_thread(const base::Location& from_here, base::OnceClosure task) { diff --git a/system/stack/include/main_thread.h b/system/stack/include/main_thread.h index 5c3b124048..b8725d279f 100644 --- a/system/stack/include/main_thread.h +++ b/system/stack/include/main_thread.h @@ -21,12 +21,14 @@ #include <base/threading/thread.h> #include "common/message_loop_thread.h" +#include "common/postable_context.h" #include "include/hardware/bluetooth.h" using BtMainClosure = std::function<void()>; -using bluetooth::common::MessageLoopThread; bluetooth::common::MessageLoopThread* get_main_thread(); +bluetooth::common::PostableContext* get_main(); + bt_status_t do_in_main_thread(const base::Location& from_here, base::OnceClosure task); bt_status_t do_in_main_thread_delayed(const base::Location& from_here, |