@@ -28,22 +28,16 @@ import java.util.concurrent.TimeUnit
28
28
class TaskQueue internal constructor(
29
29
private val taskRunner : TaskRunner
30
30
) {
31
- private var shutdown = false
31
+ internal var shutdown = false
32
32
33
33
/* * This queue's currently-executing task, or null if none is currently executing. */
34
- private var activeTask: Task ? = null
35
-
36
- /* * True if the [activeTask] should not recur when it completes. */
37
- private var cancelActiveTask = false
34
+ internal var activeTask: Task ? = null
38
35
39
36
/* * Scheduled tasks ordered by [Task.nextExecuteNanoTime]. */
40
- private val futureTasks = mutableListOf<Task >()
41
-
42
- internal fun isActive (): Boolean {
43
- check(Thread .holdsLock(taskRunner))
37
+ internal val futureTasks = mutableListOf<Task >()
44
38
45
- return activeTask != null || futureTasks.isNotEmpty()
46
- }
39
+ /* * True if the [ activeTask] should be canceled when it completes. */
40
+ internal var cancelActiveTask = false
47
41
48
42
/* *
49
43
* Returns a snapshot of tasks currently scheduled for execution. Does not include the
@@ -87,7 +81,7 @@ class TaskQueue internal constructor(
87
81
fun awaitIdle (delayNanos : Long ): Boolean {
88
82
val latch = CountDownLatch (1 )
89
83
90
- val task = object : Task (" awaitIdle" ) {
84
+ val task = object : Task (" awaitIdle" , cancelable = false ) {
91
85
override fun runOnce (): Long {
92
86
latch.countDown()
93
87
return - 1L
@@ -104,8 +98,8 @@ class TaskQueue internal constructor(
104
98
return latch.await(delayNanos, TimeUnit .NANOSECONDS )
105
99
}
106
100
107
- /* * Adds [task] to run in [delayNanos]. Returns true if the coordinator should run . */
108
- private fun scheduleAndDecide (task : Task , delayNanos : Long ): Boolean {
101
+ /* * Adds [task] to run in [delayNanos]. Returns true if the coordinator is impacted . */
102
+ internal fun scheduleAndDecide (task : Task , delayNanos : Long ): Boolean {
109
103
task.initQueue(this )
110
104
111
105
val now = taskRunner.backend.nanoTime()
@@ -124,7 +118,7 @@ class TaskQueue internal constructor(
124
118
if (insertAt == - 1 ) insertAt = futureTasks.size
125
119
futureTasks.add(insertAt, task)
126
120
127
- // Run the coordinator if we inserted at the front.
121
+ // Impact the coordinator if we inserted at the front.
128
122
return insertAt == 0
129
123
}
130
124
@@ -154,8 +148,8 @@ class TaskQueue internal constructor(
154
148
}
155
149
}
156
150
157
- /* * Returns true if the coordinator should run . */
158
- private fun cancelAllAndDecide (): Boolean {
151
+ /* * Returns true if the coordinator is impacted . */
152
+ internal fun cancelAllAndDecide (): Boolean {
159
153
if (activeTask != null && activeTask!! .cancelable) {
160
154
cancelActiveTask = true
161
155
}
@@ -169,43 +163,4 @@ class TaskQueue internal constructor(
169
163
}
170
164
return tasksCanceled
171
165
}
172
-
173
- /* *
174
- * Posts the next available task to an executor for immediate execution.
175
- *
176
- * Returns the delay until the next call to this method, -1L for no further calls, or
177
- * [Long.MAX_VALUE] to wait indefinitely.
178
- */
179
- internal fun executeReadyTask (now : Long ): Long {
180
- check(Thread .holdsLock(taskRunner))
181
-
182
- if (activeTask != null ) return Long .MAX_VALUE // This queue is busy.
183
-
184
- // Check if a task is immediately ready.
185
- val runTask = futureTasks.firstOrNull() ? : return - 1L
186
- val delayNanos = runTask.nextExecuteNanoTime - now
187
- if (delayNanos <= 0 ) {
188
- activeTask = runTask
189
- futureTasks.removeAt(0 )
190
- taskRunner.backend.executeTask(runTask.runRunnable!! )
191
- return Long .MAX_VALUE // This queue is busy until the run completes.
192
- }
193
-
194
- // Wait until the next task is ready.
195
- return delayNanos
196
- }
197
-
198
- internal fun runCompleted (task : Task , delayNanos : Long ) {
199
- synchronized(taskRunner) {
200
- check(activeTask == = task)
201
-
202
- if (delayNanos != - 1L && ! cancelActiveTask && ! shutdown) {
203
- scheduleAndDecide(task, delayNanos)
204
- }
205
-
206
- activeTask = null
207
- cancelActiveTask = false
208
- taskRunner.kickCoordinator(this )
209
- }
210
- }
211
166
}
0 commit comments