Skip to content

Commit

Permalink
ConcurrentWeakMap-related minor improvements (#3346)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwwdfsad committed Jun 24, 2022
1 parent cb65eb1 commit c9d3f7b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
Expand Up @@ -10,11 +10,10 @@ import java.lang.ref.*

// This is very limited implementation, not suitable as a generic map replacement.
// It has lock-free get and put with synchronized rehash for simplicity (and better CPU usage on contention)
@OptIn(ExperimentalStdlibApi::class)
@Suppress("UNCHECKED_CAST")
internal class ConcurrentWeakMap<K : Any, V: Any>(
/**
* Weak reference queue is needed when a small key is mapped to a large value and we need to promptly release a
* Weak reference queue is needed when a small key is mapped to a large value, and we need to promptly release a
* reference to the value when the key was already disposed.
*/
weakRefQueue: Boolean = false
Expand Down
3 changes: 2 additions & 1 deletion kotlinx-coroutines-core/jvm/test/TestBase.kt
Expand Up @@ -236,8 +236,9 @@ public actual open class TestBase(private var disableOutCheck: Boolean) {
if (expected != null) {
if (!expected(e))
error("Unexpected exception: $e", e)
} else
} else {
throw e
}
} finally {
if (ex == null && expected != null) error("Exception was expected but none produced")
}
Expand Down
Expand Up @@ -34,7 +34,7 @@ class ConcurrentWeakMapOperationStressTest : TestBase() {
var generationOffset = 0L
while (!stop.value) {
val kvs = (generationOffset + batchSize * index until generationOffset + batchSize * (index + 1))
.associateBy({ Key(it) }, { it * it })
.associateBy({ Key(it) }, { it * it })
generationOffset += batchSize * nThreads
for ((k, v) in kvs) {
assertEquals(null, m.put(k, v))
Expand All @@ -45,8 +45,8 @@ class ConcurrentWeakMapOperationStressTest : TestBase() {
for ((k, v) in kvs) {
assertEquals(v, m.remove(k))
}
for ((k, v) in kvs) {
assertEquals(null, m.get(k))
for ((k, _) in kvs) {
assertEquals(null, m[k])
}
count.incrementAndGet()
}
Expand All @@ -68,6 +68,6 @@ class ConcurrentWeakMapOperationStressTest : TestBase() {
}
stop.value = true
threads.forEach { it.join() }
assertEquals(0, m.size)
assertEquals(0, m.size, "Unexpected map state: $m")
}
}
}

0 comments on commit c9d3f7b

Please sign in to comment.