[En-Nut-Discussion] NutEnterCritical with multiple local variables

Dušan dferbas at etech.cz
Sun Nov 22 21:24:40 CET 2015


Hi guys,

I think the only important thing is, how the "target" variable is accessed.
I.e. the variable from which we load its value or where we store value.
It does not matter, what code (compiler) does with the variable value 
after (before).

I understand, you are discussing about accessing variables.
I would like to ask also about the original macros, which are using 
local variable to store uP status.

My idea was to declare the local variable with macro UseCritical, see 
example below.
Maybe this is tiring, but it can be better optimized by compiler.
If someone knows more about this, I would like to hear other opinion.

Instead of e.g.:

int function(...)
{
   ...
   NutEnterCritical();
 >  {
 >  int    temp_status;
 >  ...
 >  }
   ...
   NutExitCritical();

   NutEnterCritical();
 >  {
 >  int    temp_status;
 >  ...
 >  }
   ...
   NutExitCritical();
   ...
}

There will be:

int function(...)
{
NutUseCritical();
 >  int    temp_status;
   ...
   NutEnterCritical();
 >  ... just use the variable
   ...
   NutExitCritical();

   NutEnterCritical();
 >  ... just use the variable
   ...
   NutExitCritical();
   ...
}

*Dušan*


On 19.11.2015 16:39, Nathan Moore wrote:
> On Thu, Nov 19, 2015 at 6:00 AM, Uwe Bonnes<
> bon at elektron.ikp.physik.tu-darmstadt.de>  wrote:
>
>    
>> can you explain why you use do not use:
>>
>> #define NutCriticalUpdate( VARIABLE, VALUE)               \
>>       do {                                                 \
>>                size_t sz_ = sizeof(typeof(VARIABLE));      \
>>                size_t sza_ = sizeof(sig_atomic_t);         \
>>                typeof(VARIABLE) * VP_ =&(VARIABLE);       \
>>                typeof(VALUE) VAL_ = (VALUE);               \
>>                if (sz_<= sza_&&  is_alligned(VP_)  {      \
>>                       *VP_ = VAL_;                         \
>>                } else {                                    \
>>                     NutEnterCritical();                    \
>>                     *VP_ = VAL_;                           \
>>                     NutExitCritical();                     \
>>                }                                           \
>>         } while (0)
>>
>> Thanks
>>
>>
>>      
> The difference there being that you used<= for the size comparison rather
> than ==.
>
> I did not write this particular macro to be the 100% accurate everywhere
> but rather
> to demonstrate an idea, but I did use == on purpose.  The reason for this
> is that on
> 32 bit RISC systems if had the following code:
>
>      struct thingy {
>                uint8_t a;
>                uint8_t b;
>                uint16_t c;
>      };
>
>      struct thingy x;
>      ...
>      x.c = 4;
>
> then, you would likely end up with code like:
>
>       load   r1 from address of x
>       and   r1, 0x00ff
>       load  r2, 0x0400
>       or     r1,r2
>       store  r1 into address of x
>
> while if you were to do
>
>       struct thingy y;
>       y.a = 0;
>       y.b = 0;
>       y.c = 4;
>
>       x = y;
>
> you'd end up with something like:
>
>      load r1 with constant value 0x0400;
>      store r1 to address of x
>
> With the assignment of the value that is smaller than the native word size
> you end up with several instructions for the operation so an interrupt might
> happen that changes the a and/or b members.
>
> Thanks,
> Nathan
> _______________________________________________
> http://lists.egnite.de/mailman/listinfo/en-nut-discussion
>
>    


More information about the En-Nut-Discussion mailing list