1

I'm setting up a scheduler in C and using setcontext and swapcontext a lot.

I'm setting context to a function and swapping around to other functions using a timer, but I don't know how to check if the function has completed and has proceeded onto the uc_link.

The code is too long and it's a library, so it's incomplete, so here's pseudocode of what it's supposed to do

addThread(someFunction)
    if scheduler not setup
        setupScheduler()
    end

    setup context for someFunction
    insert to queue
end

schedule()
    if current context is not completed or was interrupted by timer
        move current context to end of queue
    else //it is completed
        do not add back into queue or setup something to skip when next in queue
    end

    setcontext to first in queue
end

setupScheduler()
    make ucontext_t for scheduler

    swap to context of schedule() whenever SIGVTALRM triggers
    setup timer to trigger SIGVTALRM after 25ms
end


main()
    addThread(foo)
    addThread(bar)

    while(1); //busy work
end
Steve
  • 88
  • 8

0 Answers0