Loading system/gd/os/alarm.h +4 −3 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ #include <memory> #include <mutex> #include "os/handler.h" #include "os/thread.h" #include "os/utils.h" Loading @@ -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(); Loading @@ -47,7 +48,7 @@ class Alarm { private: Closure task_; Thread* thread_; Handler* handler_; int fd_ = 0; Reactor::Reactable* token_; mutable std::mutex mutex_; Loading system/gd/os/alarm_benchmark.cc +6 −2 Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ using ::benchmark::State; using ::bluetooth::os::Alarm; using ::bluetooth::os::Handler; using ::bluetooth::os::RepeatingAlarm; using ::bluetooth::os::Thread; Loading @@ -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; Loading @@ -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); Loading Loading @@ -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_; }; Loading system/gd/os/handler.h +4 −0 Original line number Diff line number Diff line Loading @@ -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_; Loading system/gd/os/linux_generic/alarm.cc +3 −3 Original line number Diff line number Diff line Loading @@ -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_)); Loading system/gd/os/linux_generic/alarm_unittest.cc +4 −1 Original line number Diff line number Diff line Loading @@ -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 Loading
system/gd/os/alarm.h +4 −3 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ #include <memory> #include <mutex> #include "os/handler.h" #include "os/thread.h" #include "os/utils.h" Loading @@ -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(); Loading @@ -47,7 +48,7 @@ class Alarm { private: Closure task_; Thread* thread_; Handler* handler_; int fd_ = 0; Reactor::Reactable* token_; mutable std::mutex mutex_; Loading
system/gd/os/alarm_benchmark.cc +6 −2 Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ using ::benchmark::State; using ::bluetooth::os::Alarm; using ::bluetooth::os::Handler; using ::bluetooth::os::RepeatingAlarm; using ::bluetooth::os::Thread; Loading @@ -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; Loading @@ -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); Loading Loading @@ -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_; }; Loading
system/gd/os/handler.h +4 −0 Original line number Diff line number Diff line Loading @@ -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_; Loading
system/gd/os/linux_generic/alarm.cc +3 −3 Original line number Diff line number Diff line Loading @@ -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_)); Loading
system/gd/os/linux_generic/alarm_unittest.cc +4 −1 Original line number Diff line number Diff line Loading @@ -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