Class 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.
    • Constructor Summary

      Constructors 
      Constructor Description
      RefCnt()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean isLiveNonVolatile​(RefCnt ref)
      Returns true if and only if the given reference counter is alive.
      static int refCnt​(RefCnt ref)
      Returns the current reference count of the given RefCnt instance with a load acquire semantic.
      static boolean release​(RefCnt ref)
      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 resetRefCnt​(RefCnt ref)
      Resets the reference count of the given RefCnt instance to 1.
      static void retain​(RefCnt ref)
      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.
    • Constructor Detail

      • RefCnt

        public RefCnt()
    • Method Detail

      • 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