Boost logo

Boost :

Subject: Re: [boost] Notice: Boost.Atomic (atomic operations library)
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2009-12-04 15:55:39


Helge Bahmann wrote:
> Am Friday 04 December 2009 18:06:31 schrieb Phil Endecott:
>> Hi Helge,
>>
>> In your x86 code you have:
>>
>> bool compare_exchange_strong(T &e, T d, memory_order
>> order=memory_order_seq_cst) volatile
>> {
>> T prev=e;
>> __asm__ __volatile__("lock cmpxchgb %1, %2\n" : "=a" (prev) : "q"
>> (d), "m" (i), "a" (e) : "memory");
>> bool success=(prev==e);
>> e=prev;
>> return success;
>> }
>>
>>
>> Can you explain why 'e' is a reference and why you assign back to it?
>
> the standard requires the "found" value to be passed back, so you don't need
> to perform another "load" on retrying the operation
>
>> Maybe it would help if you could write out what that asm does in
>> pseudo-code. The kernel_cmpxchg that I have does:
>
> there is a pseudo-code explanation in the boost::atomic class documentation

Ah sorry, I didn't realise that compare_exchange_* were in the external
interface; for some reason I thought they were internal.

OK, so since my kernel_cmpxchg() function doesn't return the old value
but only a flag, I need something like:

bool success = kernel_cmpxchg(e,d,&i);
if (!success) e = i;

But i may have changed again between the kernel_cmpxchg() and that
assignment. Is that OK? Should I use load() there? If so, what
memory order is needed?

Cheers, Phil.


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