-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stack with elimination implementation
- Loading branch information
Showing
6 changed files
with
108 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,8 @@ | ||
package stack | ||
|
||
import stack.simple.ConcurrentTreiberStack | ||
import javax.print.attribute.standard.JobName | ||
import kotlin.concurrent.thread | ||
import kotlin.coroutines.coroutineContext | ||
import stack.elimination.ConcurrentStackWithElimination | ||
|
||
fun main() { | ||
val st = ConcurrentTreiberStack<Int>() | ||
val st = ConcurrentStackWithElimination<Int>() | ||
st.push(1) | ||
st.pop() | ||
|
||
val threads = List(10) { | ||
Thread { | ||
println(Thread.currentThread().id) | ||
} | ||
} | ||
threads.forEach { it.start() } | ||
|
||
threads.forEach { it.join() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
work1/src/main/kotlin/stack/elimination/EliminationArray.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package stack.elimination | ||
|
||
import kotlin.random.Random | ||
|
||
class EliminationArray<T>( | ||
private val capacity: Int = 100, | ||
private val durationNanos: Long = 500_000_000, | ||
randomSeed: Long = System.nanoTime(), | ||
) { | ||
private val random = Random(randomSeed) | ||
private val exchanger = Array<LockFreeExchanger<T>>(capacity) { LockFreeExchanger() } | ||
fun visit(value: T?): T? { | ||
val slot = random.nextInt(capacity) | ||
return exchanger[slot].exchange(value, durationNanos) | ||
} | ||
} |
33 changes: 26 additions & 7 deletions
33
work1/src/main/kotlin/stack/elimination/LockFreeExchanger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package stack | ||
|
||
import org.jetbrains.kotlinx.lincheck.annotations.Operation | ||
import org.jetbrains.kotlinx.lincheck.check | ||
import org.jetbrains.kotlinx.lincheck.strategy.managed.modelchecking.ModelCheckingOptions | ||
import org.jetbrains.kotlinx.lincheck.strategy.stress.StressOptions | ||
import stack.common.ConcurrentStack | ||
import stack.elimination.ConcurrentStackWithElimination | ||
import stack.simple.ConcurrentTreiberStack | ||
import kotlin.test.Test | ||
|
||
//abstract class ConcurrentStackTests(private val stack: ConcurrentStack<Int>) { | ||
// @Operation | ||
// fun push(value: Int) = stack.push(value) | ||
// | ||
// @Operation | ||
// fun pop(): Int? = stack.pop() | ||
// | ||
// @Operation | ||
// fun top(): Int? = stack.top() | ||
// | ||
// @Test | ||
// fun stressTest() = StressOptions() | ||
// .sequentialSpecification(SequentialStack::class.java) | ||
// .check(this::class) | ||
// | ||
// @Test | ||
// fun modelCheckingTest() = ModelCheckingOptions() | ||
// .sequentialSpecification(SequentialStack::class.java) | ||
// .checkObstructionFreedom() | ||
// .check(this::class) | ||
// | ||
// @Test | ||
// fun fourThreadsStressTest() = StressOptions() | ||
// .sequentialSpecification(SequentialStack::class.java) | ||
// .threads(4) | ||
// .iterations(5) | ||
// .invocationsPerIteration(100) | ||
// .check(this::class) | ||
// | ||
// @Test | ||
// fun fourThreadsModelCheckingTest() = ModelCheckingOptions() | ||
// .sequentialSpecification(SequentialStack::class.java) | ||
// .checkObstructionFreedom() | ||
// .threads(4) | ||
// .iterations(5) | ||
// .invocationsPerIteration(100) | ||
// .check(this::class) | ||
//} | ||
|
||
//class ConcurrentTreiberStackTests : ConcurrentStackTests(ConcurrentTreiberStack()) | ||
|
||
//class ConcurrentStackWithEliminationTests : ConcurrentStackTests(ConcurrentStackWithElimination()) |