/root/doris/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 "olap/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 | 121M | DorisCallOnce() : _has_called(false) {}_ZN5doris13DorisCallOnceIN2tl8expectedINS_21EncryptionAlgorithmPBENS_6StatusEEEEC2Ev Line | Count | Source | 54 | 923k | DorisCallOnce() : _has_called(false) {} |
_ZN5doris13DorisCallOnceINS_6StatusEEC2Ev Line | Count | Source | 54 | 121M | 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 | 370M | ReturnType call(Fn fn) { |
76 | | // Avoid lock to improve performance |
77 | 370M | if (has_called()) { |
78 | 330M | if (_eptr) { |
79 | 0 | std::rethrow_exception(_eptr); |
80 | 0 | } |
81 | 330M | return _status; |
82 | 330M | } |
83 | 39.2M | std::lock_guard l(_flag_lock); |
84 | | // should check again because maybe another thread call successfully. |
85 | 39.2M | if (has_called()) { |
86 | 1.80k | if (_eptr) { |
87 | 0 | std::rethrow_exception(_eptr); |
88 | 0 | } |
89 | 1.80k | return _status; |
90 | 1.80k | } |
91 | 39.2M | try { |
92 | 39.2M | _status = fn(); |
93 | 39.2M | } 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 | 39.0M | _has_called.store(true, std::memory_order_release); |
102 | 39.0M | return _status; |
103 | 39.2M | } _ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v212ColumnReader22set_dict_encoding_typeENS5_16DictEncodingTypeEEUlvE_EES1_T_ Line | Count | Source | 75 | 5.89k | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 5.89k | if (has_called()) { | 78 | 111 | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 111 | return _status; | 82 | 111 | } | 83 | 5.78k | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 5.78k | if (has_called()) { | 86 | 1 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 1 | return _status; | 90 | 1 | } | 91 | 5.78k | try { | 92 | 5.78k | _status = fn(); | 93 | 5.78k | } 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.78k | _has_called.store(true, std::memory_order_release); | 102 | 5.78k | return _status; | 103 | 5.78k | } |
beta_rowset.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10BetaRowset20get_segment_num_rowsEPSt6vectorIjSaIjEEPNS_20OlapReaderStatisticsEE3$_0EES1_T_ Line | Count | Source | 75 | 33.4M | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 33.4M | if (has_called()) { | 78 | 33.3M | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 33.3M | return _status; | 82 | 33.3M | } | 83 | 45.6k | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 45.6k | if (has_called()) { | 86 | 593 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 593 | return _status; | 90 | 593 | } | 91 | 45.0k | try { | 92 | 45.0k | _status = fn(); | 93 | 45.0k | } 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 | 54.8k | _has_called.store(true, std::memory_order_release); | 102 | 54.8k | return _status; | 103 | 45.0k | } |
beta_rowset_reader.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_16BetaRowsetReader19_init_iterator_onceEvE3$_0EES1_T_ Line | Count | Source | 75 | 38.1M | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 38.1M | if (has_called()) { | 78 | 103k | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 103k | return _status; | 82 | 103k | } | 83 | 38.0M | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 38.0M | if (has_called()) { | 86 | 0 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 0 | return _status; | 90 | 0 | } | 91 | 38.0M | try { | 92 | 38.0M | _status = fn(); | 93 | 38.0M | } 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.1M | _has_called.store(true, std::memory_order_release); | 102 | 38.1M | return _status; | 103 | 38.0M | } |
rowset_meta.cpp:_ZN5doris13DorisCallOnceIN2tl8expectedINS_21EncryptionAlgorithmPBENS_6StatusEEEE4callIZNS_10RowsetMeta2fsEvE3$_0EES5_T_ Line | Count | Source | 75 | 118k | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 118k | if (has_called()) { | 78 | 74.3k | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 74.3k | return _status; | 82 | 74.3k | } | 83 | 44.0k | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 44.0k | if (has_called()) { | 86 | 178 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 178 | return _status; | 90 | 178 | } | 91 | 43.8k | try { | 92 | 43.8k | _status = fn(); | 93 | 43.8k | } 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 | 43.8k | _has_called.store(true, std::memory_order_release); | 102 | 43.8k | return _status; | 103 | 43.8k | } |
segment.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v27Segment21_load_pk_bloom_filterEPNS_20OlapReaderStatisticsEE3$_0EES1_T_ Line | Count | Source | 75 | 4.05M | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 4.05M | if (has_called()) { | 78 | 4.04M | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 4.04M | return _status; | 82 | 4.04M | } | 83 | 15.0k | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 15.0k | if (has_called()) { | 86 | 16 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 16 | return _status; | 90 | 16 | } | 91 | 15.0k | try { | 92 | 15.0k | _status = fn(); | 93 | 15.0k | } 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 | 13.1k | _has_called.store(true, std::memory_order_release); | 102 | 13.1k | return _status; | 103 | 15.0k | } |
segment.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v27Segment10load_indexEPNS_20OlapReaderStatisticsEE3$_0EES1_T_ Line | Count | Source | 75 | 12.3M | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 12.3M | if (has_called()) { | 78 | 12.2M | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 12.2M | return _status; | 82 | 12.2M | } | 83 | 45.0k | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 45.0k | if (has_called()) { | 86 | 75 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 75 | return _status; | 90 | 75 | } | 91 | 45.0k | try { | 92 | 45.0k | _status = fn(); | 93 | 45.0k | } 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 | 55.7k | _has_called.store(true, std::memory_order_release); | 102 | 55.7k | return _status; | 103 | 45.0k | } |
segment.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v27Segment24_create_column_meta_onceEPNS_20OlapReaderStatisticsEE3$_0EES1_T_ Line | Count | Source | 75 | 281M | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 281M | if (has_called()) { | 78 | 280M | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 280M | return _status; | 82 | 280M | } | 83 | 392k | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 392k | if (has_called()) { | 86 | 10 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 10 | return _status; | 90 | 10 | } | 91 | 392k | try { | 92 | 392k | _status = fn(); | 93 | 392k | } 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 | 77.1k | _has_called.store(true, std::memory_order_release); | 102 | 77.1k | return _status; | 103 | 392k | } |
segment.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v27Segment18new_index_iteratorERKNS_12TabletColumnEPKNS_11TabletIndexERKNS_18StorageReadOptionsEPSt10unique_ptrINS4_13IndexIteratorESt14default_deleteISG_EEE3$_0EES1_T_ Line | Count | Source | 75 | 58.4k | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 58.4k | if (has_called()) { | 78 | 54.7k | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 54.7k | return _status; | 82 | 54.7k | } | 83 | 3.72k | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 3.72k | if (has_called()) { | 86 | 2 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 2 | return _status; | 90 | 2 | } | 91 | 3.72k | try { | 92 | 3.72k | _status = fn(); | 93 | 3.72k | } 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.72k | _has_called.store(true, std::memory_order_release); | 102 | 3.72k | return _status; | 103 | 3.72k | } |
ordinal_page_index.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v218OrdinalIndexReader4loadEbbPNS_20OlapReaderStatisticsEE3$_0EES1_T_ Line | Count | Source | 75 | 603k | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 603k | if (has_called()) { | 78 | 296k | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 296k | return _status; | 82 | 296k | } | 83 | 306k | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 306k | if (has_called()) { | 86 | 248 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 248 | return _status; | 90 | 248 | } | 91 | 306k | try { | 92 | 306k | _status = fn(); | 93 | 306k | } 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 | 306k | _has_called.store(true, std::memory_order_release); | 102 | 306k | return _status; | 103 | 306k | } |
bloom_filter_index_reader.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v222BloomFilterIndexReader4loadEbbPNS_20OlapReaderStatisticsEE3$_0EES1_T_ Line | Count | Source | 75 | 13.2k | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 13.2k | if (has_called()) { | 78 | 23 | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 23 | return _status; | 82 | 23 | } | 83 | 13.2k | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 13.2k | if (has_called()) { | 86 | 1 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 1 | return _status; | 90 | 1 | } | 91 | 13.2k | try { | 92 | 13.2k | _status = fn(); | 93 | 13.2k | } 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 | 13.2k | _has_called.store(true, std::memory_order_release); | 102 | 13.2k | return _status; | 103 | 13.2k | } |
variant_external_meta_reader.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v225VariantExternalMetaReader13load_all_onceEPNS_10vectorized14SubcolumnsTreeINS4_13SubcolumnMetaELb1EEEPNS4_17VariantStatisticsEE3$_0EES1_T_ Line | Count | Source | 75 | 13.0k | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 13.0k | if (has_called()) { | 78 | 12.1k | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 12.1k | return _status; | 82 | 12.1k | } | 83 | 843 | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 843 | if (has_called()) { | 86 | 0 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 0 | return _status; | 90 | 0 | } | 91 | 843 | try { | 92 | 843 | _status = fn(); | 93 | 843 | } 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 | 845 | _has_called.store(true, std::memory_order_release); | 102 | 845 | return _status; | 103 | 843 | } |
zone_map_index.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v218ZoneMapIndexReader4loadEbbPNS_20OlapReaderStatisticsEE3$_0EES1_T_ Line | Count | Source | 75 | 54.6k | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 54.6k | if (has_called()) { | 78 | 34.5k | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 34.5k | return _status; | 82 | 34.5k | } | 83 | 20.1k | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 20.1k | if (has_called()) { | 86 | 685 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 685 | return _status; | 90 | 685 | } | 91 | 19.4k | try { | 92 | 19.4k | _status = fn(); | 93 | 19.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 | 19.4k | _has_called.store(true, std::memory_order_release); | 102 | 19.4k | return _status; | 103 | 19.4k | } |
tablet.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_6Tablet4initEvE3$_0EES1_T_ Line | Count | Source | 75 | 336k | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 336k | if (has_called()) { | 78 | 227 | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 227 | return _status; | 82 | 227 | } | 83 | 335k | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 335k | if (has_called()) { | 86 | 0 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 0 | return _status; | 90 | 0 | } | 91 | 335k | try { | 92 | 335k | _status = fn(); | 93 | 335k | } 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 | 335k | _has_called.store(true, std::memory_order_release); | 102 | 335k | return _status; | 103 | 335k | } |
load_path_mgr.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_11LoadPathMgr12allocate_dirERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_PSA_lE3$_0EES1_T_ Line | Count | Source | 75 | 7 | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 7 | if (has_called()) { | 78 | 5 | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 5 | return _status; | 82 | 5 | } | 83 | 2 | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 2 | if (has_called()) { | 86 | 0 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 0 | return _status; | 90 | 0 | } | 91 | 2 | try { | 92 | 2 | _status = fn(); | 93 | 2 | } 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 | _has_called.store(true, std::memory_order_release); | 102 | 2 | return _status; | 103 | 2 | } |
ann_index_reader.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v214AnnIndexReader10load_indexEPNS_2io9IOContextEE3$_0EES1_T_ Line | Count | Source | 75 | 3 | ReturnType call(Fn fn) { | 76 | | // Avoid lock to improve performance | 77 | 3 | if (has_called()) { | 78 | 0 | if (_eptr) { | 79 | 0 | std::rethrow_exception(_eptr); | 80 | 0 | } | 81 | 0 | return _status; | 82 | 0 | } | 83 | 3 | std::lock_guard l(_flag_lock); | 84 | | // should check again because maybe another thread call successfully. | 85 | 3 | if (has_called()) { | 86 | 0 | if (_eptr) { | 87 | 0 | std::rethrow_exception(_eptr); | 88 | 0 | } | 89 | 0 | return _status; | 90 | 0 | } | 91 | 3 | try { | 92 | 3 | _status = fn(); | 93 | 3 | } 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 | _has_called.store(true, std::memory_order_release); | 102 | 3 | return _status; | 103 | 3 | } |
|
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 | 565M | 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 | 565M | return _has_called.load(std::memory_order_acquire); |
113 | 565M | } _ZNK5doris13DorisCallOnceINS_6StatusEE10has_calledEv Line | Count | Source | 108 | 565M | 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 | 565M | return _has_called.load(std::memory_order_acquire); | 113 | 565M | } |
_ZNK5doris13DorisCallOnceIN2tl8expectedINS_21EncryptionAlgorithmPBENS_6StatusEEEE10has_calledEv Line | Count | Source | 108 | 162k | 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 | 162k | return _has_called.load(std::memory_order_acquire); | 113 | 162k | } |
|
114 | | |
115 | | // Return the stored result. The result is only meaningful when `has_called() == true`. |
116 | 76.6M | ReturnType stored_result() const { |
117 | 76.6M | 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 | 76.6M | if (_eptr) { |
123 | 1 | std::rethrow_exception(_eptr); |
124 | 1 | } |
125 | 76.6M | return _status; |
126 | 76.6M | } |
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 |