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
31 using bluetooth::common::MessageLoopThread;
32 using namespace bluetooth;
33
34 static MessageLoopThread main_thread("bt_main_thread");
35
get_main_thread()36 bluetooth::common::MessageLoopThread* get_main_thread() { return &main_thread; }
get_main()37 bluetooth::common::PostableContext* get_main() { return main_thread.Postable(); }
38
do_in_main_thread(base::OnceClosure task)39 bt_status_t do_in_main_thread(base::OnceClosure task) {
40 if (!main_thread.DoInThread(FROM_HERE, std::move(task))) {
41 log::error("failed to post task to task runner!");
42 return BT_STATUS_JNI_THREAD_ATTACH_ERROR;
43 }
44 return BT_STATUS_SUCCESS;
45 }
46
do_in_main_thread_delayed(base::OnceClosure task,std::chrono::microseconds delay)47 bt_status_t do_in_main_thread_delayed(base::OnceClosure task, std::chrono::microseconds delay) {
48 if (!main_thread.DoInThreadDelayed(FROM_HERE, std::move(task), delay)) {
49 log::error("failed to post task to task runner!");
50 return BT_STATUS_JNI_THREAD_ATTACH_ERROR;
51 }
52 return BT_STATUS_SUCCESS;
53 }
54
do_post_on_bt_main(BtMainClosure closure)55 static void do_post_on_bt_main(BtMainClosure closure) { closure(); }
56
post_on_bt_main(BtMainClosure closure)57 void post_on_bt_main(BtMainClosure closure) {
58 log::assert_that(do_in_main_thread(base::BindOnce(do_post_on_bt_main, std::move(closure))) ==
59 BT_STATUS_SUCCESS,
60 "assert failed: do_in_main_thread("
61 "base::BindOnce(do_post_on_bt_main, std::move(closure))) == "
62 "BT_STATUS_SUCCESS");
63 }
64
main_thread_start_up()65 void main_thread_start_up() {
66 main_thread.StartUp();
67 if (!main_thread.IsRunning()) {
68 log::fatal("unable to start btu message loop thread.");
69 }
70 if (!main_thread.EnableRealTimeScheduling()) {
71 #if defined(__ANDROID__)
72 log::fatal("unable to enable real time scheduling");
73 #else
74 log::error("unable to enable real time scheduling");
75 #endif
76 }
77 }
78
main_thread_shut_down()79 void main_thread_shut_down() { main_thread.ShutDown(); }
80