summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Myles Watson <mylesgw@google.com> 2023-06-27 16:43:55 -0700
committer Myles Watson <mylesgw@google.com> 2023-06-30 17:18:45 +0000
commitb022e45464a1211896fa6b79d5f2e5b9c84226cd (patch)
tree0b73c772287160bbb5517279e148745aca154338
parentfc741c7202723622a0993b632fe381204d3e14b7 (diff)
Derive MessageLoopThread from IPostableContext
Allow construction of ContextualCallback for GD callbacks from legacy code. Bug: 289277431 Test: atest pts-bot Change-Id: I717ee13ae4083abe1f287e6bbcb537164c996615
-rw-r--r--system/common/message_loop_thread.cc4
-rw-r--r--system/common/message_loop_thread.h7
-rw-r--r--system/common/message_loop_thread_unittest.cc18
-rw-r--r--system/gd/common/contextual_callback.h7
-rw-r--r--system/gd/common/i_postable_context.h31
-rw-r--r--system/test/mock/mock_common_message_loop_thread.cc4
-rw-r--r--system/test/suite/Android.bp1
7 files changed, 64 insertions, 8 deletions
diff --git a/system/common/message_loop_thread.cc b/system/common/message_loop_thread.cc
index 2ce59c6203..e6921cc0ad 100644
--- a/system/common/message_loop_thread.cc
+++ b/system/common/message_loop_thread.cc
@@ -200,6 +200,10 @@ void MessageLoopThread::Run(std::promise<void> start_up_promise) {
}
}
+void MessageLoopThread::Post(base::OnceClosure closure) {
+ DoInThread(FROM_HERE, std::move(closure));
+}
+
} // namespace common
} // namespace bluetooth
diff --git a/system/common/message_loop_thread.h b/system/common/message_loop_thread.h
index 2bb3eeca4f..342cf15ace 100644
--- a/system/common/message_loop_thread.h
+++ b/system/common/message_loop_thread.h
@@ -28,6 +28,7 @@
#include <thread>
#include "abstract_message_loop.h"
+#include "gd/common/i_postable_context.h"
namespace bluetooth {
@@ -36,7 +37,7 @@ namespace common {
/**
* An interface to various thread related functionality
*/
-class MessageLoopThread final {
+class MessageLoopThread final : public IPostableContext {
public:
/**
* Create a message loop thread with name. Thread won't be running until
@@ -166,6 +167,10 @@ class MessageLoopThread final {
*/
bool DoInThreadDelayed(const base::Location& from_here,
base::OnceClosure task, const base::TimeDelta& delay);
+ /**
+ * Wrapper around DoInThread without a location.
+ */
+ void Post(base::OnceClosure closure) override;
private:
/**
diff --git a/system/common/message_loop_thread_unittest.cc b/system/common/message_loop_thread_unittest.cc
index c61c9046a7..80d64023ef 100644
--- a/system/common/message_loop_thread_unittest.cc
+++ b/system/common/message_loop_thread_unittest.cc
@@ -328,3 +328,21 @@ TEST_F(MessageLoopThreadTest, shut_down_start_up_multi_thread) {
auto thread = std::thread(&MessageLoopThread::StartUp, &message_loop_thread);
thread.join();
}
+
+// Verify that Post executes in order
+TEST_F(MessageLoopThreadTest, test_post_twice) {
+ std::string name = "test_thread";
+ MessageLoopThread message_loop_thread(name);
+ int counter = 0;
+ message_loop_thread.StartUp();
+ message_loop_thread.Post(
+ base::BindOnce([](MessageLoopThread* thread,
+ int* counter) { ASSERT_EQ((*counter)++, 0); },
+ &message_loop_thread, &counter));
+ message_loop_thread.Post(
+ base::BindOnce([](MessageLoopThread* thread,
+ int* counter) { ASSERT_EQ((*counter)++, 1); },
+ &message_loop_thread, &counter));
+ message_loop_thread.ShutDown();
+ ASSERT_EQ(counter, 2);
+}
diff --git a/system/gd/common/contextual_callback.h b/system/gd/common/contextual_callback.h
index ba78ed6ba1..87ffc3e861 100644
--- a/system/gd/common/contextual_callback.h
+++ b/system/gd/common/contextual_callback.h
@@ -18,16 +18,11 @@
#include "common/bind.h"
#include "common/callback.h"
+#include "common/i_postable_context.h"
namespace bluetooth {
namespace common {
-class IPostableContext {
- public:
- virtual ~IPostableContext(){};
- virtual void Post(OnceClosure closure) = 0;
-};
-
template <typename R, typename... Args>
class ContextualOnceCallback;
diff --git a/system/gd/common/i_postable_context.h b/system/gd/common/i_postable_context.h
new file mode 100644
index 0000000000..231eca7bc6
--- /dev/null
+++ b/system/gd/common/i_postable_context.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2019 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 <base/bind.h>
+
+namespace bluetooth {
+namespace common {
+
+class IPostableContext {
+ public:
+ virtual ~IPostableContext(){};
+ virtual void Post(base::OnceClosure closure) = 0;
+};
+
+} // namespace common
+} // namespace bluetooth
diff --git a/system/test/mock/mock_common_message_loop_thread.cc b/system/test/mock/mock_common_message_loop_thread.cc
index 63e76e7bd8..56b097253e 100644
--- a/system/test/mock/mock_common_message_loop_thread.cc
+++ b/system/test/mock/mock_common_message_loop_thread.cc
@@ -202,6 +202,10 @@ void MessageLoopThread::Run(std::promise<void> start_up_promise) {
}
}
+void MessageLoopThread::Post(base::OnceClosure closure) {
+ DoInThread(FROM_HERE, std::move(closure));
+}
+
} // namespace common
} // namespace bluetooth
diff --git a/system/test/suite/Android.bp b/system/test/suite/Android.bp
index 3fe102e889..29fe4ecc35 100644
--- a/system/test/suite/Android.bp
+++ b/system/test/suite/Android.bp
@@ -77,7 +77,6 @@ cc_defaults {
"libbt-audio-hal-interface",
"libbt-bta",
"libbt-bta-core",
- "libbt-common",
"libbt-hci",
"libbt-sbc-decoder",
"libbt-sbc-encoder",