4 ms·
As far as I know, it' basically impossible to interrupt a blocking IO call unless you're using something designed for preemptive scheduling, like epoll[1]. A J
by brabel 3y ago
As far as I know, it' basically impossible to interrupt a blocking IO call unless you're using something designed for preemptive scheduling, like epoll[1].
A Java Thread can be interrupted, but only if the runnable implementation "cooperates"... if it's blocking on a synchronous socket "read", it's going to stay blocked even after the Thread has been interrupted.
In a language like Dart, where async is mostly the default, it's possible to cancel every job running on a "Zone"[2] because the mechanism used for creating a new Future is interceptable by application code, which is pretty cool.
Kotlin coroutines have a mechanism for cancellation built-in[3] and it's also "cooperative", i.e. all suspend functions will cancel within the coroutine context, but a rogue coroutine that blocks on a blocking IO call will remain blocked (as I understand it - not sure if Kotlin provides ways to completely avoid blocking IO).
So I guess whatever language you use, there has to be points of "yielding" control where cancellation can occur... the `while (!preempted)` check is just one of them.
[1] https://man7.org/linux/man-pages/man7/epoll.7.html https://man7.org/linux/man-pages/man7/epoll.7.html
[2] https://dart.dev/articles/archive/zones https://dart.dev/articles/archive/zones
[3] https://kotlinlang.org/docs/cancellation-and-timeouts.html#cancellation-is-cooperative https://kotlinlang.org/docs/cancellation-and-timeouts.html#c...`
- Matthias247 3y agoYou can interrupt blocking IO calls using signals, so there is an alternative to using epoll/select for this. It can also be used to implement timeouts if another userspace thread tracks the IO operation duration and sends a signal to the IO blocked thread if the max duration has expired. I think Java thread interruption for blocking IO might be based on signals and is not using nonblocking IO under the hood. But I would need to read JDK source again to tell for sure
- girvo 3y agoYeah signals is what I’ve seen too, though I’m unsure if newlib + FreeRTOS has enough to make ‘em work! I’ll have a look though.