Skip to content

Commit f9e13f5

Browse files
committed
Renamed run to withContext
Fixes #134
1 parent d66ee72 commit f9e13f5

File tree

19 files changed

+53
-44
lines changed

19 files changed

+53
-44
lines changed

core/kotlinx-coroutines-core/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Top-level suspending functions:
4343
| ------------------- | ---------------
4444
| [delay] | Non-blocking sleep
4545
| [yield] | Yields thread in single-threaded dispatchers
46-
| [run] | Switches to a different context
46+
| [withContext] | Switches to a different context
4747
| [withTimeout] | Set execution time-limit with exception on timeout
4848
| [withTimeoutOrNull] | Set execution time-limit will null result on timeout
4949

@@ -102,7 +102,7 @@ Select expression to perform multiple suspending operations simultaneously until
102102
[CoroutineExceptionHandler]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-exception-handler/index.html
103103
[delay]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/delay.html
104104
[yield]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/yield.html
105-
[run]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/run.html
105+
[withContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/with-context.html
106106
[withTimeout]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/with-timeout.html
107107
[withTimeoutOrNull]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/with-timeout-or-null.html
108108
[Job.join]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/join.html

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Builders.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public fun launch(context: CoroutineContext, start: Boolean, block: suspend Coro
100100
* Other options can be specified via `start` parameter. See [CoroutineStart] for details.
101101
* A value of [CoroutineStart.LAZY] is not supported and produces [IllegalArgumentException].
102102
*/
103-
public suspend fun <T> run(
103+
public suspend fun <T> withContext(
104104
context: CoroutineContext,
105105
start: CoroutineStart = CoroutineStart.DEFAULT,
106106
block: suspend () -> T
@@ -131,11 +131,20 @@ public suspend fun <T> run(
131131
completion.getResult()
132132
}
133133

134+
/** @suppress **Deprecated**: Renamed to [withContext]. */
135+
@Deprecated(message = "Renamed to `withContext`", level=DeprecationLevel.WARNING,
136+
replaceWith = ReplaceWith("withContext(context, start, block)"))
137+
public suspend fun <T> run(
138+
context: CoroutineContext,
139+
start: CoroutineStart = CoroutineStart.DEFAULT,
140+
block: suspend () -> T
141+
): T =
142+
withContext(context, start, block)
143+
134144
/** @suppress **Deprecated** */
135-
@Suppress("DeprecatedCallableAddReplaceWith") // todo: the warning is incorrectly shown, see KT-17917
136145
@Deprecated(message = "It is here for binary compatibility only", level=DeprecationLevel.HIDDEN)
137146
public suspend fun <T> run(context: CoroutineContext, block: suspend () -> T): T =
138-
run(context, start = CoroutineStart.ATOMIC, block = block)
147+
withContext(context, start = CoroutineStart.ATOMIC, block = block)
139148

140149
/**
141150
* Runs new coroutine and **blocks** current thread _interruptibly_ until its completion.

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/NonCancellable.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import kotlinx.coroutines.experimental.selects.SelectClause0
2121
import kotlin.coroutines.experimental.AbstractCoroutineContextElement
2222

2323
/**
24-
* A non-cancelable job that is always [active][isActive]. It is designed to be used with [run] builder
25-
* to prevent cancellation of code blocks that need to run without cancellation.
24+
* A non-cancelable job that is always [active][isActive]. It is designed for [withContext] function
25+
* to prevent cancellation of code blocks that need to be executed without cancellation.
2626
*
2727
* Use it like this:
2828
* ```
29-
* run(NonCancellable) {
29+
* withContext(NonCancellable) {
3030
* // this code will not be cancelled
3131
* }
3232
* ```

core/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-05.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
2727
delay(500L)
2828
}
2929
} finally {
30-
run(NonCancellable) {
30+
withContext(NonCancellable) {
3131
println("I'm running finally")
3232
delay(1000L)
3333
println("And I've just delayed for 1 sec because I'm non-cancellable")

core/kotlinx-coroutines-core/src/test/kotlin/guide/example-context-04.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fun main(args: Array<String>) {
2626
newSingleThreadContext("Ctx2").use { ctx2 ->
2727
runBlocking(ctx1) {
2828
log("Started in ctx1")
29-
run(ctx2) {
29+
withContext(ctx2) {
3030
log("Working in ctx2")
3131
}
3232
log("Back to ctx1")

core/kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-04.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var counter = 0
4040

4141
fun main(args: Array<String>) = runBlocking<Unit> {
4242
massiveRun(CommonPool) { // run each coroutine in CommonPool
43-
run(counterContext) { // but confine each increment to the single-threaded context
43+
withContext(counterContext) { // but confine each increment to the single-threaded context
4444
counter++
4545
}
4646
}

core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/CoroutinesTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class CoroutinesTest : TestBase() {
285285
yield() // to test
286286
} finally {
287287
expect(5)
288-
run(NonCancellable) { yield() } // to test
288+
withContext(NonCancellable) { yield() } // to test
289289
expect(7)
290290
}
291291
expectUnreached() // will get cancelled, because parent crashes

core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/ExecutorsTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ExecutorsTest : TestBase() {
5858
val ctx2 = newSingleThreadContext("Ctx2")
5959
runBlocking(ctx1) {
6060
checkThreadName("Ctx1")
61-
run(ctx2) {
61+
withContext(ctx2) {
6262
checkThreadName("Ctx2")
6363
}
6464
checkThreadName("Ctx1")

core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/RunTest.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class RunTest : TestBase() {
3131
finish(5) // after main exits
3232
}
3333
expect(2)
34-
val result = run(coroutineContext) { // same context!
34+
val result = withContext(coroutineContext) { // same context!
3535
expect(3) // still here
3636
"OK"
3737
}
@@ -47,7 +47,7 @@ class RunTest : TestBase() {
4747
expect(4)
4848
}
4949
expect(2)
50-
val result = run(coroutineContext) { // same context!
50+
val result = withContext(coroutineContext) { // same context!
5151
expect(3) // still here
5252
yield() // now yields to launch!
5353
expect(5)
@@ -65,7 +65,7 @@ class RunTest : TestBase() {
6565
}
6666
expect(2)
6767
val job = Job()
68-
val result = run(coroutineContext + job) { // same context + new job
68+
val result = withContext(coroutineContext + job) { // same context + new job
6969
expect(3) // still here
7070
job.cancel() // cancel out job!
7171
try {
@@ -89,7 +89,7 @@ class RunTest : TestBase() {
8989
}
9090
expect(2)
9191
val job = Job()
92-
val result = run(coroutineContext + job) { // same context + new job
92+
val result = withContext(coroutineContext + job) { // same context + new job
9393
expect(3) // still here
9494
yield() // now yields to launch!
9595
expect(5)
@@ -109,7 +109,7 @@ class RunTest : TestBase() {
109109
@Test
110110
fun testCommonPoolNoSuspend() = runTest {
111111
expect(1)
112-
val result = run(CommonPool) {
112+
val result = withContext(CommonPool) {
113113
expect(2)
114114
"OK"
115115
}
@@ -120,7 +120,7 @@ class RunTest : TestBase() {
120120
@Test
121121
fun testCommonPoolWithSuspend() = runTest {
122122
expect(1)
123-
val result = run(CommonPool) {
123+
val result = withContext(CommonPool) {
124124
expect(2)
125125
delay(100)
126126
expect(3)
@@ -136,7 +136,7 @@ class RunTest : TestBase() {
136136
) {
137137
val job = Job()
138138
job.cancel() // cancel before it has a chance to run
139-
run(job + wrapperDispatcher(coroutineContext)) {
139+
withContext(job + wrapperDispatcher(coroutineContext)) {
140140
expectUnreached() // will get cancelled
141141
}
142142
}
@@ -148,7 +148,7 @@ class RunTest : TestBase() {
148148
expect(1)
149149
val job = Job()
150150
job.cancel() // try to cancel before it has a chance to run
151-
run(job + wrapperDispatcher(coroutineContext), CoroutineStart.ATOMIC) { // but start atomically
151+
withContext(job + wrapperDispatcher(coroutineContext), CoroutineStart.ATOMIC) { // but start atomically
152152
finish(2)
153153
yield() // but will cancel here
154154
expectUnreached()
@@ -162,7 +162,7 @@ class RunTest : TestBase() {
162162
expect(1)
163163
val job = Job()
164164
job.cancel() // try to cancel before it has a chance to run
165-
run(job + wrapperDispatcher(coroutineContext), CoroutineStart.UNDISPATCHED) { // but start atomically
165+
withContext(job + wrapperDispatcher(coroutineContext), CoroutineStart.UNDISPATCHED) { // but start atomically
166166
finish(2)
167167
yield() // but will cancel here
168168
expectUnreached()
@@ -176,7 +176,7 @@ class RunTest : TestBase() {
176176
job = launch(coroutineContext) {
177177
try {
178178
expect(3)
179-
run(wrapperDispatcher(coroutineContext)) {
179+
withContext(wrapperDispatcher(coroutineContext)) {
180180
expect(5)
181181
job!!.cancel() // cancel itself
182182
throw IOException() // but throw a different exception

core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/WithTimeoutOrNullTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class WithTimeoutOrNullTest : TestBase() {
178178
fun testOuterTimeoutFiredBeforeInner() = runBlocking<Unit> {
179179
val result = withTimeoutOrNull(100) {
180180
Thread.sleep(200) // wait enough for outer timeout to fire
181-
run(NonCancellable) { yield() } // give an event loop a chance to run and process that cancellation
181+
withContext(NonCancellable) { yield() } // give an event loop a chance to run and process that cancellation
182182
withTimeoutOrNull(100) {
183183
yield() // will cancel because of outer timeout
184184
expectUnreached()

0 commit comments

Comments
 (0)