Added support for stamped locks

This commit is contained in:
Andrea Cavalli 2020-06-24 01:19:22 +02:00
parent d6ac2e306a
commit fd426938f5
2 changed files with 197 additions and 0 deletions

View File

@ -0,0 +1,68 @@
package org.warp.commonutils.concurrency.future;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.locks.StampedLock;
import java.util.function.Supplier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.warp.commonutils.functional.IOSupplier;
public class FutureLockUtils {
public static <T> CompletableFuture<T> readLock(@Nullable StampedLock lock, @NotNull Supplier<CompletableFuture<T>> r) {
long lockValue;
if (lock != null) {
lockValue = lock.readLock();
} else {
lockValue = 0;
}
return r.get().whenComplete((x, y) -> {
if (lock != null) {
lock.unlockRead(lockValue);
}
});
}
public static <T> CompletableFuture<T> writeLock(@Nullable StampedLock lock, @NotNull Supplier<CompletableFuture<T>> r) {
long lockValue;
if (lock != null) {
lockValue = lock.writeLock();
} else {
lockValue = 0;
}
return r.get().whenComplete((x, y) -> {
if (lock != null) {
lock.unlockWrite(lockValue);
}
});
}
public static <T> CompletableFuture<T> readLockIO(@Nullable StampedLock lock, @NotNull IOSupplier<CompletableFuture<T>> r) throws IOException {
long lockValue;
if (lock != null) {
lockValue = lock.readLock();
} else {
lockValue = 0;
}
return r.get().whenComplete((x, y) -> {
if (lock != null) {
lock.unlockRead(lockValue);
}
});
}
public static <T> CompletableFuture<T> writeLockIO(@Nullable StampedLock lock, @NotNull IOSupplier<CompletableFuture<T>> r) throws IOException {
long lockValue;
if (lock != null) {
lockValue = lock.writeLock();
} else {
lockValue = 0;
}
return r.get().whenComplete((x, y) -> {
if (lock != null) {
lock.unlockWrite(lockValue);
}
});
}
}

View File

@ -2,6 +2,7 @@ package org.warp.commonutils.locks;
import java.io.IOException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.StampedLock;
import java.util.function.Supplier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -23,6 +24,38 @@ public class LockUtils {
}
}
public static void readLock(@Nullable StampedLock lock, @NotNull Runnable r) {
long lockValue;
if (lock != null) {
lockValue = lock.readLock();
} else {
lockValue = 0;
}
try {
r.run();
} finally {
if (lock != null) {
lock.unlockRead(lockValue);
}
}
}
public static void writeLock(@Nullable StampedLock lock, @NotNull Runnable r) {
long lockValue;
if (lock != null) {
lockValue = lock.writeLock();
} else {
lockValue = 0;
}
try {
r.run();
} finally {
if (lock != null) {
lock.unlockWrite(lockValue);
}
}
}
public static void lock(@Nullable LeftRightLock lock, boolean right, @NotNull Runnable r) {
if (lock != null) {
if (right) {
@ -57,6 +90,38 @@ public class LockUtils {
}
}
public static void readLockIO(@Nullable StampedLock lock, @NotNull IORunnable r) throws IOException {
long lockValue;
if (lock != null) {
lockValue = lock.readLock();
} else {
lockValue = 0;
}
try {
r.run();
} finally {
if (lock != null) {
lock.unlockRead(lockValue);
}
}
}
public static void writeLockIO(@Nullable StampedLock lock, @NotNull IORunnable r) throws IOException {
long lockValue;
if (lock != null) {
lockValue = lock.writeLock();
} else {
lockValue = 0;
}
try {
r.run();
} finally {
if (lock != null) {
lock.unlockWrite(lockValue);
}
}
}
public static void lockIO(@Nullable LeftRightLock lock, boolean right, @NotNull IORunnable r) throws IOException {
if (lock != null) {
if (right) {
@ -91,6 +156,38 @@ public class LockUtils {
}
}
public static <T> T readLock(@Nullable StampedLock lock, @NotNull Supplier<T> r) {
long lockValue;
if (lock != null) {
lockValue = lock.readLock();
} else {
lockValue = 0;
}
try {
return r.get();
} finally {
if (lock != null) {
lock.unlockRead(lockValue);
}
}
}
public static <T> T writeLock(@Nullable StampedLock lock, @NotNull Supplier<T> r) {
long lockValue;
if (lock != null) {
lockValue = lock.writeLock();
} else {
lockValue = 0;
}
try {
return r.get();
} finally {
if (lock != null) {
lock.unlockWrite(lockValue);
}
}
}
public static <T> T lock(@Nullable LeftRightLock lock, boolean right, @NotNull Supplier<T> r) {
if (lock != null) {
if (right) {
@ -125,6 +222,38 @@ public class LockUtils {
}
}
public static <T> T readLockIO(@Nullable StampedLock lock, @NotNull IOSupplier<T> r) throws IOException {
long lockValue;
if (lock != null) {
lockValue = lock.readLock();
} else {
lockValue = 0;
}
try {
return r.get();
} finally {
if (lock != null) {
lock.unlockRead(lockValue);
}
}
}
public static <T> T writeLockIO(@Nullable StampedLock lock, @NotNull IOSupplier<T> r) throws IOException {
long lockValue;
if (lock != null) {
lockValue = lock.writeLock();
} else {
lockValue = 0;
}
try {
return r.get();
} finally {
if (lock != null) {
lock.unlockWrite(lockValue);
}
}
}
public static <T> T lockIO(@Nullable LeftRightLock lock, boolean right, @NotNull IOSupplier<T> r) throws IOException {
if (lock != null) {
if (right) {