Boost logo

Boost :

Subject: Re: [boost] [interprocess] atomic_write32 on GCC/x86/x68_64 lacks memory barrier
From: Lars Hagström (lars_at_[hidden])
Date: 2014-08-11 09:38:54


Ok, I've managed, through trial and error and a bit of googling to make it
less verbose. Is this correct?

inline boost::uint32_t atomic_read32(volatile boost::uint32_t *mem)
{
  const boost::uint32_t val = *mem;
  asm volatile ( "" ::: "memory" );
  return val;
}

inline void atomic_write32(volatile boost::uint32_t *mem, boost::uint32_t
val)
{
  asm volatile
    (
     "xchgl %0, %1"
     : "+r" (val), "+m" (*mem)
     :: "memory"
    );
}

Does that "memory" statement at the end add a fence both before and after
the assembly statement? Or have I misunderstood how this works?

/Lars

On Mon, Aug 11, 2014 at 3:21 PM, Lars Hagström <lars_at_[hidden]> wrote:

> Thanks!
>
> As I said, I'm not an assembly programmer. I can copy/paste though, which
> lead to the verbosity. :-)
>
>
> On Mon, Aug 11, 2014 at 2:34 PM, Peter Dimov <lists_at_[hidden]> wrote:
>
>> Lars Hagström wrote:
>>
>> inline void atomic_write32(volatile boost::uint32_t *mem,
>>> boost::uint32_t val)
>>> {
>>> __asm__ __volatile__ ( "" ::: "memory" );
>>> __asm__ (
>>> "xchgl %0, %1"
>>> : "+r" (val), "+m" (*mem)
>>> );
>>> __asm__ __volatile__ ( "" ::: "memory" );
>>> }
>>>
>>
>> This will work, but it's more verbose than it needs to be. You can put
>> the __volatile__ and "memory" on the xchg asm statement and take out the
>> two others.
>>
>> You'll also need to fix atomic_read32:
>>
>> {
>> uint32 r = *mem;
>>
>> __asm__ __volatile__ ( "" ::: "memory" );
>> return r;
>>
>> }
>>
>> _______________________________________________
>> Unsubscribe & other changes: http://lists.boost.org/
>> mailman/listinfo.cgi/boost
>>
>
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk