Skip to content

recurrent scheduled functions max delay update #8949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 29 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2757aa2
Revert `compute_scheduled_recurrent_grain`
dok-net Jul 1, 2023
a81e544
wip
dok-net Jul 1, 2023
588e153
Add function to retrieve the time remaining until the (next) timeout …
dok-net Nov 26, 2019
a435fa5
Rename and reformat according to review.
dok-net Nov 27, 2019
232961e
Revert _current as member change.
dok-net Nov 27, 2019
210c3d4
Fix expiredOneShot to properly stay expired forever unless reset.
dok-net Nov 27, 2019
a6456d0
Fix "reverse notation return statement"
dok-net Nov 27, 2019
9863f71
Reset() was wrecked by resetting _timeout. Reverted.
dok-net Nov 27, 2019
9c83f9a
Expired check ALWAYS executes yield policy BEFORE checking expiration…
dok-net Dec 2, 2019
4cbe06b
Reformat
dok-net Dec 3, 2019
20e6378
Fix comment, remove redundant parentheses
dok-net Dec 4, 2019
b0eb345
expired() is not const qualified in PolledTmeout.
dok-net Mar 15, 2021
a3612dd
expiredOneShot() qualifed as const, despite asymmetry with expired() …
dok-net Mar 16, 2021
c88af7b
Remove alwaysExpired, it is identical to 0 and obfuscates the code.
dok-net Apr 5, 2021
4eab7e4
Somewhat easier on the human reader.
dok-net Apr 5, 2021
b5285c3
Avoid possible unnecessary time conversion.
dok-net Apr 5, 2021
b54576f
Fix one-shot mode in resetToNeverExpires(). Add convenient stop() syn…
dok-net Apr 5, 2021
100d2c4
Due to review and discussion, _oneShotExpired removed again.
dok-net Apr 5, 2021
81b90c9
Revert removal of PolledTimeout::alwaysExpired
dok-net Jul 1, 2023
eaf05e9
Finalize scheduled recurrent function delay logic.
dok-net Jul 1, 2023
a09726d
Fix millis/micros mismatch.
dok-net Jul 1, 2023
4d3655b
ISR safety.
dok-net Jul 1, 2023
55d465a
Returning max delay value that is uint-wrap around safe.
dok-net Jul 2, 2023
c41a2c2
Add pseudo-get delay function for scheduled functions
dok-net Jul 2, 2023
4e654d1
Worked out input from PR review.
dok-net Jul 20, 2023
717f4ad
use single constexpr definition instead of repeated expression.
dok-net Jul 27, 2023
538025a
Simplified roll-over logic
dok-net Jul 30, 2023
861cf02
Add rationale for HALF_MAX_MICROS
dok-net Jul 30, 2023
b3b4aa0
Use CAS to minimize critical sections.
dok-net Sep 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use CAS to minimize critical sections.
  • Loading branch information
dok-net committed Jan 5, 2024
commit b3b4aa0e8717e2144eba55d0c084bb914f4f3272
37 changes: 22 additions & 15 deletions cores/esp8266/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Schedule.h"
#include "PolledTimeout.h"
#include "interrupts.h"
#include <atomic>

typedef std::function<void(void)> mSchedFuncT;
struct scheduled_fn_t
Expand All @@ -47,7 +48,7 @@ struct recurrent_fn_t
static recurrent_fn_t* rFirst = nullptr;
static recurrent_fn_t* rLast = nullptr;
// The target time for scheduling the next timed recurrent function
static decltype(micros()) rTarget;
static std::atomic<decltype(micros())> rTarget;

// As 32 bit unsigned integer, micros() rolls over every 71.6 minutes.
// For unambiguous earlier/later order between two timestamps,
Expand Down Expand Up @@ -133,13 +134,17 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,

esp8266::InterruptLock lockAllInterruptsInThisScope;

// prevent new item overwriting an already expired rTarget.
const auto now = micros();
const auto itemRemaining = item->callNow.remaining();
const auto remaining = rTarget - now;
if (!rFirst || (remaining <= HALF_MAX_MICROS && remaining > itemRemaining))
for (auto _rTarget = rTarget.load(); ;)
{
rTarget = now + itemRemaining;
const auto remaining = _rTarget - now;
if (!rFirst || (remaining <= HALF_MAX_MICROS && remaining > itemRemaining))
{
// if (!rTarget.compare_exchange_weak(_rTarget, now + itemRemaining)) continue;
rTarget = now + itemRemaining; // interrupt lock is active, no ABA issue
}
break;
}

if (rLast)
Expand All @@ -158,9 +163,8 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
decltype(micros()) get_scheduled_recurrent_delay_us()
{
if (!rFirst) return HALF_MAX_MICROS;
// handle already expired rTarget.
const auto now = micros();
const auto remaining = rTarget - now;
const auto remaining = rTarget.load() - now;
return (remaining <= HALF_MAX_MICROS) ? remaining : 0;
}

Expand Down Expand Up @@ -233,9 +237,9 @@ void run_scheduled_recurrent_functions()
recurrent_fn_t* prev = nullptr;
bool done;

rTarget.store(micros() + HALF_MAX_MICROS);
// prevent scheduling of new functions during this run
stop = rLast;
rTarget = micros() + HALF_MAX_MICROS;

do
{
Expand Down Expand Up @@ -268,17 +272,20 @@ void run_scheduled_recurrent_functions()
}
else
{
esp8266::InterruptLock lockAllInterruptsInThisScope;

// prevent current item overwriting an already expired rTarget.
const auto now = micros();
const auto currentRemaining = current->callNow.remaining();
const auto remaining = rTarget - now;
if (remaining <= HALF_MAX_MICROS && remaining > currentRemaining)
for (auto _rTarget = rTarget.load(); ;)
{
rTarget = now + currentRemaining;
const auto remaining = _rTarget - now;
if (remaining <= HALF_MAX_MICROS && remaining > currentRemaining)
{
// if (!rTarget.compare_exchange_weak(_rTarget, now + currentRemaining)) continue;
esp8266::InterruptLock lockAllInterruptsInThisScope;
if (rTarget != _rTarget) { _rTarget = rTarget; continue; }
rTarget = now + currentRemaining;
}
break;
}

prev = current;
current = current->mNext;
}
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy