SMFSW / Queue

Queue handling library (designed on Arduino)
BSD 3-Clause "New" or "Revised" License
93 stars 20 forks source link

Feature request: interrupt safe #11

Closed sslupsky closed 4 years ago

sslupsky commented 5 years ago

Are there any plans to make this library interrupt safe? That is, safe to use the queue when accessing or modifying a queue in an interrupt service and the setup() and loop() functions.

SMFSW commented 5 years ago

Until now, a warning in doxygen comment is there to warn about correct implementation with di/en when Queue is used with interrupts.

We can think of various implementations, requiring di/en or not depending the context, that's why I didn't included those instructions in the Queue code. I think about 1 or 2 parameters in the constructor instructing if the Queue shall be interrupt safe for push or pop/peek maybe.

sslupsky commented 5 years ago

That would be great. I found this cross platform (Arduino platforms) library for the atomic operations. It may help, https://github.com/wizard97/SimplyAtomic

SMFSW commented 4 years ago

I finally found again why I didn't made this implementation before. It wouldn't be safe to enable/disable interrupts directly in the functions when using peek and drop functions.

Assume a LIFO queue filled by interrupt routine. If you peek an item, when returning from the function, interrupts would be enabled again. If an interrupt is triggered to fill a new item before you take decision if the previously peeked item shall de dropped, you will end up dropping the newer item, and the item you was supposed to drop is still in the queue.

Enabling/Disabling interrupts manually seems the best solution to fit any case and working without any glitch interrupt safe automation could cause.

SMFSW commented 4 years ago

Considering my last comment on the request, no change will be made. A note has been added to README.md about interrupt safe automation and why not it wouldn't be implemented, for the record.

Sorry @sslupsky, the idea was very appealing, but for some special case (sure less than 10 or even 5%), it would fall apart, so everyone should think about his own implementation and implications.. In the end, it's a good thing though, everyone should be aware of what their implementation is (considering this library is not totally designed for Arduino), as this interrupt safe mechanism is not meant for basic Arduino sketch implementation, it's almost harmless or taking basic users to approach a higher level of c/cpp coding.

SMFSW commented 4 years ago

Since there is no comment for more than a week about the enhancement proposal and the fact that I won't implement this for given reasons... I'm now closing the issue.

PS: if somehow someone have some idea/solution about this particular case (I don't think there is in C (cQueue lib), but for Cpp (Queue lib) I have some basics about cpp, but not that much practice and experience, so I might be missing. Do not hesitate to contact me or re-open the issue!