Skip to content

Commit ffe8fac

Browse files
committed
Internal API for custom waiting loops
To be used by kotlinx-io
1 parent 6396440 commit ffe8fac

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

core/kotlinx-coroutines-core/src/EventLoop.kt

+24-1
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,27 @@ internal class BlockingEventLoop(
315315
override val thread: Thread
316316
) : EventLoopImplBase()
317317

318-
internal actual fun createEventLoop(): EventLoop = BlockingEventLoop(Thread.currentThread())
318+
internal actual fun createEventLoop(): EventLoop = BlockingEventLoop(Thread.currentThread())
319+
320+
/**
321+
* Processes next event in the current thread's event loop.
322+
*
323+
* The result of this function is to be interpreted like this:
324+
* * `<= 0` -- there are potentially more events for immediate processing;
325+
* * `> 0` -- a number of nanoseconds to wait for the next scheduled event;
326+
* * [Long.MAX_VALUE] -- no more events, or was invoked from the wrong thread.
327+
*
328+
* Sample usage of this function:
329+
*
330+
* ```
331+
* while (waitingCondition) {
332+
* val time = processNextEventInCurrentThread()
333+
* LockSupport.parkNanos(time)
334+
* }
335+
* ```
336+
*
337+
* @suppress **This an internal API and should not be used from general code.**
338+
*/
339+
@InternalCoroutinesApi
340+
public fun processNextEventInCurrentThread(): Long =
341+
ThreadLocalEventLoop.currentOrNull()?.processNextEvent() ?: Long.MAX_VALUE

0 commit comments

Comments
 (0)