Have we thought about using the manager's cs to guard `refs` (and then remove the manager from the apt's list while in that cs?
I've done this, but I'm not sure if this is an improvement:
1. It's important to note that there is no such thing as the manager's cs. There is the *apartment*'s cs, which protects the manager list. The manager may or may not have a apartment, so we should guard against `This->parent == NULL` case. This inevitably makes the code more branchy, with more if statements. 2. This makes the manager's `Release()` unconditionally blocking, and introduces contention to the apartment's cs.
Let me know if there are any alternatives that looks more attractive. I have a few options, but it's not clear to me if any of these are better, either:
1. Make it opportunistic: use CAS to only decrement when it's greater than 1. This way, we can only acquire the lock on the "last release" case. 2. Always decrement, then backoff: after entering the critical section, check if the reference count has been increased in the meanwhile. If it has, then cancel the decrement and bailout. This has the disadvantage of introducing "off-by-one" nondeterminism to the return value of `AddRef()` and `Release()`. 3. Introduce a "fake" apartment that orphaned proxy managers end up, and use its address in lieu of `NULL`.