use of com.orientechnologies.common.types.OModifiableInteger in project orientdb by orientechnologies.
the class OSBTreeRidBag method updateSize.
/**
* Recalculates real bag size.
*
* @return real size
*/
private int updateSize() {
int size = 0;
if (collectionPointer != null) {
final OSBTreeBonsai<OIdentifiable, Integer> tree = loadTree();
try {
size = tree.getRealBagSize(changes);
} finally {
releaseTree();
}
} else {
for (Change change : changes.values()) {
size += change.applyTo(0);
}
}
for (OModifiableInteger diff : newEntries.values()) {
size += diff.getValue();
}
this.size = size;
return size;
}
use of com.orientechnologies.common.types.OModifiableInteger in project orientdb by orientechnologies.
the class OReadersWriterSpinLock method releaseReadLock.
public void releaseReadLock() {
final OModifiableInteger lHolds = lockHolds.get();
final int holds = lHolds.intValue();
if (holds > 1) {
lHolds.decrement();
return;
} else if (holds < 0) {
// write lock was acquired before, do nothing
return;
}
distributedCounter.decrement();
lHolds.decrement();
assert lHolds.intValue() == 0;
}
use of com.orientechnologies.common.types.OModifiableInteger in project orientdb by orientechnologies.
the class OReadersWriterSpinLock method acquireWriteLock.
public void acquireWriteLock() {
final OModifiableInteger lHolds = lockHolds.get();
if (lHolds.intValue() < 0) {
lHolds.decrement();
return;
}
final WNode node = myNode.get();
node.locked = true;
final WNode pNode = tail.getAndSet(myNode.get());
predNode.set(pNode);
while (pNode.locked) {
pNode.waitingWriter = Thread.currentThread();
if (pNode.locked)
LockSupport.park(this);
}
pNode.waitingWriter = null;
final long beginTime = System.currentTimeMillis();
while (!distributedCounter.isEmpty()) {
// IN THE WORST CASE CPU CAN BE 100% FOR MAXIMUM 1 SECOND
if (System.currentTimeMillis() - beginTime > 1000)
try {
Thread.sleep(1);
} catch (InterruptedException e) {
break;
}
}
setExclusiveOwnerThread(Thread.currentThread());
lHolds.decrement();
assert lHolds.intValue() == -1;
}
use of com.orientechnologies.common.types.OModifiableInteger in project orientdb by orientechnologies.
the class OReadersWriterSpinLock method releaseWriteLock.
public void releaseWriteLock() {
final OModifiableInteger lHolds = lockHolds.get();
if (lHolds.intValue() < -1) {
lHolds.increment();
return;
}
setExclusiveOwnerThread(null);
final WNode node = myNode.get();
node.locked = false;
final Thread waitingWriter = node.waitingWriter;
if (waitingWriter != null)
LockSupport.unpark(waitingWriter);
Thread waitingReader;
while ((waitingReader = node.waitingReaders.poll()) != null) {
LockSupport.unpark(waitingReader);
}
myNode.set(predNode.get());
predNode.set(null);
lHolds.increment();
assert lHolds.intValue() == 0;
}
use of com.orientechnologies.common.types.OModifiableInteger in project orientdb by orientechnologies.
the class OSBTreeRidBag method add.
public void add(final OIdentifiable identifiable) {
if (identifiable == null)
throw new NullPointerException("Impossible to add a null identifiable in a ridbag");
if (identifiable.getIdentity().isValid()) {
Change counter = changes.get(identifiable);
if (counter == null)
changes.put(identifiable, new DiffChange(1));
else {
if (counter.isUndefined()) {
counter = getAbsoluteValue(identifiable);
changes.put(identifiable, counter);
}
counter.increment();
}
} else {
final OModifiableInteger counter = newEntries.get(identifiable);
if (counter == null)
newEntries.put(identifiable, new OModifiableInteger(1));
else
counter.increment();
}
if (size >= 0)
size++;
if (this.owner != null)
ORecordInternal.track(this.owner, identifiable);
if (updateOwner)
fireCollectionChangedEvent(new OMultiValueChangeEvent<OIdentifiable, OIdentifiable>(OMultiValueChangeEvent.OChangeType.ADD, identifiable, identifiable, null, false));
}
Aggregations