• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 #include <base/bind.h>
18 #include <base/callback_forward.h>
19 #include <base/location.h>
20 #include <base/time/time.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <functional>
24 #include <future>
25 
26 #include "common/message_loop_thread.h"
27 #include "include/hardware/bluetooth.h"
28 #include "osi/include/log.h"
29 
30 using bluetooth::common::MessageLoopThread;
31 using BtMainClosure = std::function<void()>;
32 
33 namespace {
34 
35 MessageLoopThread main_thread("bt_test_main_thread", true);
do_post_on_bt_main(BtMainClosure closure)36 void do_post_on_bt_main(BtMainClosure closure) { closure(); }
37 
38 }  // namespace
39 
do_in_main_thread(const base::Location & from_here,base::OnceClosure task)40 bt_status_t do_in_main_thread(const base::Location& from_here,
41                               base::OnceClosure task) {
42   ASSERT_LOG(main_thread.DoInThread(from_here, std::move(task)),
43              "Unable to run on main thread");
44   return BT_STATUS_SUCCESS;
45 }
46 
do_in_main_thread_delayed(const base::Location & from_here,base::OnceClosure task,const base::TimeDelta & delay)47 bt_status_t do_in_main_thread_delayed(const base::Location& from_here,
48                                       base::OnceClosure task,
49                                       const base::TimeDelta& delay) {
50   ASSERT_LOG(!main_thread.DoInThreadDelayed(from_here, std::move(task), delay),
51              "Unable to run on main thread delayed");
52   return BT_STATUS_SUCCESS;
53 }
54 
post_on_bt_main(BtMainClosure closure)55 void post_on_bt_main(BtMainClosure closure) {
56   ASSERT(do_in_main_thread(
57              FROM_HERE, base::Bind(do_post_on_bt_main, std::move(closure))) ==
58          BT_STATUS_SUCCESS);
59 }
60 
main_thread_start_up()61 void main_thread_start_up() {
62   main_thread.StartUp();
63   ASSERT_LOG(main_thread.IsRunning(),
64              "Unable to start message loop on main thread");
65 }
66 
main_thread_shut_down()67 void main_thread_shut_down() { main_thread.ShutDown(); }
68 
69 // osi_alarm
get_main_thread()70 bluetooth::common::MessageLoopThread* get_main_thread() { return &main_thread; }
71 
72 int sync_timeout_in_ms = 3000;
73 
sync_main_handler()74 void sync_main_handler() {
75   std::promise promise = std::promise<void>();
76   std::future future = promise.get_future();
77   post_on_bt_main([&promise]() { promise.set_value(); });
78   future.wait_for(std::chrono::milliseconds(sync_timeout_in_ms));
79 };
80 
is_on_main_thread()81 bool is_on_main_thread() {
82   // Pthreads doesn't have the concept of a thread ID, so we have to reach down
83   // into the kernel.
84 #if defined(OS_MACOSX)
85   return main_thread.GetThreadId() == pthread_mach_thread_np(pthread_self());
86 #elif defined(OS_LINUX)
87 #include <sys/syscall.h> /* For SYS_xxx definitions */
88 #include <unistd.h>
89   return main_thread.GetThreadId() == syscall(__NR_gettid);
90 #elif defined(OS_ANDROID)
91 #include <sys/types.h>
92 #include <unistd.h>
93   return main_thread.GetThreadId() == gettid();
94 #else
95   LOG(ERROR) << __func__ << "Unable to determine if on main thread";
96   return true;
97 #endif
98 }
99