Replies: 5 comments
-
It's somewhat possible, yes. When introducing timers, I've made it possible for coroutines to be suspended. Currently, they can only be suspended when waiting for a timer to expire, but the mechanism can be made generic in a way that a coroutine is suspended only to be resumed by any event, e.g. a callback from another library being called. The only catch, though, is that suspending and resuming coroutines must happen only in the thread that owns them: there are no locks whatsoever in that path, so this is might require some changes in Lwan itself. coro_defer() allows you to defer a callback to be called when a coroutine dies, regardless of the reason (e.g. being aborted, ending gracefully, etc.). If you're allocating memory from within a coroutine, and you pretend to perform any I/O using the I/O wrapper functions (e.g. lwan_writev()), then you must allocate memory through coro_malloc(); otherwise, you risk a memory leak in case the coroutine is aborted due to unrecoverable connection errors. This is also the case for any other kind of resources that must be somehow freed when not being used any longer: mutexes, files, any reference-counted object (e.g. cache entries), etc. |
Beta Was this translation helpful? Give feedback.
-
I tried for fun to place a sleep(10000) function into yieldable part of code. It blocks main thread also. |
Beta Was this translation helpful? Give feedback.
-
Yes. Any blocking function in a coroutine handling a connection will block the whole thread. There's no preemptive multitasking for coroutines, or any kind of work stealing scheduler that perceives the thread is not making forward progress. That's why sleeping a handler can't be done without using lwan_request_sleep(). |
Beta Was this translation helpful? Give feedback.
-
Ah that is, I see, sleep and wake up. |
Beta Was this translation helpful? Give feedback.
-
Сan you in your blog explorer this stuff with coroutines and timers? |
Beta Was this translation helpful? Give feedback.
-
Is it possible in a current implementation of coroutines to wrap the callback-style functions and to use them from coroutines?
Is it the coro_defer() function only for free some internal stuff?
Beta Was this translation helpful? Give feedback.
All reactions