我在 C 中设置了一个调度程序,并经常使用 setcontext 和 swapcontext。
我正在为一个函数设置上下文并使用计时器切换到其他函数,但我不知道如何检查该函数是否已完成并已进入 uc_link。
代码太长而且它是一个库,所以它不完整,所以这里是它应该做什么的伪代码
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