Kotlin Help

Progress guarantees

Many concurrent algorithms provide non-blocking progress guarantees, such as lock-freedom and wait-freedom. As they are usually non-trivial, it's easy to add a bug that blocks the algorithm. Lincheck can help you find liveness bugs using the model checking strategy.

To check the progress guarantee of the algorithm, enable the checkObstructionFreedom option in ModelCheckingOptions():

ModelCheckingOptions().checkObstructionFreedom()

Create a ConcurrentMapTest.kt file. Then add the following test to detect that ConcurrentHashMap::put(key: K, value: V) from the Java standard library is a blocking operation:

import org.jetbrains.kotlinx.lincheck.* import org.jetbrains.kotlinx.lincheck.annotations.* import org.jetbrains.kotlinx.lincheck.strategy.managed.modelchecking.* import org.junit.* import java.util.concurrent.* class ConcurrentHashMapTest { private val map = ConcurrentHashMap<Int, Int>() @Operation fun put(key: Int, value: Int) = map.put(key, value) @Test fun modelCheckingTest() = ModelCheckingOptions() .actorsBefore(1) // To init the HashMap .actorsPerThread(1) .actorsAfter(0) .minimizeFailedScenario(false) .checkObstructionFreedom() .check(this::class) }

Run the modelCheckingTest(). You should get the following result:

= Obstruction-freedom is required but a lock has been found = | ---------------------- | | Thread 1 | Thread 2 | | ---------------------- | | put(1, -1) | | | ---------------------- | | put(2, -2) | put(3, 2) | | ---------------------- | --- All operations above the horizontal line | ----- | happen before those below the line --- The following interleaving leads to the error: | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Thread 1 | Thread 2 | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | put(3, 2) | | | put(3,2) at ConcurrentHashMapTest.put(ConcurrentMapTest.kt:11) | | | putVal(3,2,false) at ConcurrentHashMap.put(ConcurrentHashMap.java:1006) | | | table.READ: Node[]@1 at ConcurrentHashMap.putVal(ConcurrentHashMap.java:1014) | | | tabAt(Node[]@1,0): Node@1 at ConcurrentHashMap.putVal(ConcurrentHashMap.java:1018) | | | MONITORENTER at ConcurrentHashMap.putVal(ConcurrentHashMap.java:1031) | | | tabAt(Node[]@1,0): Node@1 at ConcurrentHashMap.putVal(ConcurrentHashMap.java:1032) | | | next.READ: null at ConcurrentHashMap.putVal(ConcurrentHashMap.java:1046) | | | switch | | put(2, -2) | | | put(2,-2) at ConcurrentHashMapTest.put(ConcurrentMapTest.kt:11) | | | putVal(2,-2,false) at ConcurrentHashMap.put(ConcurrentHashMap.java:1006) | | | table.READ: Node[]@1 at ConcurrentHashMap.putVal(ConcurrentHashMap.java:1014) | | | tabAt(Node[]@1,0): Node@1 at ConcurrentHashMap.putVal(ConcurrentHashMap.java:1018) | | | MONITORENTER at ConcurrentHashMap.putVal(ConcurrentHashMap.java:1031) | | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

Now let's add a test for the non-blocking ConcurrentSkipListMap<K, V>, expecting the test to pass successfully:

class ConcurrentSkipListMapTest { private val map = ConcurrentSkipListMap<Int, Int>() @Operation fun put(key: Int, value: Int) = map.put(key, value) @Test fun modelCheckingTest() = ModelCheckingOptions() .checkObstructionFreedom() .check(this::class) }

At the moment, Lincheck supports only the obstruction-freedom progress guarantees. However, most real-life liveness bugs add unexpected blocking code, so the obstruction-freedom check will also help with lock-free and wait-free algorithms.

Next step

Learn how to specify the sequential specification of the testing algorithm explicitly, improving the Lincheck tests robustness.

Last modified: 11 July 2024