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;
}
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();
}
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);
}
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;
}
}
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;
}
}
Aggregations