Tweaks to atomics implementation. (#1703)
This commit is contained in:
@@ -673,7 +673,7 @@ task freeze2(type: RunKonanTest) {
|
||||
|
||||
task atomic0(type: RunKonanTest) {
|
||||
disabled = (project.testTarget == 'wasm32') // Workers need pthreads.
|
||||
goldValue = "35\n" + "20\n"
|
||||
goldValue = "35\n" + "20\n" + "OK\n"
|
||||
source = "runtime/workers/atomic0.kt"
|
||||
}
|
||||
|
||||
|
||||
@@ -18,13 +18,19 @@ fun test1(workers: Array<Worker>) {
|
||||
}
|
||||
|
||||
fun test2(workers: Array<Worker>) {
|
||||
val atomic = AtomicInt(0)
|
||||
val atomic = AtomicInt(1)
|
||||
val counter = AtomicInt(0)
|
||||
val futures = Array(workers.size, { workerIndex ->
|
||||
workers[workerIndex].schedule(TransferMode.CHECKED, { Triple(atomic, workerIndex, counter) }) {
|
||||
(place, index, result) ->
|
||||
while (place.compareAndSwap(index, index + 1) != index) {}
|
||||
result.increment() == index + 1
|
||||
// Here we simulate mutex using [place] location to store tag of the current worker.
|
||||
// When it is negative - worker executes exclusively.
|
||||
val tag = index + 1
|
||||
while (place.compareAndSwap(tag, -tag) != tag) {}
|
||||
val ok1 = result.increment() == index + 1
|
||||
// Now, let the next worker run.
|
||||
val ok2 = place.compareAndSwap(-tag, tag + 1) == -tag
|
||||
ok1 && ok2
|
||||
}
|
||||
})
|
||||
futures.forEach {
|
||||
@@ -80,4 +86,9 @@ fun test4() {
|
||||
test2(workers)
|
||||
test3(workers)
|
||||
test4()
|
||||
|
||||
workers.forEach {
|
||||
it.requestTermination().consume { _ -> }
|
||||
}
|
||||
println("OK")
|
||||
}
|
||||
Reference in New Issue
Block a user