<div dir="ltr">For what it's worth, in Netty we implement our reference counting with incrementing by 2 instead of 1, and use the low odd bit to indicate the released state.<div>This allows us to acquire using getAndAdd, which scales much better than a CAS loop.</div><div>Unfortunately we still need to use a CAS loop when implementing release, so that still has contention problems.</div><div><br></div><div>For reference: <a href="https://github.com/netty/netty/blob/2b29b5e87656203fecd1732ffb472a366a1918cc/common/src/main/java/io/netty/util/internal/RefCnt.java#L258-L295">https://github.com/netty/netty/blob/2b29b5e87656203fecd1732ffb472a366a1918cc/common/src/main/java/io/netty/util/internal/RefCnt.java#L258-L295</a></div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Mon, Dec 8, 2025 at 10:42 AM Maurizio Cimadamore <<a href="mailto:maurizio.cimadamore@oracle.com">maurizio.cimadamore@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><br>
> sum() is really just a snapshot, it adds up the counters (Cells), so <br>
> it wouldn't ensure the counter was at zero. Immediately after <br>
> returning zero a thread could have already incremented it.<br>
Yes. What I mean is: you can check if close() should throw because of <br>
pending acquires. But, as I said, we can use that in any way to "block" <br>
other acquires from happening in case we _do_ want to close. Which <br>
leaves us exposed.<br>
><br>
><br>
>> For the purpose of implementation clarity -- would it be useful to <br>
>> wrap the various counters plus logic to acquire/ release (and <br>
>> "closing" state) into a separate abstraction, which is then used by <br>
>> SharedMemorySession? A sort of "atomic" LongAdder, if you will :-)<br>
>><br>
>> That might make it easier to verify the correctness of the <br>
>> implementation, by validating each aspect (the atomic long adder, and <br>
>> its use from SharedMemorySession) separately.<br>
><br>
> Sure, that would be a bit cleaner, thanks.<br>
<br>
Thanks.<br>
<br>
<br>
Maurizio<br>
</blockquote></div>