forked from I2P_Developers/i2p.i2p
BOB: NamedDB accesses are synchronized, correct findbugs fix is to remove volatile
This commit is contained in:
@ -15,8 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package net.i2p.BOB;
|
package net.i2p.BOB;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal database to relate nicknames to options to values
|
* Internal database to relate nicknames to options to values
|
||||||
*
|
*
|
||||||
@ -25,9 +23,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
public class NamedDB {
|
public class NamedDB {
|
||||||
|
|
||||||
private volatile Object[][] data;
|
private volatile Object[][] data;
|
||||||
private volatile int index;
|
private int index, writersWaiting, readers;
|
||||||
private final AtomicInteger writersWaiting = new AtomicInteger();
|
|
||||||
private final AtomicInteger readers = new AtomicInteger();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* make initial NULL object
|
* make initial NULL object
|
||||||
@ -35,27 +31,26 @@ public class NamedDB {
|
|||||||
*/
|
*/
|
||||||
public NamedDB() {
|
public NamedDB() {
|
||||||
this.data = new Object[1][2];
|
this.data = new Object[1][2];
|
||||||
this.index = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized public void getReadLock() {
|
synchronized public void getReadLock() {
|
||||||
while ((writersWaiting.get() != 0)) {
|
while ((writersWaiting != 0)) {
|
||||||
try {
|
try {
|
||||||
wait();
|
wait();
|
||||||
} catch (InterruptedException ie) {
|
} catch (InterruptedException ie) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
readers.incrementAndGet();
|
readers++;
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized public void releaseReadLock() {
|
synchronized public void releaseReadLock() {
|
||||||
readers.decrementAndGet();
|
readers--;
|
||||||
notifyAll();
|
notifyAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized public void getWriteLock() {
|
synchronized public void getWriteLock() {
|
||||||
writersWaiting.incrementAndGet();
|
writersWaiting++;
|
||||||
while (readers.get() != 0 && writersWaiting.get() != 1) {
|
while (readers != 0 && writersWaiting != 1) {
|
||||||
try {
|
try {
|
||||||
wait();
|
wait();
|
||||||
} catch (InterruptedException ie) {
|
} catch (InterruptedException ie) {
|
||||||
@ -64,7 +59,7 @@ public class NamedDB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
synchronized public void releaseWriteLock() {
|
synchronized public void releaseWriteLock() {
|
||||||
writersWaiting.decrementAndGet();
|
writersWaiting--;
|
||||||
notifyAll();
|
notifyAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user