Skip to content

Conversation

@abersnaze
Copy link

Started out as a small change to allow reentrant timer scheduling but ended up rewriting most of it.

  • changed to indirectly indexing tasks by min heap to make tick O(1) and scheduling O(log(n))
  • now supported reentrant scheduling of tasks in handlers.
  • fix a bug in cancel. if a task completes and slot reused.
  • added a debugging mode ifdef.

…d scheduling O(log(n))

now supported reentrant scheduling of tasks in handlers.
fix a bug in cancel. if a task completes and slot reused.
added a debugging mode ifdef.
@abersnaze
Copy link
Author

Here is some code I was using to test it.

#include "Timer.h"

auto timer = timer_create_default();

bool off(void*, long) {
  digitalWrite(LED_BUILTIN, LOW);
}

bool on(void*, long overdue_by) {
  digitalWrite(LED_BUILTIN, HIGH);
  timer.in(random(250 - overdue_by, 750 - overdue_by), off);
  return true;
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  timer.every(1000, on);
}

void loop() {
  timer.tick();
}

@contrem
Copy link
Owner

contrem commented May 4, 2020

Nice work. However, I think min heap is overkill for the following reasons:

  • The increase in complexity for min heap limits the number of people that can understand the code.
  • The number of timers is too small to merit a heap. It currently defaults to 16. The overhead of maintaining the heap likely outweighs any benefits.
  • The number of timers is a compile time constant, so the current implementation is technically already constant time, just a slightly larger constant.

I'm not sure that your checks handle timer overflow. Were you able to test against it?

If possible, could you provide test cases for the re-entrant and cancel problems you experienced?

Thanks for your time.

@contrem contrem closed this Nov 10, 2020
abersnaze added a commit to abersnaze/arduino-timer that referenced this pull request May 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants