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

Commit 9a5a3df8 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Common: remove GD related stuff from common"

parents 6a6da03e c1c0e7e0
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -50,7 +50,9 @@
    },
    {
      "name" : "net_test_types"
    },
    }
  ],
  "presubmit" : [
    {
      "name" : "bluetooth_test_common",
      "host" : true
+0 −10
Original line number Diff line number Diff line
@@ -11,15 +11,10 @@ cc_library_static {
    ],
    srcs: [
        "address_obfuscator.cc",
        "alarm.cc",
        "handler.cc",
        "message_loop_thread.cc",
        "metrics.cc",
        "once_timer.cc",
        "reactor.cc",
        "repeating_alarm.cc",
        "repeating_timer.cc",
        "thread.cc",
        "time_util.cc",
    ],
    shared_libs: [
@@ -44,17 +39,12 @@ cc_test {
    ],
    srcs : [
        "address_obfuscator_unittest.cc",
        "alarm_unittest.cc",
        "handler_unittest.cc",
        "leaky_bonded_queue_unittest.cc",
        "message_loop_thread_unittest.cc",
        "metrics_unittest.cc",
        "once_timer_unittest.cc",
        "reactor_unittest.cc",
        "repeating_alarm_unittest.cc",
        "repeating_timer_unittest.cc",
        "state_machine_unittest.cc",
        "thread_unittest.cc",
        "time_util_unittest.cc",
        "id_generator_unittest.cc",
    ],

system/common/alarm.cc

deleted100644 → 0
+0 −76
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 "alarm.h"

#include <sys/timerfd.h>
#include <cstring>

#include "base/logging.h"
#include "utils.h"

namespace bluetooth {
namespace common {

Alarm::Alarm(Thread* thread)
  : thread_(thread),
    fd_(timerfd_create(CLOCK_BOOTTIME_ALARM, 0)) {
  CHECK_NE(fd_, -1) << __func__ << ": cannot create timerfd: " << strerror(errno);

  token_ = thread_->GetReactor()->Register(fd_, [this] { on_fire(); }, nullptr);
}

Alarm::~Alarm() {
  thread_->GetReactor()->Unregister(token_);

  int close_status;
  RUN_NO_INTR(close_status = close(fd_));
  CHECK_NE(close_status, -1) << __func__ << ": cannot close timerfd: " << strerror(errno);
}

void Alarm::Schedule(Closure task, std::chrono::milliseconds delay) {
  std::lock_guard<std::mutex> lock(mutex_);
  long delay_ms = delay.count();
  itimerspec timer_itimerspec{
    {/* interval for periodic timer */},
    {delay_ms / 1000, delay_ms % 1000 * 1000000}
  };
  int result = timerfd_settime(fd_, 0, &timer_itimerspec, nullptr);
  CHECK_EQ(result, 0) << __func__ << ": failed, error=" << strerror(errno);

  task_ = std::move(task);
}

void Alarm::Cancel() {
  std::lock_guard<std::mutex> lock(mutex_);
  itimerspec disarm_itimerspec{/* disarm timer */};
  int result = timerfd_settime(fd_, 0, &disarm_itimerspec, nullptr);
  CHECK_EQ(result, 0) << __func__ << ": failed, error=" << strerror(errno);
}

void Alarm::on_fire() {
  std::unique_lock<std::mutex> lock(mutex_);
  auto task = std::move(task_);
  uint64_t times_invoked;
  auto bytes_read = read(fd_, &times_invoked, sizeof(uint64_t));
  lock.unlock();
  task();
  CHECK_EQ(bytes_read, static_cast<ssize_t>(sizeof(uint64_t))) << __func__ << ": failed, error=" << strerror(errno);
  CHECK_EQ(times_invoked, static_cast<uint64_t>(1));
}

}  // namespace common
}  // namespace bluetooth

system/common/alarm_unittest.cc

deleted100644 → 0
+0 −90
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 "alarm.h"

#include <future>

#include "base/logging.h"
#include "gtest/gtest.h"

namespace bluetooth {
namespace common {
namespace {

class AlarmTest : public ::testing::Test {
 protected:
  void SetUp() override {
    thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
    alarm_ = new Alarm(thread_);
  }

  void TearDown() override {
    delete alarm_;
    delete thread_;
  }
  Alarm* alarm_;

 private:
  Thread* thread_;
};

TEST_F(AlarmTest, cancel_while_not_armed) {
  alarm_->Cancel();
}

TEST_F(AlarmTest, schedule) {
  std::promise<void> promise;
  auto future = promise.get_future();
  auto before = std::chrono::steady_clock::now();
  int delay_ms = 10;
  int delay_error_ms = 3;
  alarm_->Schedule([&promise]() { promise.set_value(); }, std::chrono::milliseconds(delay_ms));
  future.get();
  auto after = std::chrono::steady_clock::now();
  auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(after - before);
  ASSERT_NEAR(duration_ms.count(), delay_ms, delay_error_ms);
}

TEST_F(AlarmTest, cancel_alarm) {
  alarm_->Schedule([]() { ASSERT_TRUE(false) << "Should not happen"; }, std::chrono::milliseconds(3));
  alarm_->Cancel();
  std::this_thread::sleep_for(std::chrono::milliseconds(5));
}

TEST_F(AlarmTest, cancel_alarm_from_callback) {
  alarm_->Schedule([this]() { this->alarm_->Cancel(); }, std::chrono::milliseconds(1));
  std::this_thread::sleep_for(std::chrono::milliseconds(5));
}

TEST_F(AlarmTest, schedule_while_alarm_armed) {
  alarm_->Schedule([]() { ASSERT_TRUE(false) << "Should not happen"; }, std::chrono::milliseconds(1));
  std::promise<void> promise;
  auto future = promise.get_future();
  alarm_->Schedule([&promise]() { promise.set_value(); }, std::chrono::milliseconds(10));
  future.get();
}

TEST_F(AlarmTest, delete_while_alarm_armed) {
  alarm_->Schedule([]() { ASSERT_TRUE(false) << "Should not happen"; }, std::chrono::milliseconds(1));
  delete alarm_;
  alarm_ = nullptr;
  std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

}  // namespace
}  // namespace common
}  // namespace bluetooth
+0 −52
Original line number Diff line number Diff line
@@ -23,16 +23,12 @@
#include <memory>
#include <thread>

#include "common/handler.h"
#include "common/message_loop_thread.h"
#include "common/thread.h"
#include "osi/include/fixed_queue.h"
#include "osi/include/thread.h"

using ::benchmark::State;
using bluetooth::common::Handler;
using bluetooth::common::MessageLoopThread;
using bluetooth::common::Thread;

#define NUM_MESSAGES_TO_SEND 100000

@@ -423,54 +419,6 @@ BENCHMARK_F(BM_LibChromeThread, sequential_execution)(State& state) {
  }
};

class BM_ReactorThread : public BM_ThreadPerformance {
 protected:
  void SetUp(State& st) override {
    BM_ThreadPerformance::SetUp(st);
    std::future<void> set_up_future = set_up_promise_->get_future();
    thread_ = new Thread("BM_ReactorThread thread", Thread::Priority::NORMAL);
    handler_ = new Handler(thread_);
    handler_->Post([this]() { set_up_promise_->set_value(); });
    set_up_future.wait();
  }

  void TearDown(State& st) override {
    delete handler_;
    handler_ = nullptr;
    thread_->Stop();
    delete thread_;
    thread_ = nullptr;
    BM_ThreadPerformance::TearDown(st);
  }

  Thread* thread_ = nullptr;
  Handler* handler_ = nullptr;
};

BENCHMARK_F(BM_ReactorThread, batch_enque_dequeue)(State& state) {
  for (auto _ : state) {
    g_counter = 0;
    g_counter_promise = std::make_unique<std::promise<void>>();
    std::future<void> counter_future = g_counter_promise->get_future();
    for (int i = 0; i < NUM_MESSAGES_TO_SEND; i++) {
      fixed_queue_enqueue(bt_msg_queue_, (void*)&g_counter);
      handler_->Post([this]() { callback_batch(bt_msg_queue_, nullptr); });
    }
    counter_future.wait();
  }
};

BENCHMARK_F(BM_ReactorThread, sequential_execution)(State& state) {
  for (auto _ : state) {
    for (int i = 0; i < NUM_MESSAGES_TO_SEND; i++) {
      g_counter_promise = std::make_unique<std::promise<void>>();
      std::future<void> counter_future = g_counter_promise->get_future();
      handler_->Post([]() { callback_sequential(nullptr); });
      counter_future.wait();
    }
  }
};

int main(int argc, char** argv) {
  // Disable LOG() output from libchrome
  logging::LoggingSettings log_settings;
Loading