All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ai.h2o.automl.utils.DKVUtils Maven / Gradle / Ivy

There is a newer version: 3.46.0.6
Show newest version
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();
        }
      }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy