diff --git a/stdlib/ktSrc/Integer.kt b/stdlib/ktSrc/Integer.kt new file mode 100644 index 00000000000..60255b6c16b --- /dev/null +++ b/stdlib/ktSrc/Integer.kt @@ -0,0 +1,9 @@ +package std + +inline fun Int.times(body : () -> Unit) { + var count = this; + while (count > 0) { + body() + count-- + } +} diff --git a/stdlib/ktSrc/Locks.kt b/stdlib/ktSrc/Locks.kt index 34cb880b748..54a5126f751 100644 --- a/stdlib/ktSrc/Locks.kt +++ b/stdlib/ktSrc/Locks.kt @@ -42,15 +42,9 @@ Returns result of the calculation */ inline fun ReentrantReadWriteLock.write(action: ()->T) : T { val rl = readLock().sure() - var upgradeCount = getWriteHoldCount() - if(upgradeCount == 0) { - upgradeCount = getReadHoldCount() - if(upgradeCount > 0) - rl.unlock(upgradeCount) - } - else { - upgradeCount = 0 - } + + val readCount = if (getWriteHoldCount() == 0) getReadHoldCount() else 0 + readCount times { rl.unlock() } val wl = writeLock().sure() wl.lock() @@ -58,25 +52,7 @@ inline fun ReentrantReadWriteLock.write(action: ()->T) : T { return action() } finally { - if(upgradeCount > 0) { - rl.lock(upgradeCount) - } + readCount times { rl.lock() } wl.unlock() } } - -inline private fun ReadLock.lock(readCount: Int) { - if(readCount > 0) { - for(j in 1..readCount) { - lock() - } - } -} - -inline private fun ReadLock.unlock(readCount: Int) { - if(readCount > 0) { - for(j in 1..readCount) { - unlock() - } - } -}