mardi 3 septembre 2019

C++11 memory ordering support in GCC

GCC does not seem to support the different memory ordering settings as it generates the same code for relaxed, acquire and sequentially consistent.

I tried the following code with GCC 7.4 and 9.1:

#include <thread>
#include <atomic>

using namespace std;

atomic<int> z(0);


void Thr1()
{
  z.store(1,memory_order_relaxed);
}

void Thr2()
{
  z.store(2,memory_order_release);
}

void Thr3()
{
  z.store(3);
}

//------------------------------------------
int main (int argc, char **argv)
{
    thread t1(Thr1);
    thread t2(Thr2);
    thread t3(Thr3);
    t1.join();
    t2.join();
    t3.join();
    return 0;
}

When I generate assembly for the above, I get the following for each of the three functions:

_Z4Thr1v:
.LFB2992:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $16, %rsp
    movl    $1, -12(%rbp)
    movl    $0, -8(%rbp)
    movl    -8(%rbp), %eax
    movl    $65535, %esi
    movl    %eax, %edi
    call    _ZStanSt12memory_orderSt23__memory_order_modifier
    movl    %eax, -4(%rbp)
    movl    -12(%rbp), %edx
    leaq    z(%rip), %rax
    movl    %edx, (%rax)
    mfence
    nop
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE2992:
    .size   _Z4Thr1v, .-_Z4Thr1v
    .globl  _Z4Thr2v
    .type   _Z4Thr2v, @function
_Z4Thr2v:
.LFB2993:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $16, %rsp
    movl    $2, -12(%rbp)
    movl    $3, -8(%rbp)
    movl    -8(%rbp), %eax
    movl    $65535, %esi
    movl    %eax, %edi
    call    _ZStanSt12memory_orderSt23__memory_order_modifier
    movl    %eax, -4(%rbp)
    movl    -12(%rbp), %edx
    leaq    z(%rip), %rax
    movl    %edx, (%rax)
    mfence
    nop
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE2993:
    .size   _Z4Thr2v, .-_Z4Thr2v
    .globl  _Z4Thr3v
    .type   _Z4Thr3v, @function
_Z4Thr3v:
.LFB2994:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $16, %rsp
    movl    $3, -12(%rbp)
    movl    $5, -8(%rbp)
    movl    -8(%rbp), %eax
    movl    $65535, %esi
    movl    %eax, %edi
    call    _ZStanSt12memory_orderSt23__memory_order_modifier
    movl    %eax, -4(%rbp)
    movl    -12(%rbp), %edx
    leaq    z(%rip), %rax
    movl    %edx, (%rax)
    mfence
    nop
    leave
    .cfi_def_cfa 7, 8
    ret

where all of the code ends in a memory fence instruction.

Aucun commentaire:

Enregistrer un commentaire