This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
406 additions
and
28 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
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 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 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 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,83 @@ | ||
package runtime.workers.atomic0 | ||
|
||
import kotlin.test.* | ||
|
||
import konan.worker.* | ||
|
||
fun test1(workers: Array<Worker>) { | ||
val atomic = AtomicInt(15) | ||
val futures = Array(workers.size, { workerIndex -> | ||
workers[workerIndex].schedule(TransferMode.CHECKED, { atomic }) { | ||
input -> input.increment() | ||
} | ||
}) | ||
futures.forEach { | ||
it.result() | ||
} | ||
println(atomic.get()) | ||
} | ||
|
||
fun test2(workers: Array<Worker>) { | ||
val atomic = AtomicInt(0) | ||
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 | ||
} | ||
}) | ||
futures.forEach { | ||
assertEquals(it.result(), true) | ||
} | ||
println(counter.get()) | ||
} | ||
|
||
data class Data(val value: Int) | ||
|
||
fun test3(workers: Array<Worker>) { | ||
val common = AtomicReference<Data>() | ||
val futures = Array(workers.size, { workerIndex -> | ||
workers[workerIndex].schedule(TransferMode.CHECKED, { Pair(common, workerIndex) }) { | ||
(place, index) -> | ||
val mine = Data(index).freeze() | ||
// Try to publish our own data, until successful, in a tight loop. | ||
while (place.compareAndSwap(null, mine) != null) {} | ||
} | ||
}) | ||
val seen = mutableSetOf<Data>() | ||
for (i in 0 until workers.size) { | ||
do { | ||
val current = common.get() | ||
if (current != null && !seen.contains(current)) { | ||
seen += current | ||
// Let others publish. | ||
assertEquals(common.compareAndSwap(current, null), current) | ||
break | ||
} | ||
} while (true) | ||
} | ||
futures.forEach { | ||
it.result() | ||
} | ||
assertEquals(seen.size, workers.size) | ||
} | ||
|
||
fun test4() { | ||
assertFailsWith<InvalidMutabilityException> { | ||
AtomicReference(Data(1)) | ||
} | ||
assertFailsWith<InvalidMutabilityException> { | ||
AtomicReference<Data>().compareAndSwap(null, Data(2)) | ||
} | ||
} | ||
|
||
@Test fun runTest() { | ||
val COUNT = 20 | ||
val workers = Array(COUNT, { _ -> startWorker()}) | ||
|
||
test1(workers) | ||
test2(workers) | ||
test3(workers) | ||
test4() | ||
} |
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,70 @@ | ||
/* | ||
* Copyright 2010-2018 JetBrains s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "Atomic.h" | ||
#include "Common.h" | ||
#include "Types.h" | ||
|
||
namespace { | ||
|
||
template <typename T> T addAndGetImpl(KRef thiz, T delta) { | ||
volatile T* location = reinterpret_cast<volatile T*>(thiz + 1); | ||
return atomicAdd(location, delta); | ||
} | ||
|
||
template <typename T> T compareAndSwapImpl(KRef thiz, T expectedValue, T newValue) { | ||
volatile T* location = reinterpret_cast<volatile T*>(thiz + 1); | ||
return compareAndSwap(location, expectedValue, newValue); | ||
} | ||
|
||
} // namespace | ||
|
||
extern "C" { | ||
|
||
RUNTIME_NORETURN void ThrowInvalidMutabilityException(); | ||
|
||
KInt Kotlin_AtomicInt_addAndGet(KRef thiz, KInt delta) { | ||
return addAndGetImpl(thiz, delta); | ||
} | ||
|
||
KInt Kotlin_AtomicInt_compareAndSwap(KRef thiz, KInt expectedValue, KInt newValue) { | ||
return compareAndSwapImpl(thiz, expectedValue, newValue); | ||
} | ||
|
||
KLong Kotlin_AtomicLong_addAndGet(KRef thiz, KLong delta) { | ||
return addAndGetImpl(thiz, delta); | ||
} | ||
|
||
KLong Kotlin_AtomicLong_compareAndSwap(KRef thiz, KLong expectedValue, KLong newValue) { | ||
return compareAndSwapImpl(thiz, expectedValue, newValue); | ||
} | ||
|
||
KNativePtr Kotlin_AtomicNativePtr_compareAndSwap(KRef thiz, KNativePtr expectedValue, KNativePtr newValue) { | ||
return compareAndSwapImpl(thiz, expectedValue, newValue); | ||
} | ||
|
||
void Kotlin_AtomicReference_checkIfFrozen(KRef value) { | ||
if (value != nullptr && !value->container()->permanentOrFrozen()) { | ||
ThrowInvalidMutabilityException(); | ||
} | ||
} | ||
|
||
KRef Kotlin_AtomicReference_compareAndSwap(KRef thiz, KRef expectedValue, KRef newValue) { | ||
Kotlin_AtomicReference_checkIfFrozen(newValue); | ||
return compareAndSwapImpl(thiz, expectedValue, newValue); | ||
} | ||
|
||
} // extern "C" |
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 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 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 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 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 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
Oops, something went wrong.