Skip to content

Instantly share code, notes, and snippets.

@yakamoto69
Created October 18, 2012 10:17
Show Gist options
  • Select an option

  • Save yakamoto69/3910874 to your computer and use it in GitHub Desktop.

Select an option

Save yakamoto69/3910874 to your computer and use it in GitHub Desktop.
Lock, Condition wrapper
import actors.threadpool.locks.{Condition, ReentrantLock}
class EasyLock {
val lock = new ReentrantLock
def mkCondition(f: => Boolean): EasyCondition = {
new EasyCondition(lock.newCondition(), f)
}
def apply[A](f: => A): A = {
lock.lock()
try {
f
} finally {
lock.unlock()
}
}
}
class EasyCondition(c: Condition, condition: => Boolean) {
def isTrue = condition
def waitUntilFulfilled[A](f: => A): A = {
while (!isTrue) {
c.await()
}
f
}
def signalAllIfFulfilled() {
if (isTrue) c.signalAll()
}
def signalIfFulfilled() {
if (isTrue) c.signal()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment