Comment 12 for bug 1722849

Revision history for this message
David Brown (davidbrown) wrote :

Memory clobbers can be costly, and may not necessarily do what you want anyway unless you have a matching dsb or dmb instruction (which can be even more costly). On the Cortex M0, however, these instructions are unlikely to be needed. But for a general critical section entry/exit macro, memory clobbers are not a bad idea to match user expectation.

I agree that it makes more sense to put a sequence of assembly instructions in one asm statement rather than break them up, in the general case. However, there is good reason to split them up here. The compiler seems to be happy to move asm volatile statements around if they return a result, but not if they do not return a result (the documentation of asm volatile is not clear about this). That is why I have two statements with an artificial dependency on the second one, as shown in my earlier comment here. This has been confirmed by testing (albeit without the memory clobber). Without the memory clobber, the compiler (-O1, -mcu=cortex-M0) will re-arrange your version (with combined mrs and cpsid) from:

  CRIT_ENTRY(primask_);
  foo();
  CRIT_EXIT(primask_);

into

  foo();
  CRIT_ENTRY(primask_);
  CRIT_EXIT(primask_);

I recommend defining these macros with separate asm statements, as I gave them, and then adding memory clobbers if that suits the application or is useful for user convenience.