On Jan 29, 2015, at 11:53 PM, Quincey Morris 
<quinceymor...@rivergatesoftware.com> wrote:

> On Jan 29, 2015, at 21:16 , Ken Thomases <k...@codeweavers.com> wrote:
>> 
>> the getter has to do the equivalent of retain+autorelease atomically
> 
> I was thinking that the same would have to be true of local variable 
> accesses, too, but I see (if I see) the difference is that references from 
> local variables are already retained per-thread, not just per-object.

The difference is that a getter for an atomic property has an atomicity 
requirement that local variable accesses don't.


> But now I don’t see how the autorelease got in there. If the getter does the 
> retain atomically, then surely it can do the release at its leisure?

Huh?  It has to be an autorelease and not a release because the object needs to 
survive to the caller's scope.

You're right, though, that I was slightly wrong.  The getter has to atomically 
do fetch+retain vs. the other thread doing a store+release.  The getter does an 
additional autorelease to balance its retain, but, as you suggest, that part 
doesn't have to be atomic with respect to the other thread.  The other thread 
has to retain the new pointer but that also doesn't have to be atomic with 
respect to the getter thread.

For example, a broken scenario:

Thread 1: fetch old pointer from memory to register
Thread 2: store new pointer from register to memory
Thread 2: release old pointer; deallocates object
Thread 1: retain old pointer; attempting to resurrect a dead object, possibly 
operating on a new object that's replace it


> The other thing that’s getting in the way is that it seems to me that the 
> retain-count *increment* needs to be done atomically, independently of 
> whether the retain as a whole is atomic. Otherwise, multi-thread read-only 
> access to a variable wouldn’t be safe outside of atomic properties.

Yes, retaining is atomic.  But here you have two threads each doing two 
operations, accessing a shared memory location and a retain or release.  On the 
relevant architectures, the memory accesses are atomic with respect to each 
other.  The retains and releases are atomic with respect to each other.  But a 
sequence of a memory access plus a retain/release is not atomic with respect to 
another such sequence.

Regards,
Ken

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to