| 34 | import java.io.Closeable; |
| 35 | |
| 36 | public abstract class AbstractPool implements Closeable { |
| 37 | public static final long CLOSED = Unsafe.getFieldOffset(AbstractPool.class, "closed"); |
| 38 | public static final long UNALLOCATED = -1L; |
| 39 | private static final int FALSE = 0; |
| 40 | private static final int TRUE = 1; |
| 41 | protected final MicrosecondClock clock; |
| 42 | protected final FilesFacade ff; |
| 43 | private final CairoConfiguration configuration; |
| 44 | private final long inactiveTtlUs; |
| 45 | @SuppressWarnings({"FieldCanBeLocal", "FieldMayBeFinal"}) |
| 46 | private volatile int closed = FALSE; |
| 47 | // keep volatile to avoid races around listener clean up in tests |
| 48 | private volatile PoolListener eventListener; |
| 49 | |
| 50 | public AbstractPool(CairoConfiguration configuration, long inactiveTtlMillis) { |
| 51 | this.configuration = configuration; |
| 52 | this.ff = configuration.getFilesFacade(); |
| 53 | this.clock = configuration.getMicrosecondClock(); |
| 54 | this.inactiveTtlUs = inactiveTtlMillis * 1000; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void close() { |
| 59 | if (Unsafe.cas(this, CLOSED, FALSE, TRUE)) { |
| 60 | closePool(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public CairoConfiguration getConfiguration() { |
| 65 | return configuration; |
| 66 | } |
| 67 | |
| 68 | public PoolListener getPoolListener() { |
| 69 | return eventListener; |
| 70 | } |
| 71 | |
| 72 | public boolean releaseAll() { |
| 73 | return releaseAll(Long.MAX_VALUE); |
| 74 | } |
| 75 | |
| 76 | public boolean releaseInactive() { |
| 77 | return releaseAll(clock.getTicks() - inactiveTtlUs); |
| 78 | } |
| 79 | |
| 80 | @TestOnly |
| 81 | public boolean reopen() { |
| 82 | return Unsafe.cas(this, CLOSED, TRUE, FALSE); |
| 83 | } |
| 84 | |
| 85 | public void setPoolListener(PoolListener eventListener) { |
| 86 | this.eventListener = eventListener; |
| 87 | } |
| 88 | |
| 89 | protected void closePool() { |
| 90 | releaseAll(Long.MAX_VALUE); |
| 91 | notifyListener(Thread.currentThread().threadId(), null, PoolListener.EV_POOL_CLOSED); |
| 92 | } |
| 93 |
nothing calls this directly
no test coverage detected
searching dependent graphs…