Is function call an effective memory barrier for modern platforms?

后端 未结 4 1261
盖世英雄少女心
盖世英雄少女心 2020-12-04 11:12

In a codebase I reviewed, I found the following idiom.

void notify(struct actor_t act) {
    write(act.pipe, \"M\", 1);
}
// thread A sending data to thread          


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 11:33

    In practice, a function call is a compiler barrier, meaning that the compiler will not move global memory accesses past the call. A caveat to this is functions which the compiler knows something about, e.g. builtins, inlined functions (keep in mind IPO!) etc.

    So a processor memory barrier (in addition to a compiler barrier) is in theory needed to make this work. However, since you're calling read and write which are syscalls that change the global state, I'm quite sure that the kernel issues memory barriers somewhere in the implementation of those. There is no such guarantee though, so in theory you need the barriers.

提交回复
热议问题