1 /******************************************************************************
2  *
3  *  Copyright 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #define LOG_TAG "bt_main_thread"
20 
21 #include "stack/include/main_thread.h"
22 
23 #include <base/functional/bind.h>
24 #include <base/run_loop.h>
25 #include <base/threading/thread.h>
26 #include <bluetooth/log.h>
27 
28 #include "common/message_loop_thread.h"
29 #include "include/hardware/bluetooth.h"
30 #include "os/log.h"
31 
32 using bluetooth::common::MessageLoopThread;
33 using namespace bluetooth;
34 
35 static MessageLoopThread main_thread("bt_main_thread");
36 
get_main_thread()37 bluetooth::common::MessageLoopThread* get_main_thread() { return &main_thread; }
get_main()38 bluetooth::common::PostableContext* get_main() {
39   return main_thread.Postable();
40 }
41 
do_in_main_thread(const base::Location & from_here,base::OnceClosure task)42 bt_status_t do_in_main_thread(const base::Location& from_here,
43                               base::OnceClosure task) {
44   if (!main_thread.DoInThread(from_here, std::move(task))) {
45     log::error("failed from {}", from_here.ToString());
46     return BT_STATUS_JNI_THREAD_ATTACH_ERROR;
47   }
48   return BT_STATUS_SUCCESS;
49 }
50 
do_in_main_thread_delayed(const base::Location & from_here,base::OnceClosure task,std::chrono::microseconds delay)51 bt_status_t do_in_main_thread_delayed(const base::Location& from_here,
52                                       base::OnceClosure task,
53                                       std::chrono::microseconds delay) {
54   if (!main_thread.DoInThreadDelayed(from_here, std::move(task), delay)) {
55     log::error("failed from {}", from_here.ToString());
56     return BT_STATUS_JNI_THREAD_ATTACH_ERROR;
57   }
58   return BT_STATUS_SUCCESS;
59 }
60 
do_post_on_bt_main(BtMainClosure closure)61 static void do_post_on_bt_main(BtMainClosure closure) { closure(); }
62 
post_on_bt_main(BtMainClosure closure)63 void post_on_bt_main(BtMainClosure closure) {
64   log::assert_that(
65       do_in_main_thread(
66           FROM_HERE, base::BindOnce(do_post_on_bt_main, std::move(closure))) ==
67           BT_STATUS_SUCCESS,
68       "assert failed: do_in_main_thread(FROM_HERE, "
69       "base::BindOnce(do_post_on_bt_main, std::move(closure))) == "
70       "BT_STATUS_SUCCESS");
71 }
72 
main_thread_start_up()73 void main_thread_start_up() {
74   main_thread.StartUp();
75   if (!main_thread.IsRunning()) {
76     log::fatal("unable to start btu message loop thread.");
77   }
78   if (!main_thread.EnableRealTimeScheduling()) {
79 #if defined(__ANDROID__)
80     log::fatal("unable to enable real time scheduling");
81 #else
82     log::error("unable to enable real time scheduling");
83 #endif
84   }
85 }
86 
main_thread_shut_down()87 void main_thread_shut_down() { main_thread.ShutDown(); }
88