Package java.util.concurrent.locks.ReentrantReadWriteLock

Examples of java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock


        /**
         * {@inheritDoc}
         */
        @Override
        public void invalidateObject(final K key, final V obj) {
            WriteLock writeLock = readWriteLock.writeLock();
            writeLock.lock();
            try {
                keyedPool.invalidateObject(key, obj);
            } catch (Exception e) {
                // swallowed as of Pool 2
            } finally {
                writeLock.unlock();
            }
        }
View Full Code Here


         * {@inheritDoc}
         */
        @Override
        public void addObject(final K key) throws Exception,
                IllegalStateException, UnsupportedOperationException {
            WriteLock writeLock = readWriteLock.writeLock();
            writeLock.lock();
            try {
                keyedPool.addObject(key);
            } finally {
                writeLock.unlock();
            }
        }
View Full Code Here

        /**
         * {@inheritDoc}
         */
        @Override
        public void clear() throws Exception, UnsupportedOperationException {
            WriteLock writeLock = readWriteLock.writeLock();
            writeLock.lock();
            try {
                keyedPool.clear();
            } finally {
                writeLock.unlock();
            }
        }
View Full Code Here

         * {@inheritDoc}
         */
        @Override
        public void clear(final K key) throws Exception,
                UnsupportedOperationException {
            WriteLock writeLock = readWriteLock.writeLock();
            writeLock.lock();
            try {
                keyedPool.clear(key);
            } finally {
                writeLock.unlock();
            }
        }
View Full Code Here

        /**
         * {@inheritDoc}
         */
        @Override
        public void close() {
            WriteLock writeLock = readWriteLock.writeLock();
            writeLock.lock();
            try {
                keyedPool.close();
            } catch (Exception e) {
                // swallowed as of Pool 2
            } finally {
                writeLock.unlock();
            }
        }
View Full Code Here

        if (closed) {
            throw new IOException(sm.getString("apr.closed", Long.valueOf(socket)));
        }

        Lock readLock = wrapper.getBlockingStatusReadLock();
        WriteLock writeLock = wrapper.getBlockingStatusWriteLock();

        try {
            readLock.lock();
            if (wrapper.getBlockingStatus() == block) {
                return doWriteInternal(b, off, len);
            }
        } finally {
            readLock.unlock();
        }

        try {
            writeLock.lock();
            // Set the current settings for this socket
            wrapper.setBlockingStatus(block);
            if (block) {
                Socket.timeoutSet(socket, endpoint.getSoTimeout() * 1000);
            } else {
                Socket.timeoutSet(socket, 0);
            }

            // Downgrade the lock
            try {
                readLock.lock();
                writeLock.unlock();
                return doWriteInternal(b, off, len);
            } finally {
                readLock.unlock();
            }
        } finally {
            // Should have been released above but may not have been on some
            // exception paths
            if (writeLock.isHeldByCurrentThread()) {
                writeLock.unlock();
            }
        }
    }
View Full Code Here

    @Override
    protected int doRead(boolean block, byte[] b, int off, int len)
            throws IOException {

        Lock readLock = wrapper.getBlockingStatusReadLock();
        WriteLock writeLock = wrapper.getBlockingStatusWriteLock();

        boolean readDone = false;
        int result = 0;
        try {
            readLock.lock();
            if (wrapper.getBlockingStatus() == block) {
                if (closed) {
                    throw new IOException(sm.getString("apr.closed", Long.valueOf(socket)));
                }
                result = Socket.recv(socket, b, off, len);
                readDone = true;
            }
        } finally {
            readLock.unlock();
        }

        if (!readDone) {
            try {
                writeLock.lock();
                wrapper.setBlockingStatus(block);
                // Set the current settings for this socket
                Socket.optSet(socket, Socket.APR_SO_NONBLOCK, (block ? 0 : 1));
                // Downgrade the lock
                try {
                    readLock.lock();
                    writeLock.unlock();
                    if (closed) {
                        throw new IOException(sm.getString("apr.closed", Long.valueOf(socket)));
                    }
                    result = Socket.recv(socket, b, off, len);
                } finally {
                    readLock.unlock();
                }
            } finally {
                // Should have been released above but may not have been on some
                // exception paths
                if (writeLock.isHeldByCurrentThread()) {
                    writeLock.unlock();
                }
            }
        }

        if (result > 0) {
View Full Code Here

     * a data store for the versions has already been found, this method will not reload it from disk. Thus it is
     * safe to make this call multiple times not only per instance, but per class loader, as the data is stored as
     * a static variable across all instances of this class.
     */
    public void loadFromDisk() {
        WriteLock lock = dataLock.writeLock();
        lock.lock();

        try {
            ObjectInputStream ois = null;
            try {

                // All instances of this class will use the same store. If one loaded it already, there's nothing to do.
                if (data != null) {
                    return;
                }

                log.debug("Loading package versions from storage for plugin [" + pluginName + "]");
                File file = new File(dataDirectory, FILENAME);

                // If there's no package-versions.dat, check for the old filename, application-versions.dat.
                if (!file.exists()) {
                    File legacyFile = new File(dataDirectory, LEGACY_FILENAME);
                    if (legacyFile.exists()) {
                        log.info("Found legacy package versions data file [" + legacyFile + "] - renaming to [" + file
                            + "]...");
                        legacyFile.renameTo(file);
                    }
                }

                // There will be no data file after a clean or on the first run, so create an empty one
                if (!file.exists()) {
                    log.debug("No package versions found for plugin [" + pluginName
                        + "]. This will be the case if the Agent was cleaned or on the first run.");
                    data = new PackageVersionData();
                } else {
                    FileInputStream fis = new FileInputStream(file);
                    ois = new ObjectInputStream(fis);
                    data = (PackageVersionData) ois.readObject();
                }
            } catch (Exception e) {
                log.error("Could not load persistent version data from disk for plugin [" + pluginName
                    + "]. Package version values will be reset.", e);
                data = new PackageVersionData();
            } finally {
                if (ois != null) {
                    try {
                        ois.close();
                    } catch (IOException e) {
                        log.error("Error closing input stream for persistent version data for plugin [" + pluginName
                            + "]", e);
                    }
                }
            }
        } finally {
            lock.unlock();
        }
    }
View Full Code Here

     * Saves the current state of the application (package) versions to disk. The values will be saved scoped to the
     * plugin specified in the constructor. This method will absorb and log any errors that occur while saving;
     * this call represents a best-effort to save the values.
     */
    public void saveToDisk() {
        WriteLock lock = dataLock.writeLock();
        lock.lock();

        try {
            if (data == null) {
                throw new IllegalStateException("Data has not been loaded prior to saving for plugin [" + pluginName
                    + "]");
            }

            ObjectOutputStream oos = null;
            try {
                File file = new File(dataDirectory, FILENAME);
                FileOutputStream fos = new FileOutputStream(file);
                oos = new ObjectOutputStream(fos);

                oos.writeObject(data);
            } catch (Exception e) {
                log.error("Error saving persistent version data for plugin [" + pluginName
                    + "]. Package versions may not be maintained between agent restarts.", e);
            } finally {
                if (oos != null) {
                    try {
                        oos.close();
                    } catch (IOException e) {
                        log.error("Error closing output stream for plugin [" + pluginName + "].", e);
                    }
                }

            }
        } finally {
            lock.unlock();
        }
    }
View Full Code Here

        InitializeCallback callback = getInitializeCallback();
        if (callback != null) {
            // block here - in effect, this will stop all commands from going out until the callback is done
            // to avoid infinite blocking, we'll only wait for a set time (though long).

            WriteLock writeLock = m_needToCallInitializeCallbackLock.writeLock();
            boolean locked;
            try {
                locked = writeLock.tryLock(m_initializeCallbackLockAcquisitionTimeoutMins * 60, TimeUnit.SECONDS);
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                locked = false;
            }

            if (locked) {
                try {
                    if (m_needToCallInitializeCallback) {
                        try {
                            m_needToCallInitializeCallback = (!callback.sendingInitialCommand(this, command));
                            LOG.debug(CommI18NResourceKeys.INITIALIZE_CALLBACK_DONE, m_needToCallInitializeCallback);
                        } catch (Throwable t) {
                            m_needToCallInitializeCallback = true; // callback failed, we'll want to call it again
                            LOG.error(t, CommI18NResourceKeys.INITIALIZE_CALLBACK_FAILED, ThrowableUtil
                                .getAllMessages(t));
                            return new GenericCommandResponse(command, false, null, t);
                        }
                    }
                } finally {
                    writeLock.unlock();
                }
            } else {
                Throwable t = new Throwable("Initialize callback lock could not be acquired");
                LOG.error(CommI18NResourceKeys.INITIALIZE_CALLBACK_FAILED, t.getMessage());
                return new GenericCommandResponse(command, false, null, t);
View Full Code Here

TOP

Related Classes of java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.