From 78a5df906f563c99c451ea2355917ce414685a30 Mon Sep 17 00:00:00 2001 From: Alexander Soldatov Date: Fri, 23 Nov 2018 14:37:50 +0300 Subject: Saving current context in DeferrableCallback added. DeferrableCallback operator() saves the context in self instance. When the timer expires, it calls operator() for the ConditionalCallback with the context saved in DeferrableCallback instance. Resolves openbmc/phosphor-dbus-monitor#1 Change-Id: I8d235b0747005c403829f8262d0290548b0a9910 Signed-off-by: Alexander Soldatov --- src/callback.hpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/callback.hpp b/src/callback.hpp index 0be7dc0..b1c8d0b 100644 --- a/src/callback.hpp +++ b/src/callback.hpp @@ -227,6 +227,12 @@ class DeferrableCallback : public ConditionalCallback { } + /** @brief Start internal timer if the condition is satisfied. + * + * When the timer expires, it calls operator() for the + * ConditionalCallback with the context saved in + * DeferrableCallback instance. + */ void operator()(Context ctx) override { if (!timer) @@ -234,8 +240,10 @@ class DeferrableCallback : public ConditionalCallback timer = std::make_unique( sdeventplus::Event::get_default(), // **INDENT-OFF** - [ctx, this](auto& source) { - this->ConditionalCallback::operator()(ctx); + [this](auto& source) { + // The timer uses the context saved on timer enable + this->ConditionalCallback::operator()( + this->ctx); }); // **INDENT-ON** } @@ -245,6 +253,8 @@ class DeferrableCallback : public ConditionalCallback if (!timer->isEnabled()) { // This is the first time the condition evaluated. + // Save current context for timer use. + this->ctx = ctx; // Start the countdown. timer->restartOnce(delayInterval); } @@ -262,6 +272,9 @@ class DeferrableCallback : public ConditionalCallback /** @brief Delegated timer functions. */ std::unique_ptr timer; + + /** @brief Current context for timer. */ + Context ctx; }; } // namespace monitoring -- cgit v1.2.1