@Override public void close(Runnable closeTask) { // Allow only one thread to close if (this.closed.compareAndSet(false, true)) { // Closing is final - we don't need the stamp this.lock.writeLock(); closeTask.run(); } } }
private <T> T writeConcurrently(final Supplier<T> writeSupplier) { T result; long stamp = stampedLock.writeLock(); try { result = writeSupplier.get(); } finally { stampedLock.unlockWrite(stamp); } return result; } }
/** * {@inheritDoc} */ @Override public void clear() { final long stamp = lock.writeLock(); try { cacheMap.clear(); } finally { lock.unlockWrite(stamp); } }
/** * {@inheritDoc} */ @Override public Map<K, V> snapshot() { final long stamp = lock.writeLock(); try { Map<K, V> map = new HashMap<>(cacheMap.size()); cacheMap.forEach((key, cacheValue) -> map.put(key, cacheValue.getObject())); return map; } finally { lock.unlockWrite(stamp); } }
public void removeCursor(String name) { long stamp = rwLock.writeLock(); try { Item item = cursors.remove(name); // Move the item to the right end of the heap to be removed Item lastItem = heap.get(heap.size() - 1); swap(item, lastItem); heap.remove(item.idx); // Update the heap siftDown(lastItem); } finally { rwLock.unlockWrite(stamp); } }
@Override protected void freeMemory() { final long stamp = stampedLock.writeLock(); try { super.freeMemory(); } finally { stampedLock.unlockWrite(stamp); } } }
/** * {@inheritDoc} */ @Override public final int prune() { final long stamp = lock.writeLock(); try { return pruneCache(); } finally { lock.unlockWrite(stamp); } }
public void add(ManagedCursor cursor) { long stamp = rwLock.writeLock(); try { // Append a new entry at the end of the list Item item = new Item(cursor, heap.size()); cursors.put(cursor.getName(), item); heap.add(item); siftUp(item); } finally { rwLock.unlockWrite(stamp); } }
@Override public ByteBuffer allocate(final int size) { final long stamp = stampedLock.writeLock(); try { return super.allocate(size); } finally { stampedLock.unlockWrite(stamp); } }
@Override public void destroyDirectBuffer(final Buffer buffer) { final long stamp = stampedLock.writeLock(); try { super.destroyDirectBuffer(buffer); } finally { stampedLock.unlockWrite(stamp); } }
private static long convertToWriteLock(long stamp) { final long writeStamp = lock.tryConvertToWriteLock(stamp); if (writeStamp == 0L) { lock.unlockRead(stamp); stamp = lock.writeLock(); } else { stamp = writeStamp; } return stamp; }
/** * {@inheritDoc} */ @Override public V remove(final K key) { V removedValue = null; final long stamp = lock.writeLock(); try { CacheObject<K,V> co = cacheMap.remove(key); if (co != null) { onRemove(co.key, co.cachedObject); removedValue = co.cachedObject; } } finally { lock.unlockWrite(stamp); } return removedValue; }
private long applyProcessorCount( long lock ) { if ( numberOfForkedProcessors != targetNumberOfProcessors ) { stripingLock.unlock( lock ); lock = stripingLock.writeLock(); awaitAllCompleted(); int processors = targetNumberOfProcessors; while ( numberOfForkedProcessors < processors ) { if ( forkedProcessors[numberOfForkedProcessors] == null ) { forkedProcessors[numberOfForkedProcessors] = new ForkedProcessor( numberOfForkedProcessors, tail.get() ); } numberOfForkedProcessors++; } if ( numberOfForkedProcessors > processors ) { numberOfForkedProcessors = processors; // Excess processors will notice that they are not needed right now, and will park until they are. // The most important thing here is that future Units will have a lower number of processor as expected max. } } return lock; }
/** * {@inheritDoc} */ @Override public void put(final K key, final V object, final long timeout) { Objects.requireNonNull(object); final long stamp = lock.writeLock(); try { CacheObject<K,V> co = new CacheObject<>(key, object, timeout); if (timeout != 0) { existCustomTimeout = true; } if (isReallyFull(key)) { pruneCache(); } cacheMap.put(key, co); } finally { lock.unlockWrite(stamp); } }
/** * Signal that a cursor position has been updated and that the container must re-order the cursor list. * * @param cursor * @return a pair of positions, representing the previous slowest consumer and the new slowest consumer (after the * update). */ public Pair<PositionImpl, PositionImpl> cursorUpdated(ManagedCursor cursor, Position newPosition) { checkNotNull(cursor); long stamp = rwLock.writeLock(); try { Item item = cursors.get(cursor.getName()); if (item == null) { return null; } PositionImpl previousSlowestConsumer = heap.get(0).position; // When the cursor moves forward, we need to push it toward the // bottom of the tree and push it up if a reset was done item.position = (PositionImpl) newPosition; if (item.idx == 0 || getParent(item).position.compareTo(item.position) <= 0) { siftDown(item); } else { siftUp(item); } PositionImpl newSlowestConsumer = heap.get(0).position; return Pair.of(previousSlowestConsumer, newSlowestConsumer); } finally { rwLock.unlockWrite(stamp); } }
stamp = lock.writeLock();
/** * enqueue the attempt into our underlying queue. since it's expensive to dynamically * resize the queue, we have a separate rejection threshold which, if less than 0 is * ignored, but otherwise is the practical cap on the size of the queue. */ private boolean tryEnqueueAttempt(DelayedExecution<T> delayedExecution) { int rejectionThreshold = queueRejectionThreshold.get(); if (rejectionThreshold < 0) { return requestQueue.offer(delayedExecution); } long stamp = stampedLock.readLock(); try { while (requestQueue.size() < rejectionThreshold) { long writeStamp = stampedLock.tryConvertToWriteLock(stamp); if (writeStamp != 0L) { stamp = writeStamp; return requestQueue.offer(delayedExecution); } else { stampedLock.unlock(stamp); stamp = stampedLock.writeLock(); } } return false; } finally { stampedLock.unlock(stamp); } }
/** * Resets the maven configuration. If new system properties were added, those will be taken into account after reloading the * config. */ public static void reset() { long stamp = lock.writeLock(); try { mavenConfig = null; invalidateCaches(); initialiseGlobalConfig(); } finally { lock.unlockWrite(stamp); } }
public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(2); StampedLock lock = new StampedLock(); executor.submit(() -> { long stamp = lock.readLock(); try { if (count == 0) { stamp = lock.tryConvertToWriteLock(stamp); if (stamp == 0L) { System.out.println("Could not convert to write lock"); stamp = lock.writeLock(); } count = 23; } System.out.println(count); } finally { lock.unlock(stamp); } }); ConcurrentUtils.stop(executor); }
/** * @return the maven configuration to use for the runtime. */ public static MavenConfiguration getMavenConfig() { long stamp = lock.readLock(); try { if (mavenConfig == null) { long writeStamp = lock.tryConvertToWriteLock(stamp); if (writeStamp == 0L) { lock.unlockRead(stamp); stamp = lock.writeLock(); } else { stamp = writeStamp; } if (mavenConfig == null) { initialiseGlobalConfig(); } } return mavenConfig; } finally { lock.unlock(stamp); } }