annotations for j.u.c.locks + related fixes of tests

This commit is contained in:
Alex Tkachman
2012-09-26 18:52:46 +02:00
parent 7ed2126ebb
commit e2d514907a
3 changed files with 46 additions and 5 deletions
@@ -4,6 +4,7 @@ import java.util.concurrent.locks.Lock
import java.util.concurrent.locks.ReadWriteLock
import java.util.concurrent.locks.ReentrantReadWriteLock
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock
import java.util.concurrent.CountDownLatch
/**
Executes given calculation under lock
@@ -24,7 +25,7 @@ Executes given calculation under read lock
Returns result of the calculation
*/
public inline fun <T> ReentrantReadWriteLock.read(action: ()->T) : T {
val rl = readLock()!!
val rl = readLock()
rl.lock()
try {
return action()
@@ -41,12 +42,12 @@ If such write has been initiated by checking some condition, the condition must
Returns result of the calculation
*/
public inline fun <T> ReentrantReadWriteLock.write(action: ()->T) : T {
val rl = readLock()!!
val rl = readLock()
val readCount = if (getWriteHoldCount() == 0) getReadHoldCount() else 0
readCount times { rl.unlock() }
val wl = writeLock()!!
val wl = writeLock()
wl.lock()
try {
return action()
@@ -56,3 +57,14 @@ public inline fun <T> ReentrantReadWriteLock.write(action: ()->T) : T {
wl.unlock()
}
}
/**
Execute given calculation and await for CountDownLatch
Returns result of the calculation
*/
fun <T> Int.latch(op: CountDownLatch.() -> T) : T {
val cdl = CountDownLatch(this)
val res = cdl.op()
cdl.await()
return res
}