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

gitbucket.core.util.LockUtil.scala Maven / Gradle / Ivy

package gitbucket.core.util

import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.locks.{ReentrantLock, Lock}
import SyntaxSugars._

object LockUtil {

  /**
   * lock objects
   */
  private val locks = new ConcurrentHashMap[String, Lock]()

  /**
   * Returns the lock object for the specified repository.
   */
  private def getLockObject(key: String): Lock = synchronized {
    if(!locks.containsKey(key)){
      locks.put(key, new ReentrantLock())
    }
    locks.get(key)
  }

  /**
   * Synchronizes a given function which modifies the working copy of the wiki repository.
   */
  def lock[T](key: String)(f: => T): T = defining(getLockObject(key)){ lock =>
    try {
      lock.lock()
      f
    } finally {
      lock.unlock()
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy