Search in sources :

Example 11 with Lock

use of java.util.concurrent.locks.Lock in project tomcat by apache.

the class Util method getExpressionFactory.

/**
     * Provides a per class loader cache of ExpressionFactory instances without
     * pinning any in memory as that could trigger a memory leak.
     */
static ExpressionFactory getExpressionFactory() {
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    CacheValue cacheValue = null;
    ExpressionFactory factory = null;
    if (tccl == null) {
        cacheValue = nullTcclFactory;
    } else {
        CacheKey key = new CacheKey(tccl);
        cacheValue = factoryCache.get(key);
        if (cacheValue == null) {
            CacheValue newCacheValue = new CacheValue();
            cacheValue = factoryCache.putIfAbsent(key, newCacheValue);
            if (cacheValue == null) {
                cacheValue = newCacheValue;
            }
        }
    }
    final Lock readLock = cacheValue.getLock().readLock();
    readLock.lock();
    try {
        factory = cacheValue.getExpressionFactory();
    } finally {
        readLock.unlock();
    }
    if (factory == null) {
        final Lock writeLock = cacheValue.getLock().writeLock();
        writeLock.lock();
        try {
            factory = cacheValue.getExpressionFactory();
            if (factory == null) {
                factory = ExpressionFactory.newInstance();
                cacheValue.setExpressionFactory(factory);
            }
        } finally {
            writeLock.unlock();
        }
    }
    return factory;
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 12 with Lock

use of java.util.concurrent.locks.Lock in project tomcat by apache.

the class StandardContext method setManager.

@Override
public void setManager(Manager manager) {
    Lock writeLock = managerLock.writeLock();
    writeLock.lock();
    Manager oldManager = null;
    try {
        // Change components if necessary
        oldManager = this.manager;
        if (oldManager == manager)
            return;
        this.manager = manager;
        // Stop the old component if necessary
        if (oldManager instanceof Lifecycle) {
            try {
                ((Lifecycle) oldManager).stop();
                ((Lifecycle) oldManager).destroy();
            } catch (LifecycleException e) {
                log.error("StandardContext.setManager: stop-destroy: ", e);
            }
        }
        // Start the new component if necessary
        if (manager != null) {
            manager.setContext(this);
        }
        if (getState().isAvailable() && manager instanceof Lifecycle) {
            try {
                ((Lifecycle) manager).start();
            } catch (LifecycleException e) {
                log.error("StandardContext.setManager: start: ", e);
            }
        }
    } finally {
        writeLock.unlock();
    }
    // Report this property change to interested listeners
    support.firePropertyChange("manager", oldManager, manager);
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) Lifecycle(org.apache.catalina.Lifecycle) Manager(org.apache.catalina.Manager) InstanceManager(org.apache.tomcat.InstanceManager) StandardManager(org.apache.catalina.session.StandardManager) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 13 with Lock

use of java.util.concurrent.locks.Lock in project tomcat by apache.

the class StandardContext method getManager.

@Override
public Manager getManager() {
    Lock readLock = managerLock.readLock();
    readLock.lock();
    try {
        return manager;
    } finally {
        readLock.unlock();
    }
}
Also used : ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 14 with Lock

use of java.util.concurrent.locks.Lock in project tomcat by apache.

the class StandardContext method getResources.

@Override
public WebResourceRoot getResources() {
    Lock readLock = resourcesLock.readLock();
    readLock.lock();
    try {
        return resources;
    } finally {
        readLock.unlock();
    }
}
Also used : ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 15 with Lock

use of java.util.concurrent.locks.Lock in project tomcat by apache.

the class GenericKeyedObjectPool method register.

/**
     * Register the use of a key by an object.
     * <p>
     * register() and deregister() must always be used as a pair.
     *
     * @param k The key to register
     *
     * @return The objects currently associated with the given key. If this
     *         method returns without throwing an exception then it will never
     *         return null.
     */
private ObjectDeque<T> register(final K k) {
    Lock lock = keyLock.readLock();
    ObjectDeque<T> objectDeque = null;
    try {
        lock.lock();
        objectDeque = poolMap.get(k);
        if (objectDeque == null) {
            // Upgrade to write lock
            lock.unlock();
            lock = keyLock.writeLock();
            lock.lock();
            objectDeque = poolMap.get(k);
            if (objectDeque == null) {
                objectDeque = new ObjectDeque<>(fairness);
                objectDeque.getNumInterested().incrementAndGet();
                // NOTE: Keys must always be added to both poolMap and
                //       poolKeyList at the same time while protected by
                //       keyLock.writeLock()
                poolMap.put(k, objectDeque);
                poolKeyList.add(k);
            } else {
                objectDeque.getNumInterested().incrementAndGet();
            }
        } else {
            objectDeque.getNumInterested().incrementAndGet();
        }
    } finally {
        lock.unlock();
    }
    return objectDeque;
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Aggregations

Lock (java.util.concurrent.locks.Lock)717 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)171 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)144 ReentrantLock (java.util.concurrent.locks.ReentrantLock)96 Test (org.junit.Test)57 ArrayList (java.util.ArrayList)41 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)36 IOException (java.io.IOException)33 CountDownLatch (java.util.concurrent.CountDownLatch)30 HashMap (java.util.HashMap)22 Triple (org.apache.clerezza.commons.rdf.Triple)21 Map (java.util.Map)20 Ignite (org.apache.ignite.Ignite)17 Condition (java.util.concurrent.locks.Condition)15 OSBTreeBonsaiLocalException (com.orientechnologies.orient.core.exception.OSBTreeBonsaiLocalException)13 OAtomicOperation (com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation)13 HashSet (java.util.HashSet)13 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)13 AtomicLong (java.util.concurrent.atomic.AtomicLong)12 OCacheEntry (com.orientechnologies.orient.core.storage.cache.OCacheEntry)11