From e2d514907aad8b9be0c71b41ae9760b17ed0b669 Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Wed, 26 Sep 2012 18:52:46 +0200 Subject: [PATCH] annotations for j.u.c.locks + related fixes of tests --- .../tests/controlFlowAnalysis/kt1189.kt | 4 +-- .../util/concurrent/locks/annotations.xml | 29 +++++++++++++++++++ .../stdlib/src/kotlin/concurrent/Locks.kt | 18 ++++++++++-- 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 jdk-annotations/java/util/concurrent/locks/annotations.xml diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/kt1189.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/kt1189.kt index ac05a5f9e5b..0039e5d4d04 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/kt1189.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/kt1189.kt @@ -3,7 +3,7 @@ package kt1189 import java.util.concurrent.locks.ReentrantReadWriteLock inline fun ReentrantReadWriteLock.write(action: ()->T) : T { - val rl = readLock()!! + val rl = readLock() var readCount = 0 val writeCount = getWriteHoldCount() if(writeCount == 0) { @@ -13,7 +13,7 @@ inline fun ReentrantReadWriteLock.write(action: ()->T) : T { rl.unlock() } - val wl = writeLock()!! + val wl = writeLock() wl.lock() try { return action() diff --git a/jdk-annotations/java/util/concurrent/locks/annotations.xml b/jdk-annotations/java/util/concurrent/locks/annotations.xml new file mode 100644 index 00000000000..c8951f5d66d --- /dev/null +++ b/jdk-annotations/java/util/concurrent/locks/annotations.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/concurrent/Locks.kt b/libraries/stdlib/src/kotlin/concurrent/Locks.kt index cf029a7e637..93db166b0dd 100644 --- a/libraries/stdlib/src/kotlin/concurrent/Locks.kt +++ b/libraries/stdlib/src/kotlin/concurrent/Locks.kt @@ -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 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 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 ReentrantReadWriteLock.write(action: ()->T) : T { wl.unlock() } } + +/** +Execute given calculation and await for CountDownLatch +Returns result of the calculation +*/ +fun Int.latch(op: CountDownLatch.() -> T) : T { + val cdl = CountDownLatch(this) + val res = cdl.op() + cdl.await() + return res +}