|
CGraph
|
主线程类,核心成员是primary_queue_和secondary_queue_ 自身准备执行任务时,优先从primary_queue_取,其次从secondary_queue_取 自身准备窃取任务时,优先从secondary_queue_,其次从primary_queue_取 这样设计的好处是A线程的执行和B线程从A线程窃取这两个异步事件发生竞争的可能性大大减小,从而提高性能 主线程所属的主线程池pool_threads_的指针也记录在每个主线程对象里面,在窃取相邻线程任务、安全检查等会用到 More...
#include <UThreadPrimary.h>


Protected Member Functions | |
| UThreadPrimary () | |
| CStatus | init () override |
| CStatus | setThreadPoolInfo (int index, UAtomicQueue< UTask > *poolTaskQueue, std::vector< UThreadPrimary * > *poolThreads, UThreadPoolConfigPtr config) |
| CStatus | run () final |
| CVoid | processTask () override |
| 按照以下顺序拿任务到本线程执行(如果都没拿到就fatWait一直等着): 本地任务(优先primary_queue_,其次secondary_queue_) -> 偷任务(相邻一定范围内的其他主线程,优先secondary_queue_,其次primary_queue_) -> 公共任务队列(pool_task_queue_) -> 优先级任务队列(pool_priority_task_queue_,只有辅助线程会走到这个分支) More... | |
| CVoid | processTasks () override |
| CVoid | fatWait () |
| CVoid | pushTask (UTask &&task) |
| CVoid | pushTask (UTask &&task, CBool enable, CBool lockable) |
| CBool | popTask (UTaskRef task) |
| CBool | popTask (UTaskArrRef tasks) |
| CBool | stealTask (UTaskRef task) |
| CBool | stealTask (UTaskArrRef tasks) |
| CVoid | buildStealTargets () |
| ~UThreadPrimary () override | |
Protected Member Functions inherited from UThreadBase | |
| UThreadBase () | |
| ~UThreadBase () override | |
| CStatus | destroy () override |
| virtual CBool | popPoolTask (UTaskRef task) |
| virtual CBool | popPoolTask (UTaskArrRef tasks) |
| CVoid | runTask (UTask &task) |
| CVoid | runTasks (UTaskArr &tasks) |
| CVoid | reset () |
| CVoid | loopProcess () |
| CVoid | setSchedParam () |
| CVoid | setAffinity (int index) |
Protected Member Functions inherited from UThreadObject | |
| CStatus | run () override |
Protected Member Functions inherited from UtilsObject | |
| CStatus | run () override |
Private Attributes | |
| CInt | index_ |
| CInt | cur_empty_epoch_ = 0 |
| UWorkStealingQueue< UTask > | primary_queue_ |
| UWorkStealingQueue< UTask > | secondary_queue_ |
| std::vector< UThreadPrimary * > * | pool_threads_ |
| std::vector< CInt > | steal_targets_ |
Friends | |
| class | UThreadPool |
| class | CAllocator |
Additional Inherited Members | |
Public Member Functions inherited from CObject | |
| CObject ()=default | |
| virtual | ~CObject ()=default |
Protected Attributes inherited from UThreadBase | |
| CBool | done_ |
| CBool | is_init_ |
| CBool | is_running_ |
| CInt | type_ = 0 |
| CULong | total_task_num_ = 0 |
| UAtomicQueue< UTask > * | pool_task_queue_ |
| UAtomicPriorityQueue< UTask > * | pool_priority_task_queue_ |
| UThreadPoolConfigPtr | config_ = nullptr |
| UMetrics | metrics_ |
| std::thread | thread_ |
| std::mutex | mutex_ |
| std::condition_variable | cv_ |
主线程类,核心成员是primary_queue_和secondary_queue_ 自身准备执行任务时,优先从primary_queue_取,其次从secondary_queue_取 自身准备窃取任务时,优先从secondary_queue_,其次从primary_queue_取 这样设计的好处是A线程的执行和B线程从A线程窃取这两个异步事件发生竞争的可能性大大减小,从而提高性能 主线程所属的主线程池pool_threads_的指针也记录在每个主线程对象里面,在窃取相邻线程任务、安全检查等会用到
|
inlineexplicitprotected |
|
inlineoverrideprotected |
在开启展示宏的时候,会在主线程退出的时候,打印相关内容 默认情况下,不会开启

|
inlineprotected |
构造 steal 范围的 target,避免每次盗取的时候,重复计算

|
inlineprotected |
如果总是进入无task的状态,则开始休眠 休眠一定时间后,然后恢复执行状态,避免出现异常情况导致无法唤醒

|
inlineoverrideprotectedvirtual |
|
inlineprotected |
从本地弹出一批任务
| tasks |

从本地弹出一个任务
| task |

|
inlineoverrideprotectedvirtual |
按照以下顺序拿任务到本线程执行(如果都没拿到就fatWait一直等着): 本地任务(优先primary_queue_,其次secondary_queue_) -> 偷任务(相邻一定范围内的其他主线程,优先secondary_queue_,其次primary_queue_) -> 公共任务队列(pool_task_queue_) -> 优先级任务队列(pool_priority_task_queue_,只有辅助线程会走到这个分支)
Implements UThreadBase.

|
inlineoverrideprotectedvirtual |
依次push到任一队列里。如果都失败,则yield,然后重新push
| task |

写入 task信息,是否上锁由入参enable和lockable决定
| task | |
| enable | 确认是否有锁 |
| lockable | true 的时候需要上锁,false 的时候会解锁 |

|
inlinefinalprotectedvirtual |
线程执行函数
线程池中任何一个primary线程为null都不可以执行 防止线程初始化失败的情况,导致的崩溃 理论不会走到这个判断逻辑里面
Implements CObject.

|
inlineprotected |
注册线程池相关内容,需要在init之前使用
| index | |
| poolTaskQueue | |
| poolThreads | |
| config |
|
inlineprotected |
从其他线程盗取一批任务
| tasks |
在这里,我们对模型进行了简化。实现的思路是: 尝试从邻居主线程(先secondary,再primary)中,获取 x(=max_steal_batch_size_) 个task, 如果从某一个邻居中,获取了 y(<=x) 个task,则也终止steal的流程 且如果如果有一次批量steal成功,就认定成功

从其他线程窃取一个任务
| task |
线程池还未初始化完毕的时候,无法进行steal。 确保程序安全运行。
窃取的时候,仅从相邻的primary线程中窃取 待窃取相邻的数量,不能超过默认primary线程数 steal_targets_在init的时候就已经计算好了
从线程中周围的thread中,窃取任务。 如果成功,则返回true,并且执行任务。 steal 的时候,先从第二个队列里偷,从而降低触碰锁的概率

|
friend |
|
friend |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |