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

Commit 345e7840 authored by Chienyuan's avatar Chienyuan
Browse files

Implement blocking queue

Test: run unittest
Change-Id: I93c59a2d8c9bbe9bee762412b368773c4b35456f
parent fb702ee6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ filegroup {
    name: "BluetoothCommonTestSources",
    srcs: [
        "address_unittest.cc",
        "blocking_queue_unittest.cc",
        "class_of_device_unittest.cc",
    ]
}
+65 −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 <condition_variable>
#include <mutex>
#include <queue>

namespace bluetooth {
namespace common {

template <typename T>
class BlockingQueue {
 public:
  void push(T data) {
    std::unique_lock<std::mutex> lock(mutex_);
    queue_.push(std::move(data));
    if (queue_.size() == 1) {
      not_empty_.notify_all();
    }
  };

  const T& take() {
    std::unique_lock<std::mutex> lock(mutex_);
    while (queue_.empty()) {
      not_empty_.wait(lock);
    }
    const T& data = queue_.front();
    queue_.pop();
    return data;
  };

  bool empty() const {
    std::unique_lock<std::mutex> lock(mutex_);
    return queue_.empty();
  };

  void clear() {
    std::unique_lock<std::mutex> lock(mutex_);
    std::queue<T> empty;
    std::swap(queue_, empty);
  };

 private:
  std::queue<T> queue_;
  mutable std::mutex mutex_;
  std::condition_variable not_empty_;
};

}  // namespace common
}  // namespace bluetooth
+104 −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/blocking_queue.h"

#include <thread>

#include <gtest/gtest.h>

namespace bluetooth {
namespace common {
namespace {
class BlockingQueueTest : public ::testing::Test {
 protected:
  void SetUp() override {
    EXPECT_TRUE(queue_.empty());
  }

  // Postcondition for each test case: clear the blocking queue
  void TearDown() override {
    EXPECT_TRUE(queue_.empty());
  }

  BlockingQueue<int> queue_;
};

TEST_F(BlockingQueueTest, initial_empty) {
  EXPECT_TRUE(queue_.empty());
}

TEST_F(BlockingQueueTest, same_thread_push_and_pop) {
  int data = 1;
  queue_.push(data);
  EXPECT_FALSE(queue_.empty());
  EXPECT_EQ(queue_.take(), data);
  EXPECT_TRUE(queue_.empty());
}

TEST_F(BlockingQueueTest, same_thread_push_and_pop_sequential) {
  for (int data = 0; data < 10; data++) {
    queue_.push(data);
    EXPECT_FALSE(queue_.empty());
    EXPECT_EQ(queue_.take(), data);
    EXPECT_TRUE(queue_.empty());
  }
}

TEST_F(BlockingQueueTest, same_thread_push_and_pop_batch) {
  for (int data = 0; data < 10; data++) {
    queue_.push(data);
  }
  EXPECT_FALSE(queue_.empty());
  for (int data = 0; data < 10; data++) {
    EXPECT_EQ(queue_.take(), data);
  }
  EXPECT_TRUE(queue_.empty());
}

TEST_F(BlockingQueueTest, clear_queue) {
  for (int data = 0; data < 10; data++) {
    queue_.push(data);
  }
  EXPECT_FALSE(queue_.empty());
  queue_.clear();
  EXPECT_TRUE(queue_.empty());
}

TEST_F(BlockingQueueTest, wait_for_non_empty) {
  int data = 1;
  std::thread waiter_thread([this, data] { EXPECT_EQ(queue_.take(), data); });
  queue_.push(data);
  waiter_thread.join();
  EXPECT_TRUE(queue_.empty());
}

TEST_F(BlockingQueueTest, wait_for_non_empty_batch) {
  std::thread waiter_thread([this] {
    for (int data = 0; data < 10; data++) {
      EXPECT_EQ(queue_.take(), data);
    }
  });
  for (int data = 0; data < 10; data++) {
    queue_.push(data);
  }
  waiter_thread.join();
  EXPECT_TRUE(queue_.empty());
}

}  // namespace
}  // namespace common
}  // namespace bluetooth