Re: OSAtomic
Re: OSAtomic
- Subject: Re: OSAtomic
- From: Ariel Burton <email@hidden>
- Date: Mon, 18 Jan 2010 14:59:34 -0500
Cant't you do it in C like this:
int32_t /* return result of operation */
OSAtomicAnd32 ( int32_t theAmount, volatile int32_t *p )
{
int32_t mem_val;
int32_t new_val;
do
{
mem_val = *p;
new_val = mem_val & theAmount;
}
while ( ! OSAtomicCompareAndSwap32 ( mem_val, new_val, p ) );
return new_val;
}
If p is volatile, then won't the assignment to mem_val be
repeated on each iteration? If the value of *p changes
in the meantime, that is, between the time it is read here
and the time it is read again in OSAtomicCompareAndSwap32,
the operation will fail and the attempt repeated.
If it succeeds, the required effect has been achieved.
This should have the same ABA properties as the assemebler.
Ariel
==
On Mon 18 Jan 11:30, Greg Parker wrote:
> On Jan 18, 2010, at 11:04 AM, Derek Darwin Lists wrote:
> > I believe you are misreading/mistranslating the x86 assembly implementation of OSAtomicAnd32. The compare and swap operation (cmpxchg) in the "commpage" will populate GPR EAX with the value in memory on a mismatch, so the loop on failure will restart with the "latest" value of the memory operand after the failed comparison.
>
> That's right. You can't do that with C code and OSAtomicCompareAndSwap(), but x86 OSAtomicAnd32() is built atop other assembly code that is not identical to OSAtomicCompareAndSwap().
>
>
> --
> Greg Parker email@hidden Runtime Wrangler
>
>
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden