Re: OSAtomic
Re: OSAtomic
- Subject: Re: OSAtomic
- From: Derek Darwin Lists <email@hidden>
- Date: Mon, 18 Jan 2010 11:04:39 -0800
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.
- D
On Mon, Jan 18, 2010 at 4:31 AM,
<email@hidden> wrote:
Hello,
I've been looking at the set of operations defined
in OSAtomic.h. The manual page describes the logical
operations (and, or, and xor) as being "layered on top
of the OSAtomicCompareAndSwap() primitives". From what
I can tell looking at the assmember for OSAtomicAnd32,
it is implemented (at least on x86) something like this:
int32_t /* return result of operation */
OSAtomicAnd32 ( int32_t theAmount, volatile int32_t *p )
{
int32_t mem_val = *p;
/* POINT 1 */
int32_t new_val = mem_val & theAmount;
/* PONT 2 */
while ( ! OSAtomicCompareAndSwap ( mem_val, new_val, p ) )
{
}
return new_val;
}
What this appears to be doing is reading and recording
the contents of the memory location (mem_val). It then
computes what the new value should be (new_val).
Now to ensure atomicity, it then calls the atomic compare
and swap operation with mem_val and new_val. The atomic
swap should be performed if and only if the contents of
the memory location are mem_val.
What I don't understand is this. The contents of memory
could change through some other agency after the first read,
but before the call to OSAtomicCompareAndSwap, that between
points 1 and 2 above.
That's ok if the value of the memory location returns
to mem_val at some point. But what if the value doesn't
revert back to mem_val? In that case, the operation
will loop indefinitely.
In view of all this, I feel there must be some assumuptions
about how these routines are to be used. Are they supposed
to be general-purpose, or restricted to particular patterns
of usage?
Many thanks,
Ariel Burton
_______________________________________________
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
_______________________________________________
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
References: | |
| >OSAtomic (From: email@hidden) |