Re: Subclassing NSControl and inheritance of target, action properties
Re: Subclassing NSControl and inheritance of target, action properties
- Subject: Re: Subclassing NSControl and inheritance of target, action properties
- From: Ken Thomases <email@hidden>
- Date: Fri, 30 Jan 2015 00:21:13 -0600
On Jan 29, 2015, at 11:53 PM, Quincey Morris <email@hidden> wrote:
> On Jan 29, 2015, at 21:16 , Ken Thomases <email@hidden> 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 (email@hidden)
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:
This email sent to email@hidden
References: | |
| >Subclassing NSControl and inheritance of target, action properties (From: Graham Cox <email@hidden>) |
| >Re: Subclassing NSControl and inheritance of target, action properties (From: Roland King <email@hidden>) |
| >Re: Subclassing NSControl and inheritance of target, action properties (From: Graham Cox <email@hidden>) |
| >Re: Subclassing NSControl and inheritance of target, action properties (From: Quincey Morris <email@hidden>) |
| >Re: Subclassing NSControl and inheritance of target, action properties (From: Roland King <email@hidden>) |
| >Re: Subclassing NSControl and inheritance of target, action properties (From: Quincey Morris <email@hidden>) |
| >Re: Subclassing NSControl and inheritance of target, action properties (From: Greg Parker <email@hidden>) |
| >Re: Subclassing NSControl and inheritance of target, action properties (From: Quincey Morris <email@hidden>) |
| >Re: Subclassing NSControl and inheritance of target, action properties (From: Ken Thomases <email@hidden>) |
| >Re: Subclassing NSControl and inheritance of target, action properties (From: Quincey Morris <email@hidden>) |