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
Show file tree
Hide file tree
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
Next Next commit
use single constexpr definition instead of repeated expression.
  • Loading branch information
dok-net committed Jan 5, 2024
commit 717f4ad4a5e68e3991350b32b7f2dec6160f1777
21 changes: 11 additions & 10 deletions cores/esp8266/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "Schedule.h"
#include "PolledTimeout.h"
#include "interrupts.h"
#include "coredecls.h"

typedef std::function<void(void)> mSchedFuncT;
struct scheduled_fn_t
Expand Down Expand Up @@ -50,6 +49,8 @@ static recurrent_fn_t* rLast = nullptr;
// The target time for scheduling the next timed recurrent function
static decltype(micros()) rTarget;

constexpr decltype(micros()) HALF_MAX_MICROS = ~static_cast<decltype(micros())>(0) >> 1;

// Returns a pointer to an unused sched_fn_t,
// or if none are available allocates a new one,
// or nullptr if limit is reached
Expand Down Expand Up @@ -106,7 +107,7 @@ bool schedule_function(const std::function<void(void)>& fn)

IRAM_ATTR // (not only) called from ISR
bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
uint32_t repeat_us, const std::function<bool(void)>& alarm)
decltype(micros()) repeat_us, const std::function<bool(void)>& alarm)
{
assert(repeat_us < decltype(recurrent_fn_t::callNow)::neverExpires); //~26800000us (26.8s)

Expand All @@ -126,7 +127,7 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
const auto now = micros();
const auto itemRemaining = item->callNow.remaining();
const int32_t remaining = rTarget - now;
if (!rFirst || (remaining > 0 && static_cast<uint32_t>(remaining) > itemRemaining))
if (!rFirst || (remaining > 0 && static_cast<decltype(micros())>(remaining) > itemRemaining))
{
rTarget = now + itemRemaining;
}
Expand All @@ -144,17 +145,17 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
return true;
}

uint32_t get_scheduled_recurrent_delay_us()
decltype(micros()) get_scheduled_recurrent_delay_us()
{
if (!rFirst) return ~static_cast<decltype(micros())>(0) >> 1;
if (!rFirst) return HALF_MAX_MICROS;
// handle already expired rTarget.
const int32_t remaining = rTarget - micros();
return (remaining > 0) ? static_cast<uint32_t>(remaining) : 0;
return (remaining > 0) ? static_cast<decltype(micros())>(remaining) : 0;
}

uint32_t get_scheduled_delay_us()
decltype(micros()) get_scheduled_delay_us()
{
return sFirst ? 0 : ~static_cast<decltype(micros())>(0) >> 1;
return sFirst ? 0 : HALF_MAX_MICROS;
}

void run_scheduled_functions()
Expand Down Expand Up @@ -223,7 +224,7 @@ void run_scheduled_recurrent_functions()

// prevent scheduling of new functions during this run
stop = rLast;
rTarget = micros() + (~static_cast<decltype(micros())>(0) >> 1);
rTarget = micros() + HALF_MAX_MICROS;

do
{
Expand Down Expand Up @@ -262,7 +263,7 @@ void run_scheduled_recurrent_functions()
const auto now = micros();
const auto currentRemaining = current->callNow.remaining();
const int32_t remaining = rTarget - now;
if (remaining > 0 && static_cast<uint32_t>(remaining) > currentRemaining)
if (remaining > 0 && static_cast<decltype(micros())>(remaining) > currentRemaining)
{
rTarget = now + currentRemaining;
}
Expand Down
8 changes: 5 additions & 3 deletions cores/esp8266/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <functional>
#include <stdint.h>

#include "coredecls.h"

#define SCHEDULED_FN_MAX_COUNT 32

// The purpose of scheduled functions is to trigger, from SYS stack (like in
Expand All @@ -42,7 +44,7 @@
// get_scheduled_recurrent_delay_us() is used by delay() to give a chance to
// all recurrent functions to run per their timing requirement.

uint32_t get_scheduled_recurrent_delay_us();
decltype(micros()) get_scheduled_recurrent_delay_us();

// scheduled functions called once:
//
Expand All @@ -65,7 +67,7 @@ uint32_t get_scheduled_recurrent_delay_us();
// values, viz. 0 in case of any pending scheduled functions, or a large delay time if
// there is no function in the queue.

uint32_t get_scheduled_delay_us();
decltype(micros()) get_scheduled_delay_us();

bool schedule_function (const std::function<void(void)>& fn);

Expand Down Expand Up @@ -93,7 +95,7 @@ void run_scheduled_functions();
// any remaining delay from repeat_us is disregarded, and fn is executed.

bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
uint32_t repeat_us, const std::function<bool(void)>& alarm = nullptr);
decltype(micros()) repeat_us, const std::function<bool(void)>& alarm = nullptr);

// Test recurrence and run recurrent scheduled functions.
// (internally called at every `yield()` and `loop()`)
Expand Down
3 changes: 2 additions & 1 deletion cores/esp8266/core_esp8266_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ bool esp_try_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uin
}

// compute greatest delay interval with respect to scheduled recurrent functions
const uint32_t max_delay_ms = std::min(intvl_ms, get_scheduled_recurrent_delay_us() / 1000);
const uint32_t scheduled_recurrent_delay_ms = get_scheduled_recurrent_delay_us() / 1000UL;
const uint32_t max_delay_ms = std::min(intvl_ms, scheduled_recurrent_delay_ms);

// recurrent scheduled functions will be called from esp_delay()->esp_suspend()
esp_delay(std::min((timeout_ms - expired), max_delay_ms));
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