diff --git a/src/main/java/org/warp/commonutils/concurrency/future/FutureLockUtils.java b/src/main/java/org/warp/commonutils/concurrency/future/FutureLockUtils.java new file mode 100644 index 0000000..09e56be --- /dev/null +++ b/src/main/java/org/warp/commonutils/concurrency/future/FutureLockUtils.java @@ -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 CompletableFuture readLock(@Nullable StampedLock lock, @NotNull Supplier> 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 CompletableFuture writeLock(@Nullable StampedLock lock, @NotNull Supplier> 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 CompletableFuture readLockIO(@Nullable StampedLock lock, @NotNull IOSupplier> 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 CompletableFuture writeLockIO(@Nullable StampedLock lock, @NotNull IOSupplier> 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); + } + }); + } +} diff --git a/src/main/java/org/warp/commonutils/locks/LockUtils.java b/src/main/java/org/warp/commonutils/locks/LockUtils.java index 7995218..dbf4dde 100644 --- a/src/main/java/org/warp/commonutils/locks/LockUtils.java +++ b/src/main/java/org/warp/commonutils/locks/LockUtils.java @@ -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 readLock(@Nullable StampedLock lock, @NotNull Supplier 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 writeLock(@Nullable StampedLock lock, @NotNull Supplier 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 lock(@Nullable LeftRightLock lock, boolean right, @NotNull Supplier r) { if (lock != null) { if (right) { @@ -125,6 +222,38 @@ public class LockUtils { } } + public static T readLockIO(@Nullable StampedLock lock, @NotNull IOSupplier 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 writeLockIO(@Nullable StampedLock lock, @NotNull IOSupplier 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 lockIO(@Nullable LeftRightLock lock, boolean right, @NotNull IOSupplier r) throws IOException { if (lock != null) { if (right) {