OSDN Git Service

Add is_on_jni_thread
authorJakub Pawlowski <jpawlowski@google.com>
Thu, 30 Nov 2017 02:31:13 +0000 (18:31 -0800)
committerJakub Pawlowski <jpawlowski@google.com>
Thu, 30 Nov 2017 06:21:18 +0000 (22:21 -0800)
is_on_jni_thread can be used to check if current thread is btif thread.

Test: compilation
Change-Id: I26e794c3771ae26e15e6cee6c3d4f9a1513571f4

btif/include/btif_common.h
btif/src/btif_core.cc

index b65a24b..94fadee 100644 (file)
@@ -176,6 +176,7 @@ typedef struct {
 extern bt_status_t do_in_jni_thread(const base::Closure& task);
 extern bt_status_t do_in_jni_thread(const tracked_objects::Location& from_here,
                                     const base::Closure& task);
+extern bool is_on_jni_thread();
 /**
  * This template wraps callback into callback that will be executed on jni
  * thread
index 1a0c56a..296b9d7 100644 (file)
@@ -31,6 +31,7 @@
 #include <base/at_exit.h>
 #include <base/bind.h>
 #include <base/run_loop.h>
+#include <base/threading/platform_thread.h>
 #include <base/threading/thread.h>
 #include <ctype.h>
 #include <dirent.h>
@@ -65,7 +66,9 @@
 #include "osi/include/thread.h"
 #include "stack_manager.h"
 
+using base::PlatformThread;
 using bluetooth::Uuid;
+
 /*******************************************************************************
  *  Constants & Macros
  ******************************************************************************/
@@ -129,6 +132,7 @@ static const char* BT_JNI_WORKQUEUE_NAME = "bt_jni_workqueue";
 static uid_set_t* uid_set = NULL;
 base::MessageLoop* message_loop_ = NULL;
 base::RunLoop* jni_run_loop = NULL;
+static base::PlatformThreadId btif_thread_id_ = -1;
 
 /*******************************************************************************
  *  Static functions
@@ -237,6 +241,10 @@ bt_status_t do_in_jni_thread(const base::Closure& task) {
   return do_in_jni_thread(FROM_HERE, task);
 }
 
+bool is_on_jni_thread() {
+  return btif_thread_id_ == PlatformThread::CurrentId();
+}
+
 /*******************************************************************************
  *
  * Function         btif_is_dut_mode
@@ -316,6 +324,7 @@ void btif_thread_post(thread_fn func, void* context) {
 
 void run_message_loop(UNUSED_ATTR void* context) {
   LOG_INFO(LOG_TAG, "%s entered", __func__);
+  btif_thread_id_ = PlatformThread::CurrentId();
 
   // TODO(jpawlowski): exit_manager should be defined in main(), but there is no
   // main method.
@@ -338,6 +347,7 @@ void run_message_loop(UNUSED_ATTR void* context) {
   delete jni_run_loop;
   jni_run_loop = NULL;
 
+  btif_thread_id_ = -1;
   LOG_INFO(LOG_TAG, "%s finished", __func__);
 }
 /*******************************************************************************