> However, I think the M:N task is very difficult and might not be achievable 
> without some other sacrifices on the actual project (speed ? Compile time ? 
> drastic change in API usage ? safety ? ). But I really don't know.

I have done so many threadpools now, I can do them with closed eyes.

Here is a very simple, decent performance threadpool: 
<https://github.com/status-im/nim-taskpools>

As it was written for very high-stakes (billions of dollars secured) and to run 
24/7, I intentionally kept it extremely simple and easy to audit and maintain.

I use std/tasks here - 
<https://github.com/status-im/nim-taskpools/blob/d4c4313/taskpools/tasks.nim#L58-L62>
 but if you can replace by your coroutines, you have easy M:N.

The core of a M:N scheduler is there:

  * 20 lines event loop: 
<https://github.com/status-im/nim-taskpools/blob/d4c4313/taskpools/tasks.nim#L58-L62>
  * 40 lines awaiting a future: 
<https://github.com/status-im/nim-taskpools/blob/d4c4313/taskpools/tasks.nim#L58-L62>
  * Then there is lots of logic to create a future but it's the same as for a 
coroutine or creating a closure. Ideally Nim closures are more flexible 
regarding allocation and you wouldn't need this: 
<https://github.com/status-im/nim-taskpools/blob/d4c4313/taskpools/tasks.nim#L58-L62>



The current Weave-IO is at the moment just a copy paste of Constantine's 
threadpool: 
<https://github.com/mratsim/constantine/tree/master/constantine/threadpool> 
which is an evolution of nim-taskpools for improvement in **compute** 
(parallel-for). The task system has been significantly improved (single 
allocation per task instead of 3): 
<https://github.com/mratsim/weave-io/blob/8672f1cc/weave_io/crossthread/tasks_flowvars.nim#L46-L68>
 The core has extra bells and whistles but it's not much:

  * 40 lines event loop: 
<https://github.com/mratsim/weave-io/blob/8672f1c/weave_io/threadpool.nim#L691-L730>
  * 100 lines waiting for a future: 
<https://github.com/mratsim/weave-io/blob/8672f1c/weave_io/threadpool.nim#L691-L730>



Also as mentioned here in the changed required for Weave-IO to achieve its 
vision: <https://github.com/mratsim/weave-io/issues/1> the goal is to be usable 
with scheduler agnostic async frameworks like 
<https://github.com/yglukhov/yasync>

Reply via email to