Concurrent API update. (#1949)
This commit is contained in:
@@ -9,15 +9,13 @@ and connected to other worker. This relies on fact that memory management
|
||||
engine can ensure, that one worker doesn't keep references to certain object and
|
||||
whatever it refers to, and so the object could be safely transferred to another worker.
|
||||
|
||||
Workers do not share any state (i.e. globals and Kotlin static objects have different
|
||||
values in different workers), but share executable code of the program and some
|
||||
immutable data, such as immutable binary blobs. But Kotlin objects can be transferred
|
||||
Workers do not share mutable state, but share executable code of the program and some
|
||||
immutable data, such as immutable blobs. But Kotlin objects can be transferred
|
||||
between workers, as long, as they do not refer to objects, having external references.
|
||||
|
||||
The transfer is implemented with the function `schedule()` having the following signature
|
||||
The transfer is implemented with the function `execute()` having the following signature
|
||||
|
||||
fun <T1, T2>
|
||||
schedule(mode: TransferMode,
|
||||
fun <T1, T2> execute(mode: TransferMode,
|
||||
producer: () -> T1,
|
||||
@VolatileLambda job: (T1) -> T2): Future<T2>
|
||||
|
||||
|
||||
@@ -5,10 +5,10 @@ data class WorkerResult(val intResult: Int, val stringResult: String)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val COUNT = 5
|
||||
val workers = Array(COUNT, { _ -> startWorker()})
|
||||
val workers = Array(COUNT, { _ -> Worker.start()})
|
||||
|
||||
for (attempt in 1 .. 3) {
|
||||
val futures = Array(workers.size, { workerIndex -> workers[workerIndex].schedule(TransferMode.CHECKED, {
|
||||
val futures = Array(workers.size, { workerIndex -> workers[workerIndex].execute(TransferMode.SAFE, {
|
||||
WorkerArgument(workerIndex, "attempt $attempt") }) { input ->
|
||||
var sum = 0
|
||||
for (i in 0..input.intParam * 1000) {
|
||||
@@ -24,12 +24,13 @@ fun main(args: Array<String>) {
|
||||
ready.forEach {
|
||||
it.consume { result ->
|
||||
if (result.stringResult != "attempt $attempt result") throw Error("Unexpected $result")
|
||||
consumed++ }
|
||||
consumed++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
workers.forEach {
|
||||
it.requestTermination().consume { _ -> }
|
||||
it.requestTermination().result
|
||||
}
|
||||
println("OK")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user