TaskControl:全局一个,控制所有TaskGroup和工作线程,初始化时根据配置数量创建指定数量的工作线程。包含4个parkinglot供线程睡眠等待任务使用。非工作线程添加任务时通过TaskControl进行,在TaskControl中随机选择一个TaskGroup并将任务添加到该TaskGroup队列中
TaskGroup:每个工作线程绑定一个TaskGroup,TaskGroup中包含工作队列等信息
源码地址:bthread/task_control.h bthread/task_control.cpp bthread/task_group.h bthread/task_group_inl.h bthread/task_group.cpp
| // 工作线程执行函数 TaskGroup::run_main_task() {
TaskGroup dummy // 每次获取一个待执行的bthread,若没有在parkinglot上睡眠等待唤醒 while (wait_task(&tid)) {
// 切换到tid标记的bthread的上下文 sched_to(&dummy, tid) // 这里工作线程得到调度,回到工作线程的上下文继续执行 // 下一个任务分配栈失败或者指定了pthread模式,直接在pthread的栈上调用task_runner() if (cur_meta.tid != main_tid) task_runner() } // 这里线程退出 } // 此函数中发生上下文切换 sched_to(TaskGroup g, TaskMeta next_meta) {
cur_me
|