• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.asyncTraced
19 import com.android.app.tracing.coroutines.traceCoroutine
20 import com.android.app.tracing.traceSection
21 import com.example.tracing.demo.FixedThread1
22 import com.example.tracing.demo.FixedThread2
23 import com.example.tracing.demo.FixedThread3
24 import javax.inject.Inject
25 import javax.inject.Singleton
26 import kotlinx.coroutines.CoroutineDispatcher
27 import kotlinx.coroutines.CoroutineStart.LAZY
28 import kotlinx.coroutines.Dispatchers
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     @FixedThread1 private var dispatcher1: CoroutineDispatcher,
38     @FixedThread2 private var dispatcher2: CoroutineDispatcher,
39     @FixedThread3 private val dispatcher3: CoroutineDispatcher,
40 ) : Experiment() {
41     override val description: String = "async{} then start()"
42 
<lambda>null43     override suspend fun runExperiment(): Unit = coroutineScope {
44         // deferred10 -> deferred20 -> deferred30
45         val deferred30 =
46             async(start = LAZY, context = dispatcher2) {
47                 traceCoroutine("async#30") { forceSuspend("deferred30", 25) }
48             }
49         val deferred20 =
50             async(start = LAZY, context = Dispatchers.Unconfined) {
51                 traceCoroutine("async#20") { forceSuspend("deferred20", 25) }
52                 traceSection("start30") { deferred30.start() }
53             }
54         val deferred10 =
55             async(start = LAZY, context = dispatcher3) {
56                 traceCoroutine("async#10") { forceSuspend("deferred10", 25) }
57                 traceSection("start20") { deferred20.start() }
58             }
59 
60         // deferredA -> deferredB -> deferredC
61         val deferredC =
62             async(start = LAZY, context = dispatcher2) {
63                 traceCoroutine("async#C") { forceSuspend("deferredC", 25) }
64             }
65         val deferredB =
66             async(start = LAZY, context = Dispatchers.Unconfined) {
67                 traceCoroutine("async#B") { forceSuspend("deferredB", 25) }
68                 traceSection("startC") { deferredC.start() }
69             }
70         val deferredA =
71             async(start = LAZY, context = dispatcher3) {
72                 traceCoroutine("async#A") { forceSuspend("deferredA", 25) }
73                 traceSection("startB") { deferredB.start() }
74             }
75 
76         // no dispatcher specified, so will inherit dispatcher from whoever called
77         // run(), meaning the main thread
78         val deferredE =
79             asyncTraced("overridden-scope-name-for-deferredE") {
80                 traceCoroutine("async#E") { forceSuspend("deferredE", 25) }
81             }
82 
83         launch(dispatcher1) {
84             traceSection("start10") { deferred10.start() }
85             traceSection("startA") { deferredA.start() }
86             traceSection("startE") { deferredE.start() }
87         }
88     }
89 }
90