Created
October 18, 2012 10:17
-
-
Save yakamoto69/3910874 to your computer and use it in GitHub Desktop.
Lock, Condition wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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