Class RefCnt

java.lang.Object
io.netty.util.internal.RefCnt

public final class RefCnt extends Object
Monomorphic reference counter implementation that always use the most efficient available atomic updater. This implementation is easier for the JIT compiler to optimize, compared to when ReferenceCountUpdater is used.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static final class 
     
    private static final class 
     
    private static final class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final int
     
    private static final int
     
    private static final int
     
    (package private) int
     
    private static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Returns true if and only if the given reference counter is alive.
    static int
    Returns the current reference count of the given RefCnt instance with a load acquire semantic.
    static boolean
    Decreases the reference count of the given RefCnt instance by 1.
    static boolean
    release(RefCnt ref, int decrement)
    Decreases the reference count of the given RefCnt instance by the specified decrement.
    static void
    Resets the reference count of the given RefCnt instance to 1.
    static void
    Increases the reference count of the given RefCnt instance by 1.
    static void
    retain(RefCnt ref, int increment)
    Increases the reference count of the given RefCnt instance by the specified increment.
    static void
    setRefCnt(RefCnt ref, int refCnt)
    WARNING: An unsafe operation that sets the reference count of the given RefCnt instance directly.
    (package private) static void
    throwIllegalRefCountOnRelease(int decrement, int curr)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • UNSAFE

      private static final int UNSAFE
      See Also:
    • VAR_HANDLE

      private static final int VAR_HANDLE
      See Also:
    • ATOMIC_UPDATER

      private static final int ATOMIC_UPDATER
      See Also:
    • REF_CNT_IMPL

      private static final int REF_CNT_IMPL
    • value

      volatile int value
  • Constructor Details

    • RefCnt

      public RefCnt()
  • Method Details

    • refCnt

      public static int refCnt(RefCnt ref)
      Returns the current reference count of the given RefCnt instance with a load acquire semantic.
      Parameters:
      ref - the target RefCnt instance
      Returns:
      the reference count
    • retain

      public static void retain(RefCnt ref)
      Increases the reference count of the given RefCnt instance by 1.
      Parameters:
      ref - the target RefCnt instance
    • retain

      public static void retain(RefCnt ref, int increment)
      Increases the reference count of the given RefCnt instance by the specified increment.
      Parameters:
      ref - the target RefCnt instance
      increment - the amount to increase the reference count by
      Throws:
      IllegalArgumentException - if increment is not positive
    • release

      public static boolean release(RefCnt ref)
      Decreases the reference count of the given RefCnt instance by 1.
      Parameters:
      ref - the target RefCnt instance
      Returns:
      true if the reference count became 0 and the object should be deallocated
    • release

      public static boolean release(RefCnt ref, int decrement)
      Decreases the reference count of the given RefCnt instance by the specified decrement.
      Parameters:
      ref - the target RefCnt instance
      decrement - the amount to decrease the reference count by
      Returns:
      true if the reference count became 0 and the object should be deallocated
      Throws:
      IllegalArgumentException - if decrement is not positive
    • isLiveNonVolatile

      public static boolean isLiveNonVolatile(RefCnt ref)
      Returns true if and only if the given reference counter is alive. This method is useful to check if the object is alive without incurring the cost of a volatile read.
      Parameters:
      ref - the target RefCnt instance
      Returns:
      true if alive
    • setRefCnt

      public static void setRefCnt(RefCnt ref, int refCnt)
      WARNING: An unsafe operation that sets the reference count of the given RefCnt instance directly.
      Parameters:
      ref - the target RefCnt instance
      refCnt - new reference count
    • resetRefCnt

      public static void resetRefCnt(RefCnt ref)
      Resets the reference count of the given RefCnt instance to 1.

      Warning: This method uses release memory semantics, meaning the change may not be immediately visible to other threads. It should only be used in quiescent states where no other threads are accessing the reference count.

      Parameters:
      ref - the target RefCnt instance
    • throwIllegalRefCountOnRelease

      static void throwIllegalRefCountOnRelease(int decrement, int curr)