5 releases

0.1.4 Nov 23, 2023
0.1.3 Nov 16, 2023
0.1.2 Nov 16, 2023
0.1.1 Nov 14, 2023
0.1.0 Nov 4, 2023

#200 in Memory management

Download history 16/week @ 2023-10-30 6/week @ 2023-11-06 81/week @ 2023-11-13 40/week @ 2023-11-20 18/week @ 2023-11-27

148 downloads per month

MIT license

215 lines

Thread-Safe Split Reference Counts

splitrc provides a pair of reference counts that together manage the lifetime of an object. The object is notified when either half's count reaches zero.

It is primarily aimed at implementing ownership of channel types, where the channel needs notification when one half is fully dropped.

The implementation stores two 32-bit counters in a shared 64-bit atomic. On 64-bit platforms, atomic increment and decrements are used. On 32-bit platforms, increments and decrements are a CAS loop.

Four billion references should be plenty. Exceeding that leads to a panic.

The pointers are arbitrarily named [Tx] and [Rx] to indicate their intended use by channels.

let (tx, rx) = splitrc::new(MyValue {});

No runtime deps