Coverage Report

Created: 2026-10-29 14:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/bthread_utils.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
18
#pragma once
19
20
#include <bthread/bthread.h>
21
22
#include <functional>
23
#include <memory>
24
25
#include "runtime/thread_context.h"
26
27
namespace doris {
28
29
// Launch a callable in a background bthread (fire-and-forget).
30
// The callable is heap-allocated and deleted after execution.
31
// Returns true on success, false if bthread creation failed (callable is still deleted).
32
//
33
// init_thread_ctx: when true, initialises the bthread's thread-local ThreadContext via
34
// ScopedInitThreadContext before invoking fn.  Set this when the bthread needs memory-tracker
35
// or workload-group context (e.g. attaches an AttachTask inside fn).  Defaults to false so
36
// existing call-sites that do not need thread-context initialisation are unaffected.
37
template <typename Fn>
38
24
bool start_bthread(Fn&& fn, bool init_thread_ctx = false, const bthread_attr_t* attr = nullptr) {
39
24
    struct Args {
40
24
        std::function<void()> fn;
41
24
        bool init_thread_ctx;
42
24
    };
43
24
    auto args = std::make_unique<Args>(Args {std::forward<Fn>(fn), init_thread_ctx});
44
24
    auto entry = [](void* arg) -> void* {
45
        // Reclaim ownership so Args is deleted when this scope exits.
46
24
        auto a = std::unique_ptr<Args>(reinterpret_cast<Args*>(arg));
47
24
        std::optional<ScopedInitThreadContext> scoped;
48
24
        if (a->init_thread_ctx) {
49
21
            scoped.emplace();
50
21
        }
51
24
        a->fn();
52
24
        return nullptr;
53
24
    };
unity_0_cxx.cxx:_ZZN5doris13start_bthreadIZNKS_2io22CachedRemoteFileReader38_fetch_peer_candidates_unless_cooldownEvE3$_0EEbOT_bPK14bthread_attr_tENKUlPvE_clES9_
Line
Count
Source
44
3
    auto entry = [](void* arg) -> void* {
45
        // Reclaim ownership so Args is deleted when this scope exits.
46
3
        auto a = std::unique_ptr<Args>(reinterpret_cast<Args*>(arg));
47
3
        std::optional<ScopedInitThreadContext> scoped;
48
3
        if (a->init_thread_ctx) {
49
0
            scoped.emplace();
50
0
        }
51
3
        a->fn();
52
3
        return nullptr;
53
3
    };
unity_0_cxx.cxx:_ZZN5doris13start_bthreadIZNS_2io22CachedRemoteFileReader20_execute_winner_raceERKSt6vectorINS1_9FileBlock5RangeESaIS5_EEmmRSt10unique_ptrIA_cSt14default_deleteISB_EEPNS1_15PeerFetchResultERNS1_14ReadStatisticsEPKNS1_9IOContextERKS3_INS_13PeerCandidateESaISN_EElE3$_0EEbOT_bPK14bthread_attr_tENKUlPvE_clESY_
Line
Count
Source
44
21
    auto entry = [](void* arg) -> void* {
45
        // Reclaim ownership so Args is deleted when this scope exits.
46
21
        auto a = std::unique_ptr<Args>(reinterpret_cast<Args*>(arg));
47
21
        std::optional<ScopedInitThreadContext> scoped;
48
21
        if (a->init_thread_ctx) {
49
21
            scoped.emplace();
50
21
        }
51
21
        a->fn();
52
21
        return nullptr;
53
21
    };
54
24
    bthread_t tid;
55
24
    if (bthread_start_background(&tid, attr, entry, args.get()) != 0) {
56
0
        return false; // args unique_ptr destructs here, no leak
57
0
    }
58
24
    args.release(); // bthread entry now owns the pointer
59
24
    return true;
60
24
}
unity_0_cxx.cxx:_ZN5doris13start_bthreadIZNKS_2io22CachedRemoteFileReader38_fetch_peer_candidates_unless_cooldownEvE3$_0EEbOT_bPK14bthread_attr_t
Line
Count
Source
38
3
bool start_bthread(Fn&& fn, bool init_thread_ctx = false, const bthread_attr_t* attr = nullptr) {
39
3
    struct Args {
40
3
        std::function<void()> fn;
41
3
        bool init_thread_ctx;
42
3
    };
43
3
    auto args = std::make_unique<Args>(Args {std::forward<Fn>(fn), init_thread_ctx});
44
3
    auto entry = [](void* arg) -> void* {
45
        // Reclaim ownership so Args is deleted when this scope exits.
46
3
        auto a = std::unique_ptr<Args>(reinterpret_cast<Args*>(arg));
47
3
        std::optional<ScopedInitThreadContext> scoped;
48
3
        if (a->init_thread_ctx) {
49
3
            scoped.emplace();
50
3
        }
51
3
        a->fn();
52
3
        return nullptr;
53
3
    };
54
3
    bthread_t tid;
55
3
    if (bthread_start_background(&tid, attr, entry, args.get()) != 0) {
56
0
        return false; // args unique_ptr destructs here, no leak
57
0
    }
58
3
    args.release(); // bthread entry now owns the pointer
59
3
    return true;
60
3
}
unity_0_cxx.cxx:_ZN5doris13start_bthreadIZNS_2io22CachedRemoteFileReader20_execute_winner_raceERKSt6vectorINS1_9FileBlock5RangeESaIS5_EEmmRSt10unique_ptrIA_cSt14default_deleteISB_EEPNS1_15PeerFetchResultERNS1_14ReadStatisticsEPKNS1_9IOContextERKS3_INS_13PeerCandidateESaISN_EElE3$_0EEbOT_bPK14bthread_attr_t
Line
Count
Source
38
21
bool start_bthread(Fn&& fn, bool init_thread_ctx = false, const bthread_attr_t* attr = nullptr) {
39
21
    struct Args {
40
21
        std::function<void()> fn;
41
21
        bool init_thread_ctx;
42
21
    };
43
21
    auto args = std::make_unique<Args>(Args {std::forward<Fn>(fn), init_thread_ctx});
44
21
    auto entry = [](void* arg) -> void* {
45
        // Reclaim ownership so Args is deleted when this scope exits.
46
21
        auto a = std::unique_ptr<Args>(reinterpret_cast<Args*>(arg));
47
21
        std::optional<ScopedInitThreadContext> scoped;
48
21
        if (a->init_thread_ctx) {
49
21
            scoped.emplace();
50
21
        }
51
21
        a->fn();
52
21
        return nullptr;
53
21
    };
54
21
    bthread_t tid;
55
21
    if (bthread_start_background(&tid, attr, entry, args.get()) != 0) {
56
0
        return false; // args unique_ptr destructs here, no leak
57
0
    }
58
21
    args.release(); // bthread entry now owns the pointer
59
21
    return true;
60
21
}
61
62
} // namespace doris