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

Commit af89a04f authored by Myles Watson's avatar Myles Watson Committed by android-build-merger
Browse files

Merge "OS: Create Alarms on a Handler's thread"

am: bd0af121

Change-Id: Ie4238a454195704381a14337cf6c62ba340569f4
parents 52920832 bd0af121
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <memory>
#include <mutex>

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

@@ -31,8 +32,8 @@ namespace os {
// itself from the thread.
class Alarm {
 public:
  // Create and register a single-shot alarm on given thread
  explicit Alarm(Thread* thread);
  // Create and register a single-shot alarm on a given handler
  explicit Alarm(Handler* handler);

  // Unregister this alarm from the thread and release resource
  ~Alarm();
@@ -47,7 +48,7 @@ class Alarm {

 private:
  Closure task_;
  Thread* thread_;
  Handler* handler_;
  int fd_ = 0;
  Reactor::Reactable* token_;
  mutable std::mutex mutex_;
+6 −2
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@

using ::benchmark::State;
using ::bluetooth::os::Alarm;
using ::bluetooth::os::Handler;
using ::bluetooth::os::RepeatingAlarm;
using ::bluetooth::os::Thread;

@@ -34,8 +35,9 @@ class BM_ReactableAlarm : public ::benchmark::Fixture {
  void SetUp(State& st) override {
    ::benchmark::Fixture::SetUp(st);
    thread_ = std::make_unique<Thread>("timer_benchmark", Thread::Priority::REAL_TIME);
    alarm_ = std::make_unique<Alarm>(thread_.get());
    repeating_alarm_ = std::make_unique<RepeatingAlarm>(thread_.get());
    handler_ = std::make_unique<Handler>(thread_.get());
    alarm_ = std::make_unique<Alarm>(handler_.get());
    repeating_alarm_ = std::make_unique<RepeatingAlarm>(handler_.get());
    map_.clear();
    scheduled_tasks_ = 0;
    task_length_ = 0;
@@ -47,6 +49,7 @@ class BM_ReactableAlarm : public ::benchmark::Fixture {
  void TearDown(State& st) override {
    alarm_ = nullptr;
    repeating_alarm_ = nullptr;
    handler_ = nullptr;
    thread_->Stop();
    thread_ = nullptr;
    ::benchmark::Fixture::TearDown(st);
@@ -75,6 +78,7 @@ class BM_ReactableAlarm : public ::benchmark::Fixture {
  std::promise<void> promise_;
  std::chrono::time_point<std::chrono::steady_clock> start_time_;
  std::unique_ptr<Thread> thread_;
  std::unique_ptr<Handler> handler_;
  std::unique_ptr<Alarm> alarm_;
  std::unique_ptr<RepeatingAlarm> repeating_alarm_;
};
+4 −0
Original line number Diff line number Diff line
@@ -49,6 +49,10 @@ class Handler {
  template <typename T>
  friend class Queue;

  friend class Alarm;

  friend class RepeatingAlarm;

 private:
  std::queue<Closure> tasks_;
  Thread* thread_;
+3 −3
Original line number Diff line number Diff line
@@ -32,14 +32,14 @@
namespace bluetooth {
namespace os {

Alarm::Alarm(Thread* thread) : thread_(thread), fd_(timerfd_create(ALARM_CLOCK, 0)) {
Alarm::Alarm(Handler* handler) : handler_(handler), fd_(timerfd_create(ALARM_CLOCK, 0)) {
  ASSERT_LOG(fd_ != -1, "cannot create timerfd: %s", strerror(errno));

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

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

  int close_status;
  RUN_NO_INTR(close_status = close(fd_));
+4 −1
Original line number Diff line number Diff line
@@ -28,16 +28,19 @@ class AlarmTest : public ::testing::Test {
 protected:
  void SetUp() override {
    thread_ = new Thread("test_thread", Thread::Priority::NORMAL);
    alarm_ = new Alarm(thread_);
    handler_ = new Handler(thread_);
    alarm_ = new Alarm(handler_);
  }

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

 private:
  Handler* handler_;
  Thread* thread_;
};

Loading