site_archiver@lists.apple.com Delivered-To: darwin-dev@lists.apple.com User-agent: nail 11.25 7/29/05 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 (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/site_archiver%40lists.appl... This email sent to site_archiver@lists.apple.com
participants (1)
-
ariel.burtonï¼ totalviewtech.com