diff options
author | William A. Kennington III <wak@google.com> | 2018-10-18 19:20:01 -0700 |
---|---|---|
committer | Patrick Venture <venture@google.com> | 2018-10-26 14:24:57 +0000 |
commit | 0dd0c4d728757b78d07f57fe3f92193960ac7076 (patch) | |
tree | e84c45f247d5ddf6b013e0c951e707dc826d942b | |
parent | ee73f5bdd1996ee9d4f65ddbc69804ea0b1b59e9 (diff) | |
download | phosphor-hwmon-0dd0c4d728757b78d07f57fe3f92193960ac7076.tar.gz phosphor-hwmon-0dd0c4d728757b78d07f57fe3f92193960ac7076.zip |
timer: Remove in favor of sdeventplus/timer
Lets use the new standard event loop timer utility that comes with
sdeventplus instead of the one copied from other projects.
Change-Id: Ie7c8f462f2c0e2e05ce14da58b30a1f91acbf75d
Signed-off-by: William A. Kennington III <wak@google.com>
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | mainloop.cpp | 8 | ||||
-rw-r--r-- | mainloop.hpp | 11 | ||||
-rw-r--r-- | timer.cpp | 67 | ||||
-rw-r--r-- | timer.hpp | 106 |
5 files changed, 9 insertions, 184 deletions
diff --git a/Makefile.am b/Makefile.am index 3d5f786..b1052e8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -28,7 +28,6 @@ libhwmon_la_SOURCES = \ env.cpp \ fan_speed.cpp \ fan_pwm.cpp \ - timer.cpp \ hwmon.cpp \ hwmonio.cpp \ sensor.cpp \ diff --git a/mainloop.cpp b/mainloop.cpp index 81faa9b..5cae0d3 100644 --- a/mainloop.cpp +++ b/mainloop.cpp @@ -252,7 +252,8 @@ MainLoop::MainLoop(sdbusplus::bus::bus&& bus, const std::string& param, _bus(std::move(bus)), _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(), _devPath(devPath), _prefix(prefix), _root(root), state(), ioAccess(path), - event(sdeventplus::Event::get_default()) + event(sdeventplus::Event::get_default()), + timer(event, std::bind(&MainLoop::read, this)) { // Strip off any trailing slashes. std::string p = path; @@ -276,7 +277,6 @@ MainLoop::MainLoop(sdbusplus::bus::bus&& bus, const std::string& param, void MainLoop::shutdown() noexcept { - timer->state(phosphor::hwmon::timer::OFF); event.exit(0); } @@ -287,9 +287,7 @@ void MainLoop::run() std::function<void()> callback(std::bind(&MainLoop::read, this)); try { - timer = std::make_unique<phosphor::hwmon::Timer>( - event.get(), callback, std::chrono::microseconds(_interval), - phosphor::hwmon::timer::ON); + timer.restart(std::chrono::microseconds(_interval)); // TODO: Issue#6 - Optionally look at polling interval sysfs entry. diff --git a/mainloop.hpp b/mainloop.hpp index 87f6ed0..dbf86fb 100644 --- a/mainloop.hpp +++ b/mainloop.hpp @@ -5,14 +5,15 @@ #include "sensor.hpp" #include "sensorset.hpp" #include "sysfs.hpp" -#include "timer.hpp" #include "types.hpp" #include <any> #include <memory> #include <optional> #include <sdbusplus/server.hpp> +#include <sdeventplus/clock.hpp> #include <sdeventplus/event.hpp> +#include <sdeventplus/utility/timer.hpp> #include <string> #include <vector> @@ -31,8 +32,8 @@ class MainLoop MainLoop() = delete; MainLoop(const MainLoop&) = delete; MainLoop& operator=(const MainLoop&) = delete; - MainLoop(MainLoop&&) = default; - MainLoop& operator=(MainLoop&&) = default; + MainLoop(MainLoop&&) = delete; + MainLoop& operator=(MainLoop&&) = delete; ~MainLoop() = default; /** @brief Constructor @@ -98,10 +99,10 @@ class MainLoop uint64_t _interval = default_interval; /** @brief Hwmon sysfs access. */ hwmonio::HwmonIO ioAccess; - /** @brief Timer */ - std::unique_ptr<phosphor::hwmon::Timer> timer; /** @brief the Event Loop structure */ sdeventplus::Event event; + /** @brief Read Timer */ + sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer; /** @brief Store the specifications of sensor objects */ std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>> sensorObjects; diff --git a/timer.cpp b/timer.cpp deleted file mode 100644 index 72c9f96..0000000 --- a/timer.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "timer.hpp" - -#include <chrono> -#include <cstring> -#include <system_error> - -namespace phosphor -{ -namespace hwmon -{ - -static std::chrono::microseconds getTime() -{ - using namespace std::chrono; - auto usec = steady_clock::now().time_since_epoch(); - return duration_cast<microseconds>(usec); -} - -Timer::Timer(sd_event* event, std::function<void()> callback, - std::chrono::microseconds usec, timer::Action action) : - event(event), - callback(callback), duration(usec), action(action) -{ - auto r = sd_event_add_time(event, &eventSource, - CLOCK_MONOTONIC, // Time base - (getTime() + usec).count(), // When to fire - 0, // Use default event accuracy - timeoutHandler, // Callback handler on timeout - this); // User data - if (r < 0) - { - throw std::system_error(r, std::generic_category(), std::strerror(-r)); - } -} - -int Timer::timeoutHandler(sd_event_source* eventSource, uint64_t usec, - void* userData) -{ - auto timer = static_cast<Timer*>(userData); - - if (timer->getAction() == timer::ON) - { - auto r = sd_event_source_set_time( - eventSource, (getTime() + timer->getDuration()).count()); - if (r < 0) - { - throw std::system_error(r, std::generic_category(), - std::strerror(-r)); - } - r = sd_event_source_set_enabled(eventSource, timer::ON); - if (r < 0) - { - throw std::system_error(r, std::generic_category(), - std::strerror(-r)); - } - } - - if (timer->callback) - { - timer->callback(); - } - - return 0; -} - -} // namespace hwmon -} // namespace phosphor diff --git a/timer.hpp b/timer.hpp deleted file mode 100644 index 03f642d..0000000 --- a/timer.hpp +++ /dev/null @@ -1,106 +0,0 @@ -#pragma once - -#include <systemd/sd-event.h> - -#include <chrono> -#include <functional> - -namespace phosphor -{ -namespace hwmon -{ -namespace timer -{ - -enum Action -{ - OFF = SD_EVENT_OFF, - ON = SD_EVENT_ON, - ONESHOT = SD_EVENT_ONESHOT -}; -} - -/** @class Timer - * @brief Provides a timer source and a mechanism to callback when the timer - * expires. - * - * The timer is armed upon construction. The constructor requires a timeout - * handler function, the timer expiry duration, and the timer state (one-shot, - * repetitive, disabled). - * It's possible to change the state of the timer after it's been armed via the - * state() API. - */ -class Timer -{ - public: - Timer() = delete; - Timer(const Timer&) = delete; - Timer& operator=(const Timer&) = delete; - Timer(Timer&&) = delete; - Timer& operator=(Timer&&) = delete; - - /** @brief Constructs timer object - * - * @param[in] events - sd_event pointer - * @param[in] callback - function callback for timer expiry - * @param[in] usec - timer duration, in micro seconds - * @param[in] action - controls the timer's lifetime - */ - Timer(sd_event* event, std::function<void()> userCallback, - std::chrono::microseconds usec, timer::Action action); - - ~Timer() - { - if (eventSource) - { - eventSource = sd_event_source_unref(eventSource); - } - } - - /** @brief Timer expiry handler - invokes callback - * - * @param[in] eventSource - Source of the event - * @param[in] usec - time in micro seconds - * @param[in] userData - User data pointer - * - */ - static int timeoutHandler(sd_event_source* eventSource, uint64_t usec, - void* userData); - - /** @brief Enables / disables the timer - * @param[in] action - controls the timer's lifetime - */ - int state(timer::Action action) - { - return sd_event_source_set_enabled(eventSource, action); - } - - timer::Action getAction() const - { - return action; - } - - std::chrono::microseconds getDuration() const - { - return duration; - } - - private: - /** @brief the sd_event structure */ - sd_event* event = nullptr; - - /** @brief Source of events */ - sd_event_source* eventSource = nullptr; - - /** @brief Optional function to call on timer expiration */ - std::function<void()> callback{}; - - /** @brief Duration of the timer */ - std::chrono::microseconds duration{}; - - /** @brief whether the timer is enabled/disabled/one-shot */ - timer::Action action = timer::OFF; -}; - -} // namespace hwmon -} // namespace phosphor |