rhoot/sc

Porting from Windows Fiber

boberfly opened this issue · 3 comments

Hi @rhoot this is an awesome library!

I just wanted to ask, I am trying to port an existing codebase with Windows Fibers, and I can tell some of the terms and concepts do convert across, but I am not 100% sure what is correct.

I'm using a forked codebase here:
https://github.com/boberfly/Engine/blob/master/src/core/private/concurrency.cpp#L170

From what I see, to use Windows fibers you need to book-keep quite a bit around it, but sc supplies quite a bit which isn't needed?

Cheers!

rhoot commented

It depends on what book-keeping you are referring to, but it does look like that implementation does a lot of the same work as sc, just with Windows fibers instead. I think you'd find the biggest changes to be:

  1. You don't have to call ConvertThreadToFiber. There's nothing like it in sc, as everything is a context. You can just call sc_main_context whenever.
  2. Unlike GetFiberData you can call sc_get_data for a context other than the currently executing one.
  3. sc has no equivalent of fiber-local storage. It would end up adding a large amount of complexity for something that is very rarely needed. I just store data in the user-data myself. That said, it does support Windows fiber-local storage so you can keep using that if you have a need for it, but it would not work on other platforms.

Did you also look at the example? Much of the API should be mostly pretty much a 1:1 mapping, but if you have any more concrete questions I'd be happy to answer them.

Hi @rhoot

Thank you for getting back to me on this, I finally got some time to return to this also.

To bypass ConvertThreadToFiber I assume you do something like this to create a context onto the thread_local of that thread?

sc_context_t threadFiber = sc_main_context();
threadFiber = sc_context_create(stack, stackSize, &myEntryPoint);

Pretty much the only function I have is this:
https://github.com/boberfly/Engine/blob/linux/src/core/tests/concurrency_tests.cpp#L355
I can most likely change the user-facing function to work better with sc (so it just uses the regular constructor which can set an entry point + stack size), most likely I will probably use sc for the window-side once it is working (I'm porting on Linux).

Cheers!

rhoot commented

You can skip the second line entirely. The thread fiber has already been created for you; it's what sc_main_context returns.