Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22024

Read optimizations on shared memory

$
0
0

Suppose you have a function that make several read access to a shared variable whose access is atomic. All in running in the same process. Imagine them as threads of a process or as a sw running on bare metal platform with no MMU.

As a requirement you must ensure that the value of that read is consistent for all the length of the function so the code must not re-read the memory location and have to put in a local variable or on a register. How can we ensure that this behaviour is respected?

As an example...

shared is the only shared variable

extern uint32_t a, b, shared;

void useless_function()
{
  __ASM volatile ("":::"memory");
  uint32_t value = shared;
  a = value *2;
  b = value << 3;
}

Can value be optimized out by direct readings of shared variable in some contexts? If yes, how can I be sure this cannot happen?


Viewing all articles
Browse latest Browse all 22024

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>