Thunderbrook commented on code in PR #149:
URL: https://github.com/apache/brpc-website/pull/149#discussion_r1209215497


##########
content/zh/docs/blogs/sourcecodes/bthread_schedule/index.md:
##########
@@ -0,0 +1,341 @@
+---
+title: "bRPC源码解析·bthread调度执行流程"
+linkTitle: "bRPC源码解析·bthread调度执行流程"
+weight: 3
+date: 2023-05-23
+---
+(作者简介:KIDGINBROOK,在昆仑芯参与训练框架开发工作)
+
+## 整体流程
+task_group负责对bthread的调度执行,一个task_group对应一个pthread,内部有两个执行队列,分别为_rq和_remote_rq,执行队列中存放着待执行的bthread。task_control为全局单例,内部有多个task_group。
+![bthread整体流程](/images/docs/bthread_schedule.PNG)
+
+### 主要接口
+
+#### TaskControl
+TaskControl是一个单例,下面是初始化的过程,主要逻辑即为创建_concurrency个worker(bthread_worker)线程,每个worker执行worker_thread函数
+```c++
+int TaskControl::init(int concurrency) {
+    _concurrency = concurrency;
+    _workers.resize(_concurrency);   
+    for (int i = 0; i < _concurrency; ++i) {
+        const int rc = pthread_create(&_workers[i], NULL, worker_thread, this);
+        ...
+    }
+    ...
+}
+```
+worker_thread的逻辑为通过create_group创建一个TaskGroup 
g,添加到TaskControl中,设置tls_task_group为g,tls_task_group为tls变量,因此只有worker线程的tls_task_group为非null,然后执行TaskGroup的run_main_task函数
+```c++
+void* TaskControl::worker_thread(void* arg) {
+    TaskControl* c = static_cast<TaskControl*>(arg);
+    TaskGroup* g = c->create_group();
+    ...
+    tls_task_group = g;
+    c->_nworkers << 1;
+    g->run_main_task();
+    ...
+}
+```
+
+#### TaskGroup
+TaskGroup对应一个pthread,初始化函数如下,创建rq和remote_rq,都是负责存放待执行bthread的队列,然后创建main_stack和main_tid,main_tid代表主流程对应的bthread
 id,后面会具体讲main_stack和main_tid的作用。TaskMeta为一个bthread的meta信息,如执行函数,参数,local 
storage等,这里会将cur_meta设置为main_tid对应的TaskMeta。
+
+```c++
+int TaskGroup::init(size_t runqueue_capacity) {
+    _rq.init(runqueue_capacity);
+    _remote_rq.init(runqueue_capacity / 2);
+    ContextualStack* stk = get_stack(STACK_TYPE_MAIN, NULL);
+    ...
+    butil::ResourceId<TaskMeta> slot;
+    TaskMeta* m = butil::get_resource<TaskMeta>(&slot);
+    ...
+    m->stop = false;
+    m->interrupted = false;
+    m->about_to_quit = false;
+    m->fn = NULL;
+    m->arg = NULL;
+    m->local_storage = LOCAL_STORAGE_INIT;
+    m->cpuwide_start_ns = butil::cpuwide_time_ns();
+    m->stat = EMPTY_STAT;
+    m->attr = BTHREAD_ATTR_TASKGROUP;
+    m->tid = make_tid(*m->version_butex, slot);
+    m->set_stack(stk);
+
+    _cur_meta = m;
+    _main_tid = m->tid;
+    _main_stack = stk;
+    _last_run_ns = butil::cpuwide_time_ns();
+    return 0;
+}
+
+```
+
+每个worker会一直在while循环中,如果有可执行的bthread,wait_task会返回对应bthread的tid,否则当前worker会阻塞;wait_task(???)的具体逻辑是先去当前task_group的_remote_rq中pop,如果没有,则去其他的task_group的_rq和_remote_rq中pop。

Review Comment:
   已删除;ending_sched的时候会去_rq中pop



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to