-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Improved Periodic class #9501
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
base: master
Are you sure you want to change the base?
Improved Periodic class #9501
Conversation
@harry-iii-lord, Welcome to Meshtastic!Thanks for opening your first pull request. We really appreciate it. We discuss work as a team in discord, please join us in the #firmware channel. Welcome to the team 😄 |
|
Looks like this PR unintentionally picked up changes in the device-install scripts. The flash size change is a mere 80 bytes. |
Indeed. It wasn't intention :) and I see you fix that. Thank you. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Extends the concurrency::Periodic thread helper to support “modern” C++ callables (via std::function) in addition to the existing legacy function-pointer callback, enabling lambdas and member-function captures while keeping source compatibility.
Changes:
- Add
std::function<int32_t()>-based constructor overload forPeriodic. - Replace the raw function pointer member with a
std::functioncallback invocation path. - Update class docstring to reflect dual callback support.
src/concurrency/Periodic.h
Outdated
| #include <functional> | ||
|
|
||
| #include "concurrency/OSThread.h" | ||
|
|
||
| namespace concurrency | ||
| { | ||
|
|
||
| /** | ||
| * @brief Periodically invoke a callback. This just provides C-style callback conventions | ||
| * rather than a virtual function - FIXME, remove? | ||
| * @brief Periodically invoke a callback. | ||
| * Supports both legacy function pointers and modern callables. | ||
| */ | ||
| class Periodic : public OSThread | ||
| { | ||
| int32_t (*callback)(); | ||
|
|
||
| public: | ||
| // callback returns the period for the next callback invocation (or 0 if we should no longer be called) | ||
| Periodic(const char *name, int32_t (*_callback)()) : OSThread(name), callback(_callback) {} | ||
| Periodic(const char *name, int32_t (*cb)()) : OSThread(name), m_callback(cb) {} | ||
| Periodic(const char *name, std::function<int32_t()> cb) : OSThread(name), m_callback(std::move(cb)) {} |
Copilot
AI
Feb 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::move(cb) is used in the std::function constructor, but this header does not include <utility> (the standard header that provides std::move). Relying on it being pulled in transitively via <functional> is not portable; please add #include <utility> (or avoid std::move here).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That make sense. I will add it.
src/concurrency/Periodic.h
Outdated
|
|
||
| protected: | ||
| int32_t runOnce() override { return callback(); } | ||
| int32_t runOnce() override { return m_callback ? m_callback() : 0; } |
Copilot
AI
Feb 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m_callback is declared under protected:, which changes it from the previous implicit-private member (callback) and exposes it to subclasses. To keep the Periodic scheduling behavior encapsulated and avoid unintended mutation from derived classes, make the callback member private (keeping only runOnce() protected).
| int32_t runOnce() override { return m_callback ? m_callback() : 0; } | |
| int32_t runOnce() override { return m_callback ? m_callback() : 0; } | |
| private: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Wil change that.
src/concurrency/Periodic.h
Outdated
| int32_t runOnce() override { return m_callback ? m_callback() : 0; } | ||
| std::function<int32_t()> m_callback; |
Copilot
AI
Feb 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Member naming here (m_callback) introduces an m_ prefix that doesn’t match the surrounding concurrency code’s member style (typically plain camelCase, e.g. controller in concurrency/OSThread.h:33 and notification in concurrency/NotifiedWorkerThread.h:16). Consider renaming to callback/callbackFn (and keep consistent casing) to match established conventions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This make sense too.
27f8d70 to
bf55b5f
Compare
Hello folks.
I wanted to propose a small extension of current Periodic class to make it more c++ friendly (or so called more modern). Idea behind is to use std::function to hold callback.
Having class in such form give you ability of making nice encapsulation of functionalities without need to declaring C-style callbacks, for instance this way:
Proposed code is keeping existing C-style ctor, thus such change should be backward compatible.
Unfortunately such change is not binary equal (there is a member
m_callback) and is bit bigger. Not sure this is that big issue. Leaving to your consideration.Happy reviewing.