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

Commit b022e454 authored by Myles Watson's avatar Myles Watson
Browse files

Derive MessageLoopThread from IPostableContext

Allow construction of ContextualCallback for GD callbacks
from legacy code.

Bug: 289277431
Test: atest pts-bot
Change-Id: I717ee13ae4083abe1f287e6bbcb537164c996615
parent fc741c72
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -200,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 −1
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
@@ -166,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:
  /**
+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);
}
+1 −6
Original line number Diff line number Diff line
@@ -18,16 +18,11 @@

#include "common/bind.h"
#include "common/callback.h"
#include "common/i_postable_context.h"

namespace bluetooth {
namespace common {

class IPostableContext {
 public:
  virtual ~IPostableContext(){};
  virtual void Post(OnceClosure closure) = 0;
};

template <typename R, typename... Args>
class ContextualOnceCallback;

+31 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <base/bind.h>

namespace bluetooth {
namespace common {

class IPostableContext {
 public:
  virtual ~IPostableContext(){};
  virtual void Post(base::OnceClosure closure) = 0;
};

}  // namespace common
}  // namespace bluetooth
Loading