java.util.concurrent.locks.ReentrantReadWriteLock Scala Examples

The following examples show how to use java.util.concurrent.locks.ReentrantReadWriteLock. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: CachedData.scala    From matcher   with MIT License 5 votes vote down vote up
package com.wavesplatform.dex.it.cache

import java.util.concurrent.locks.ReentrantReadWriteLock

class CachedData[T <: AnyRef](getData: => T) {

  private val lock  = new ReentrantReadWriteLock()
  private val read  = lock.readLock()
  private val write = lock.writeLock()

  private var cached = Option.empty[T]

  def get(): T =
    try {
      read.lock()
      cached match {
        case Some(x) => x
        case None =>
          val r = getData
          cached = Some(r)
          r
      }
    } finally read.unlock()

  def invalidate(): Unit =
    try {
      write.lock()
      cached = None
    } finally write.unlock()
}

object CachedData {
  def apply[T <: AnyRef](getData: => T): CachedData[T] = new CachedData[T](getData)
} 
Example 2
Source File: Symbol.scala    From perf_tester   with Apache License 2.0 5 votes vote down vote up
package scala


private[scala] abstract class UniquenessCache[K, V >: Null]
{
  import java.lang.ref.WeakReference
  import java.util.WeakHashMap
  import java.util.concurrent.locks.ReentrantReadWriteLock

  private val rwl = new ReentrantReadWriteLock()
  private val rlock = rwl.readLock
  private val wlock = rwl.writeLock
  private val map = new WeakHashMap[K, WeakReference[V]]

  protected def valueFromKey(k: K): V
  protected def keyFromValue(v: V): Option[K]

  def apply(name: K): V = {
    def cached(): V = {
      rlock.lock
      try {
        val reference = map get name
        if (reference == null) null
        else reference.get  // will be null if we were gc-ed
      }
      finally rlock.unlock
    }
    def updateCache(): V = {
      wlock.lock
      try {
        val res = cached()
        if (res != null) res
        else {
          // If we don't remove the old String key from the map, we can
          // wind up with one String as the key and a different String as
          // the name field in the Symbol, which can lead to surprising GC
          // behavior and duplicate Symbols. See scala/bug#6706.
          map remove name
          val sym = valueFromKey(name)
          map.put(name, new WeakReference(sym))
          sym
        }
      }
      finally wlock.unlock
    }

    val res = cached()
    if (res == null) updateCache()
    else res
  }
  def unapply(other: V): Option[K] = keyFromValue(other)
} 
Example 3
Source File: UniquenessCache.scala    From incubator-daffodil   with Apache License 2.0 5 votes vote down vote up
package org.apache.daffodil.util


abstract class UniquenessCache[K, V >: Null]
{
  import java.lang.ref.WeakReference
  import java.util.WeakHashMap
  import java.util.concurrent.locks.ReentrantReadWriteLock

  private[this] val rwl = new ReentrantReadWriteLock()
  private[this] val rlock = rwl.readLock
  private[this] val wlock = rwl.writeLock
  private[this] val map = new WeakHashMap[K, WeakReference[V]]

  protected def valueFromKey(k: K): V
  protected def keyFromValue(v: V): Option[K]

  def apply(name: K): V = {
    def cached(): V = {
      rlock.lock
      try {
        val reference = map get name
        if (reference == null) null
        else reference.get  // will be null if we were gc-ed
      }
      finally rlock.unlock
    }
    def updateCache(): V = {
      wlock.lock
      try {
        val res = cached()
        if (res != null) res
        else {
          // If we don't remove the old String key from the map, we can
          // wind up with one String as the key and a different String as
          // the name field in the Symbol, which can lead to surprising GC
          // behavior and duplicate Symbols. See scala/bug#6706.
          map remove name
          val sym = valueFromKey(name)
          map.put(name, new WeakReference(sym))
          sym
        }
      }
      finally wlock.unlock
    }

    val res = cached()
    if (res == null) updateCache()
    else res
  }
  def unapply(other: V): Option[K] = keyFromValue(other)
} 
Example 4
Source File: RwLock.scala    From mist   with Apache License 2.0 5 votes vote down vote up
package io.hydrosphere.mist.master.data

import java.util.concurrent.locks.{Lock, ReentrantReadWriteLock}

trait RwLock {

  private val rwLock = new ReentrantReadWriteLock()

  protected def withLock[T](l: Lock, f: => T): T = {
    l.lock()
    try {
      f
    } finally {
      l.unlock()
    }
  }

  protected def withReadLock[T](f: => T): T = withLock(rwLock.readLock, f)
  protected def withWriteLock[T](f: => T): T = withLock(rwLock.writeLock, f)
}