Another question for the similar topic. I need to ...
# coroutines
a
Another question for the similar topic. I need to use custom clock to count delays (virtual clock to be specific). Do I need to implement my own event loop to be able to "wake" on specific event?
d
You certainly need a
while (true)
at some point, because no one but you knows whether there are tasks left for your custom dispatcher to process. You can abstract the lower-level details away, though, by reusing the event loop from
runBlocking
. We do that in
kotlinx-coroutines-test
(https://github.com/Kotlin/kotlinx.coroutines/blob/465e29d325841244f3a1aac2e13073bc965f9736/kotlinx-coroutines-test/common/src/TestBuilders.kt#L[…]61), though I suspect we'll change that eventually.
a
The primary question is how can I notify scheduler that delay is ended
d
I'm not sure that I understand the question, but probably something like this https://github.com/Kotlin/kotlinx.coroutines/issues/2024 ?
a
I will check. What I mean is that I set
delay
, say for 200 milliseconds, then I need for it to record time mark in virtual time scheduler and if all other timelines are right it should immediately continue without waiting for real 200 milliseconds. I mean that I need to notify scheduler that time has passed.
Or I want to hold delay for longer time until virtual time is right
In general, I need to manually trigger the end of delay
a
Exactly that I am studying right now. I just thought that maybe there is a simpler way to give the scheduler wake up call
d
This heavily depends on how you implement the process of sleeping. If it's
LockSupport.parkNanos
, call
unpark
. If it's
kotlinx.coroutines.delay
, replace that with
select { }
with the timeout clause and the clause for obtaining the "time to wake up" message channel. If it's
Thread.sleep
, your friend is
Thread.interrupt
.
a
I see. I will study it further.