Skip to content

Commit e81439a

Browse files
committed
Check tread access to HashSet.
1 parent 4c30784 commit e81439a

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/dotty/tools/dotc/util/HashSet.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class HashSet[T >: Null <: AnyRef](initialCapacity: Int, loadFactor: Float = 0.2
1414

1515
private def allocate(size: Int) = {
1616
table = new Array[AnyRef](size)
17+
checkTread
1718
limit = (size * loadFactor).toInt
1819
}
1920

@@ -47,6 +48,7 @@ class HashSet[T >: Null <: AnyRef](initialCapacity: Int, loadFactor: Float = 0.2
4748
private def addEntryAt(idx: Int, x: T) = {
4849
table(idx) = x
4950
used += 1
51+
checkTread
5052
if (used > limit) growTable()
5153
x
5254
}
@@ -63,6 +65,7 @@ class HashSet[T >: Null <: AnyRef](initialCapacity: Int, loadFactor: Float = 0.2
6365
}
6466

6567
private var rover: Int = -1
68+
private var thread: Thread = null
6669

6770
/** Add entry `x` to set */
6871
def addEntry(x: T): Unit = {
@@ -75,6 +78,7 @@ class HashSet[T >: Null <: AnyRef](initialCapacity: Int, loadFactor: Float = 0.2
7578
}
7679
table(h) = x
7780
used += 1
81+
checkTread
7882
if (used > (table.length >> 2)) growTable()
7983
}
8084

@@ -95,8 +99,15 @@ class HashSet[T >: Null <: AnyRef](initialCapacity: Int, loadFactor: Float = 0.2
9599
else null
96100
}
97101

102+
private def checkTread: Unit = {
103+
if (thread == null)
104+
thread = Thread.currentThread()
105+
else assert(thread == Thread.currentThread())
106+
}
107+
98108
/** Privileged access: Find first entry with given hashcode */
99109
protected def findEntryByHash(hashCode: Int): T = {
110+
checkTread
100111
rover = index(hashCode)
101112
nextEntryByHash(hashCode)
102113
}
@@ -107,6 +118,7 @@ class HashSet[T >: Null <: AnyRef](initialCapacity: Int, loadFactor: Float = 0.2
107118
protected def nextEntryByHash(hashCode: Int): T = {
108119
var entry = table(rover)
109120
while (entry ne null) {
121+
checkTread
110122
rover = index(rover + 1)
111123
if (hash(entry.asInstanceOf[T]) == hashCode) return entry.asInstanceOf[T]
112124
entry = table(rover)
@@ -124,6 +136,7 @@ class HashSet[T >: Null <: AnyRef](initialCapacity: Int, loadFactor: Float = 0.2
124136
private def addOldEntry(x: T): Unit = {
125137
var h = index(hash(x))
126138
var entry = table(h)
139+
checkTread
127140
while (entry ne null) {
128141
h = index(h + 1)
129142
entry = table(h)

0 commit comments

Comments
 (0)