/** * clear the zkdatabase. * Note to developers - be careful to see that * the clear method does clear out all the * data structures in zkdatabase. */ public void clear() { minCommittedLog = 0; maxCommittedLog = 0; /* to be safe we just create a new * datatree. */ dataTree = new DataTree(); sessionsWithTimeouts.clear(); WriteLock lock = logLock.writeLock(); try { lock.lock(); committedLog.clear(); } finally { lock.unlock(); } initialized = false; }
@RequestMapping("/redirect_uri") public void callback(@RequestParam String code, HttpServletRequest request, HttpServletResponse response) throws ClientProtocolException, IOException { WriteLock writeLock = lock.writeLock(); try { writeLock.lock(); googleToken = googleTokenRetriever.postForRefreshAndAccessToken(code, request.getRequestURL().toString()); jweToken = jweTokenRetriever.fetchJweToken(googleToken.getIdToken()); try (PrintWriter out = new PrintWriter(REFRESH_TOKEN_FILENAME)) { out.println(googleToken.getRefreshToken()); } } finally { writeLock.unlock(); } scheduleRefreshTask(googleToken.getExpiresIn()); response.sendRedirect(initialRedirect); }
@Override public void run() { GoogleIdAndRefreshToken newGoogleIdToken = null; WriteLock writeLock = lock.writeLock(); try { newGoogleIdToken = googleTokenRetriever.refreshToken(googleToken.getRefreshToken()); writeLock.lock(); googleToken.setIdToken(newGoogleIdToken.getIdToken()); jweToken = jweTokenRetriever.fetchJweToken(newGoogleIdToken.getIdToken()); } catch (Throwable e) { logger.error("Error while refreshing the id token.", e); } finally { writeLock.unlock(); scheduleRefreshTask(newGoogleIdToken == null ? DEFAULT_REFRESH_TASK_FREQUENCY_IN_SECONDS : newGoogleIdToken.getExpiresIn()); } }
/** * clear the zkdatabase. Note to developers - be careful to see that the * clear method does clear out all the data structures in zkdatabase. */ public void clear() { minCommittedLog = 0; maxCommittedLog = 0; /* * to be safe we just create a new datatree. */ dataTree = new DataTree(); sessionsWithTimeouts.clear(); WriteLock lock = logLock.writeLock(); try { lock.lock(); committedLog.clear(); } finally { lock.unlock(); } initialized = false; }
@Override public void close() throws IOException { CloseableChannel toClose = null; WriteLock writeLock = delegateLock.writeLock(); writeLock.lock(); try { toClose = delegate; delegate = null; } finally { writeLock.unlock(); } if (toClose != null) { toClose.close(); } synchronized (closingAsynchronously) { while (closingAsynchronously.get() > 0) { try { closingAsynchronously.wait(CLOSE_WAIT_TIME); } catch (InterruptedException ignored){ // TODO(angusdavis): rework this to allow the thread // interrupted state to propagate. } } } }
/** * Remove resources matching the given {@code location} and all their direct and indirect descendant resources. * * Note that this method may block if a discovery scan is currently in progress. The removal will occur when * the discovery scan finishes - only then will this method return. * * @param location a location that can contain wildcards */ public void removeResources(L location) { status.assertRunning(getClass(), "removeResources()"); try (S session = openSession()) { // we must not alter the resource manager while a discovery scan is in progress WriteLock lock = EndpointService.this.discoveryScanRWLock.writeLock(); lock.lock(); try { List<Resource<L>> removed = getResourceManager().removeResources(location, session.getLocationResolver()); inventoryListenerSupport.fireResourcesRemoved(removed); } finally { lock.unlock(); } } catch (Exception e) { LOG.errorCouldNotAccess(this, e); } }
/** * Closing the capability collector and frees up all resources. During the call of this method, * the tracker will be closed (and by closing the tracker, the consumer will be called with the * unsatisfied flag even if there are no requirements). */ public void close() { WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { if (!opened) { throw new IllegalStateException( "Close was called on a Capability Collector that was already closed."); } opened = false; closeTracker(); if (noItems()) { satisfied = false; notifyConsumer(); } } finally { writeLock.unlock(); } }
/** * Opens the capability collector. The tracker that is implemented by the subclass is also opened. */ public void open() { WriteLock writeLock = readWriteLock.writeLock(); writeLock.lock(); try { if (opened) { throw new IllegalStateException( "Open was called on a CapabilityCollector that was already opened."); } opened = true; if (noItems()) { this.satisfied = true; } notifyConsumer(); openTracker(); } finally { writeLock.unlock(); } }
/** * Execute operation with in-memory data update. * * @param key the key * @param op the op * @return the for update * @throws NativeMemoryException the j emalloc exception * @throws IOException Signals that an I/O exception has occurred. */ public boolean executeForUpdate(ByteBuffer key, Command<?> op) throws NativeMemoryException, IOException { SpinReadWriteLock lock = getLockForKey(key); WriteLock writeLock = null; if(lock != null){ writeLock = lock.writeLock(); writeLock.lock(); } try{ return op.execute(key, this); }finally{ if(writeLock != null) writeLock.unlock(); } }
private boolean touch(ByteBuffer key, long bufPtr) { key.position(0); int index = Math.abs(Utils.hash_murmur3(key, 4, key.getInt(), 0)) % mBucketNumber; // Get Bucket Read Lock SpinReadWriteLock lock = getLock(index); WriteLock writeLock = null; if(lock != null){ writeLock = lock.writeLock(); writeLock.lock(); } try{ long ptr = IOUtils.getLong(mMemPointer, ((long)index) * 8); if(ptr == 0){ return false; } else{ long resultPtr = getPtr(bufPtr, getRealAddress(ptr)); if(resultPtr == 0) return false; mEvictionAlgo.hitEntry(resultPtr, lock != null? lock.getSpinLock(): null); return true; } }finally{ if(writeLock != null) writeLock.unlock(); } }
/** * Removes the value. * * @param key the key * @return the object * @throws NativeMemoryException the native memory exception * @throws IOException Signals that an I/O exception has occurred. */ public Object removeValue(Object key) throws NativeMemoryException, IOException { ReentrantReadWriteLock lock = getLockForKey(key); WriteLock writeLock = null; if(lock != null){ writeLock = lock.writeLock(); writeLock.lock(); } try{ Object value = get(key); if(value == null) return null; remove(key); return value; }finally{ if(writeLock != null) writeLock.unlock(); } }
LockHandle aquireWriteLock(VersionIdentity version) { final WriteLock writeLock = readWriteLock.writeLock(); boolean success; try { success = writeLock.tryLock() || writeLock.tryLock(10, TimeUnit.SECONDS); } catch (InterruptedException ex) { success = false; } IllegalStateAssertion.assertTrue(success, "Cannot obtain profile write lock in time for: " + version); return new LockHandle() { @Override public void unlock() { writeLock.unlock(); } }; }
private LockHandle aquireWriteLock() { final WriteLock writeLock = readWriteLock.writeLock(); boolean success; try { success = writeLock.tryLock() || writeLock.tryLock(100, TimeUnit.MILLISECONDS); } catch (InterruptedException ex) { success = false; } IllegalStateAssertion.assertTrue(success, "Cannot obtain topology write lock in time"); return new LockHandle() { @Override public void unlock() { writeLock.unlock(); } }; }
private <T> StreamWriter<T> getWriter(final ObjectOutputStream objectOutputStream) { final WriteLock writeLock = new ReentrantReadWriteLock(true).writeLock(); return new StreamWriter<T>() { @Override public void write(T obj) throws IOException { writeLock.lock(); try { objectOutputStream.writeObject(obj); } finally { writeLock.unlock(); } } @Override public void write(Iterable<T> it) throws IOException { writeLock.lock(); try { for (T t : it) { objectOutputStream.writeObject(t); } } finally { writeLock.unlock(); } } }; }
@Override public void propertyChange(PropertyChangeEvent event) { WriteLock writeLock = propertyChangeLock.writeLock(); writeLock.lock(); try { this.propertyChanded = true; } finally { writeLock.unlock(); } updateCurrentDate(); // IMPORTANT : this method must be called first (the date must be updated first) !!! updateLabelSelectedTimezone(); updatePrayInputs(); triggerUpdatePrayTableFilters(); if (praysTableViewer != null) { praysTableViewer.refresh(); } }
/** * maintains a list of last <i>committedLog</i> * or so committed requests. This is used for * fast follower synchronization. * @param request committed request */ public void addCommittedProposal(Request request) { WriteLock wl = logLock.writeLock(); try { wl.lock(); if (committedLog.size() > commitLogCount) { committedLog.removeFirst(); minCommittedLog = committedLog.getFirst().packet.getZxid(); } if (committedLog.size() == 0) { minCommittedLog = request.zxid; maxCommittedLog = request.zxid; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); try { request.hdr.serialize(boa, "hdr"); if (request.txn != null) { request.txn.serialize(boa, "txn"); } baos.close(); } catch (IOException e) { LOG.error("This really should be impossible", e); } QuorumPacket pp = new QuorumPacket(Leader.PROPOSAL, request.zxid, baos.toByteArray(), null); Proposal p = new Proposal(); p.packet = pp; p.request = request; committedLog.add(p); maxCommittedLog = p.packet.getZxid(); } finally { wl.unlock(); } }
/** * maintains a list of last <i>committedLog</i> * or so committed requests. This is used for * fast follower synchronization. * @param request committed request */ public void addCommittedProposal(Request request) { WriteLock wl = logLock.writeLock(); try { wl.lock(); if (committedLog.size() > commitLogCount) { committedLog.removeFirst(); minCommittedLog = committedLog.getFirst().packet.getZxid(); } if (committedLog.isEmpty()) { minCommittedLog = request.zxid; maxCommittedLog = request.zxid; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); try { request.getHdr().serialize(boa, "hdr"); if (request.getTxn() != null) { request.getTxn().serialize(boa, "txn"); } baos.close(); } catch (IOException e) { LOG.error("This really should be impossible", e); } QuorumPacket pp = new QuorumPacket(Leader.PROPOSAL, request.zxid, baos.toByteArray(), null); Proposal p = new Proposal(); p.packet = pp; p.request = request; committedLog.add(p); maxCommittedLog = p.packet.getZxid(); } finally { wl.unlock(); } }
public void closeAllMerchants() { WriteLock wlock = merchant_lock.writeLock(); wlock.lock(); try { final Iterator<HiredMerchant> hmit = hiredMerchants.values().iterator(); while (hmit.hasNext()) { hmit.next().forceClose(); hmit.remove(); } } catch (Exception e) { } finally { wlock.unlock(); } }
public void addHiredMerchant(int chrid, HiredMerchant hm) { WriteLock wlock = merchant_lock.writeLock(); wlock.lock(); try { hiredMerchants.put(chrid, hm); } finally { wlock.unlock(); } }
public void removeHiredMerchant(int chrid) { WriteLock wlock = merchant_lock.writeLock(); wlock.lock(); try { hiredMerchants.remove(chrid); } finally { wlock.unlock(); } }
/** * Register own config action. * * @param action * action to register. * @param priority * priority of action. */ public static void registerAction(ConfigPropertyAction action, double priority) { WriteLock writeLock = lock.writeLock(); try { writeLock.lock(); actions.add(new ConfigPropertyActionEntry(action, priority)); } finally { writeLock.unlock(); } }
/** Adds a value to the counter associated with a given key. * * @param array a byte array. * @param offset the first valid byte in {@code array}. * @param length the number of valid elements in {@code array}. * @param delta a value to be added to the counter associated with the specified key. * @return the previous value of the counter associated with the specified key. */ public int addTo(final byte[] array, final int offset, final int length, final int delta) { final long hash = MurmurHash3.hash(array, offset, length); final WriteLock writeLock = lock[(int)(hash >>> shift)].writeLock(); try { writeLock.lock(); return stripe[(int)(hash >>> shift)].addTo(array, offset, length, hash, delta); } finally { writeLock.unlock(); } }
/** Sets the value associated with a given key. * * @param array a byte array. * @param offset the first valid byte in {@code array}. * @param length the number of valid elements in {@code array}. * @param value a value to be associated with the specified key. * @return the previous value of the counter associated with the specified key. */ public int put(final byte[] array, final int offset, final int length, final int value) { final long hash = MurmurHash3.hash(array, offset, length); final WriteLock writeLock = lock[(int)(hash >>> shift)].writeLock(); try { writeLock.lock(); return stripe[(int)(hash >>> shift)].put(array, offset, length, hash, value); } finally { writeLock.unlock(); } }
public IAutoCloseable lockForWriting() { // assume _lock is not null final WriteLock writeLock = _lock.writeLock(); logger.debug("Acquiring write lock for {}: {}", this, writeLock); writeLock.lock(); logger.debug("Acquired write lock for {}: {}", this, writeLock); return () -> { writeLock.unlock(); logger.debug("Released write lock for {}: {}", this, writeLock); }; }