From 0a9e1db181f9464596c8692723425803abc59a58 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Thu, 7 May 2020 16:41:35 +0300 Subject: [PATCH] Correct Timeout rescheduling Chrono changes "optimised" `Timeout::handler` in a way that broke users who rescheduled the timeout during their attached callback. Attempted optimisation is less necessary now that `platform.callback-nontrivial` is set to false by default - that setting reduces overhead of copying the `Callback` to almost nothing. --- drivers/source/Timeout.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/source/Timeout.cpp b/drivers/source/Timeout.cpp index c6000a82797..577560885fd 100644 --- a/drivers/source/Timeout.cpp +++ b/drivers/source/Timeout.cpp @@ -27,11 +27,15 @@ namespace mbed { void TimeoutBase::handler() { if (_function) { + Callback function_to_call = _function; + // Clean up state to "detached" before calling callback; it may attach + // a new callback. Equivalent to detach(), but skips the remove(); + // it's unnecessary because we're in the ticker's handler. + _function = nullptr; if (_lock_deepsleep) { sleep_manager_unlock_deep_sleep(); } - _function(); - _function = nullptr; + function_to_call(); } }