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

Commit 0e9a51a7 authored by Zach Johnson's avatar Zach Johnson Committed by android-build-merger
Browse files

Add BidiQueue

am: e4fa74e0

Change-Id: I00a6898e46cb7e8ad550944fe63bc0cd3a9105ca
parents f5068954 e4fa74e0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -12,5 +12,6 @@ filegroup {
        "address_unittest.cc",
        "blocking_queue_unittest.cc",
        "class_of_device_unittest.cc",
        "bidi_queue_unittest.cc"
    ]
}
+89 −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 "os/queue.h"

namespace bluetooth {
namespace common {

template <typename TENQUEUE, typename TDEQUEUE>
class BidiQueueEnd
    : public ::bluetooth::os::IQueueEnqueue<TENQUEUE>,
      public ::bluetooth::os::IQueueDequeue<TDEQUEUE> {
 public:
  using EnqueueCallback = std::function<std::unique_ptr<TENQUEUE>()>;
  using DequeueCallback = std::function<void()>;

  BidiQueueEnd(::bluetooth::os::IQueueEnqueue<TENQUEUE>* tx, ::bluetooth::os::IQueueDequeue<TDEQUEUE>* rx)
      : tx_(tx), rx_(rx) {
  }

  void RegisterEnqueue(::bluetooth::os::Handler* handler, EnqueueCallback callback) override {
    tx_->RegisterEnqueue(handler, callback);
  }

  void UnregisterEnqueue() override {
    tx_->UnregisterEnqueue();
  }

  void RegisterDequeue(::bluetooth::os::Handler* handler, DequeueCallback callback) override {
    rx_->RegisterDequeue(handler, callback);
  }

  void UnregisterDequeue() override {
    rx_->UnregisterDequeue();
  }

  std::unique_ptr<TDEQUEUE> TryDequeue() override {
    return rx_->TryDequeue();
  }

 private:
  ::bluetooth::os::IQueueEnqueue<TENQUEUE>* tx_;
  ::bluetooth::os::IQueueDequeue<TDEQUEUE>* rx_;
};

template <typename TUP, typename TDOWN>
class BidiQueue {
 public:
  explicit BidiQueue(size_t capacity)
      : up_queue_(capacity),
        down_queue_(capacity),
        up_end_(&down_queue_, &up_queue_),
        down_end_(&up_queue_, &down_queue_) {
  }

  BidiQueueEnd<TDOWN, TUP>* GetUpEnd() {
    return &up_end_;
  }

  BidiQueueEnd<TUP, TDOWN>* GetDownEnd() {
    return &down_end_;
  }

 private:
  ::bluetooth::os::Queue<TUP> up_queue_;
  ::bluetooth::os::Queue<TDOWN> down_queue_;
  BidiQueueEnd<TDOWN, TUP> up_end_;
  BidiQueueEnd<TUP, TDOWN> down_end_;
};

}  // namespace common
}  // namespace bluetooth
+123 −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.
 */

#include "common/bidi_queue.h"

#include <future>

#include "gtest/gtest.h"
#include "os/thread.h"
#include "os/handler.h"

using ::bluetooth::os::Thread;
using ::bluetooth::os::Handler;

namespace bluetooth {
namespace common {
namespace {

class BidiQueueTest : public ::testing::Test {
 protected:
  void SetUp() override {
    up_thread_ = new Thread("up_thread", Thread::Priority::NORMAL);
    up_handler_ = new Handler(up_thread_);
    down_thread_ = new Thread("down_thread", Thread::Priority::NORMAL);
    down_handler_ = new Handler(down_thread_);
  }

  void TearDown() override {
    delete up_handler_;
    delete up_thread_;
    delete down_handler_;
    delete down_thread_;
  }

  Thread* up_thread_;
  Handler* up_handler_;
  Thread* down_thread_;
  Handler* down_handler_;
};

class A {
};

class B {
};

template <typename TA, typename TB>
class TestBidiQueueEnd {
 public:
  explicit TestBidiQueueEnd(BidiQueueEnd<TA, TB>* end, Handler* handler)
      : handler_(handler), end_(end) {}

  ~TestBidiQueueEnd() {
  }

  std::promise<void>* Send(TA* value) {
    std::promise<void>* promise = new std::promise<void>();
    handler_->Post([this, value, promise] {
      end_->RegisterEnqueue(handler_, [this, value, promise]() -> std::unique_ptr<TA> {
        end_->UnregisterEnqueue();
        promise->set_value();
        return std::unique_ptr<TA>(value);
      });
    });

    return promise;
  }

  std::promise<TB*>* Receive() {
    std::promise<TB*>* promise = new std::promise<TB*>();
    handler_->Post([this, promise] {
      end_->RegisterDequeue(handler_, [this, promise] {
        promise->set_value(end_->TryDequeue().get());
        end_->UnregisterDequeue();
      });
    });

    return promise;
  }

 private:
  Handler* handler_;
  BidiQueueEnd<TA, TB>* end_;
};

TEST_F(BidiQueueTest, simple_test) {
  BidiQueue<A, B> queue(100);
  TestBidiQueueEnd<B, A> test_up(queue.GetUpEnd(), up_handler_);
  TestBidiQueueEnd<A, B> test_down(queue.GetDownEnd(), down_handler_);

  auto sending_b = new B();
  auto promise_sending_b = test_up.Send(sending_b);
  promise_sending_b->get_future().wait();
  auto promise_receive_b = test_down.Receive();
  EXPECT_EQ(promise_receive_b->get_future().get(), sending_b);
  delete promise_receive_b;
  delete promise_sending_b;

  auto sending_a = new A();
  auto promise_sending_a = test_down.Send(sending_a);
  promise_sending_a->get_future().wait();
  auto promise_receive_a = test_up.Receive();
  EXPECT_EQ(promise_receive_a->get_future().get(), sending_a);
  delete promise_receive_a;
  delete promise_sending_a;
}

}  // namespace
}  // namespace os
}  // namespace bluetooth