Do spin locks always require a memory barrier? Is spinning on a memory barrier expensive?

后端 未结 3 1197
日久生厌
日久生厌 2020-12-14 12:11

I wrote some lock-free code that works fine with local reads, under most conditions.

Does local spinning on a memory read necessarily imply I have to ALWAYS insert a

3条回答
  •  旧时难觅i
    2020-12-14 13:15

    Keep in mind that barriers typically are used to order sets of memory accesses, so your code could very likely also need barriers in other places. For example, it wouldn't be uncommon for the barrier requirement to look like this instead:

    while ( 1 ) {
    
        v = pShared->value;
        __acquire_barrier() ;
    
        if ( v != 0 ) {
            foo( pShared->something ) ;
        }
    }
    

    This barrier would prevent loads and stores in the if block (ie: pShared->something) from executing before the value load is complete. A typical example is that you have some "producer" that used a store of v != 0 to flag that some other memory (pShared->something) is in some other expected state, as in:

    pShared->something = 1 ;  // was 0
    __release_barrier() ;
    pShared->value = 1 ;  // was 0
    

    In this typical producer consumer scenario, you'll almost always need paired barriers, one for the store that flags that the auxiliary memory is visible (so that the effects of the value store aren't seen before the something store), and one barrier for the consumer (so that the something load isn't started before the value load is complete).

    Those barriers are also platform specific. For example, on powerpc (using the xlC compiler), you'd use __isync() and __lwsync() for the consumer and producer respectively. What barriers are required may also depend on the mechanism that you use for the store and load of value. If you've used an atomic intrinsic that results in an intel LOCK (perhaps implicit), then this will introduce an implicit barrier, so you may not need anything. Additionally, you'll likely also need to judicious use of volatile (or preferably use an atomic implementation that does so under the covers) in order to get the compiler to do what you want.

提交回复
热议问题