1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.example.tracing.demo.experiments
17 
18 import com.android.app.tracing.coroutines.nameCoroutine
19 import com.android.app.tracing.coroutines.traceCoroutine
20 import com.android.app.tracing.traceSection
21 import com.example.tracing.demo.FixedThreadA
22 import com.example.tracing.demo.FixedThreadB
23 import com.example.tracing.demo.FixedThreadC
24 import com.example.tracing.demo.Unconfined
25 import javax.inject.Inject
26 import javax.inject.Singleton
27 import kotlinx.coroutines.CoroutineDispatcher
28 import kotlinx.coroutines.CoroutineStart.LAZY
29 import kotlinx.coroutines.async
30 import kotlinx.coroutines.coroutineScope
31 import kotlinx.coroutines.launch
32 
33 @Singleton
34 class CombineDeferred
35 @Inject
36 constructor(
37     @FixedThreadA private var dispatcherA: CoroutineDispatcher,
38     @FixedThreadB private var dispatcherB: CoroutineDispatcher,
39     @FixedThreadC private val dispatcherC: CoroutineDispatcher,
40     @Unconfined private var unconfinedContext: CoroutineDispatcher,
41 ) : Experiment {
42     override val description: String = "async{} then start()"
43 
<lambda>null44     override suspend fun start(): Unit = coroutineScope {
45         // deferred10 -> deferred20 -> deferred30
46         val deferred30 =
47             async(start = LAZY, context = dispatcherB) {
48                 traceCoroutine("async#30") { forceSuspend("deferred30", 250) }
49             }
50         val deferred20 =
51             async(start = LAZY, context = unconfinedContext) {
52                 traceCoroutine("async#20") { forceSuspend("deferred20", 250) }
53                 traceSection("start30") { deferred30.start() }
54             }
55         val deferred10 =
56             async(start = LAZY, context = dispatcherC) {
57                 traceCoroutine("async#10") { forceSuspend("deferred10", 250) }
58                 traceSection("start20") { deferred20.start() }
59             }
60 
61         // deferredA -> deferredB -> deferredC
62         val deferredC =
63             async(start = LAZY, context = dispatcherB) {
64                 traceCoroutine("async#C") { forceSuspend("deferredC", 250) }
65             }
66         val deferredB =
67             async(start = LAZY, context = unconfinedContext) {
68                 traceCoroutine("async#B") { forceSuspend("deferredB", 250) }
69                 traceSection("startC") { deferredC.start() }
70             }
71         val deferredA =
72             async(start = LAZY, context = dispatcherC) {
73                 traceCoroutine("async#A") { forceSuspend("deferredA", 250) }
74                 traceSection("startB") { deferredB.start() }
75             }
76 
77         // no dispatcher specified, so will inherit dispatcher from whoever called
78         // run(), meaning the main thread
79         val deferredE =
80             async(nameCoroutine("overridden-scope-name-for-deferredE")) {
81                 traceCoroutine("async#E") { forceSuspend("deferredE", 250) }
82             }
83 
84         launch(dispatcherA) {
85             traceSection("start10") { deferred10.start() }
86             traceSection("startA") { deferredA.start() }
87             traceSection("startE") { deferredE.start() }
88         }
89     }
90 }
91