Coverage Report

Created: 2026-04-03 12:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/once.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/once.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <atomic>
24
#include <mutex>
25
#include <stdexcept>
26
27
#include "common/exception.h"
28
#include "storage/olap_common.h"
29
30
namespace doris {
31
32
// Utility class for implementing thread-safe call-once semantics.
33
//
34
// call() will return stored result regardless of whether the first invocation
35
// returns a success status or not.
36
//
37
// Example:
38
//   class Resource {
39
//   public:
40
//     Status init() {
41
//       _init_once.call([this] { return _do_init(); });
42
//     }
43
//
44
//     bool is_inited() const {
45
//       return _init_once.has_called() && _init_once.stored_result().ok();
46
//     }
47
//   private:
48
//     Status _do_init() { /* init logic here */ }
49
//     DorisCallOnce<Status> _init_once;
50
//   };
51
template <typename ReturnType>
52
class DorisCallOnce {
53
public:
54
36.6M
    DorisCallOnce() : _has_called(false) {}
_ZN5doris13DorisCallOnceIN2tl8expectedINS_21EncryptionAlgorithmPBENS_6StatusEEEEC2Ev
Line
Count
Source
54
1.06M
    DorisCallOnce() : _has_called(false) {}
_ZN5doris13DorisCallOnceINS_6StatusEEC2Ev
Line
Count
Source
54
35.5M
    DorisCallOnce() : _has_called(false) {}
55
56
    // this method is not exception safe, it will core when exception occurs in
57
    // callback method. I have tested the code https://en.cppreference.com/w/cpp/thread/call_once.
58
    // If the underlying `once_flag` has yet to be invoked, invokes the provided
59
    // lambda and stores its return value. Otherwise, returns the stored Status.
60
    // template <typename Fn>
61
    // ReturnType call(Fn fn) {
62
    //     std::call_once(_once_flag, [this, fn] {
63
    //         _status = fn();
64
    //         _has_called.store(true, std::memory_order_release);
65
    //     });
66
    //     return _status;
67
    // }
68
69
    // If exception occurs in the function, the call flag is set, if user call
70
    // it again, the same exception will be thrown.
71
    // It is different from std::call_once. This is because if a method is called once
72
    // some internal state is changed, it maybe not called again although exception
73
    // occurred.
74
    template <typename Fn>
75
78.1M
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
78.1M
        if (has_called()) {
78
72.5M
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
72.5M
            return _status;
82
72.5M
        }
83
5.63M
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
5.63M
        if (has_called()) {
86
3.29k
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
3.29k
            return _status;
90
3.29k
        }
91
5.63M
        try {
92
5.63M
            _status = fn();
93
5.63M
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
5.60M
        _has_called.store(true, std::memory_order_release);
102
5.60M
        return _status;
103
5.63M
    }
_ZN5doris13DorisCallOnceIN2tl8expectedINS_21EncryptionAlgorithmPBENS_6StatusEEEE4callIZNS_10RowsetMeta24set_encryption_algorithmES3_EUlvE_EES5_T_
Line
Count
Source
75
168k
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
168k
        if (has_called()) {
78
20.1k
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
20.1k
            return _status;
82
20.1k
        }
83
148k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
148k
        if (has_called()) {
86
0
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
0
            return _status;
90
0
        }
91
148k
        try {
92
148k
            _status = fn();
93
148k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
148k
        _has_called.store(true, std::memory_order_release);
102
148k
        return _status;
103
148k
    }
_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v212ColumnReader22set_dict_encoding_typeENS5_16DictEncodingTypeEEUlvE_EES1_T_
Line
Count
Source
75
8.31k
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
8.31k
        if (has_called()) {
78
38
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
38
            return _status;
82
38
        }
83
8.27k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
8.27k
        if (has_called()) {
86
2
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
2
            return _status;
90
2
        }
91
8.27k
        try {
92
8.27k
            _status = fn();
93
8.27k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
8.27k
        _has_called.store(true, std::memory_order_release);
102
8.27k
        return _status;
103
8.27k
    }
beta_rowset.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10BetaRowset20get_segment_num_rowsEPSt6vectorIjSaIjEEbPNS_20OlapReaderStatisticsEE3$_0EES1_T_
Line
Count
Source
75
3.47M
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
3.47M
        if (has_called()) {
78
3.28M
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
3.28M
            return _status;
82
3.28M
        }
83
187k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
187k
        if (has_called()) {
86
917
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
917
            return _status;
90
917
        }
91
186k
        try {
92
186k
            _status = fn();
93
186k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
187k
        _has_called.store(true, std::memory_order_release);
102
187k
        return _status;
103
186k
    }
beta_rowset_reader.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_16BetaRowsetReader19_init_iterator_onceEvE3$_0EES1_T_
Line
Count
Source
75
3.86M
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
3.86M
        if (has_called()) {
78
270k
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
270k
            return _status;
82
270k
        }
83
3.59M
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
3.59M
        if (has_called()) {
86
0
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
0
            return _status;
90
0
        }
91
3.59M
        try {
92
3.59M
            _status = fn();
93
3.59M
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
3.59M
        _has_called.store(true, std::memory_order_release);
102
3.59M
        return _status;
103
3.59M
    }
rowset_meta.cpp:_ZN5doris13DorisCallOnceIN2tl8expectedINS_21EncryptionAlgorithmPBENS_6StatusEEEE4callIZNS_10RowsetMeta2fsEvE3$_0EES5_T_
Line
Count
Source
75
228k
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
228k
        if (has_called()) {
78
170k
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
170k
            return _status;
82
170k
        }
83
58.1k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
58.1k
        if (has_called()) {
86
10
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
10
            return _status;
90
10
        }
91
58.1k
        try {
92
58.1k
            _status = fn();
93
58.1k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
58.1k
        _has_called.store(true, std::memory_order_release);
102
58.1k
        return _status;
103
58.1k
    }
ordinal_page_index.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v218OrdinalIndexReader4loadEbbPNS_20OlapReaderStatisticsEE3$_0EES1_T_
Line
Count
Source
75
2.82M
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
2.82M
        if (has_called()) {
78
1.82M
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
1.82M
            return _status;
82
1.82M
        }
83
1.00M
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
1.00M
        if (has_called()) {
86
816
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
816
            return _status;
90
816
        }
91
1.00M
        try {
92
1.00M
            _status = fn();
93
1.00M
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
1.00M
        _has_called.store(true, std::memory_order_release);
102
1.00M
        return _status;
103
1.00M
    }
zone_map_index.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v218ZoneMapIndexReader4loadEbbPNS_20OlapReaderStatisticsEE3$_0EES1_T_
Line
Count
Source
75
81.6k
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
81.6k
        if (has_called()) {
78
46.6k
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
46.6k
            return _status;
82
46.6k
        }
83
34.9k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
34.9k
        if (has_called()) {
86
529
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
529
            return _status;
90
529
        }
91
34.4k
        try {
92
34.4k
            _status = fn();
93
34.4k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
34.6k
        _has_called.store(true, std::memory_order_release);
102
34.6k
        return _status;
103
34.4k
    }
bloom_filter_index_reader.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v222BloomFilterIndexReader4loadEbbPNS_20OlapReaderStatisticsEE3$_0EES1_T_
Line
Count
Source
75
41.4k
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
41.4k
        if (has_called()) {
78
11
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
11
            return _status;
82
11
        }
83
41.4k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
41.4k
        if (has_called()) {
86
0
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
0
            return _status;
90
0
        }
91
41.4k
        try {
92
41.4k
            _status = fn();
93
41.4k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
41.5k
        _has_called.store(true, std::memory_order_release);
102
41.5k
        return _status;
103
41.4k
    }
segment.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v27Segment21_load_pk_bloom_filterEPNS_20OlapReaderStatisticsEE3$_0EES1_T_
Line
Count
Source
75
3.68M
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
3.68M
        if (has_called()) {
78
3.64M
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
3.64M
            return _status;
82
3.64M
        }
83
41.5k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
41.5k
        if (has_called()) {
86
24
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
24
            return _status;
90
24
        }
91
41.5k
        try {
92
41.5k
            _status = fn();
93
41.5k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
41.2k
        _has_called.store(true, std::memory_order_release);
102
41.2k
        return _status;
103
41.5k
    }
segment.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v27Segment10load_indexEPNS_20OlapReaderStatisticsEE3$_0EES1_T_
Line
Count
Source
75
5.63M
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
5.63M
        if (has_called()) {
78
5.54M
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
5.54M
            return _status;
82
5.54M
        }
83
93.7k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
93.7k
        if (has_called()) {
86
614
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
614
            return _status;
90
614
        }
91
93.1k
        try {
92
93.1k
            _status = fn();
93
93.1k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
95.5k
        _has_called.store(true, std::memory_order_release);
102
95.5k
        return _status;
103
93.1k
    }
segment.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v27Segment24_create_column_meta_onceEPNS_20OlapReaderStatisticsEE3$_0EES1_T_
Line
Count
Source
75
57.7M
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
57.7M
        if (has_called()) {
78
57.5M
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
57.5M
            return _status;
82
57.5M
        }
83
139k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
139k
        if (has_called()) {
86
380
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
380
            return _status;
90
380
        }
91
138k
        try {
92
138k
            _status = fn();
93
138k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
101k
        _has_called.store(true, std::memory_order_release);
102
101k
        return _status;
103
138k
    }
segment.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v27Segment18new_index_iteratorERKNS_12TabletColumnEPKNS_11TabletIndexERKNS_18StorageReadOptionsEPSt10unique_ptrINS4_13IndexIteratorESt14default_deleteISG_EEE3$_0EES1_T_
Line
Count
Source
75
78.9k
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
78.9k
        if (has_called()) {
78
73.6k
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
73.6k
            return _status;
82
73.6k
        }
83
5.31k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
5.31k
        if (has_called()) {
86
6
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
6
            return _status;
90
6
        }
91
5.30k
        try {
92
5.30k
            _status = fn();
93
5.30k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
5.30k
        _has_called.store(true, std::memory_order_release);
102
5.30k
        return _status;
103
5.30k
    }
variant_external_meta_reader.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v225VariantExternalMetaReader13load_all_onceEPNS_14SubcolumnsTreeINS4_13SubcolumnMetaELb1EEEPNS4_17VariantStatisticsEE3$_0EES1_T_
Line
Count
Source
75
28.5k
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
28.5k
        if (has_called()) {
78
25.9k
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
25.9k
            return _status;
82
25.9k
        }
83
2.61k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
2.61k
        if (has_called()) {
86
0
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
0
            return _status;
90
0
        }
91
2.61k
        try {
92
2.61k
            _status = fn();
93
2.61k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
2.61k
        _has_called.store(true, std::memory_order_release);
102
2.61k
        return _status;
103
2.61k
    }
tablet.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_6Tablet4initEvE3$_0EES1_T_
Line
Count
Source
75
283k
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
283k
        if (has_called()) {
78
227
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
227
            return _status;
82
227
        }
83
283k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
283k
        if (has_called()) {
86
0
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
0
            return _status;
90
0
        }
91
283k
        try {
92
283k
            _status = fn();
93
283k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
283k
        _has_called.store(true, std::memory_order_release);
102
283k
        return _status;
103
283k
    }
load_path_mgr.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_11LoadPathMgr12allocate_dirERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_PSA_lE3$_0EES1_T_
Line
Count
Source
75
24
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
24
        if (has_called()) {
78
20
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
20
            return _status;
82
20
        }
83
4
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
4
        if (has_called()) {
86
0
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
0
            return _status;
90
0
        }
91
4
        try {
92
4
            _status = fn();
93
4
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
4
        _has_called.store(true, std::memory_order_release);
102
4
        return _status;
103
4
    }
ann_index_reader.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v214AnnIndexReader10load_indexEPNS_2io9IOContextEE3$_0EES1_T_
Line
Count
Source
75
102
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
102
        if (has_called()) {
78
62
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
62
            return _status;
82
62
        }
83
40
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
40
        if (has_called()) {
86
0
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
0
            return _status;
90
0
        }
91
40
        try {
92
40
            _status = fn();
93
40
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
38
        _has_called.store(true, std::memory_order_release);
102
38
        return _status;
103
40
    }
104
105
    // Has to pay attention to memory order
106
    // see https://en.cppreference.com/w/cpp/atomic/memory_order
107
    // Return whether `call` has been invoked or not.
108
139M
    bool has_called() const {
109
        // std::memory_order_acquire here and std::memory_order_release in
110
        // init(), taken together, mean that threads can safely synchronize on
111
        // _has_called.
112
139M
        return _has_called.load(std::memory_order_acquire);
113
139M
    }
_ZNK5doris13DorisCallOnceIN2tl8expectedINS_21EncryptionAlgorithmPBENS_6StatusEEEE10has_calledEv
Line
Count
Source
108
603k
    bool has_called() const {
109
        // std::memory_order_acquire here and std::memory_order_release in
110
        // init(), taken together, mean that threads can safely synchronize on
111
        // _has_called.
112
603k
        return _has_called.load(std::memory_order_acquire);
113
603k
    }
_ZNK5doris13DorisCallOnceINS_6StatusEE10has_calledEv
Line
Count
Source
108
138M
    bool has_called() const {
109
        // std::memory_order_acquire here and std::memory_order_release in
110
        // init(), taken together, mean that threads can safely synchronize on
111
        // _has_called.
112
138M
        return _has_called.load(std::memory_order_acquire);
113
138M
    }
114
115
    // Return the stored result. The result is only meaningful when `has_called() == true`.
116
26.2M
    ReturnType stored_result() const {
117
26.2M
        if (!has_called()) {
118
            // Could not return status if the method not called.
119
0
            throw doris::Exception(doris::ErrorCode::INTERNAL_ERROR,
120
0
                                   "calling stored_result while has not been called");
121
0
        }
122
26.2M
        if (_eptr) {
123
1
            std::rethrow_exception(_eptr);
124
1
        }
125
26.2M
        return _status;
126
26.2M
    }
127
128
private:
129
    std::atomic<bool> _has_called;
130
    // std::once_flag _once_flag;
131
    std::mutex _flag_lock;
132
    std::exception_ptr _eptr;
133
    ReturnType _status;
134
};
135
136
} // namespace doris