@@ -9,7 +9,6 @@ import kotlinx.coroutines.experimental.internal.*
9
9
import kotlinx.coroutines.experimental.timeunit.*
10
10
import java.util.concurrent.locks.*
11
11
import kotlin.coroutines.experimental.*
12
- import kotlin.jvm.*
13
12
14
13
/* *
15
14
* Implemented by [CoroutineDispatcher] implementations that have event loop inside and can
@@ -111,9 +110,6 @@ internal abstract class EventLoopBase: CoroutineDispatcher(), Delay, EventLoop {
111
110
override fun scheduleResumeAfterDelay (time : Long , unit : TimeUnit , continuation : CancellableContinuation <Unit >) =
112
111
schedule(DelayedResumeTask (time, unit, continuation))
113
112
114
- override fun invokeOnTimeout (time : Long , unit : TimeUnit , block : Runnable ): DisposableHandle =
115
- DelayedRunnableTask (time, unit, block).also { schedule(it) }
116
-
117
113
override fun processNextEvent (): Long {
118
114
if (! isCorrectThread()) return Long .MAX_VALUE
119
115
// queue all delayed tasks that are due to be executed
@@ -141,8 +137,9 @@ internal abstract class EventLoopBase: CoroutineDispatcher(), Delay, EventLoop {
141
137
if (enqueueImpl(task)) {
142
138
// todo: we should unpark only when this delayed task became first in the queue
143
139
unpark()
144
- } else
140
+ } else {
145
141
DefaultExecutor .execute(task)
142
+ }
146
143
}
147
144
148
145
@Suppress(" UNCHECKED_CAST" )
@@ -266,23 +263,26 @@ internal abstract class EventLoopBase: CoroutineDispatcher(), Delay, EventLoop {
266
263
267
264
fun timeToExecute (now : Long ): Boolean = now - nanoTime >= 0L
268
265
269
- fun rescheduleOnShutdown () = synchronized(this ) {
266
+ @Synchronized
267
+ fun rescheduleOnShutdown () {
270
268
if (state != DELAYED ) return
271
269
if (_delayed .value!! .remove(this )) {
272
270
state = RESCHEDULED
273
271
DefaultExecutor .schedule(this )
274
- } else
272
+ } else {
275
273
state = REMOVED
274
+ }
276
275
}
277
276
278
- final override fun dispose () = synchronized(this ) {
279
- when (state) {
280
- DELAYED -> _delayed .value?.remove(this )
281
- RESCHEDULED -> DefaultExecutor .removeDelayedImpl(this )
282
- else -> return
283
- }
284
- state = REMOVED
277
+ @Synchronized
278
+ final override fun dispose () {
279
+ when (state) {
280
+ DELAYED -> _delayed .value?.remove(this )
281
+ RESCHEDULED -> DefaultExecutor .removeDelayedImpl(this )
282
+ else -> return
285
283
}
284
+ state = REMOVED
285
+ }
286
286
287
287
override fun toString (): String = " Delayed[nanos=$nanoTime ]"
288
288
}
@@ -302,7 +302,8 @@ internal abstract class EventLoopBase: CoroutineDispatcher(), Delay, EventLoop {
302
302
}
303
303
}
304
304
305
- private inner class DelayedRunnableTask (
305
+ // Cannot be moved to DefaultExecutor due to BE bug
306
+ internal inner class DelayedRunnableTask (
306
307
time : Long , timeUnit : TimeUnit ,
307
308
private val block : Runnable
308
309
) : DelayedTask(time, timeUnit) {
0 commit comments