RichieSams/FiberTaskingLib

Integration into event loop based system

henrywiechert opened this issue · 5 comments

Hi,

can this framework be integrated into an event loop based system ?
E.g. boost::fibers allow this by adding the this_fiber::yield() call to the event loop, which triggers the execution of a waiting fiber, if available. Is a similar mechanism possible with fibertaskinglib ?
I would like to gradually introduce fibers for some specific workloads in my project.

Thanks,
Henry

FTL is a Tasking System that is built using fibers. That said, the fibers themselves are not exposed via the API.

To answer your question: Yes, FTL can be used in an event loop based system. A crude implementation would be something like:

void EventFunc(ftl::TaskScheduler *taskScheduler, void *arg) {
    // Code
}

void MainTask(ftl::TaskScheduler *taskScheduler, void *arg) {
    while (true) {
        Event *event;
        if (PollForEvent(&event)) {
            ftl::Task task;
            task.Function = EventFunc;
            task.ArgData = event;
            
            ftl::AtomicCounter *counter = new ftl::AtomicCounter(taskScheduler);
            taskScheduler->AddTask(task, &counter);
        }
    }
}

int main(int argc, char *argv) {
    ftl::TaskScheduler taskScheduler;
    taskScheduler.Run(25, MainTask);

    return 0;
}

Thanks for your quick answer.
That means the main fiber need to run the event loop.
I cannot arrange that yet unfortunately. I would like to have one or more threads taking queued tasks, while the main thread is running a proprietary event loop/handler (not connected to any fibers, yet).
The main thread shall still be able to enqueue tasks and if needed execute single tasks from the queue in a somehow "manual fashion" by triggering the scheduler to take the next task.
Don't know if this is clear enough. Probably a bit confusing still ...

Anyways, FTL is a nice reflection of what Christian was presenting on the GDC back then :-)

You're welcome :)

I understand what you mean. You would like to be able to call AddTask() from a thread outside the TaskScheduler scope. Currently, that's not possible, because FTL uses a form of Thread Local Variables to store state / avoid locks. Therefore, an external thread would not be able to access the correct data.

One way to get around this perhaps would be to have a MainTask() event loop that's consuming from a custom queue. Your "real" event loop would Poll / consume events from the outside, and if you want it to be taken by FTL event loop, then queue it in the custom queue. MainTask will continually try to pop from the queue, and then execute Tasks.

Ok, got it. This makes sense.
My main target is super fine grained parallelism among fully isolated cores - but introduced gradually for an existing classical non-parallel project. Other frameworks like OpenMP and TBB are also possible. But FTL looked promising at first hand because of its "controlled concurrency" via fibers and their flexibility.

Thanks for your help. I think you can close this topic.

You're welcome. Let me know if you have any other questions