Skip to content

Commit 43e3af7

Browse files
committed
Rename CoroutineContext.context to coroutineContext
1 parent 777cce1 commit 43e3af7

File tree

85 files changed

+355
-341
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+355
-341
lines changed

benchmarks/src/main/kotlin/benchmarks/PingPongActorBenchmark.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ open class PingPongActorBenchmark {
128128

129129
@Benchmark
130130
fun pingPongCoroutineMain() = runBlocking<Unit> {
131-
val pong = pongActorCoroutine(context)
132-
val ping = pingActorCoroutine(context, pong)
131+
val pong = pongActorCoroutine(coroutineContext)
132+
val ping = pingActorCoroutine(coroutineContext, pong)
133133
val me = Channel<Letter>()
134134
ping.send(Letter(Start(), me))
135135
me.receive()

coroutines-guide.md

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -749,16 +749,16 @@ to a specific thread, dispatch it to a thread pool, or let it run unconfined. Tr
749749
fun main(args: Array<String>) = runBlocking<Unit> {
750750
val jobs = arrayListOf<Job>()
751751
jobs += launch(Unconfined) { // not confined -- will work with main thread
752-
println(" 'Unconfined': I'm working in thread ${Thread.currentThread().name}")
752+
println(" 'Unconfined': I'm working in thread ${Thread.currentThread().name}")
753753
}
754-
jobs += launch(context) { // context of the parent, runBlocking coroutine
755-
println(" 'context': I'm working in thread ${Thread.currentThread().name}")
754+
jobs += launch(coroutineContext) { // context of the parent, runBlocking coroutine
755+
println("'coroutineContext': I'm working in thread ${Thread.currentThread().name}")
756756
}
757757
jobs += launch(CommonPool) { // will get dispatched to ForkJoinPool.commonPool (or equivalent)
758-
println(" 'CommonPool': I'm working in thread ${Thread.currentThread().name}")
758+
println(" 'CommonPool': I'm working in thread ${Thread.currentThread().name}")
759759
}
760760
jobs += launch(newSingleThreadContext("MyOwnThread")) { // will get its own new thread
761-
println(" 'newSTC': I'm working in thread ${Thread.currentThread().name}")
761+
println(" 'newSTC': I'm working in thread ${Thread.currentThread().name}")
762762
}
763763
jobs.forEach { it.join() }
764764
}
@@ -769,15 +769,16 @@ fun main(args: Array<String>) = runBlocking<Unit> {
769769
It produces the following output (maybe in different order):
770770

771771
```text
772-
'Unconfined': I'm working in thread main
773-
'CommonPool': I'm working in thread ForkJoinPool.commonPool-worker-1
774-
'newSTC': I'm working in thread MyOwnThread
775-
'context': I'm working in thread main
772+
'Unconfined': I'm working in thread main
773+
'CommonPool': I'm working in thread ForkJoinPool.commonPool-worker-1
774+
'newSTC': I'm working in thread MyOwnThread
775+
'coroutineContext': I'm working in thread main
776776
```
777777

778778
<!--- TEST LINES_START_UNORDERED -->
779779

780-
The difference between parent [context][CoroutineScope.context] and [Unconfined] context will be shown later.
780+
The difference between parent [coroutineContext][CoroutineScope.coroutineContext] and
781+
[Unconfined] context will be shown later.
781782

782783
### Unconfined vs confined dispatcher
783784

@@ -786,7 +787,7 @@ first suspension point. After suspension it resumes in the thread that is fully
786787
suspending function that was invoked. Unconfined dispatcher is appropriate when coroutine does not
787788
consume CPU time nor updates any shared data (like UI) that is confined to a specific thread.
788789

789-
On the other side, [context][CoroutineScope.context] property that is available inside the block of any coroutine
790+
On the other side, [coroutineContext][CoroutineScope.coroutineContext] property that is available inside the block of any coroutine
790791
via [CoroutineScope] interface, is a reference to a context of this particular coroutine.
791792
This way, a parent context can be inherited. The default context of [runBlocking], in particular,
792793
is confined to be invoker thread, so inheriting it has the effect of confining execution to
@@ -796,14 +797,14 @@ this thread with a predictable FIFO scheduling.
796797
fun main(args: Array<String>) = runBlocking<Unit> {
797798
val jobs = arrayListOf<Job>()
798799
jobs += launch(Unconfined) { // not confined -- will work with main thread
799-
println(" 'Unconfined': I'm working in thread ${Thread.currentThread().name}")
800+
println(" 'Unconfined': I'm working in thread ${Thread.currentThread().name}")
800801
delay(500)
801-
println(" 'Unconfined': After delay in thread ${Thread.currentThread().name}")
802+
println(" 'Unconfined': After delay in thread ${Thread.currentThread().name}")
802803
}
803-
jobs += launch(context) { // context of the parent, runBlocking coroutine
804-
println(" 'context': I'm working in thread ${Thread.currentThread().name}")
804+
jobs += launch(coroutineContext) { // context of the parent, runBlocking coroutine
805+
println("'coroutineContext': I'm working in thread ${Thread.currentThread().name}")
805806
delay(1000)
806-
println(" 'context': After delay in thread ${Thread.currentThread().name}")
807+
println("'coroutineContext': After delay in thread ${Thread.currentThread().name}")
807808
}
808809
jobs.forEach { it.join() }
809810
}
@@ -814,16 +815,17 @@ fun main(args: Array<String>) = runBlocking<Unit> {
814815
Produces the output:
815816

816817
```text
817-
'Unconfined': I'm working in thread main
818-
'context': I'm working in thread main
819-
'Unconfined': After delay in thread kotlinx.coroutines.DefaultExecutor
820-
'context': After delay in thread main
818+
'Unconfined': I'm working in thread main
819+
'coroutineContext': I'm working in thread main
820+
'Unconfined': After delay in thread kotlinx.coroutines.DefaultExecutor
821+
'coroutineContext': After delay in thread main
821822
```
822823

823824
<!--- TEST LINES_START -->
824825

825-
So, the coroutine that had inherited `context` of `runBlocking {...}` continues to execute in the `main` thread,
826-
while the unconfined one had resumed in the default executor thread that [delay] function is using.
826+
So, the coroutine that had inherited `coroutineContext` of `runBlocking {...}` continues to execute
827+
in the `main` thread, while the unconfined one had resumed in the default executor thread that [delay]
828+
function is using.
827829

828830
### Debugging coroutines and threads
829831

@@ -840,11 +842,11 @@ Run the following code with `-Dkotlinx.coroutines.debug` JVM option:
840842
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
841843

842844
fun main(args: Array<String>) = runBlocking<Unit> {
843-
val a = async(context) {
845+
val a = async(coroutineContext) {
844846
log("I'm computing a piece of the answer")
845847
6
846848
}
847-
val b = async(context) {
849+
val b = async(coroutineContext) {
848850
log("I'm computing another piece of the answer")
849851
7
850852
}
@@ -910,11 +912,11 @@ same coroutine as you can see in the output below:
910912
### Job in the context
911913

912914
The coroutine [Job] is part of its context. The coroutine can retrieve it from its own context
913-
using `context[Job]` expression:
915+
using `coroutineContext[Job]` expression:
914916

915917
```kotlin
916918
fun main(args: Array<String>) = runBlocking<Unit> {
917-
println("My job is ${context[Job]}")
919+
println("My job is ${coroutineContext[Job]}")
918920
}
919921
```
920922

@@ -928,11 +930,12 @@ My job is BlockingCoroutine{Active}@65ae6ba4
928930

929931
<!--- TEST lines.size == 1 && lines[0].startsWith("My job is BlockingCoroutine{Active}@") -->
930932

931-
So, [isActive][CoroutineScope.isActive] in [CoroutineScope] is just a convenient shortcut for `context[Job]!!.isActive`.
933+
So, [isActive][CoroutineScope.isActive] in [CoroutineScope] is just a convenient shortcut for
934+
`coroutineContext[Job]!!.isActive`.
932935

933936
### Children of a coroutine
934937

935-
When [context][CoroutineScope.context] of a coroutine is used to launch another coroutine,
938+
When [coroutineContext][CoroutineScope.coroutineContext] of a coroutine is used to launch another coroutine,
936939
the [Job] of the new coroutine becomes
937940
a _child_ of the parent coroutine's job. When the parent coroutine is cancelled, all its children
938941
are recursively cancelled, too.
@@ -948,7 +951,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
948951
println("job1: I am not affected by cancellation of the request")
949952
}
950953
// and the other inherits the parent context
951-
val job2 = launch(context) {
954+
val job2 = launch(coroutineContext) {
952955
println("job2: I am a child of the request coroutine")
953956
delay(1000)
954957
println("job2: I will not execute this line if my parent request is cancelled")
@@ -986,9 +989,9 @@ its dispatcher replaced:
986989
```kotlin
987990
fun main(args: Array<String>) = runBlocking<Unit> {
988991
// start a coroutine to process some kind of incoming request
989-
val request = launch(context) { // use the context of `runBlocking`
992+
val request = launch(coroutineContext) { // use the context of `runBlocking`
990993
// spawns CPU-intensive child job in CommonPool !!!
991-
val job = launch(context + CommonPool) {
994+
val job = launch(coroutineContext + CommonPool) {
992995
println("job: I am a child of the request coroutine, but with a different dispatcher")
993996
delay(1000)
994997
println("job: I will not execute this line if my parent request is cancelled")
@@ -1075,7 +1078,7 @@ fun main(args: Array<String>) = runBlocking<Unit> {
10751078
// now launch ten coroutines for a demo, each working for a different time
10761079
val coroutines = List(10) { i ->
10771080
// they are all children of our job object
1078-
launch(context + job) { // we use the context of main runBlocking thread, but with our own job object
1081+
launch(coroutineContext + job) { // we use the context of main runBlocking thread, but with our own job object
10791082
delay(i * 200L) // variable delay 0ms, 200ms, 400ms, ... etc
10801083
println("Coroutine $i is done")
10811084
}
@@ -1305,11 +1308,11 @@ running the whole pipeline in the context of the main thread:
13051308

13061309
```kotlin
13071310
fun main(args: Array<String>) = runBlocking<Unit> {
1308-
var cur = numbersFrom(context, 2)
1311+
var cur = numbersFrom(coroutineContext, 2)
13091312
for (i in 1..10) {
13101313
val prime = cur.receive()
13111314
println(prime)
1312-
cur = filter(context, cur, prime)
1315+
cur = filter(coroutineContext, cur, prime)
13131316
}
13141317
}
13151318
```
@@ -1426,8 +1429,8 @@ Now, let us see what happens if we launch a couple of coroutines sending strings
14261429
```kotlin
14271430
fun main(args: Array<String>) = runBlocking<Unit> {
14281431
val channel = Channel<String>()
1429-
launch(context) { sendString(channel, "foo", 200L) }
1430-
launch(context) { sendString(channel, "BAR!", 500L) }
1432+
launch(coroutineContext) { sendString(channel, "foo", 200L) }
1433+
launch(coroutineContext) { sendString(channel, "BAR!", 500L) }
14311434
repeat(6) { // receive first six
14321435
println(channel.receive())
14331436
}
@@ -1464,7 +1467,7 @@ Take a look at the behavior of the following code:
14641467
```kotlin
14651468
fun main(args: Array<String>) = runBlocking<Unit> {
14661469
val channel = Channel<Int>(4) // create buffered channel
1467-
launch(context) { // launch sender coroutine
1470+
launch(coroutineContext) { // launch sender coroutine
14681471
repeat(10) {
14691472
println("Sending $it") // print before sending each element
14701473
channel.send(it) // will suspend when buffer is full
@@ -1504,8 +1507,8 @@ data class Ball(var hits: Int)
15041507

15051508
fun main(args: Array<String>) = runBlocking<Unit> {
15061509
val table = Channel<Ball>() // a shared table
1507-
launch(context) { player("ping", table) }
1508-
launch(context) { player("pong", table) }
1510+
launch(coroutineContext) { player("ping", table) }
1511+
launch(coroutineContext) { player("pong", table) }
15091512
table.send(Ball(0)) // serve the ball
15101513
delay(1000) // delay 1 second
15111514
table.receive() // game over, grab the ball
@@ -1893,8 +1896,8 @@ Let us run it all seven times:
18931896

18941897
```kotlin
18951898
fun main(args: Array<String>) = runBlocking<Unit> {
1896-
val fizz = fizz(context)
1897-
val buzz = buzz(context)
1899+
val fizz = fizz(coroutineContext)
1900+
val buzz = buzz(coroutineContext)
18981901
repeat(7) {
18991902
selectFizzBuzz(fizz, buzz)
19001903
}
@@ -1948,10 +1951,10 @@ channel `b` that produces "World" four times:
19481951
```kotlin
19491952
fun main(args: Array<String>) = runBlocking<Unit> {
19501953
// we are using the context of the main thread in this example for predictability ...
1951-
val a = produce<String>(context) {
1954+
val a = produce<String>(coroutineContext) {
19521955
repeat(4) { send("Hello $it") }
19531956
}
1954-
val b = produce<String>(context) {
1957+
val b = produce<String>(coroutineContext) {
19551958
repeat(4) { send("World $it") }
19561959
}
19571960
repeat(8) { // print first eight results
@@ -2012,7 +2015,7 @@ Consumer is going to be quite slow, taking 250 ms to process each number:
20122015
```kotlin
20132016
fun main(args: Array<String>) = runBlocking<Unit> {
20142017
val side = Channel<Int>() // allocate side channel
2015-
launch(context) { // this is a very fast consumer for the side channel
2018+
launch(coroutineContext) { // this is a very fast consumer for the side channel
20162019
side.consumeEach { println("Side channel has $it") }
20172020
}
20182021
produceNumbers(side).consumeEach {
@@ -2145,7 +2148,7 @@ data to it:
21452148
```kotlin
21462149
fun main(args: Array<String>) = runBlocking<Unit> {
21472150
val chan = Channel<Deferred<String>>() // the channel for test
2148-
launch(context) { // launch printing coroutine
2151+
launch(coroutineContext) { // launch printing coroutine
21492152
for (s in switchMapDeferreds(chan))
21502153
println(s) // print each received string
21512154
}
@@ -2202,7 +2205,7 @@ Channel was closed
22022205
[Job.start]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/start.html
22032206
[CommonPool]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-common-pool/index.html
22042207
[CoroutineDispatcher]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-dispatcher/index.html
2205-
[CoroutineScope.context]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-scope/context.html
2208+
[CoroutineScope.coroutineContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-scope/coroutine-context.html
22062209
[Unconfined]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-unconfined/index.html
22072210
[newCoroutineContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/new-coroutine-context.html
22082211
[CoroutineName]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-name/index.html

integration/kotlinx-coroutines-guava/src/main/kotlin/kotlinx/coroutines/experimental/guava/ListenableFuture.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public fun <T> future(
6464
private class ListenableFutureCoroutine<T>(
6565
override val context: CoroutineContext
6666
) : AbstractFuture<T>(), Continuation<T>, CoroutineScope {
67+
override val coroutineContext: CoroutineContext get() = context
6768
override val isActive: Boolean get() = context[Job]!!.isActive
6869
override fun resume(value: T) { set(value) }
6970
override fun resumeWithException(exception: Throwable) { setException(exception) }

integration/kotlinx-coroutines-guava/src/test/kotlin/kotlinx/coroutines/experimental/guava/ListenableFutureTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class ListenableFutureTest : TestBase() {
119119
@Test
120120
fun testCompletedDeferredAsListenableFuture() = runBlocking {
121121
expect(1)
122-
val deferred = async(context, CoroutineStart.UNDISPATCHED) {
122+
val deferred = async(coroutineContext, CoroutineStart.UNDISPATCHED) {
123123
expect(2) // completed right away
124124
"OK"
125125
}
@@ -132,7 +132,7 @@ class ListenableFutureTest : TestBase() {
132132
@Test
133133
fun testWaitForDeferredAsListenableFuture() = runBlocking {
134134
expect(1)
135-
val deferred = async(context) {
135+
val deferred = async(coroutineContext) {
136136
expect(3) // will complete later
137137
"OK"
138138
}
@@ -146,7 +146,7 @@ class ListenableFutureTest : TestBase() {
146146
fun testCancellableAwait() = runBlocking {
147147
expect(1)
148148
val toAwait = SettableFuture.create<String>()
149-
val job = launch(context, CoroutineStart.UNDISPATCHED) {
149+
val job = launch(coroutineContext, CoroutineStart.UNDISPATCHED) {
150150
expect(2)
151151
try {
152152
toAwait.await() // suspends

integration/kotlinx-coroutines-jdk8/src/main/kotlin/kotlinx/coroutines/experimental/future/Future.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public fun <T> future(
6666
private class CompletableFutureCoroutine<T>(
6767
override val context: CoroutineContext
6868
) : CompletableFuture<T>(), Continuation<T>, CoroutineScope {
69+
override val coroutineContext: CoroutineContext get() = context
6970
override val isActive: Boolean get() = context[Job]!!.isActive
7071
override fun resume(value: T) { complete(value) }
7172
override fun resumeWithException(exception: Throwable) { completeExceptionally(exception) }

integration/kotlinx-coroutines-jdk8/src/test/kotlin/kotlinx/coroutines/experimental/future/FutureTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class FutureTest : TestBase() {
168168
@Test
169169
fun testCompletedDeferredAsCompletableFuture() = runBlocking {
170170
expect(1)
171-
val deferred = async(context, CoroutineStart.UNDISPATCHED) {
171+
val deferred = async(coroutineContext, CoroutineStart.UNDISPATCHED) {
172172
expect(2) // completed right away
173173
"OK"
174174
}
@@ -181,7 +181,7 @@ class FutureTest : TestBase() {
181181
@Test
182182
fun testWaitForDeferredAsCompletableFuture() = runBlocking {
183183
expect(1)
184-
val deferred = async(context) {
184+
val deferred = async(coroutineContext) {
185185
expect(3) // will complete later
186186
"OK"
187187
}
@@ -195,7 +195,7 @@ class FutureTest : TestBase() {
195195
fun testCancellableAwaitFuture() = runBlocking {
196196
expect(1)
197197
val toAwait = CompletableFuture<String>()
198-
val job = launch(context, CoroutineStart.UNDISPATCHED) {
198+
val job = launch(coroutineContext, CoroutineStart.UNDISPATCHED) {
199199
expect(2)
200200
try {
201201
toAwait.await() // suspends
@@ -217,7 +217,7 @@ class FutureTest : TestBase() {
217217
expect(1)
218218
val completable = CompletableFuture<String>()
219219
val toAwait: CompletionStage<String> = completable
220-
val job = launch(context, CoroutineStart.UNDISPATCHED) {
220+
val job = launch(coroutineContext, CoroutineStart.UNDISPATCHED) {
221221
expect(2)
222222
assertThat(toAwait.await(), IsEqual("OK")) // suspends
223223
expect(5)

integration/kotlinx-coroutines-nio/src/test/kotlin/examples/echo-example.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fun main(args: Array<String>) = runBlocking {
5050
}
5151
log("Accepted client connection from $address")
5252
// just start a new coroutine for each client connection
53-
launch(context) {
53+
launch(coroutineContext) {
5454
try {
5555
handleClient(client)
5656
log("Client connection from $address has terminated normally")

integration/kotlinx-coroutines-nio/src/test/kotlin/kotlinx/coroutines/experimental/nio/AsyncIOTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class AsyncIOTest {
8383

8484
val serverPort = (serverChannel.localAddress as InetSocketAddress).port
8585

86-
val c1 = launch(context) {
86+
val c1 = launch(coroutineContext) {
8787
val client = serverChannel.aAccept()
8888
val buffer = ByteBuffer.allocate(2)
8989
client.aRead(buffer)
@@ -94,7 +94,7 @@ class AsyncIOTest {
9494
client.close()
9595
}
9696

97-
val c2 = launch(context) {
97+
val c2 = launch(coroutineContext) {
9898
val connection =
9999
AsynchronousSocketChannel.open()
100100
// async calls

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public abstract class AbstractCoroutine<in T>(
3333
) : JobSupport(active), Continuation<T>, CoroutineScope {
3434
@Suppress("LeakingThis")
3535
public final override val context: CoroutineContext = parentContext + this
36+
public final override val coroutineContext: CoroutineContext get() = context
3637

3738
final override val hasCancellingState: Boolean get() = true
3839

0 commit comments

Comments
 (0)