Merge pull request #5069 from eatkins/stream-locks

Add per file stream locks
This commit is contained in:
eugene yokota 2019-09-17 23:14:54 -04:00 committed by GitHub
commit 9d0bb68bd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 1 deletions

View File

@ -9,6 +9,7 @@ package sbt
package std
import java.io.{ File => _, _ }
import java.util.concurrent.ConcurrentHashMap
import sbt.internal.io.DeferredWriter
import sbt.internal.util.ManagedLogger
@ -98,6 +99,7 @@ object Streams {
try {
c.close()
} catch { case _: IOException => () }
private[this] val streamLocks = new ConcurrentHashMap[File, AnyRef]()
def closeable[Key](delegate: Streams[Key]): CloseableStreams[Key] = new CloseableStreams[Key] {
private[this] val streams = new collection.mutable.HashMap[Key, ManagedStreams[Key]]
@ -184,7 +186,18 @@ object Streams {
def make[T <: Closeable](a: Key, sid: String)(f: File => T): T = synchronized {
checkOpen()
val file = taskDirectory(a) / sid
IO.touch(file, false)
val parent = file.getParentFile
val newLock = new AnyRef
val lock = streamLocks.putIfAbsent(parent, newLock) match {
case null => newLock
case l => l
}
try lock.synchronized {
if (!file.exists) IO.touch(file, setModified = false)
} finally {
streamLocks.remove(parent)
()
}
val t = f(file)
opened ::= t
t