We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, to disable and re-enable interrupts in a safe way, I wrote the following class:
class DisableIntsShortly { public: __forceinline DisableIntsShortly(){ __disable_irq(); } __forceinline ~DisableIntsShortly() {__enable_irq(); } };
Thus then I should be able to protect my code with non-atomic access to interrupt variables like this:
{ DisableIntsShortly DI(); ... do some things where interrupt needs to be disabled. }
Unfortunately this does not work - the DI is not used inside this block, therefore the optimizer seems to think that it is useless and just kills it.
To come around this, I tried it like this:
class DisableIntsShortly { public: __forceinline DisableIntsShortly( int i){ __disable_irq(); i=i; } __forceinline ~DisableIntsShortly() {__enable_irq(); } };
Then it works if I write this (at least in optimization level 1):
{ DisableIntsShortly DI( 0); ... do some things where interrupt needs to be disabled. }
Just one severe problem: If I write "DisableIntsShortly DI();" (omit the 0), then NO ERROR appears (and again the optimizer decides to just kill it - without any warning or error - could some people at Keil please solve this? Here definitely an error should appear.
(And further the optimizer should not remove class instances with custructor and/or destructor functions).
The compiler is not wrong and not optimising anything away. This:
DisableIntsShortly DI();
does not create an object; it declares a function that takes no arguments and returns an object.
Write it without the () to create an object.
See also www.parashift.com/.../empty-parens-in-object-decl.html
Ups - what a great idea - I really did not think about this.
I just tried in Visual C++ - you are right, Visual C++ also accepts this. But Visual C++ gives a very nice warning in such a case:
"warning C4930: 'DisableIntsShortly DI(void)': Function with prototype was not invoked (was a variable declaration intended?)"
I feel much more confident in my code if Keil C++ could also give such a warning in such a case... .