Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #81342

Re: DCAS-supplement ?

From "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Newsgroups comp.lang.c++
Subject Re: DCAS-supplement ?
Date 2021-09-19 22:40 -0700
Organization Aioe.org NNTP Server
Message-ID <si96t8$1jv$1@gioia.aioe.org> (permalink)
References <si7njq$udv$1@dont-email.me> <si8dkm$6cv$1@gioia.aioe.org> <si94nf$vf0$1@dont-email.me> <si95lr$1lsg$1@gioia.aioe.org> <si961b$mhl$1@dont-email.me>

Show all headers | View raw


On 9/19/2021 10:26 PM, Bonita Montero wrote:
> Am 20.09.2021 um 07:19 schrieb Chris M. Thomasson:
>> On 9/19/2021 10:03 PM, Bonita Montero wrote:
>>> Am 20.09.2021 um 00:29 schrieb Chris M. Thomasson:
>>>> On 9/19/2021 9:13 AM, Bonita Montero wrote:
>>>>> I just wanted to test if my compiler recognizes ...
>>>>>      atomic<pair<uintptr_t, uintptr_t>
>>>>> ... and uses DCASes on it or if it supplements the pair with a usual
>>>>> lock. 
>>>>
>>>> [...]
>>>>
>>>> Can you get this to compile on your MSVC?
>>>> _________________________
>>>> #include <iostream>
>>>> #include <atomic>
>>>> #include <xmmintrin.h>
>>>>
>>>> int main()
>>>> {
>>>>      std::atomic<__int64> foo_64;
>>>>      std::atomic<__m128> foo_128;
>>>>
>>>>      std::cout << sizeof(__int64) << "\n";
>>>>      std::cout << sizeof(__m128) << "\n";
>>>>
>>>>      std::cout << foo_64.is_lock_free() << "\n";
>>>>      std::cout << foo_128.is_lock_free() << "\n";
>>>>
>>>>      return 0;
>>>> }
>>>> _________________________
>>>>
>>>
>>> That's not what I wanted to test.
>>> Read this thread:
>>>
>>> https://stackoverflow.com/questions/69245183/dcas-alternative-with-no-help-of-the-kernel 
>>>
>>
>> A quote:
>>
>> "I just wanted to test if my compiler recognizes ...and uses DCASes on 
>> it or if it supplements the pair with a usual lock"
>>
>> Well, is_lock_free() should help?
> 
> I put the compare_exchangE_weak-code into a noinline function:
> 
> struct uip_pair
> {
>      uip_pair() = default;
>      uip_pair( uintptr_t first, uintptr_t second ) :
>          first( first ),
>          second( second )
>      {
>      }
>      uintptr_t first, second;
> };
> 
> using atomic_pair = atomic<uip_pair>;
> 
> #if defined(_MSC_VER)
>      #define NOINLINE __declspec(noinline)
> #elif defined(__GNUC__) || defined(__clang__)
>      #define NOINLINE __attribute((noinline))
> #endif
> 
> NOINLINE
> bool cmpXchg( atomic_pair &ap, uip_pair &cmp, uip_pair xchg )
> {
>      return ap.compare_exchange_weak( cmp, xchg, memory_order_relaxed, 
> memory_order_relaxed );
> }
> 
> ?cmpXchg@@YA_NAEAU?$atomic@Uuip_pair@@@std@@AEAUuip_pair@@U3@@Z PROC
> cmpXchg, COMDAT
>      mov    eax, 1
>      mov    r10, rcx
>      mov    r9d, eax
>      xchg   DWORD PTR [rcx], eax
>      test   eax, eax
>      je     SHORT $LN14@cmpXchg
> $LL13@cmpXchg:
>      mov    eax, DWORD PTR [rcx]
>      test   eax, eax
>      je     SHORT $LN16@cmpXchg
> $LL15@cmpXchg:
>      mov    eax, r9d
>      test   r9d, r9d
>      je     SHORT $LN38@cmpXchg
>      npad   1
> $LL19@cmpXchg:
>      pause
>      sub    eax, 1
>      jne    SHORT $LL19@cmpXchg
>      cmp    r9d, 64
>      jl     SHORT $LN38@cmpXchg
>      lea    r9d, QWORD PTR [rax+64]
>      jmp    SHORT $LN22@cmpXchg
> $LN38@cmpXchg:
>      add    r9d, r9d
> $LN22@cmpXchg:
>      mov    eax, DWORD PTR [rcx]
>      test   eax, eax
>      jne    SHORT $LL15@cmpXchg
> $LN16@cmpXchg:
>      mov    eax, 1
>      xchg   DWORD PTR [rcx], eax
>      test   eax, eax
>      jne    SHORT $LL13@cmpXchg
> $LN14@cmpXchg:
>      mov    rax, QWORD PTR [rcx+8]
>      sub    rax, QWORD PTR [rdx]
>      jne    SHORT $LN39@cmpXchg
>      mov    rax, QWORD PTR [rcx+16]
>      sub    rax, QWORD PTR [rdx+8]
> $LN39@cmpXchg:
>      test   rax, rax
>      sete   al
>      test   al, al
>      je     SHORT $LN6@cmpXchg
>      movups xmm0, XMMWORD PTR [r8]
>      movups XMMWORD PTR [rcx+8], xmm0
>      xor    ecx, ecx
>      xchg   DWORD PTR [r10], ecx
>      ret    0
> $LN6@cmpXchg:
>      movups xmm0, XMMWORD PTR [rcx+8]
>      xor    ecx, ecx
>      movups XMMWORD PTR [rdx], xmm0
>      xchg   DWORD PTR [r10], ecx
>      ret    0
> ?cmpXchg@@YA_NAEAU?$atomic@Uuip_pair@@@std@@AEAUuip_pair@@U3@@Z ENDP
> 
> Maybe someone here understands what the above assembly-code does.
> 

Need to examine it, however there is a way to make a lock without using 
LOCK'ed anything in x86, x64. BTW, did you know that xchg has an implied 
LOCK prefix? So, it looks locked to me. What does is_lock_free say?

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

DCAS-supplement ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-19 18:13 +0200
  Re: DCAS-supplement ? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-09-19 15:29 -0700
    Re: DCAS-supplement ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-20 07:03 +0200
      Re: DCAS-supplement ? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-09-19 22:19 -0700
        Re: DCAS-supplement ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-20 07:26 +0200
          Re: DCAS-supplement ? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-09-19 22:40 -0700
          Re: DCAS-supplement ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-20 07:53 +0200
            Re: DCAS-supplement ? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-09-20 16:02 -0700
              Re: DCAS-supplement ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-21 09:31 +0200
                Re: DCAS-supplement ? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-09-21 16:09 -0700

csiph-web