diff --git a/src/main/java/org/caosdb/server/database/DatabaseAccessManager.java b/src/main/java/org/caosdb/server/database/DatabaseAccessManager.java index 196a9fb2b4418d81ced4b174482f8f6a5dce8ce8..f12c78e31bf28799b8c8ead1c9ff311a65bb01ef 100644 --- a/src/main/java/org/caosdb/server/database/DatabaseAccessManager.java +++ b/src/main/java/org/caosdb/server/database/DatabaseAccessManager.java @@ -26,6 +26,7 @@ package org.caosdb.server.database; import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; import org.caosdb.server.database.access.Access; import org.caosdb.server.database.access.AccessControlAccess; @@ -56,7 +57,7 @@ import org.caosdb.server.utils.Releasable; class ReadAccessSemaphore extends Semaphore implements Releasable { private static final long serialVersionUID = 4384921156838881337L; - private int acquired = 0; // how many threads have read access + private AtomicInteger acquired = new AtomicInteger(0); // how many threads have read access Semaphore writersBlock = new Semaphore(1, true); // This semaphore is blocked as long as there are any // unreleased read permits. @@ -73,10 +74,9 @@ class ReadAccessSemaphore extends Semaphore implements Releasable { @Override public void acquire() throws InterruptedException { super.acquire(); // Protect the next few lines - if (this.acquired == 0) { + if (this.acquired.getAndIncrement() == 0) { this.writersBlock.acquire(); } - this.acquired++; super.release(); } @@ -87,9 +87,7 @@ class ReadAccessSemaphore extends Semaphore implements Releasable { */ @Override public void release() { - this.acquired--; - if (this.acquired <= 0) { // Last permit: release - this.acquired = 0; + if (this.acquired.decrementAndGet() == 0) { // Last permit: release if (this.writersBlock.availablePermits() <= 0) { this.writersBlock.release(); }