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

com.wavesplatform.utils.OptimisticLockable.scala Maven / Gradle / Ivy

The newest version!
package com.wavesplatform.utils

import java.util.concurrent.locks.StampedLock

import scala.util.control.NonFatal

trait OptimisticLockable {
  private[this] val lock = new StampedLock

  protected def readLockCond[A](getValue: => A)(shouldRecheck: A => Boolean): A = {
    def readLocked(): A = {
      val stamp = lock.readLockInterruptibly()
      try {
        getValue
      } finally {
        lock.unlockRead(stamp)
      }
    }

    val stamp = lock.tryOptimisticRead()
    try {
      val result = getValue
      if (!lock.validate(stamp) && shouldRecheck(result)) readLocked()
      else result
    } catch {
      case NonFatal(_) if !lock.validate(stamp) =>
        readLocked()
    }
  }

  protected def writeLock[A](a: => A): A = {
    val stamp = lock.writeLock()
    try {
      a
    } finally {
      lock.unlockWrite(stamp)
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy