11

posix standard says that things like mutex will enforce a memory sync. However, the compiler may reorder the memory access. Say we have

lock(mutex);
setdata(0);
ready = 1;
unlock(mutex);

It might be changed to code below by compiler reordering, right?

ready = 1;
lock(mutex);
setdata(0);
unlock(mutex);

So how can mutex sync the memory access? To be more precise, how do compilers know that reordering should not happen across lock/unlock?

actually here for single thread aspect, ready assignment reorder is totally safe since ready is not used in function call lock(mutex).

EDITED: So if function call is something that compiler will not get across, can we regard it as a compiler memory barrier like

asm volatile("" ::: "memory")
2
  • Do you have a sample where such a reordering happens? Since lock() and unlock() are simply function calls for the compiler, I doubt that such a reordering will happen Jan 21, 2013 at 11:52
  • No actually. I was just wondering... Jan 21, 2013 at 12:23

2 Answers 2

8

General answer is that your compiler should support POSIX if you want to use it for POSIX targets, and that support means it should know to avoid reordering across lock and unlock.

That said, this kind of knowledge is commonly achieved in a trivial way: compiler would not reorder access to (non-provably-local) data across a call to an external function which may use or modify them. It should have known something special about lock and unlock to be able to reorder.

And no, it's not that simple as "a call to global function is always a compiler barrier" -- we should add "unless the compiler knows something specific about that function". It does really happen: e.g. pthread_self on Linux (NPTL) is declared with __const__ attribute, allowing gcc to reorder across pthread_self() calls, even eliminating unnecessary calls altogether.

We can easily imagine a compiler supporting function attributes for acquire/release semantics, making lock and unlock less than a full compiler barrier.

3
  • POSIX is an API definition. As far as the compiler goes, there's nothing to support beyond making a particular library of functions available. That is... compiler tpoics such as optimization (which memory access reordering is) are fully unrelated to POSIX.
    – mah
    Jan 21, 2013 at 11:55
  • 4
    @mah See this post for what Dave Butenhof has to say about POSIX vs. compilers relation. Jan 21, 2013 at 12:01
  • wow..the posts is exactly about my question! and nice explanation! Jan 21, 2013 at 12:20
3

Compilers will not reorder things where it is not clear that it is safe. In your "what if" example, you are not proposing a reordered memory access, you're asking what if the compiler totally changes the code ordering -- and it won't. Something the compiler might do is change the order of actual memory reads/writes but not function calls (with or without respect to those memory accesses).

An example of where the compiler might reorder memory access... lets say you have this code:

a = *pAddressA;
b = *pAddressB;

and lets consider the case where the value of pAddressB is in a register while pAddressA is not. It's fair game for the compiler to read address B first, then move the value of pAddressA into that same register so that the new location can be received. If there happens to be a function call between these accesses, the compiler cannot do this.

3
  • actually here for single thread aspect, ready assignment reorder is totally safe since ready is not used in function call lock(mutex). Jan 21, 2013 at 11:54
  • @user1192878 the compiler cannot always be certain that the function call does not hold the address of a variable with intention of changing it, thus the compiler must not consider what you suggest to be safe (as a blanket statement). Compilers generally cannot optimize across functions (that are in different source files), which is what your suggestion would require.
    – mah
    Jan 21, 2013 at 11:58
  • @user1192878 We were not told whether ready is a global or a local variable and whether or not it's a volatile variable. The compiler may not know what's inside lock() and unlock(), if those are in a separate compilation unit, especially if they are implemented as assembly code in a separate file. Jan 21, 2013 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.