From 68a264154fdb236da01484924175d537a6f1882c Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Wed, 1 Feb 2012 13:06:11 +0200 Subject: [PATCH] fixing upgrade logic bug --- stdlib/ktSrc/Locks.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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()