10.2.1.1 Simple Counting

Simple counting, with neither atomic operations nor memory barriers, can be used when the reference-counter acquisition and release are both protected by the same lock. In this case, it should be clear that the reference count itself may be manipulated non-atomically, because the lock provides any necessary exclusion, memory barriers, atomic instructions, and disabling of compiler optimizations. This is the method of choice when the lock is required to protect other operations in addition to the reference count, but where a reference to the object must be held after the lock is released. Figure [*] shows a simple API that might be used to implement simple non-atomic reference counting - although simple reference counting is almost always open-coded instead.

Figure: Simple Reference-Count API
\begin{figure}{ \scriptsize
\begin{verbatim}1 struct sref {
2 int refcount;
...
...ase(sref);
23 return 1;
24 }
25 return 0;
26 }\end{verbatim}
}\end{figure}



Paul E. McKenney 2011-12-16