void *a = malloc(10);

Results with:

        movl    $10, %edi
        call    malloc
        movq    %rax, -8(%rbp)
  void *a = 0;
  __sync_bool_compare_and_swap(&a, 0, malloc(10));

Results with:

        movl    $10, %edi
        call    malloc
        movq    %rax, %rdx
        movl    $0, %eax
        lock cmpxchgq   %rdx, -8(%rbp)
  void *a = 0;
  __sync_lock_test_and_set(&a, malloc(10));

Results with:

        movq    $0, -8(%rbp)
        movl    $10, %edi
        call    malloc
        xchgq   -8(%rbp), %rax

The LOCK prefix essentially means that the current CPU has ownership of the cache line for the duration of the operation, so you get the atomicity for the memory access. All the internet says that xchgq has implicit LOCK if one of it's arguments is a memory address, which is the case here, so indeed __sync_bool_compare_and_swap and __sync_lock_test_and_set should roughly have the same penalty.


Reply to this email directly or view it on GitHub.



Use REPLY-ALL to reply to list

To unsubscribe from the XROOTD-DEV list, click the following link:
https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=XROOTD-DEV&A=1