Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ac77df2a authored by Chris Manton's avatar Chris Manton
Browse files

Add API is_main_thread() check

Bug: 163134718
Tag: #refactor
Test: gd/cert/run

Change-Id: Idfd67df5f3f6fb2526b69aff62901ae0c5f404c3
parent 2646b0da
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -126,3 +126,22 @@ void main_thread_start_up() {
}

void main_thread_shut_down() { main_thread.ShutDown(); }

bool is_on_main_thread() {
  // Pthreads doesn't have the concept of a thread ID, so we have to reach down
  // into the kernel.
#if defined(OS_MACOSX)
  return main_thread.GetThreadId() == pthread_mach_thread_np(pthread_self());
#elif defined(OS_LINUX)
#include <sys/syscall.h> /* For SYS_xxx definitions */
#include <unistd.h>
  return main_thread.GetThreadId() == syscall(__NR_gettid);
#elif defined(OS_ANDROID)
#include <sys/types.h>
#include <unistd.h>
  return main_thread.GetThreadId() == gettid();
#else
  LOG(ERROR) << __func__ << "Unable to determine if on main thread";
  return true;
#endif
}
+1 −0
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ bt_status_t do_in_main_thread_delayed(const base::Location& from_here,
                                      base::OnceClosure task,
                                      const base::TimeDelta& delay);

bool is_on_main_thread();
using BtMainClosure = std::function<void()>;
void post_on_bt_main(BtMainClosure closure);

+21 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@
#include <base/callback_forward.h>
#include <base/location.h>
#include <base/time/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <functional>
#include <future>

@@ -75,3 +77,22 @@ void sync_main_handler() {
  post_on_bt_main([&promise]() { promise.set_value(); });
  future.wait_for(std::chrono::milliseconds(sync_timeout_in_ms));
};

bool is_on_main_thread() {
  // Pthreads doesn't have the concept of a thread ID, so we have to reach down
  // into the kernel.
#if defined(OS_MACOSX)
  return main_thread.GetThreadId() == pthread_mach_thread_np(pthread_self());
#elif defined(OS_LINUX)
#include <sys/syscall.h> /* For SYS_xxx definitions */
#include <unistd.h>
  return main_thread.GetThreadId() == syscall(__NR_gettid);
#elif defined(OS_ANDROID)
#include <sys/types.h>
#include <unistd.h>
  return main_thread.GetThreadId() == gettid();
#else
  LOG(ERROR) << __func__ << "Unable to determine if on main thread";
  return true;
#endif
}
+1 −0
Original line number Diff line number Diff line
@@ -37,3 +37,4 @@ void main_thread_shut_down();
extern int sync_timeout_in_ms;
void sync_main_handler();
bluetooth::common::MessageLoopThread* get_main_thread();
bool is_on_main_thread();