CGraph
Protected Member Functions | Private Attributes | Friends | List of all members
UThreadPrimary Class Reference

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

#include <UThreadPrimary.h>

Inheritance diagram for UThreadPrimary:
Inheritance graph
[legend]
Collaboration diagram for UThreadPrimary:
Collaboration graph
[legend]

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< UTaskprimary_queue_
 
UWorkStealingQueue< UTasksecondary_queue_
 
std::vector< UThreadPrimary * > * pool_threads_
 
std::vector< CIntsteal_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_
 

Detailed Description

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

Constructor & Destructor Documentation

◆ UThreadPrimary()

UThreadPrimary::UThreadPrimary ( )
inlineexplicitprotected

◆ ~UThreadPrimary()

UThreadPrimary::~UThreadPrimary ( )
inlineoverrideprotected

在开启展示宏的时候,会在主线程退出的时候,打印相关内容 默认情况下,不会开启

Here is the call graph for this function:

Member Function Documentation

◆ buildStealTargets()

CVoid UThreadPrimary::buildStealTargets ( )
inlineprotected

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

Returns
Here is the call graph for this function:

◆ fatWait()

CVoid UThreadPrimary::fatWait ( )
inlineprotected

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

Here is the call graph for this function:

◆ init()

CStatus UThreadPrimary::init ( )
inlineoverrideprotectedvirtual

初始化函数

Reimplemented from CObject.

Here is the call graph for this function:

◆ popTask() [1/2]

CBool UThreadPrimary::popTask ( UTaskArrRef  tasks)
inlineprotected

从本地弹出一批任务

Parameters
tasks
Returns
Here is the call graph for this function:

◆ popTask() [2/2]

CBool UThreadPrimary::popTask ( UTaskRef  task)
inlineprotected

从本地弹出一个任务

Parameters
task
Returns
Here is the call graph for this function:

◆ processTask()

CVoid UThreadPrimary::processTask ( )
inlineoverrideprotectedvirtual

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

Returns
CVoid

Implements UThreadBase.

Here is the call graph for this function:

◆ processTasks()

CVoid UThreadPrimary::processTasks ( )
inlineoverrideprotectedvirtual

获取批量执行task信息

Implements UThreadBase.

Here is the call graph for this function:

◆ pushTask() [1/2]

CVoid UThreadPrimary::pushTask ( UTask &&  task)
inlineprotected

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

Parameters
task
Returns
Here is the call graph for this function:

◆ pushTask() [2/2]

CVoid UThreadPrimary::pushTask ( UTask &&  task,
CBool  enable,
CBool  lockable 
)
inlineprotected

写入 task信息,是否上锁由入参enable和lockable决定

Parameters
task
enable确认是否有锁
lockabletrue 的时候需要上锁,false 的时候会解锁
Returns
Here is the call graph for this function:

◆ run()

CStatus UThreadPrimary::run ( )
inlinefinalprotectedvirtual

线程执行函数

Returns

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

Implements CObject.

Here is the call graph for this function:

◆ setThreadPoolInfo()

CStatus UThreadPrimary::setThreadPoolInfo ( int  index,
UAtomicQueue< UTask > *  poolTaskQueue,
std::vector< UThreadPrimary * > *  poolThreads,
UThreadPoolConfigPtr  config 
)
inlineprotected

注册线程池相关内容,需要在init之前使用

Parameters
index
poolTaskQueue
poolThreads
config

◆ stealTask() [1/2]

CBool UThreadPrimary::stealTask ( UTaskArrRef  tasks)
inlineprotected

从其他线程盗取一批任务

Parameters
tasks
Returns

在这里,我们对模型进行了简化。实现的思路是: 尝试从邻居主线程(先secondary,再primary)中,获取 x(=max_steal_batch_size_) 个task, 如果从某一个邻居中,获取了 y(<=x) 个task,则也终止steal的流程 且如果如果有一次批量steal成功,就认定成功

Here is the call graph for this function:

◆ stealTask() [2/2]

CBool UThreadPrimary::stealTask ( UTaskRef  task)
inlineprotected

从其他线程窃取一个任务

Parameters
task
Returns

线程池还未初始化完毕的时候,无法进行steal。 确保程序安全运行。

窃取的时候,仅从相邻的primary线程中窃取 待窃取相邻的数量,不能超过默认primary线程数 steal_targets_在init的时候就已经计算好了

从线程中周围的thread中,窃取任务。 如果成功,则返回true,并且执行任务。 steal 的时候,先从第二个队列里偷,从而降低触碰锁的概率

Here is the call graph for this function:

Friends And Related Function Documentation

◆ CAllocator

friend class CAllocator
friend

◆ UThreadPool

friend class UThreadPool
friend

Member Data Documentation

◆ cur_empty_epoch_

CInt UThreadPrimary::cur_empty_epoch_ = 0
private

◆ index_

CInt UThreadPrimary::index_
private

◆ pool_threads_

std::vector<UThreadPrimary *>* UThreadPrimary::pool_threads_
private

◆ primary_queue_

UWorkStealingQueue<UTask> UThreadPrimary::primary_queue_
private

◆ secondary_queue_

UWorkStealingQueue<UTask> UThreadPrimary::secondary_queue_
private

◆ steal_targets_

std::vector<CInt> UThreadPrimary::steal_targets_
private

The documentation for this class was generated from the following file: