8000 Squash: Disallow parallelism in CHM on Scala.js · scala-js/scala-js@daaa55f · GitHub
[go: up one dir, main page]

Skip to content

Commit daaa55f

Browse files
committed
Squash: Disallow parallelism in CHM on Scala.js
1 parent d3c42e1 commit daaa55f

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

javalib/src/main/scala/java/util/concurrent/ConcurrentHashMap.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,17 @@ class ConcurrentHashMap[K, V] private (initialCapacity: Int, loadFactor: Float)
7474
new ConcurrentHashMap.KeySetView[K, V](this.inner, mappedValue)
7575
}
7676

77+
private def checkNotParallel(parallelismThreshold: Long): Unit = {
78+
if (parallelismThreshold != Long.MaxValue) {
79+
throw new UnsupportedOperationException(
80+
"ConcurrentHashMap in Scala.js does not support parallel operations. " +
81+
"Set parallelismThreshold to Long.MaxValue to disable parallelism.")
82+
}
83+
}
84+
7785
def forEach(parallelismThreshold: Long, action: BiConsumer[_ >: K, _ >: V]): Unit = {
86+
checkNotParallel(parallelismThreshold)
87+
7888
// Note: It is tempting to simply call inner.forEach here:
7989
// However, this will not have the correct snapshotting behavior.
8090
val i = inner.nodeIterator()
@@ -84,11 +94,15 @@ class ConcurrentHashMap[K, V] private (initialCapacity: Int, loadFactor: Float)
8494
}
8595
}
8696

87-
def forEachKey(parallelismThreshold: Long, action: Consumer[_ >: K]): Unit =
97+
def forEachKey(parallelismThreshold: Long, action: Consumer[_ >: K]): Unit = {
98+
checkNotParallel(parallelismThreshold)
8899
inner.keyIterator().forEachRemaining(action)
100+
}
89101

90-
def forEachValue(parallelismThreshold: Long, action: Consumer[_ >: V]): Unit =
102+
def forEachValue(parallelismThreshold: Long, action: Consumer[_ >: V]): Unit = {
103+
checkNotParallel(parallelismThreshold)
91104
inner.valueIterator().forEachRemaining(action)
105+
}
92106

93107
override def values(): Collection[V] =
94108
inner.values()

test-suite/shared/src/test/scala/org/scalajs/testsuite/javalib/util/concurrent/ConcurrentHashMapTest.scala

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,11 @@ class ConcurrentHashMapTest extends MapTest {
235235

236236
val seen = mutable.Set.empty[(String, Int)]
237237

238-
map.forEach(1L, { (k, v) =>
238+
map.forEach(Long.MaxValue, { (k, v) =>
239239
if (k == "TWO")
240240
map.remove("TWO") // check snapshotting behavior.
241241

242-
seen.synchronized {
243-
seen += k -> v
244-
}
242+
seen += k -> v
245243
})
246244

247245
assertEquals(2, map.size())
@@ -256,13 +254,11 @@ class ConcurrentHashMapTest extends MapTest {
256254

257255
val seen = mutable.Set.empty[String]
258256

259-
map.forEachKey(1L, { k =>
257+
map.forEachKey(Long.MaxValue, { k =>
260258
if (k == "TWO")
261259
map.remove("TWO") // check snapshotting behavior.
262260

263-
seen.synchronized {
264-
seen += k
265-
}
261+
seen += k
266262
})
267263

268264
assertEquals(2, map.size())
@@ -277,13 +273,11 @@ class ConcurrentHashMapTest extends MapTest {
277273

278274
val seen = mutable.Set.empty[Int]
279275

280-
map.forEachValue(1L, { v =>
276+
map.forEachValue(Long.MaxValue, { v =>
281277
if (v == 2)
282278
map.remove("TWO") // check snapshotting behavior.
283279

284-
seen.synchronized {
285-
seen += v
286-
}
280+
seen += v
287281
})
288282

289283
assertEquals(2, map.size())

0 commit comments

Comments
 (0)
0