public ManagedCursor get(String name) { long stamp = rwLock.readLock(); try { Item item = cursors.get(name); return item != null ? item.cursor : null; } finally { rwLock.unlockRead(stamp); } }
/** * Get the slowest reader position, meaning older acknowledged position between all the cursors. * * @return the slowest reader position */ public PositionImpl getSlowestReaderPosition() { long stamp = rwLock.readLock(); try { return heap.isEmpty() ? null : heap.get(0).position; } finally { rwLock.unlockRead(stamp); } }
public ManagedCursor getSlowestReader() { long stamp = rwLock.readLock(); try { return heap.isEmpty() ? null : heap.get(0).cursor; } finally { rwLock.unlockRead(stamp); } }
private <T> T readConcurrentlyWithoutSpin(final Supplier<T> readSupplier) { T result; long stamp = stampedLock.readLock(); try { result = readSupplier.get(); } finally { stampedLock.unlockRead(stamp); } return result; }
@Override public String toString() { long stamp = rwLock.readLock(); try { StringBuilder sb = new StringBuilder(); sb.append('['); boolean first = true; for (Item item : cursors.values()) { if (!first) { sb.append(", "); } first = false; sb.append(item.cursor); } sb.append(']'); return sb.toString(); } finally { rwLock.unlockRead(stamp); } }
private static CacheEntry getOrCreate(String key) { long stamp = lock.readLock(); try { final CacheEntry entry = cache.get(key); if (entry != null) { return entry; } stamp = convertToWriteLock(stamp); return cache.computeIfAbsent(key, CacheEntry::new); } finally { lock.unlock(stamp); } }
/** * Clears the cache. */ public static void clear() { int size; long stamp = lock.readLock(); try { size = cache.size(); if (size == 0) { return; } stamp = convertToWriteLock(stamp); size = cache.size(); cache.clear(); } finally { lock.unlock(stamp); } if (size != 0 && logger.isDebugEnabled()) { if (size != 1) { logger.debug("Cleared: {} entries", size); } else { logger.debug("Cleared: 1 entry"); } } }
@Override public long receive( long ticket, T batch ) { long time = nanoTime(); while ( queuedBatches.get() >= maxQueueLength ) { PARK.park( receiverThread = Thread.currentThread() ); } // It is of importance that all items in the queue at the same time agree on the number of processors. We take this lock in order to make sure that we // do not interfere with another thread trying to drain the queue in order to change the processor count. long lock = applyProcessorCount( stripingLock.readLock() ); queuedBatches.incrementAndGet(); Unit unit = new Unit( ticket, batch, numberOfForkedProcessors ); // [old head] [unit] // ^ // head Unit myHead = head.getAndSet( unit ); // [old head] -next-> [unit] myHead.next = unit; stripingLock.unlock( lock ); return nanoTime() - time; }
public boolean isEmpty() { long stamp = rwLock.tryOptimisticRead(); boolean isEmpty = heap.isEmpty(); if (!rwLock.validate(stamp)) { // Fallback to read lock stamp = rwLock.readLock(); try { isEmpty = heap.isEmpty(); } finally { rwLock.unlockRead(stamp); } } return isEmpty; }
private <T> T readConcurrently(final Supplier<T> readSupplier) { T result; long stamp; for (int i = 0; i < RETRIES; i++) { stamp = stampedLock.tryOptimisticRead(); if(stamp == 0) { continue; } result = readSupplier.get(); if (stampedLock.validate(stamp)) { return result; } } stamp = stampedLock.readLock(); try { result = readSupplier.get(); } finally { stampedLock.unlockRead(stamp); } return result; }
protected ForkedProcessorStep( StageControl control, String name, Configuration config, StatsProvider... statsProviders ) { super( control, name, config, statsProviders ); this.maxProcessors = config.maxNumberOfProcessors(); this.forkedProcessors = new Object[this.maxProcessors]; stripingLock = new StampedLock(); Unit noop = new Unit( -1, null, 0 ); head = new AtomicReference<>( noop ); tail = new AtomicReference<>( noop ); stripingLock.unlock( applyProcessorCount( stripingLock.readLock() ) ); downstreamSender = new CompletedBatchesSender( name + " [CompletedBatchSender]" ); maxQueueLength = 200 + maxProcessors; }
/** * Returns {@code true} if the specified {@code remoteAddress} is known to have no support for * the specified {@link SessionProtocol}. */ public static boolean isUnsupported(SocketAddress remoteAddress, SessionProtocol protocol) { final String key = key(remoteAddress); final CacheEntry e; final long stamp = lock.readLock(); try { e = cache.get(key); } finally { lock.unlockRead(stamp); } if (e == null) { // Can't tell if it's unsupported return false; } return e.isUnsupported(protocol); }
long stamp = lock.readLock();
public int getQueueSize() { long stamp = stampedLock.tryOptimisticRead(); int queueSize = requestQueue.size(); if (!stampedLock.validate(stamp)) { stamp = stampedLock.readLock(); try { queueSize = requestQueue.size(); } finally { stampedLock.unlockRead(stamp); } } return queueSize; }
/** * 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); } }
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); }
public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(2); Map<String, String> map = new HashMap<>(); StampedLock lock = new StampedLock(); executor.submit(() -> { long stamp = lock.writeLock(); try { ConcurrentUtils.sleep(1); map.put("foo", "bar"); } finally { lock.unlockWrite(stamp); } }); Runnable readTask = () -> { long stamp = lock.readLock(); try { System.out.println(map.get("foo")); ConcurrentUtils.sleep(1); } finally { lock.unlockRead(stamp); } }; executor.submit(readTask); executor.submit(readTask); 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); } }
@Override public ClassLoaderModel load(File artifactFile, Map<String, Object> attributes, ArtifactType artifactType) throws InvalidDescriptorLoaderException { long stamp = lock.readLock(); try { MavenConfiguration updatedMavenConfiguration = getMavenConfig();
/** * Decorator to call {@link StampedLock#readLock()} if the {@code condition} * is {@code true}. * * @param lock * @param condition * @return the stamp */ public static long stampLockReadIfCondition(StampedLock lock, boolean condition) { return condition ? lock.readLock() : 0L; }