CRITICAL and return

Sorry – this maybe a repost – I’m not sure my first atempt made it through my ADSL which is playing up this morning… The question is, is it okay to return a global variable inside a critical section :- inline unsigned short MessagesToSend( void ) {     taskENTER_CRITICAL();     return m_MessagesToSend;     taskEXIT_CRITICAL(); } rather than the more secure, but slower :- inline unsigned short MessagesToSend( void ) {     unsigned short Val;     taskENTER_CRITICAL();     Val = m_MessagesToSend;     taskEXIT_CRITICAL();     return Val; } TIA Tim (@vaquita.co.uk)

CRITICAL and return

Unfortunately not.  This could/would (depending on the port) exit the function with interrupts disabled, leave the stack incorrect and thus not return correctly.

CRITICAL and return

Also note the inline request. I assume that if the compiler does inline it, it will be fine. Maybe not though. I’m just not convinced what will happen if it doesn’t. Basically, will the EXIT_CRITICAL bit get called, or will the compiler do what I’m telling it to, and just skip to the end of the routine ? : : Okay, I think I’ve answered my question. If the compiler is more clever than I think, and fix my dumb errors, then let me know… Tim