Skip to content

Commit 31aff8f

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

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,25 @@ internal object ThreadLocalEventLoop {
129129

130130
internal expect fun createEventLoop(): EventLoop
131131

132+
/**
133+
* Processes next event in the current thread's event loop.
134+
*
135+
* The result of this function is to be interpreted like this:
136+
* * `<= 0` -- there are potentially more events for immediate processing;
137+
* * `> 0` -- a number of nanoseconds to wait for the next scheduled event;
138+
* * [Long.MAX_VALUE] -- no more events, or was invoked from the wrong thread.
139+
*
140+
* Sample usage of this function:
141+
*
142+
* ```
143+
* while (waitingCondition) {
144+
* val time = processNextEventInCurrentThread()
145+
* LockSupport.parkNanos(time)
146+
* }
147+
* ```
148+
*
149+
* @suppress **This an internal API and should not be used from general code.**
150+
*/
151+
@InternalCoroutinesApi
152+
public fun processNextEventInCurrentThread(): Long =
153+
ThreadLocalEventLoop.currentOrNull()?.processNextEvent() ?: Long.MAX_VALUE

0 commit comments

Comments
 (0)