Skip to content
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

Add light versions. #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add light versions. #32

wants to merge 1 commit into from

Conversation

stanmain
Copy link

Light versions of Thread and StaticThreadController that uses less memory.
Maximum period is only 32,767 seconds.
It is recommended to use only in case of a memory deficit.

Light versions of Thread and StaticThreadController that uses less memory.
Copy link
Contributor

@CAHEK7 CAHEK7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it can be good lightweight standalone solution, but doesn't seem it is compatible with current library. Basically you can use either one kind of threads or another but can't mix it together. Both normal and lite threads have some significant differences which may affect application design (I mean enabling\disabling functionality and inheritance).

#endif

#define TIME_INT uint16_t
#define TIME_OVERFLOW 0x8000 // uint16_t = 0x8000 or uint32_t = 0x80000000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just curious why do you define a new type in that way? Do you need extra flexibility to easily change underlying type? Don't you consider to make a template class in this case?
Anyway, why do you use define instead of typedef or using?
Also overflow value can be defined based on type information, something like this:

using TIME_INT=uint16_t;
constexpr TIME_INT TIME_OVERFLOW = (1 << (sizeof (TIME_INT) * 8 - 1));

Seems it should be done in the mainline too (instead of using 0x80000000)...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for giving this correction. I used bad practice before.

// If the "sign" bit is set the signed difference would be negative
bool time_remaining = (time - _cached_next_run) & TIME_OVERFLOW;

// Exceeded the time limit, AND is enabled? Then should run...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Exceeded the time limit? Then should run...

ThreadLite is always enabled :)

ThreadLite::ThreadLite(void (*callback)(void), TIME_INT _interval) {
_onRun = callback;
_cached_next_run = 0;
last_run = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoiding to initialize last_run may lead to missed first interval.
For example, if we set very small interval, for example 10ms and current millis() is greater than 32780 (we still use unsigned type, so it can be) then _cached_next_run will be 10 (lasr_run + interval) and sign bit will be set for all the operations until we overflow and thus should_run() will return false.
So for the first run we have to wait around 32 seconds instead of 10ms.

It's kind of rare situation, but I think last_run should be initialized to millis() here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I had a special case in my project and could not use initialization to save memory.

@stanmain
Copy link
Author

stanmain commented Apr 24, 2018

The lite version is really incompatible with the original library. This is an alternative. It would be better to create a new lite library instead of the merger? And how is it better to do this? I do not have the experience of pulling a request, but I think that this version can help someone.

@CAHEK7
Copy link
Contributor

CAHEK7 commented Apr 24, 2018

I'm not the maintainer of this project, so I can't make a decision. But I guess the best solution is to make separate library (with proper documentation and examples) and put some cross-reference to it from this project to help people to find lightweight version.
Hope @ivanseidel can take a look and comment.

@ivanseidel
Copy link
Owner

Hi @stanmain! Thanks for reaching out and making contribution to the library!

We need to understand how compatible this can be to choose if it will be merged into ArduinoThread, or a new Library (we could reference it in this case in the README).

I have a few questions to guide us during this process.

  • As I can see, the both versions are very different. One would be able to use both Thread and ThreadLite inside a ThreadController? If not, would that be possible altering the code?

  • Do you think users will get confused by not being able to use both classes in the same project? Also, using both original and lite one, would cost even more than what the Lite itself proposes to help right?

  • How much overhead/program-space is reduced with this proposal?

@CAHEK7 thank you very much for the help!

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.

3 participants