• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.coroutines
6 
7 internal actual abstract class EventLoopImplPlatform: EventLoop() {
8     protected abstract val thread: Thread
9 
unparknull10     protected actual fun unpark() {
11         val thread = thread // atomic read
12         if (Thread.currentThread() !== thread)
13             unpark(thread)
14     }
15 
reschedulenull16     protected actual open fun reschedule(now: Long, delayedTask: EventLoopImplBase.DelayedTask) {
17         DefaultExecutor.schedule(now, delayedTask)
18     }
19 }
20 
21 internal class BlockingEventLoop(
22     override val thread: Thread
23 ) : EventLoopImplBase()
24 
createEventLoopnull25 internal actual fun createEventLoop(): EventLoop = BlockingEventLoop(Thread.currentThread())
26 
27 /**
28  * Processes next event in the current thread's event loop.
29  *
30  * The result of this function is to be interpreted like this:
31  * * `<= 0` -- there are potentially more events for immediate processing;
32  * * `> 0` -- a number of nanoseconds to wait for the next scheduled event;
33  * * [Long.MAX_VALUE] -- no more events or no thread-local event loop.
34  *
35  * Sample usage of this function:
36  *
37  * ```
38  * while (waitingCondition) {
39  *     val time = processNextEventInCurrentThread()
40  *     LockSupport.parkNanos(time)
41  * }
42  * ```
43  *
44  * @suppress **This an internal API and should not be used from general code.**
45  */
46 @InternalCoroutinesApi
47 public fun processNextEventInCurrentThread(): Long =
48     ThreadLocalEventLoop.currentOrNull()?.processNextEvent() ?: Long.MAX_VALUE
49 
50 internal actual inline fun platformAutoreleasePool(crossinline block: () -> Unit) = block()
51