ai.h2o.automl.utils.DKVUtils Maven / Gradle / Ivy
package ai.h2o.automl.utils;
import water.Job;
import water.Key;
import water.Lockable;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public final class DKVUtils {
private DKVUtils() {}
public static > void atomicUpdate(Lockable target, Runnable update, Key jobKey) {
target.write_lock(jobKey);
try {
update.run();
target.update(jobKey);
} finally {
target.unlock(jobKey);
}
}
public static > void atomicUpdate(Lockable target, Runnable update, Key jobKey, ReadWriteLock lock) {
final Lock writeLock = lock.writeLock();
if (lock instanceof ReentrantReadWriteLock && ((ReentrantReadWriteLock.WriteLock)writeLock).isHeldByCurrentThread()) {
writeLock.lock();
try {
update.run();
} finally {
writeLock.unlock();
}
} else {
writeLock.lock();
try {
atomicUpdate(target, update, jobKey);
} finally {
writeLock.unlock();
}
}
}
}