diff --git a/stdlib/ktSrc/Locks.kt b/stdlib/ktSrc/Locks.kt index 605d15c891b..34cb880b748 100644 --- a/stdlib/ktSrc/Locks.kt +++ b/stdlib/ktSrc/Locks.kt @@ -35,7 +35,9 @@ inline fun ReentrantReadWriteLock.read(action: ()->T) : T { } /* -Executes given calculation under write lock doing upgrade from read lock if needed +Executes given calculation under write lock. +The method does upgrade from read to write lock if needed +If such write has been initiated by checking some condition, the condition must be rechecked inside the action to avoid possible races Returns result of the calculation */ inline fun ReentrantReadWriteLock.write(action: ()->T) : T { @@ -46,6 +48,9 @@ inline fun ReentrantReadWriteLock.write(action: ()->T) : T { if(upgradeCount > 0) rl.unlock(upgradeCount) } + else { + upgradeCount = 0 + } val wl = writeLock().sure() wl.lock()