Coverage Report

Created: 2025-09-16 13:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/thread.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/thread.h
19
// and modified by Doris
20
21
#pragma once
22
#include <butil/macros.h>
23
#include <pthread.h>
24
#include <stdint.h>
25
26
#include <functional>
27
#include <string>
28
#include <utility>
29
30
#include "common/status.h"
31
#include "util/countdown_latch.h"
32
33
namespace doris {
34
class WebPageHandler;
35
36
class Thread {
37
public:
38
    template <class F>
39
    static Status create(const std::string& category, const std::string& name, const F& f,
40
423
                         std::shared_ptr<Thread>* holder) {
41
423
        return start_thread(category, name, f, holder);
42
423
    }
task_worker_pool.cpp:_ZN5doris6Thread6createIZNS_19PriorTaskWorkerPoolC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiiSt8functionIFvRKNS_17TAgentTaskRequestEEEE3$_0EENS_6StatusESA_SA_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
37
                         std::shared_ptr<Thread>* holder) {
41
37
        return start_thread(category, name, f, holder);
42
37
    }
task_worker_pool.cpp:_ZN5doris6Thread6createIZNS_19PriorTaskWorkerPoolC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiiSt8functionIFvRKNS_17TAgentTaskRequestEEEE3$_1EENS_6StatusESA_SA_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
37
                         std::shared_ptr<Thread>* holder) {
41
37
        return start_thread(category, name, f, holder);
42
37
    }
task_worker_pool.cpp:_ZN5doris6Thread6createIZNS_12ReportWorkerC1ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPKNS_11ClusterInfoEiSt8functionIFvvEEE3$_0EENS_6StatusERKS8_SI_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
29
                         std::shared_ptr<Thread>* holder) {
41
29
        return start_thread(category, name, f, holder);
42
29
    }
daemon.cpp:_ZN5doris6Thread6createIZNS_6Daemon5startEvE3$_0EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
daemon.cpp:_ZN5doris6Thread6createIZNS_6Daemon5startEvE3$_1EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
daemon.cpp:_ZN5doris6Thread6createIZNS_6Daemon5startEvE3$_2EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
daemon.cpp:_ZN5doris6Thread6createIZNS_6Daemon5startEvE3$_3EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
daemon.cpp:_ZN5doris6Thread6createIZNS_6Daemon5startEvE3$_4EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
daemon.cpp:_ZN5doris6Thread6createIZNS_6Daemon5startEvE3$_5EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
daemon.cpp:_ZN5doris6Thread6createIZNS_6Daemon5startEvE3$_6EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
daemon.cpp:_ZN5doris6Thread6createIZNS_6Daemon5startEvE3$_7EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
daemon.cpp:_ZN5doris6Thread6createIZNS_6Daemon5startEvE3$_8EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
4
                         std::shared_ptr<Thread>* holder) {
41
4
        return start_thread(category, name, f, holder);
42
4
    }
daemon.cpp:_ZN5doris6Thread6createIZNS_6Daemon5startEvE3$_9EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_0EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
6
                         std::shared_ptr<Thread>* holder) {
41
6
        return start_thread(category, name, f, holder);
42
6
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_1EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
6
                         std::shared_ptr<Thread>* holder) {
41
6
        return start_thread(category, name, f, holder);
42
6
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_2EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
6
                         std::shared_ptr<Thread>* holder) {
41
6
        return start_thread(category, name, f, holder);
42
6
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_3EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
6
                         std::shared_ptr<Thread>* holder) {
41
6
        return start_thread(category, name, f, holder);
42
6
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_4EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
6
                         std::shared_ptr<Thread>* holder) {
41
6
        return start_thread(category, name, f, holder);
42
6
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_5EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
6
                         std::shared_ptr<Thread>* holder) {
41
6
        return start_thread(category, name, f, holder);
42
6
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_6EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
6
                         std::shared_ptr<Thread>* holder) {
41
6
        return start_thread(category, name, f, holder);
42
6
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_7EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
6
                         std::shared_ptr<Thread>* holder) {
41
6
        return start_thread(category, name, f, holder);
42
6
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_8EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
10
                         std::shared_ptr<Thread>* holder) {
41
10
        return start_thread(category, name, f, holder);
42
10
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_9EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
6
                         std::shared_ptr<Thread>* holder) {
41
6
        return start_thread(category, name, f, holder);
42
6
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE4$_10EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
6
                         std::shared_ptr<Thread>* holder) {
41
6
        return start_thread(category, name, f, holder);
42
6
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE4$_11EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
6
                         std::shared_ptr<Thread>* holder) {
41
6
        return start_thread(category, name, f, holder);
42
6
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE4$_12EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
6
                         std::shared_ptr<Thread>* holder) {
41
6
        return start_thread(category, name, f, holder);
42
6
    }
olap_server.cpp:_ZN5doris6Thread6createIZNS_13StorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE4$_13EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
6
                         std::shared_ptr<Thread>* holder) {
41
6
        return start_thread(category, name, f, holder);
42
6
    }
wal_manager.cpp:_ZN5doris6Thread6createIZNS_10WalManager4initEvE3$_0EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
wal_manager.cpp:_ZN5doris6Thread6createIZNS_10WalManager19_init_wal_dirs_infoEvE3$_0EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
broker_mgr.cpp:_ZN5doris6Thread6createIZNS_9BrokerMgrC1EPNS_7ExecEnvEE3$_1EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESE_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
external_scan_context_mgr.cpp:_ZN5doris6Thread6createIZNS_22ExternalScanContextMgrC1EPNS_7ExecEnvEE3$_1EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESE_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
11
                         std::shared_ptr<Thread>* holder) {
41
11
        return start_thread(category, name, f, holder);
42
11
    }
fragment_mgr.cpp:_ZN5doris6Thread6createIZNS_11FragmentMgrC1EPNS_7ExecEnvEE3$_0EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESE_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
14
                         std::shared_ptr<Thread>* holder) {
41
14
        return start_thread(category, name, f, holder);
42
14
    }
load_channel_mgr.cpp:_ZN5doris6Thread6createIZNS_14LoadChannelMgr16_start_bg_workerEvE3$_0EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
load_path_mgr.cpp:_ZN5doris6Thread6createIZNS_11LoadPathMgr4initEvE3$_0EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
result_buffer_mgr.cpp:_ZN5doris6Thread6createIZNS_15ResultBufferMgr4initEvE3$_0EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
data_consumer_pool.cpp:_ZN5doris6Thread6createIZNS_16DataConsumerPool15start_bg_workerEvE3$_0EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
8
                         std::shared_ptr<Thread>* holder) {
41
8
        return start_thread(category, name, f, holder);
42
8
    }
workload_sched_policy_mgr.cpp:_ZN5doris6Thread6createIZNS_22WorkloadSchedPolicyMgr5startEPNS_7ExecEnvEE3$_0EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESE_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
7
                         std::shared_ptr<Thread>* holder) {
41
7
        return start_thread(category, name, f, holder);
42
7
    }
spill_stream_manager.cpp:_ZN5doris6Thread6createIZNS_10vectorized18SpillStreamManager4initEvE3$_0EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESD_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
75
                         std::shared_ptr<Thread>* holder) {
41
75
        return start_thread(category, name, f, holder);
42
75
    }
cloud_storage_engine.cpp:_ZN5doris6Thread6createIZNS_18CloudStorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_0EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
1
                         std::shared_ptr<Thread>* holder) {
41
1
        return start_thread(category, name, f, holder);
42
1
    }
cloud_storage_engine.cpp:_ZN5doris6Thread6createIZNS_18CloudStorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_1EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
1
                         std::shared_ptr<Thread>* holder) {
41
1
        return start_thread(category, name, f, holder);
42
1
    }
cloud_storage_engine.cpp:_ZN5doris6Thread6createIZNS_18CloudStorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_2EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
1
                         std::shared_ptr<Thread>* holder) {
41
1
        return start_thread(category, name, f, holder);
42
1
    }
cloud_storage_engine.cpp:_ZN5doris6Thread6createIZNS_18CloudStorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_3EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
1
                         std::shared_ptr<Thread>* holder) {
41
1
        return start_thread(category, name, f, holder);
42
1
    }
cloud_storage_engine.cpp:_ZN5doris6Thread6createIZNS_18CloudStorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_4EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
1
                         std::shared_ptr<Thread>* holder) {
41
1
        return start_thread(category, name, f, holder);
42
1
    }
cloud_storage_engine.cpp:_ZN5doris6Thread6createIZNS_18CloudStorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_5EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
1
                         std::shared_ptr<Thread>* holder) {
41
1
        return start_thread(category, name, f, holder);
42
1
    }
cloud_storage_engine.cpp:_ZN5doris6Thread6createIZNS_18CloudStorageEngine16start_bg_threadsESt10shared_ptrINS_13WorkloadGroupEEE3$_6EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_PS3_IS0_E
Line
Count
Source
40
1
                         std::shared_ptr<Thread>* holder) {
41
1
        return start_thread(category, name, f, holder);
42
1
    }
cloud_txn_delete_bitmap_cache.cpp:_ZN5doris6Thread6createIZNS_25CloudTxnDeleteBitmapCache4initEvE3$_0EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_RKT_PSt10shared_ptrIS0_E
Line
Count
Source
40
1
                         std::shared_ptr<Thread>* holder) {
41
1
        return start_thread(category, name, f, holder);
42
1
    }
43
44
    template <class F, class A1>
45
    static Status create(const std::string& category, const std::string& name, const F& f,
46
31.4k
                         const A1& a1, std::shared_ptr<Thread>* holder) {
47
31.4k
        return start_thread(category, name, std::bind(f, a1), holder);
48
31.4k
    }
Unexecuted instantiation: _ZN5doris6Thread6createIMNS_2io15FileHandleCacheEFvvEPS3_EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_RKT0_PSt10shared_ptrIS0_E
_ZN5doris6Thread6createIMNS_10ThreadPoolEFvvEPS2_EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESE_RKT_RKT0_PSt10shared_ptrIS0_E
Line
Count
Source
46
31.1k
                         const A1& a1, std::shared_ptr<Thread>* holder) {
47
31.1k
        return start_thread(category, name, std::bind(f, a1), holder);
48
31.1k
    }
_ZN5doris6Thread6createIMNS_10vectorized23TimeSharingTaskExecutorEFvvEPS3_EENS_6StatusERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESF_RKT_RKT0_PSt10shared_ptrIS0_E
Line
Count
Source
46
350
                         const A1& a1, std::shared_ptr<Thread>* holder) {
47
350
        return start_thread(category, name, std::bind(f, a1), holder);
48
350
    }
49
50
    template <class F, class A1, class A2>
51
    static Status create(const std::string& category, const std::string& name, const F& f,
52
                         const A1& a1, const A2& a2, std::shared_ptr<Thread>* holder) {
53
        return start_thread(category, name, std::bind(f, a1, a2), holder);
54
    }
55
56
    template <class F, class A1, class A2, class A3>
57
    static Status create(const std::string& category, const std::string& name, const F& f,
58
                         const A1& a1, const A2& a2, const A3& a3,
59
                         std::shared_ptr<Thread>* holder) {
60
        return start_thread(category, name, std::bind(f, a1, a2, a3), holder);
61
    }
62
63
    template <class F, class A1, class A2, class A3, class A4>
64
    static Status create(const std::string& category, const std::string& name, const F& f,
65
                         const A1& a1, const A2& a2, const A3& a3, const A4& a4,
66
                         std::shared_ptr<Thread>* holder) {
67
        return start_thread(category, name, std::bind(f, a1, a2, a3, a4), holder);
68
    }
69
70
    static void set_self_name(const std::string& name);
71
72
#ifndef __APPLE__
73
    static void set_idle_sched();
74
75
    static void set_thread_nice_value();
76
#endif
77
78
    ~Thread();
79
80
    // Blocks until this thread finishes execution. Once this method returns, the thread
81
    // will be unregistered with the ThreadMgr and will not appear in the debug UI.
82
    void join();
83
84
    // The thread ID assigned to this thread by the operating system. If the thread
85
    // has not yet started running, returns INVALID_TID.
86
    //
87
    // NOTE: this may block for a short amount of time if the thread has just been
88
    // started.
89
    int64_t tid() const;
90
91
    // Returns the thread's pthread ID.
92
    pthread_t pthread_id() const;
93
94
    const std::string& name() const;
95
    const std::string& category() const;
96
    std::string to_string() const;
97
98
    // The current thread of execution, or nullptr if the current thread isn't a doris::Thread.
99
    // This call is signal-safe.
100
    static Thread* current_thread();
101
102
    // Returns a unique, stable identifier for this thread. Note that this is a static
103
    // method and thus can be used on any thread, including the main thread of the
104
    // process.
105
    //
106
    // In general, this should be used when a value is required that is unique to
107
    // a thread and must work on any thread including the main process thread.
108
    //
109
    // NOTE: this is _not_ the TID, but rather a unique value assigned by the
110
    // thread implementation. So, this value should not be presented to the user
111
    // in log messages, etc.
112
    static int64_t unique_thread_id();
113
114
    // Returns the system thread ID (tid on Linux) for the current thread. Note
115
    // that this is a static method and thus can be used from any thread,
116
    // including the main thread of the process. This is in contrast to
117
    // Thread::tid(), which only works on doris::Threads.
118
    //
119
    // Thread::tid() will return the same value, but the value is cached in the
120
    // Thread object, so will be faster to call.
121
    //
122
    // Thread::unique_thread_id() (or Thread::tid()) should be preferred for
123
    // performance sensitive code, however it is only guaranteed to return a
124
    // unique and stable thread ID, not necessarily the system thread ID.
125
    static int64_t current_thread_id();
126
127
    // User function to be executed by this thread.
128
    using ThreadFunctor = std::function<void()>;
129
    Thread(std::string category, std::string name, ThreadFunctor functor)
130
32.9k
            : _thread(0),
131
32.9k
              _tid(INVALID_TID),
132
32.9k
              _functor(std::move(functor)),
133
32.9k
              _category(std::move(category)),
134
32.9k
              _name(std::move(name)),
135
32.9k
              _done(1),
136
32.9k
              _joinable(false) {}
137
138
private:
139
    friend class ThreadJoiner;
140
141
    enum {
142
        INVALID_TID = -1,
143
        PARENT_WAITING_TID = -2,
144
    };
145
146
    // Library-specific thread ID.
147
    pthread_t _thread;
148
149
    // OS-specific thread ID. Once the constructor finishes start_thread(),
150
    // guaranteed to be set either to a non-negative integer, or to INVALID_TID.
151
    //
152
    // The tid_ member goes through the following states:
153
    // 1. INVALID_TID: the thread has not been started, or has already exited.
154
    // 2. PARENT_WAITING_TID: the parent has started the thread, but the
155
    //    thread has not yet begun running. Therefore the TID is not yet known
156
    //    but it will be set once the thread starts.
157
    // 3. <positive value>: the thread is running.
158
    std::atomic<int64_t> _tid;
159
160
    const ThreadFunctor _functor;
161
162
    const std::string _category;
163
    const std::string _name;
164
165
    // Joiners wait on this latch to be notified if the thread is done.
166
    //
167
    // Note that Joiners must additionally pthread_join(), otherwise certain
168
    // resources that callers expect to be destroyed (like TLS) may still be
169
    // alive when a Joiner finishes.
170
    CountDownLatch _done;
171
172
    bool _joinable;
173
174
    // Thread local pointer to the current thread of execution. Will be nullptr if the current
175
    // thread is not a Thread.
176
    static __thread Thread* _tls;
177
178
    // keep self do not released when thread is running
179
    // release it after thread joined
180
    std::shared_ptr<Thread> _shared_self;
181
182
    // Wait for the running thread to publish its tid.
183
    int64_t wait_for_tid() const;
184
185
    // Starts the thread running supervise_thread(), and returns once that thread has
186
    // initialised and its TID has been read. Waits for notification from the started
187
    // thread that initialisation is complete before returning. On success, stores a
188
    // reference to the thread in holder.
189
    static Status start_thread(const std::string& category, const std::string& name,
190
                               const ThreadFunctor& functor, std::shared_ptr<Thread>* holder);
191
192
    // Wrapper for the user-supplied function. Invoked from the new thread,
193
    // with the Thread as its only argument. Executes _functor, but before
194
    // doing so registers with the global ThreadMgr and reads the thread's
195
    // system ID. After _functor terminates, unregisters with the ThreadMgr.
196
    // Always returns nullptr.
197
    //
198
    // supervise_thread() notifies start_thread() when thread initialisation is
199
    // completed via the _tid, which is set to the new thread's system ID.
200
    // By that point in time supervise_thread() has also taken a reference to
201
    // the Thread object, allowing it to safely refer to it even after the
202
    // caller drops its reference.
203
    //
204
    // Additionally, start_thread() notifies supervise_thread() when the actual
205
    // Thread object has been assigned (supervise_thread() is spinning during
206
    // this time). Without this, the new thread may reference the actual
207
    // Thread object before it has been assigned by start_thread(). See
208
    // KUDU-11 for more details.
209
    static void* supervise_thread(void* arg);
210
211
    // Invoked when the user-supplied function finishes or in the case of an
212
    // abrupt exit (i.e. pthread_exit()). Cleans up after supervise_thread().
213
    static void finish_thread(void* arg);
214
215
    static void init_threadmgr();
216
};
217
218
// Utility to join on a thread, printing warning messages if it
219
// takes too long. For example:
220
//
221
//   ThreadJoiner(&my_thread, "processing thread")
222
//     .warn_after_ms(1000)
223
//     .warn_every_ms(5000)
224
//     .Join();
225
//
226
// TODO: would be nice to offer a way to use ptrace() or signals to
227
// dump the stack trace of the thread we're trying to join on if it
228
// gets stuck. But, after looking for 20 minutes or so, it seems
229
// pretty complicated to get right.
230
class ThreadJoiner {
231
public:
232
    explicit ThreadJoiner(Thread* thread);
233
234
    // Start emitting warnings after this many milliseconds.
235
    //
236
    // Default: 1000 ms.
237
    ThreadJoiner& warn_after_ms(int ms);
238
239
    // After the warnings after started, emit another warning at the
240
    // given interval.
241
    //
242
    // Default: 1000 ms.
243
    ThreadJoiner& warn_every_ms(int ms);
244
245
    // If the thread has not stopped after this number of milliseconds, give up
246
    // joining on it and return Status::Aborted.
247
    //
248
    // -1 (the default) means to wait forever trying to join.
249
    ThreadJoiner& give_up_after_ms(int ms);
250
251
    // Join the thread, subject to the above parameters. If the thread joining
252
    // fails for any reason, returns RuntimeError. If it times out, returns
253
    // Aborted.
254
    Status join();
255
256
private:
257
    enum {
258
        kDefaultWarnAfterMs = 1000,
259
        kDefaultWarnEveryMs = 1000,
260
        kDefaultGiveUpAfterMs = -1 // forever
261
    };
262
263
    Thread* _thread = nullptr;
264
265
    int _warn_after_ms;
266
    int _warn_every_ms;
267
    int _give_up_after_ms;
268
269
    DISALLOW_COPY_AND_ASSIGN(ThreadJoiner);
270
};
271
272
// Registers /threadz with the debug webserver.
273
void register_thread_display_page(WebPageHandler* web_page_handler);
274
275
} //namespace doris