Search in sources :

Example 26 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project orientdb by orientechnologies.

the class OPartitionedLockManager method acquireExclusiveLock.

public Lock acquireExclusiveLock(long value) {
    final int hashCode = longHashCode(value);
    final int index = index(hashCode);
    if (useSpinLock) {
        OReadersWriterSpinLock spinLock = spinLocks[index];
        spinLock.acquireWriteLock();
        return new SpinLockWrapper(false, spinLock);
    }
    final ReadWriteLock rwLock = locks[index];
    final Lock lock = rwLock.writeLock();
    lock.lock();
    return lock;
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 27 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project orientdb by orientechnologies.

the class OPartitionedLockManager method releaseSharedLock.

public void releaseSharedLock(final int value) {
    final int index = index(value);
    if (useSpinLock) {
        OReadersWriterSpinLock spinLock = spinLocks[index];
        spinLock.releaseReadLock();
        return;
    }
    final ReadWriteLock rwLock = locks[index];
    rwLock.readLock().unlock();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 28 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project hadoop by apache.

the class AbstractNodeLabelsProvider method serviceInit.

@Override
protected void serviceInit(Configuration conf) throws Exception {
    this.intervalTime = conf.getLong(YarnConfiguration.NM_NODE_LABELS_PROVIDER_FETCH_INTERVAL_MS, YarnConfiguration.DEFAULT_NM_NODE_LABELS_PROVIDER_FETCH_INTERVAL_MS);
    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    readLock = readWriteLock.readLock();
    writeLock = readWriteLock.writeLock();
    super.serviceInit(conf);
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 29 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project perun by CESNET.

the class PerunLocksUtils method lockGroupMembership.

/**
	 * Create transaction locks for combination of group and member (from list of members)
	 * and also bind them to the transaction (as resource by Object uniqueKey)
	 *
	 * @param group the group
	 * @param members list of members
	 * @throws InternalErrorException
	 * @throws InterruptedException
	 */
public static void lockGroupMembership(Group group, List<Member> members) throws InternalErrorException, InterruptedException {
    if (group == null)
        throw new InternalErrorException("Group can't be null when creating lock for group and list of members.");
    if (members == null)
        throw new InternalErrorException("Members can't be null or empty when creating lock for group and list of members.");
    //Sort list of members strictly by ids (there is compareTo method in perunBean using ids for comparing)
    Collections.sort(members);
    List<Lock> returnedLocks = new ArrayList<>();
    try {
        //TODO - On java8 use computeIfAbsent instead
        //try to lock all needed locks there
        ReadWriteLock groupReadWriteLock = groupsLocks.get(group);
        if (groupReadWriteLock == null) {
            groupsLocks.putIfAbsent(group, new ReentrantReadWriteLock(true));
            groupReadWriteLock = groupsLocks.get(group);
        }
        groupReadWriteLock.readLock().lock();
        returnedLocks.add(groupReadWriteLock.readLock());
        for (Member member : members) {
            //Get members lock map by group if exists or create a new one
            ConcurrentHashMap<Member, Lock> membersLocks = groupsMembersLocks.get(group);
            if (membersLocks == null) {
                groupsMembersLocks.putIfAbsent(group, new ConcurrentHashMap<Member, Lock>());
                membersLocks = groupsMembersLocks.get(group);
            }
            //TODO - On java8 use computeIfAbsent instead
            //Get concrete member lock from members lock map or create a new one if not exists
            Lock memberLock = membersLocks.get(member);
            if (memberLock == null) {
                membersLocks.putIfAbsent(member, new ReentrantLock(true));
                memberLock = membersLocks.get(member);
            }
            //Lock the lock and return it
            memberLock.lock();
            returnedLocks.add(memberLock);
        }
        //bind these locks like transaction resource
        if (TransactionSynchronizationManager.getResource(uniqueKey) == null) {
            TransactionSynchronizationManager.bindResource(uniqueKey, returnedLocks);
        } else {
            ((List<Lock>) TransactionSynchronizationManager.getResource(uniqueKey)).addAll(returnedLocks);
        }
    } catch (Exception ex) {
        //if some exception has been thrown, unlock all already locked locks
        unlockAll(returnedLocks);
        throw ex;
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Member(cz.metacentrum.perun.core.api.Member) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 30 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project perun by CESNET.

the class PerunLocksUtils method lockGroupMembership.

/**
	 * Create transaction locks for group and bind them to the transaction (as resource by Object uniqueKey)
	 *
	 * @param group the group
	 * @throws InternalErrorException
	 * @throws InterruptedException
	 */
public static void lockGroupMembership(Group group) throws InternalErrorException, InterruptedException {
    if (group == null)
        throw new InternalErrorException("Group can't be null when creating lock for group.");
    List<Lock> returnedLocks = new ArrayList<>();
    try {
        //TODO - On java8 use computeIfAbsent instead
        //Need to investigate if we have all needed locks already in the structure or we need to create them
        ReadWriteLock groupReadWriteLock = groupsLocks.get(group);
        if (groupReadWriteLock == null) {
            groupsLocks.putIfAbsent(group, new ReentrantReadWriteLock(true));
            groupReadWriteLock = groupsLocks.get(group);
        }
        groupReadWriteLock.writeLock().lock();
        returnedLocks.add(groupReadWriteLock.writeLock());
        //bind these locks like transaction resource
        if (TransactionSynchronizationManager.getResource(uniqueKey) == null) {
            TransactionSynchronizationManager.bindResource(uniqueKey, returnedLocks);
        } else {
            ((List<Lock>) TransactionSynchronizationManager.getResource(uniqueKey)).addAll(returnedLocks);
        }
    } catch (Exception ex) {
        //if some exception has been thrown, unlock all already locked locks
        unlockAll(returnedLocks);
        throw ex;
    }
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Aggregations

ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)45 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)36 Lock (java.util.concurrent.locks.Lock)21 Test (org.junit.Test)8 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 Nullable (org.jetbrains.annotations.Nullable)4 ArrayList (java.util.ArrayList)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 Ignore (org.junit.Ignore)2 Member (cz.metacentrum.perun.core.api.Member)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Random (java.util.Random)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 Phaser (java.util.concurrent.Phaser)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1