Re: OSIncrementAtomic(), lck_mtx_lock() interoperability.
Re: OSIncrementAtomic(), lck_mtx_lock() interoperability.
- Subject: Re: OSIncrementAtomic(), lck_mtx_lock() interoperability.
- From: Terry Lambert <email@hidden>
- Date: Wed, 15 Nov 2006 17:10:00 -0800
On Nov 15, 2006, at 2:04 PM, Matt Darland wrote:
If I have an integer value X that is usually incremented through
OSIncrementAtomic, but in occasional cases I'm holding a mutex (via
lck_mtx_lock) to a structure that contains X, and I want to
increment X: Can I just ++X while holding the mutex and avoid also
having to call OSIncrementAtomic in those rare cases? i.e. Do
OSxxxAtomic and lck_mtx_xxx enforce the same memory coherency with
respect to an integer?
For the most part, no.
The value is not guaranteed to be updated in other CPU caches until
after a memory barrier, if the operation is not itself an atomic one,
and on x86, there is no guarantee that an unlock will result in a
memory barrier (it depends on strong write ordering, and the fact that
no one else is permitted to write a lock that's locked in order to do
the unlock operation). Unless your operation is protected by the same
lock in all cases, there's no reliable guarantee.
There's ways to order code to usefully use a barrier only on a 1->0 on
a release (for example), and use Atomic operations everywhere, but the
converse is not true.
-- Terry
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden