• 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 import kotlinx.cinterop.*
8 import kotlinx.coroutines.internal.*
9 import kotlinx.coroutines.internal.multithreadingSupported
10 import platform.posix.*
11 import kotlin.coroutines.*
12 import kotlin.native.concurrent.*
13 import kotlin.system.*
14 
15 internal actual abstract class EventLoopImplPlatform : EventLoop() {
16 
17     private val current = Worker.current
18 
unparknull19     protected actual fun unpark() {
20         current.executeAfter(0L, {})// send an empty task to unpark the waiting event loop
21     }
22 
reschedulenull23     protected actual fun reschedule(now: Long, delayedTask: EventLoopImplBase.DelayedTask) {
24         if (multithreadingSupported) {
25             DefaultExecutor.invokeOnTimeout(now, delayedTask, EmptyCoroutineContext)
26         } else {
27             error("Cannot execute task because event loop was shut down")
28         }
29     }
30 }
31 
32 internal class EventLoopImpl: EventLoopImplBase() {
invokeOnTimeoutnull33     override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle {
34         if (!multithreadingSupported) {
35             return scheduleInvokeOnTimeout(timeMillis, block)
36         }
37         return DefaultDelay.invokeOnTimeout(timeMillis, block, context)
38     }
39 }
40 
createEventLoopnull41 internal actual fun createEventLoop(): EventLoop = EventLoopImpl()
42 
43 internal actual fun nanoTime(): Long = getTimeNanos()
44