diff --git a/backend.native/tests/runtime/basic/worker_random.kt b/backend.native/tests/runtime/basic/worker_random.kt index 2aaf1515066..f3350a2df8d 100644 --- a/backend.native/tests/runtime/basic/worker_random.kt +++ b/backend.native/tests/runtime/basic/worker_random.kt @@ -29,7 +29,7 @@ fun testRandomWorkers() { // Now collect all results into current attempt's list val futureSet = futures.toSet() for (i in 0 until futureSet.size) { - val ready = futureSet.waitForMultipleFutures(10000) + val ready = waitForMultipleFutures(futureSet, 10000) ready.forEach { results[attempt].addAll(it.result) } } } diff --git a/backend.native/tests/runtime/workers/worker11.kt b/backend.native/tests/runtime/workers/worker11.kt index 91527808da3..72da9dffeb8 100644 --- a/backend.native/tests/runtime/workers/worker11.kt +++ b/backend.native/tests/runtime/workers/worker11.kt @@ -31,7 +31,7 @@ fun initJobs(count: Int) = Array(count) { i -> Job(i, i * 2, i)} val futureSet = futures.toSet() var consumed = 0 while (consumed < futureSet.size) { - val ready = futureSet.waitForMultipleFutures(10000) + val ready = waitForMultipleFutures(futureSet, 10000) ready.forEach { it.consume { job -> assertEquals(job.index * 3, job.counter) diff --git a/backend.native/tests/runtime/workers/worker2.kt b/backend.native/tests/runtime/workers/worker2.kt index c831b103dfc..2cc47f2afb6 100644 --- a/backend.native/tests/runtime/workers/worker2.kt +++ b/backend.native/tests/runtime/workers/worker2.kt @@ -29,7 +29,7 @@ data class WorkerResult(val intResult: Int, val stringResult: String) val futureSet = futures.toSet() var consumed = 0 while (consumed < futureSet.size) { - val ready = futureSet.waitForMultipleFutures(10000) + val ready = waitForMultipleFutures(futureSet, 10000) ready.forEach { it.consume { result -> if (result.stringResult != "attempt $attempt result") throw Error("Unexpected $result") diff --git a/runtime/src/main/kotlin/kotlin/native/concurrent/Future.kt b/runtime/src/main/kotlin/kotlin/native/concurrent/Future.kt index c994cfd590a..2208c3995b3 100644 --- a/runtime/src/main/kotlin/kotlin/native/concurrent/Future.kt +++ b/runtime/src/main/kotlin/kotlin/native/concurrent/Future.kt @@ -63,28 +63,33 @@ public class Future internal constructor(val id: Int) { override public fun toString(): String = "future $id" } + +@Deprecated("Use 'waitForMultipleFutures' top-level function instead", ReplaceWith("waitForMultipleFutures(this, millis)"), DeprecationLevel.ERROR) +public fun Collection>.waitForMultipleFutures(millis: Int): Set> = waitForMultipleFutures(this, millis) + + /** * Wait for availability of futures in the collection. Returns set with all futures which have - * value available for the consumption, i.e. [FutureState.COMPUTED] + * value available for the consumption, i.e. [FutureState.COMPUTED]. * - * @param millis the amount of time to wait for the computed future + * @param timeoutMillis the amount of time in milliseconds to wait for the computed future */ -public fun Collection>.waitForMultipleFutures(millis: Int): Set> { +public fun waitForMultipleFutures(futures: Collection>, timeoutMillis: Int): Set> { val result = mutableSetOf>() while (true) { val versionToken = versionToken() - for (future in this) { + for (future in futures) { if (future.state == FutureState.COMPUTED) { result += future } } if (result.isNotEmpty()) return result - if (waitForAnyFuture(versionToken, millis)) break + if (waitForAnyFuture(versionToken, timeoutMillis)) break } - for (future in this) { + for (future in futures) { if (future.state == FutureState.COMPUTED) { result += future } diff --git a/samples/workers/src/workersMain/kotlin/Workers.kt b/samples/workers/src/workersMain/kotlin/Workers.kt index e1aea903558..580c6155dfc 100644 --- a/samples/workers/src/workersMain/kotlin/Workers.kt +++ b/samples/workers/src/workersMain/kotlin/Workers.kt @@ -29,7 +29,7 @@ fun main() { val futureSet = futures.toSet() var consumed = 0 while (consumed < futureSet.size) { - val ready = futureSet.waitForMultipleFutures(10000) + val ready = waitForMultipleFutures(futureSet, 10000) ready.forEach { it.consume { result -> if (result.stringResult != "attempt $attempt result") throw Error("Unexpected $result")