Skip to content

Commit 41f4394

Browse files
committed
Make Vector safe to share across threads without synchronization
1 parent 56ef1d9 commit 41f4394

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/library/scala/collection/immutable/Vector.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.io.{ObjectInputStream, ObjectOutputStream}
66

77
import scala.collection.mutable.{Builder, ReusableBuilder}
88
import scala.annotation.unchecked.uncheckedVariance
9+
import scala.runtime.ScalaRunTime
910

1011
/** $factoryInfo
1112
* @define Coll `Vector`
@@ -221,6 +222,7 @@ final class Vector[+A] private[immutable] (private[collection] val startIndex: I
221222
s.dirty = dirty
222223
s.gotoPosWritable(focus, idx, focus ^ idx) // if dirty commit changes; go to new pos and prepare for writing
223224
s.display0(idx & 31) = elem.asInstanceOf[AnyRef]
225+
ScalaRunTime.releaseFence()
224226
s
225227
}
226228

@@ -239,7 +241,7 @@ final class Vector[+A] private[immutable] (private[collection] val startIndex: I
239241
}
240242

241243
override def prepended[B >: A](value: B): Vector[B] = {
242-
if (endIndex != startIndex) {
244+
val result = if (endIndex != startIndex) {
243245
val blockIndex = (startIndex - 1) & ~31
244246
val lo = (startIndex - 1) & 31
245247

@@ -314,10 +316,12 @@ final class Vector[+A] private[immutable] (private[collection] val startIndex: I
314316
s.display0 = elems
315317
s
316318
}
319+
ScalaRunTime.releaseFence()
320+
result
317321
}
318322

319323
override def appended[B >: A](value: B): Vector[B] = {
320-
if (endIndex != startIndex) {
324+
val result = if (endIndex != startIndex) {
321325
val blockIndex = endIndex & ~31
322326
val lo = endIndex & 31
323327

@@ -376,6 +380,8 @@ final class Vector[+A] private[immutable] (private[collection] val startIndex: I
376380
s.display0 = elems
377381
s
378382
}
383+
ScalaRunTime.releaseFence()
384+
result
379385
}
380386

381387

@@ -512,6 +518,7 @@ final class Vector[+A] private[immutable] (private[collection] val startIndex: I
512518
s.gotoPosWritable(focus, blockIndex, focus ^ blockIndex)
513519
s.preClean(d)
514520
s.cleanLeftEdge(cutIndex - shift)
521+
ScalaRunTime.releaseFence()
515522
s
516523
}
517524

@@ -527,6 +534,7 @@ final class Vector[+A] private[immutable] (private[collection] val startIndex: I
527534
s.gotoPosWritable(focus, blockIndex, focus ^ blockIndex)
528535
s.preClean(d)
529536
s.cleanRightEdge(cutIndex - shift)
537+
ScalaRunTime.releaseFence()
530538
s
531539
}
532540

@@ -619,6 +627,7 @@ final class VectorBuilder[A]() extends ReusableBuilder[A, Vector[A]] with Vector
619627
val s = new Vector[A](0, size, 0) // should focus front or back?
620628
s.initFrom(this)
621629
if (depth > 1) s.gotoPos(0, size - 1) // we're currently focused to size - 1, not size!
630+
ScalaRunTime.releaseFence()
622631
s
623632
}
624633

0 commit comments

Comments
 (0)