Module stormpot

Class BSlot<T extends Poolable>

java.lang.Object
stormpot.internal.BSlot<T>
Type Parameters:
T - The concrete poolable type.
All Implemented Interfaces:
Slot, SlotInfo<T>
Direct Known Subclasses:
BSlotPadded

public class BSlot<T extends Poolable> extends Object implements Slot, SlotInfo<T>
The BSlot implements the Slot and SlotInfo interfaces for the blaze pool implementation.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    A reference to the allocated object, used by the PreciseLeakDetector.
    The object being pooled.
    Any exception encountered when trying to allocate an object for this slot.
  • Constructor Summary

    Constructors
    Constructor
    Description
    BSlot(BlockingQueue<BSlot<T>> live, AtomicLong poisonedSlots)
    Create a new BSlot instance, which will return to the given live queue when released from a claim, and update the given counter when poisoned.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Transition this slot from claimed to dead.
    void
    Transition this slot from thread-locally claimed, to live.
    void
    Transition this slot from dead to live.
    void
    Mark the object represented by this slot as expired.
    long
    Get the approximate number of milliseconds that have transpired since the object was allocated.
    long
    Get the approximate System.nanoTime() timestamp for when the object was allocated.
    Get the Poolable object represented by this SlotInfo instance.
    long
    Get the stamp value that has been set on this SlotInfo, or 0 if none has been set since the Poolable was allocated.
    boolean
    Attempt to transition this slot from live to claimed.
    boolean
    Attempt to transition this slot from live to thread-locally claimed.
    void
    Signal to the pool that the currently claimed object in this slot has been released.
    void
    setStamp(long stamp)
    Set the stamp value on this SlotInfo.
     

    Methods inherited from class java.lang.Object

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

    • obj

      public T extends Poolable obj
      The object being pooled.
    • poison

      public Exception poison
      Any exception encountered when trying to allocate an object for this slot.
    • leakCheck

      public Reference<Object> leakCheck
      A reference to the allocated object, used by the PreciseLeakDetector.
  • Constructor Details

    • BSlot

      public BSlot(BlockingQueue<BSlot<T>> live, AtomicLong poisonedSlots)
      Create a new BSlot instance, which will return to the given live queue when released from a claim, and update the given counter when poisoned.
      Parameters:
      live - The queue of live slots.
      poisonedSlots - The counter of poisoned slots.
  • Method Details

    • expire

      public void expire(Poolable obj)
      Description copied from interface: Slot
      Mark the object represented by this slot as expired. Expired objects cannot be claimed again, and will be deallocated by the pool at the earliest convenience. Objects are normally expired automatically using the Expiration policy that the pool has been configured with. If an object through its use is found to be unusable, then it can be explicitly expired using this method. Objects can only be explicitly expired while they are claimed, and such expired objects will not be deallocated until they are released back to the pool.

      It is a user error to expire objects that are not currently claimed. It is likewise a user error to expire a Slot while inside the Allocators allocate method.

      The expiration only takes effect after the object has been released, so calling this method from within the Expirations hasExpired method will not prevent the object from being claimed, but will prevent it from being claimed again after it's been released back to the pool. An Expiration policy that expires all objects with this method, is effectively allowing objects to only be claimed once.

      Pools are free to throw a PoolException if they detect any wrong uses, but it is not guaranteed and the exact behaviour is not specified. "Unspecified behaviour" means that deadlocks and infinite loops are fair responses as well. Therefore, heed the advice and don't misuse expire!

      Pools must, however, guarantee that an object is never deallocated more than once. This guarantee must hold even if expire is misused.

      Specified by:
      expire in interface Slot
      Parameters:
      obj - A reference to the Poolable object that is allocated for this slot, and is being expired.
    • release

      public void release(Poolable obj)
      Description copied from interface: Slot
      Signal to the pool that the currently claimed object in this slot has been released.

      It is a user error to release a slot that is not currently claimed. It is likewise a user error to release a Slot while inside the Allocators allocate method, or the Expirations hasExpired method.

      On the other hand, it is not an error to release a Poolable from a thread other than the one that claimed it.

      Pools are free to throw a PoolException if they detect any of these wrong uses, but it is not guaranteed and the exact behaviour is not specified. "Unspecified behaviour" means that deadlocks and infinite loops are fair responses as well. Therefore, heed the advice and don't misuse release!

      Pools must, however, guarantee that an object is never deallocated more than once. This guarantee must hold even if release is misused.

      Specified by:
      release in interface Slot
      Parameters:
      obj - A reference to the Poolable object that is allocated for this slot, and is being released.
    • claimTlr2live

      public void claimTlr2live()
      Transition this slot from thread-locally claimed, to live.
    • dead2live

      public void dead2live()
      Transition this slot from dead to live.
    • claim2dead

      public void claim2dead()
      Transition this slot from claimed to dead.
    • live2claim

      public boolean live2claim()
      Attempt to transition this slot from live to claimed.
      Returns:
      true if the state transition succeeds, otherwise false.
    • live2claimTlr

      public boolean live2claimTlr()
      Attempt to transition this slot from live to thread-locally claimed.
      Returns:
      true if the state transition succeeds, otherwise false.
    • getAgeMillis

      public long getAgeMillis()
      Description copied from interface: SlotInfo
      Get the approximate number of milliseconds that have transpired since the object was allocated.
      Specified by:
      getAgeMillis in interface SlotInfo<T extends Poolable>
      Returns:
      The age of the object in milliseconds.
    • getCreatedNanoTime

      public long getCreatedNanoTime()
      Description copied from interface: SlotInfo
      Get the approximate System.nanoTime() timestamp for when the object was allocated.
      Specified by:
      getCreatedNanoTime in interface SlotInfo<T extends Poolable>
      Returns:
      The object allocation System.nanoTime() timestamp.
    • getPoolable

      public T getPoolable()
      Description copied from interface: SlotInfo
      Get the Poolable object represented by this SlotInfo instance.

      WARNING: Do not release() Poolables from within an Expiration — doing so is a user error, and the behaviour of the pool in such a situation is unspecified and implementation specific. This means that deadlocks and infinite loops are possible outcomes as well.

      WARNING: Also note that accessing the Poolable through this method, from your Expiration implementation, is a potentially concurrent access. This means that you need to take thread-safety issues into consideration - especially if you intend on manipulating the Poolable. In particular, you might be racing with other threads that are checking if this Poolable is expired or not, and they might even have claimed the Poolable and put it to use, by the time it is returned from this method.

      Specified by:
      getPoolable in interface SlotInfo<T extends Poolable>
      Returns:
      The Poolable being examined for validity. Never null.
    • getStamp

      public long getStamp()
      Description copied from interface: SlotInfo
      Get the stamp value that has been set on this SlotInfo, or 0 if none has been set since the Poolable was allocated.

      Apart from the zero-value, the actual meaning of this value is completely up to the Expiration that sets it.

      Specified by:
      getStamp in interface SlotInfo<T extends Poolable>
      Returns:
      The current stamp value.
    • setStamp

      public void setStamp(long stamp)
      Description copied from interface: SlotInfo
      Set the stamp value on this SlotInfo.

      This method is only thread-safe to call from within the scope of the Expiration.hasExpired(SlotInfo) method.

      The stamp value is 0 by default, if it has not been set after the Poolable has been allocated. Its meaning is otherwise up to the particular Expiration that might use it.

      Specified by:
      setStamp in interface SlotInfo<T extends Poolable>
      Parameters:
      stamp - The new stamp value.
    • toString

      public String toString()
      Overrides:
      toString in class Object