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

Commit 786c59a3 authored by Myles Watson's avatar Myles Watson Committed by Gerrit Code Review
Browse files

Merge changes I717ee13a,I0028f0a8 into main

* changes:
  Derive MessageLoopThread from IPostableContext
  Remove is_main_thread
parents 9481e51a b022e454
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -156,7 +156,6 @@ bt_status_t do_in_jni_thread(base::OnceClosure task);
bt_status_t do_in_jni_thread(const base::Location& from_here,
                             base::OnceClosure task);
bool is_on_jni_thread();
btbase::AbstractMessageLoop* get_jni_message_loop();

using BtJniClosure = std::function<void()>;
void post_on_bt_jni(BtJniClosure closure);
+0 −4
Original line number Diff line number Diff line
@@ -177,10 +177,6 @@ bool is_on_jni_thread() {
  return jni_thread.GetThreadId() == PlatformThread::CurrentId();
}

btbase::AbstractMessageLoop* get_jni_message_loop() {
  return jni_thread.message_loop();
}

static void do_post_on_bt_jni(BtJniClosure closure) { closure(); }

void post_on_bt_jni(BtJniClosure closure) {
+6 −9
Original line number Diff line number Diff line
@@ -33,10 +33,6 @@ namespace common {
static constexpr int kRealTimeFifoSchedulingPriority = 1;

MessageLoopThread::MessageLoopThread(const std::string& thread_name)
    : MessageLoopThread(thread_name, false) {}

MessageLoopThread::MessageLoopThread(const std::string& thread_name,
                                     bool is_main)
    : thread_name_(thread_name),
      message_loop_(nullptr),
      run_loop_(nullptr),
@@ -44,8 +40,7 @@ MessageLoopThread::MessageLoopThread(const std::string& thread_name,
      thread_id_(-1),
      linux_tid_(-1),
      weak_ptr_factory_(this),
      shutting_down_(false),
      is_main_(is_main) {}
      shutting_down_(false) {}

MessageLoopThread::~MessageLoopThread() { ShutDown(); }

@@ -143,10 +138,8 @@ void MessageLoopThread::RunThread(MessageLoopThread* thread,
  thread->Run(std::move(start_up_promise));
}

// This is only for use in tests.
btbase::AbstractMessageLoop* MessageLoopThread::message_loop() const {
  ASSERT_LOG(!is_main_,
             "you are not allowed to get the main thread's message loop");

  std::lock_guard<std::recursive_mutex> api_lock(api_mutex_);
  return message_loop_;
}
@@ -207,6 +200,10 @@ void MessageLoopThread::Run(std::promise<void> start_up_promise) {
  }
}

void MessageLoopThread::Post(base::OnceClosure closure) {
  DoInThread(FROM_HERE, std::move(closure));
}

}  // namespace common

}  // namespace bluetooth
+6 −3
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#include <thread>

#include "abstract_message_loop.h"
#include "gd/common/i_postable_context.h"

namespace bluetooth {

@@ -36,7 +37,7 @@ namespace common {
/**
 * An interface to various thread related functionality
 */
class MessageLoopThread final {
class MessageLoopThread final : public IPostableContext {
 public:
  /**
   * Create a message loop thread with name. Thread won't be running until
@@ -45,7 +46,6 @@ class MessageLoopThread final {
   * @param thread_name name of this worker thread
   */
  explicit MessageLoopThread(const std::string& thread_name);
  explicit MessageLoopThread(const std::string& thread_name, bool is_main);

  MessageLoopThread(const MessageLoopThread&) = delete;
  MessageLoopThread& operator=(const MessageLoopThread&) = delete;
@@ -167,6 +167,10 @@ class MessageLoopThread final {
   */
  bool DoInThreadDelayed(const base::Location& from_here,
                         base::OnceClosure task, const base::TimeDelta& delay);
  /**
   * Wrapper around DoInThread without a location.
   */
  void Post(base::OnceClosure closure) override;

 private:
  /**
@@ -199,7 +203,6 @@ class MessageLoopThread final {
  pid_t linux_tid_;
  base::WeakPtrFactory<MessageLoopThread> weak_ptr_factory_;
  bool shutting_down_;
  bool is_main_;
};

inline std::ostream& operator<<(std::ostream& os,
+18 −0
Original line number Diff line number Diff line
@@ -328,3 +328,21 @@ TEST_F(MessageLoopThreadTest, shut_down_start_up_multi_thread) {
  auto thread = std::thread(&MessageLoopThread::StartUp, &message_loop_thread);
  thread.join();
}

// Verify that Post executes in order
TEST_F(MessageLoopThreadTest, test_post_twice) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  int counter = 0;
  message_loop_thread.StartUp();
  message_loop_thread.Post(
      base::BindOnce([](MessageLoopThread* thread,
                        int* counter) { ASSERT_EQ((*counter)++, 0); },
                     &message_loop_thread, &counter));
  message_loop_thread.Post(
      base::BindOnce([](MessageLoopThread* thread,
                        int* counter) { ASSERT_EQ((*counter)++, 1); },
                     &message_loop_thread, &counter));
  message_loop_thread.ShutDown();
  ASSERT_EQ(counter, 2);
}
Loading