rhoot/sc

Implementing `sc_sleep(uint32_t milliseconds)`

Closed this issue ยท 2 comments

@rhoot Hey, thanks for the project!

Any thought regarding the implementation of sc_sleep(uint32_t milliseconds) to suspend continuous execution of a certain coroutine for N amount of milliseconds similarly to libdill or libfiber for example?

I see this as a timer which is checked before switching a context to bypass execution if a suspension time is not over yet, but I'm not sure.

rhoot commented

While sc_sleep would be useful, it would require a scheduler. Having written one on top of sc before, having a scheduler integrated in sc would make it more restrictive than what you can make yourself (knowing your use-case), while overly bloating the project for people who don't need it (it is a large undertaking to write a good general-case scheduler).

libdill and libfiber are two examples of different approaches of doing it, but none of them are great in my experience:

  • libdill only considers context switching when one of its own functions are called, which makes it intrusive in client code. You would have to go update third party code to call into it too, or lose out on CPU potential.

  • libfiber works with system calls too by hooking them, which makes it just work, but makes it unusable on systems where you cannot hook them (there are big proprietary platforms where this is true).

  • Neither of them work multi-threaded. You can use it in multiple threads, but a fiber cannot move from one thread to another. In a job-system for instance this might be desirable. But maybe you don't want it to move between all threads, only your job threads.

So while having a scheduler is useful, it is much better done in another project on a higher level, cause that's where opinions and use-cases become factors. :)

(Sorry for spamming your inbox and screwing up the thread timeline, I accidentally submitted this before I was done typing...)

Thanks for the detailed explanation.

I agree, just wanted to take into account your opinion before implementing this myself. ๐Ÿ‘