1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

Added read/write locks for the PDOMDatabase.

This commit is contained in:
Doug Schaefer 2006-03-27 18:22:55 +00:00
parent e9c9f58a11
commit 6d80cc5d00

View file

@ -240,5 +240,55 @@ public class PDOMDatabase implements IPDOM {
else
return PDOMLinkage.getLinkage(this, record).getBinding(record);
}
// Read-write lock. Since we want to allow reads during a long
// running index, readers take precidence.
private Object lockMutex = new Object();
private int lockCount;
private int waitingReaders;
private int waitingWriters;
public void getReadLock(boolean waitForWrites) throws InterruptedException {
synchronized (lockMutex) {
if (!waitForWrites)
++waitingReaders;
while (lockCount < 0 || (waitForWrites && waitingWriters > 0))
// wait for the writers to finish
lockMutex.wait();
// free to go
++lockCount;
if (!waitForWrites)
--waitingReaders;
}
}
public void getWriteLock() throws InterruptedException {
synchronized (lockMutex) {
++waitingWriters;
while (lockCount != 0 || waitingReaders > 0)
// wait for everyone to finish
// readers get precidence
lockMutex.wait();
// free to go
--lockCount;
--waitingWriters;
}
}
public void releaseReadLock() {
synchronized (lockMutex) {
if (lockCount > 0)
--lockCount;
lockMutex.notifyAll();
}
}
public void releaseWriteLock() {
synchronized (lockMutex) {
if (lockCount < 0)
++lockCount;
lockMutex.notifyAll();
}
}
}