/root/doris/be/src/util/blocking_queue.hpp
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/blocking-queue.hpp |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <unistd.h> |
24 | | |
25 | | #include <atomic> |
26 | | #include <condition_variable> |
27 | | #include <list> |
28 | | #include <mutex> |
29 | | |
30 | | #include "common/logging.h" |
31 | | #include "util/stopwatch.hpp" |
32 | | |
33 | | #ifdef BE_TEST |
34 | | #include "cpp/sync_point.h" |
35 | | #endif |
36 | | |
37 | | namespace doris { |
38 | | #include "common/compile_check_begin.h" |
39 | | // Fixed capacity FIFO queue, where both BlockingGet and BlockingPut operations block |
40 | | // if the queue is empty or full, respectively. |
41 | | template <typename T> |
42 | | class BlockingQueue { |
43 | | public: |
44 | | BlockingQueue(uint32_t max_elements) |
45 | 40 | : _shutdown(false), |
46 | 40 | _max_elements(max_elements), |
47 | 40 | _total_get_wait_time(0), |
48 | 40 | _total_put_wait_time(0), |
49 | 40 | _get_waiting(0), |
50 | 40 | _put_waiting(0) {}_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEEC2Ej Line | Count | Source | 45 | 28 | : _shutdown(false), | 46 | 28 | _max_elements(max_elements), | 47 | 28 | _total_get_wait_time(0), | 48 | 28 | _total_put_wait_time(0), | 49 | 28 | _get_waiting(0), | 50 | 28 | _put_waiting(0) {} |
_ZN5doris13BlockingQueueIiEC2Ej Line | Count | Source | 45 | 4 | : _shutdown(false), | 46 | 4 | _max_elements(max_elements), | 47 | 4 | _total_get_wait_time(0), | 48 | 4 | _total_put_wait_time(0), | 49 | 4 | _get_waiting(0), | 50 | 4 | _put_waiting(0) {} |
_ZN5doris13BlockingQueueIlEC2Ej Line | Count | Source | 45 | 1 | : _shutdown(false), | 46 | 1 | _max_elements(max_elements), | 47 | 1 | _total_get_wait_time(0), | 48 | 1 | _total_put_wait_time(0), | 49 | 1 | _get_waiting(0), | 50 | 1 | _put_waiting(0) {} |
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEEC2Ej Line | Count | Source | 45 | 6 | : _shutdown(false), | 46 | 6 | _max_elements(max_elements), | 47 | 6 | _total_get_wait_time(0), | 48 | 6 | _total_put_wait_time(0), | 49 | 6 | _get_waiting(0), | 50 | 6 | _put_waiting(0) {} |
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEEC2Ej Line | Count | Source | 45 | 1 | : _shutdown(false), | 46 | 1 | _max_elements(max_elements), | 47 | 1 | _total_get_wait_time(0), | 48 | 1 | _total_put_wait_time(0), | 49 | 1 | _get_waiting(0), | 50 | 1 | _put_waiting(0) {} |
|
51 | | |
52 | | // Get an element from the queue, waiting indefinitely for one to become available. |
53 | | // Returns false if we were shut down prior to getting the element, and there |
54 | | // are no more elements available. |
55 | 60.0k | bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); }_ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE12blocking_getEPS3_ Line | Count | Source | 55 | 142 | bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); } |
_ZN5doris13BlockingQueueIiE12blocking_getEPi Line | Count | Source | 55 | 59.9k | bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); } |
_ZN5doris13BlockingQueueIlE12blocking_getEPl Line | Count | Source | 55 | 2 | bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); } |
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE12blocking_getEPS4_ Line | Count | Source | 55 | 2 | bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); } |
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE12blocking_getEPS3_ Line | Count | Source | 55 | 1 | bool blocking_get(T* out) { return controlled_blocking_get(out, MAX_CV_WAIT_TIMEOUT_MS); } |
|
56 | | |
57 | | // Blocking_get and blocking_put may cause deadlock, |
58 | | // but we still don't find root cause, |
59 | | // introduce condition variable wait timeout to avoid blocking queue deadlock temporarily. |
60 | 60.0k | bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) { |
61 | 60.0k | MonotonicStopWatch timer; |
62 | 60.0k | timer.start(); |
63 | 60.0k | std::unique_lock<std::mutex> unique_lock(_lock); |
64 | 60.0k | #ifdef BE_TEST |
65 | 60.0k | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock"); |
66 | 60.0k | #endif |
67 | 61.4k | while (!(_shutdown || !_list.empty())) { |
68 | 1.39k | ++_get_waiting; |
69 | 1.39k | #ifdef BE_TEST |
70 | 1.39k | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait"); |
71 | 1.39k | #endif |
72 | 1.39k | _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)); |
73 | 1.39k | DCHECK_GT(_get_waiting, 0); |
74 | 1.39k | --_get_waiting; |
75 | 1.39k | } |
76 | 60.0k | _total_get_wait_time += timer.elapsed_time(); |
77 | | |
78 | 60.0k | if (!_list.empty()) { |
79 | 50.0k | *out = _list.front(); |
80 | 50.0k | _list.pop_front(); |
81 | 50.0k | const bool has_put_waiter = _put_waiting > 0; |
82 | 50.0k | unique_lock.unlock(); |
83 | 50.0k | if (has_put_waiter) { |
84 | 6.48k | _put_cv.notify_one(); |
85 | 6.48k | } |
86 | 50.0k | return true; |
87 | 50.0k | } else { |
88 | 10.0k | assert(_shutdown); |
89 | 10.1k | return false; |
90 | 10.0k | } |
91 | 60.0k | } _ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE23controlled_blocking_getEPS3_l Line | Count | Source | 60 | 142 | bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) { | 61 | 142 | MonotonicStopWatch timer; | 62 | 142 | timer.start(); | 63 | 142 | std::unique_lock<std::mutex> unique_lock(_lock); | 64 | 142 | #ifdef BE_TEST | 65 | 142 | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock"); | 66 | 142 | #endif | 67 | 284 | while (!(_shutdown || !_list.empty())) { | 68 | 142 | ++_get_waiting; | 69 | 142 | #ifdef BE_TEST | 70 | 142 | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait"); | 71 | 142 | #endif | 72 | 142 | _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)); | 73 | 142 | DCHECK_GT(_get_waiting, 0); | 74 | 142 | --_get_waiting; | 75 | 142 | } | 76 | 142 | _total_get_wait_time += timer.elapsed_time(); | 77 | | | 78 | 142 | if (!_list.empty()) { | 79 | 30 | *out = _list.front(); | 80 | 30 | _list.pop_front(); | 81 | 30 | const bool has_put_waiter = _put_waiting > 0; | 82 | 30 | unique_lock.unlock(); | 83 | 30 | if (has_put_waiter) { | 84 | 0 | _put_cv.notify_one(); | 85 | 0 | } | 86 | 30 | return true; | 87 | 112 | } else { | 88 | 112 | assert(_shutdown); | 89 | 112 | return false; | 90 | 112 | } | 91 | 142 | } |
_ZN5doris13BlockingQueueIiE23controlled_blocking_getEPil Line | Count | Source | 60 | 59.9k | bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) { | 61 | 59.9k | MonotonicStopWatch timer; | 62 | 59.9k | timer.start(); | 63 | 59.9k | std::unique_lock<std::mutex> unique_lock(_lock); | 64 | 59.9k | #ifdef BE_TEST | 65 | 59.9k | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock"); | 66 | 59.9k | #endif | 67 | 61.1k | while (!(_shutdown || !_list.empty())) { | 68 | 1.24k | ++_get_waiting; | 69 | 1.24k | #ifdef BE_TEST | 70 | 1.24k | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait"); | 71 | 1.24k | #endif | 72 | 1.24k | _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)); | 73 | 1.24k | DCHECK_GT(_get_waiting, 0); | 74 | 1.24k | --_get_waiting; | 75 | 1.24k | } | 76 | 59.9k | _total_get_wait_time += timer.elapsed_time(); | 77 | | | 78 | 59.9k | if (!_list.empty()) { | 79 | 50.0k | *out = _list.front(); | 80 | 50.0k | _list.pop_front(); | 81 | 50.0k | const bool has_put_waiter = _put_waiting > 0; | 82 | 50.0k | unique_lock.unlock(); | 83 | 50.0k | if (has_put_waiter) { | 84 | 6.48k | _put_cv.notify_one(); | 85 | 6.48k | } | 86 | 50.0k | return true; | 87 | 50.0k | } else { | 88 | 9.92k | assert(_shutdown); | 89 | 10.0k | return false; | 90 | 9.92k | } | 91 | 59.9k | } |
_ZN5doris13BlockingQueueIlE23controlled_blocking_getEPll Line | Count | Source | 60 | 2 | bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) { | 61 | 2 | MonotonicStopWatch timer; | 62 | 2 | timer.start(); | 63 | 2 | std::unique_lock<std::mutex> unique_lock(_lock); | 64 | 2 | #ifdef BE_TEST | 65 | 2 | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock"); | 66 | 2 | #endif | 67 | 2 | while (!(_shutdown || !_list.empty())) { | 68 | 0 | ++_get_waiting; | 69 | 0 | #ifdef BE_TEST | 70 | 0 | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait"); | 71 | 0 | #endif | 72 | 0 | _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)); | 73 | 0 | DCHECK_GT(_get_waiting, 0); | 74 | 0 | --_get_waiting; | 75 | 0 | } | 76 | 2 | _total_get_wait_time += timer.elapsed_time(); | 77 | | | 78 | 2 | if (!_list.empty()) { | 79 | 1 | *out = _list.front(); | 80 | 1 | _list.pop_front(); | 81 | 1 | const bool has_put_waiter = _put_waiting > 0; | 82 | 1 | unique_lock.unlock(); | 83 | 1 | if (has_put_waiter) { | 84 | 0 | _put_cv.notify_one(); | 85 | 0 | } | 86 | 1 | return true; | 87 | 1 | } else { | 88 | 1 | assert(_shutdown); | 89 | 1 | return false; | 90 | 1 | } | 91 | 2 | } |
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE23controlled_blocking_getEPS4_l Line | Count | Source | 60 | 2 | bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) { | 61 | 2 | MonotonicStopWatch timer; | 62 | 2 | timer.start(); | 63 | 2 | std::unique_lock<std::mutex> unique_lock(_lock); | 64 | 2 | #ifdef BE_TEST | 65 | 2 | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock"); | 66 | 2 | #endif | 67 | 2 | while (!(_shutdown || !_list.empty())) { | 68 | 0 | ++_get_waiting; | 69 | 0 | #ifdef BE_TEST | 70 | 0 | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait"); | 71 | 0 | #endif | 72 | 0 | _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)); | 73 | 0 | DCHECK_GT(_get_waiting, 0); | 74 | 0 | --_get_waiting; | 75 | 0 | } | 76 | 2 | _total_get_wait_time += timer.elapsed_time(); | 77 | | | 78 | 2 | if (!_list.empty()) { | 79 | 2 | *out = _list.front(); | 80 | 2 | _list.pop_front(); | 81 | 2 | const bool has_put_waiter = _put_waiting > 0; | 82 | 2 | unique_lock.unlock(); | 83 | 2 | if (has_put_waiter) { | 84 | 0 | _put_cv.notify_one(); | 85 | 0 | } | 86 | 2 | return true; | 87 | 2 | } else { | 88 | 0 | assert(_shutdown); | 89 | 0 | return false; | 90 | 0 | } | 91 | 2 | } |
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE23controlled_blocking_getEPS3_l Line | Count | Source | 60 | 2 | bool controlled_blocking_get(T* out, int64_t cv_wait_timeout_ms) { | 61 | 2 | MonotonicStopWatch timer; | 62 | 2 | timer.start(); | 63 | 2 | std::unique_lock<std::mutex> unique_lock(_lock); | 64 | 2 | #ifdef BE_TEST | 65 | 2 | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::after_lock"); | 66 | 2 | #endif | 67 | 8 | while (!(_shutdown || !_list.empty())) { | 68 | 6 | ++_get_waiting; | 69 | 6 | #ifdef BE_TEST | 70 | 6 | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_get::before_wait"); | 71 | 6 | #endif | 72 | 6 | _get_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)); | 73 | 6 | DCHECK_GT(_get_waiting, 0); | 74 | 6 | --_get_waiting; | 75 | 6 | } | 76 | 2 | _total_get_wait_time += timer.elapsed_time(); | 77 | | | 78 | 2 | if (!_list.empty()) { | 79 | 0 | *out = _list.front(); | 80 | 0 | _list.pop_front(); | 81 | 0 | const bool has_put_waiter = _put_waiting > 0; | 82 | 0 | unique_lock.unlock(); | 83 | 0 | if (has_put_waiter) { | 84 | 0 | _put_cv.notify_one(); | 85 | 0 | } | 86 | 0 | return true; | 87 | 2 | } else { | 88 | 2 | assert(_shutdown); | 89 | 2 | return false; | 90 | 2 | } | 91 | 2 | } |
|
92 | | |
93 | | // Puts an element into the queue, waiting indefinitely until there is space. |
94 | | // If the queue is shut down, returns false. |
95 | 49.9k | bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); }Unexecuted instantiation: _ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE12blocking_putERKS3_ _ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE12blocking_putERKS4_ Line | Count | Source | 95 | 4 | bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); } |
_ZN5doris13BlockingQueueIiE12blocking_putERKi Line | Count | Source | 95 | 49.9k | bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); } |
_ZN5doris13BlockingQueueIlE12blocking_putERKl Line | Count | Source | 95 | 2 | bool blocking_put(const T& val) { return controlled_blocking_put(val, MAX_CV_WAIT_TIMEOUT_MS); } |
|
96 | | |
97 | | // Blocking_get and blocking_put may cause deadlock, |
98 | | // but we still don't find root cause, |
99 | | // introduce condition variable wait timeout to avoid blocking queue deadlock temporarily. |
100 | 49.9k | bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) { |
101 | 49.9k | MonotonicStopWatch timer; |
102 | 49.9k | timer.start(); |
103 | 49.9k | std::unique_lock<std::mutex> unique_lock(_lock); |
104 | 51.7k | while (!(_shutdown || _list.size() < _max_elements)) { |
105 | 1.72k | ++_put_waiting; |
106 | 1.72k | #ifdef BE_TEST |
107 | 1.72k | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_put::before_wait"); |
108 | 1.72k | #endif |
109 | 1.72k | _put_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)); |
110 | 1.72k | DCHECK_GT(_put_waiting, 0); |
111 | 1.72k | --_put_waiting; |
112 | 1.72k | } |
113 | 49.9k | _total_put_wait_time += timer.elapsed_time(); |
114 | | |
115 | 49.9k | if (_shutdown) { |
116 | 1 | return false; |
117 | 1 | } |
118 | | |
119 | 49.9k | _list.push_back(val); |
120 | 49.9k | const bool has_get_waiter = _get_waiting > 0; |
121 | 49.9k | unique_lock.unlock(); |
122 | 49.9k | if (has_get_waiter) { |
123 | 3.85k | _get_cv.notify_one(); |
124 | 3.85k | } |
125 | 49.9k | return true; |
126 | 49.9k | } Unexecuted instantiation: _ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE23controlled_blocking_putERKS3_l _ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE23controlled_blocking_putERKS4_l Line | Count | Source | 100 | 4 | bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) { | 101 | 4 | MonotonicStopWatch timer; | 102 | 4 | timer.start(); | 103 | 4 | std::unique_lock<std::mutex> unique_lock(_lock); | 104 | 4 | while (!(_shutdown || _list.size() < _max_elements)) { | 105 | 0 | ++_put_waiting; | 106 | 0 | #ifdef BE_TEST | 107 | 0 | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_put::before_wait"); | 108 | 0 | #endif | 109 | 0 | _put_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)); | 110 | 0 | DCHECK_GT(_put_waiting, 0); | 111 | 0 | --_put_waiting; | 112 | 0 | } | 113 | 4 | _total_put_wait_time += timer.elapsed_time(); | 114 | | | 115 | 4 | if (_shutdown) { | 116 | 0 | return false; | 117 | 0 | } | 118 | | | 119 | 4 | _list.push_back(val); | 120 | 4 | const bool has_get_waiter = _get_waiting > 0; | 121 | 4 | unique_lock.unlock(); | 122 | 4 | if (has_get_waiter) { | 123 | 0 | _get_cv.notify_one(); | 124 | 0 | } | 125 | 4 | return true; | 126 | 4 | } |
_ZN5doris13BlockingQueueIiE23controlled_blocking_putERKil Line | Count | Source | 100 | 49.8k | bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) { | 101 | 49.8k | MonotonicStopWatch timer; | 102 | 49.8k | timer.start(); | 103 | 49.8k | std::unique_lock<std::mutex> unique_lock(_lock); | 104 | 51.7k | while (!(_shutdown || _list.size() < _max_elements)) { | 105 | 1.72k | ++_put_waiting; | 106 | 1.72k | #ifdef BE_TEST | 107 | 1.72k | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_put::before_wait"); | 108 | 1.72k | #endif | 109 | 1.72k | _put_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)); | 110 | 1.72k | DCHECK_GT(_put_waiting, 0); | 111 | 1.72k | --_put_waiting; | 112 | 1.72k | } | 113 | 49.8k | _total_put_wait_time += timer.elapsed_time(); | 114 | | | 115 | 49.8k | if (_shutdown) { | 116 | 0 | return false; | 117 | 0 | } | 118 | | | 119 | 49.8k | _list.push_back(val); | 120 | 49.8k | const bool has_get_waiter = _get_waiting > 0; | 121 | 49.8k | unique_lock.unlock(); | 122 | 49.8k | if (has_get_waiter) { | 123 | 3.85k | _get_cv.notify_one(); | 124 | 3.85k | } | 125 | 49.8k | return true; | 126 | 49.8k | } |
_ZN5doris13BlockingQueueIlE23controlled_blocking_putERKll Line | Count | Source | 100 | 2 | bool controlled_blocking_put(const T& val, int64_t cv_wait_timeout_ms) { | 101 | 2 | MonotonicStopWatch timer; | 102 | 2 | timer.start(); | 103 | 2 | std::unique_lock<std::mutex> unique_lock(_lock); | 104 | 2 | while (!(_shutdown || _list.size() < _max_elements)) { | 105 | 0 | ++_put_waiting; | 106 | 0 | #ifdef BE_TEST | 107 | 0 | TEST_SYNC_POINT("BlockingQueue::controlled_blocking_put::before_wait"); | 108 | 0 | #endif | 109 | 0 | _put_cv.wait_for(unique_lock, std::chrono::milliseconds(cv_wait_timeout_ms)); | 110 | 0 | DCHECK_GT(_put_waiting, 0); | 111 | 0 | --_put_waiting; | 112 | 0 | } | 113 | 2 | _total_put_wait_time += timer.elapsed_time(); | 114 | | | 115 | 2 | if (_shutdown) { | 116 | 1 | return false; | 117 | 1 | } | 118 | | | 119 | 1 | _list.push_back(val); | 120 | 1 | const bool has_get_waiter = _get_waiting > 0; | 121 | 1 | unique_lock.unlock(); | 122 | 1 | if (has_get_waiter) { | 123 | 0 | _get_cv.notify_one(); | 124 | 0 | } | 125 | 1 | return true; | 126 | 2 | } |
Unexecuted instantiation: _ZN5doris13BlockingQueueIPN7RdKafka7MessageEE23controlled_blocking_putERKS3_l |
127 | | |
128 | | // Return false if queue full or has been shutdown. |
129 | 33 | bool try_put(const T& val) { |
130 | 33 | MonotonicStopWatch timer; |
131 | 33 | timer.start(); |
132 | 33 | std::unique_lock<std::mutex> unique_lock(_lock); |
133 | 33 | #ifdef BE_TEST |
134 | 33 | TEST_SYNC_POINT("BlockingQueue::try_put::after_lock"); |
135 | 33 | #endif |
136 | 33 | _total_put_wait_time += timer.elapsed_time(); |
137 | | |
138 | 33 | if (_shutdown || _list.size() >= _max_elements) { |
139 | 0 | return false; |
140 | 0 | } |
141 | | |
142 | 33 | _list.push_back(val); |
143 | 33 | const bool has_get_waiter = _get_waiting > 0; |
144 | 33 | unique_lock.unlock(); |
145 | 33 | if (has_get_waiter) { |
146 | 32 | _get_cv.notify_one(); |
147 | 32 | } |
148 | 33 | return true; |
149 | 33 | } _ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE7try_putERKS3_ Line | Count | Source | 129 | 30 | bool try_put(const T& val) { | 130 | 30 | MonotonicStopWatch timer; | 131 | 30 | timer.start(); | 132 | 30 | std::unique_lock<std::mutex> unique_lock(_lock); | 133 | 30 | #ifdef BE_TEST | 134 | 30 | TEST_SYNC_POINT("BlockingQueue::try_put::after_lock"); | 135 | 30 | #endif | 136 | 30 | _total_put_wait_time += timer.elapsed_time(); | 137 | | | 138 | 30 | if (_shutdown || _list.size() >= _max_elements) { | 139 | 0 | return false; | 140 | 0 | } | 141 | | | 142 | 30 | _list.push_back(val); | 143 | 30 | const bool has_get_waiter = _get_waiting > 0; | 144 | 30 | unique_lock.unlock(); | 145 | 30 | if (has_get_waiter) { | 146 | 30 | _get_cv.notify_one(); | 147 | 30 | } | 148 | 30 | return true; | 149 | 30 | } |
_ZN5doris13BlockingQueueIiE7try_putERKi Line | Count | Source | 129 | 3 | bool try_put(const T& val) { | 130 | 3 | MonotonicStopWatch timer; | 131 | 3 | timer.start(); | 132 | 3 | std::unique_lock<std::mutex> unique_lock(_lock); | 133 | 3 | #ifdef BE_TEST | 134 | 3 | TEST_SYNC_POINT("BlockingQueue::try_put::after_lock"); | 135 | 3 | #endif | 136 | 3 | _total_put_wait_time += timer.elapsed_time(); | 137 | | | 138 | 3 | if (_shutdown || _list.size() >= _max_elements) { | 139 | 0 | return false; | 140 | 0 | } | 141 | | | 142 | 3 | _list.push_back(val); | 143 | 3 | const bool has_get_waiter = _get_waiting > 0; | 144 | 3 | unique_lock.unlock(); | 145 | 3 | if (has_get_waiter) { | 146 | 2 | _get_cv.notify_one(); | 147 | 2 | } | 148 | 3 | return true; | 149 | 3 | } |
|
150 | | |
151 | | // Shut down the queue. Wakes up all threads waiting on BlockingGet or BlockingPut. |
152 | 37 | void shutdown() { |
153 | 37 | { |
154 | 37 | std::lock_guard<std::mutex> guard(_lock); |
155 | 37 | _shutdown = true; |
156 | 37 | } |
157 | | |
158 | 37 | _get_cv.notify_all(); |
159 | 37 | _put_cv.notify_all(); |
160 | 37 | } _ZN5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE8shutdownEv Line | Count | Source | 152 | 28 | void shutdown() { | 153 | 28 | { | 154 | 28 | std::lock_guard<std::mutex> guard(_lock); | 155 | 28 | _shutdown = true; | 156 | 28 | } | 157 | | | 158 | 28 | _get_cv.notify_all(); | 159 | 28 | _put_cv.notify_all(); | 160 | 28 | } |
_ZN5doris13BlockingQueueIlE8shutdownEv Line | Count | Source | 152 | 1 | void shutdown() { | 153 | 1 | { | 154 | 1 | std::lock_guard<std::mutex> guard(_lock); | 155 | 1 | _shutdown = true; | 156 | 1 | } | 157 | | | 158 | 1 | _get_cv.notify_all(); | 159 | 1 | _put_cv.notify_all(); | 160 | 1 | } |
_ZN5doris13BlockingQueueIiE8shutdownEv Line | Count | Source | 152 | 3 | void shutdown() { | 153 | 3 | { | 154 | 3 | std::lock_guard<std::mutex> guard(_lock); | 155 | 3 | _shutdown = true; | 156 | 3 | } | 157 | | | 158 | 3 | _get_cv.notify_all(); | 159 | 3 | _put_cv.notify_all(); | 160 | 3 | } |
_ZN5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE8shutdownEv Line | Count | Source | 152 | 2 | void shutdown() { | 153 | 2 | { | 154 | 2 | std::lock_guard<std::mutex> guard(_lock); | 155 | 2 | _shutdown = true; | 156 | 2 | } | 157 | | | 158 | 2 | _get_cv.notify_all(); | 159 | 2 | _put_cv.notify_all(); | 160 | 2 | } |
_ZN5doris13BlockingQueueIPN7RdKafka7MessageEE8shutdownEv Line | Count | Source | 152 | 3 | void shutdown() { | 153 | 3 | { | 154 | 3 | std::lock_guard<std::mutex> guard(_lock); | 155 | 3 | _shutdown = true; | 156 | 3 | } | 157 | | | 158 | 3 | _get_cv.notify_all(); | 159 | 3 | _put_cv.notify_all(); | 160 | 3 | } |
|
161 | | |
162 | 143 | uint32_t get_size() const { |
163 | 143 | std::lock_guard<std::mutex> l(_lock); |
164 | 143 | return static_cast<uint32_t>(_list.size()); |
165 | 143 | } _ZNK5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE8get_sizeEv Line | Count | Source | 162 | 142 | uint32_t get_size() const { | 163 | 142 | std::lock_guard<std::mutex> l(_lock); | 164 | 142 | return static_cast<uint32_t>(_list.size()); | 165 | 142 | } |
Unexecuted instantiation: _ZNK5doris13BlockingQueueISt10shared_ptrIN5arrow11RecordBatchEEE8get_sizeEv _ZNK5doris13BlockingQueueIPN7RdKafka7MessageEE8get_sizeEv Line | Count | Source | 162 | 1 | uint32_t get_size() const { | 163 | 1 | std::lock_guard<std::mutex> l(_lock); | 164 | 1 | return static_cast<uint32_t>(_list.size()); | 165 | 1 | } |
|
166 | | |
167 | 0 | uint32_t get_capacity() const { return _max_elements; } |
168 | | |
169 | | #ifdef BE_TEST |
170 | 8 | size_t get_waiting_count_for_test() const { |
171 | 8 | std::lock_guard<std::mutex> guard(_lock); |
172 | 8 | return _get_waiting; |
173 | 8 | } |
174 | | |
175 | 8 | size_t put_waiting_count_for_test() const { |
176 | 8 | std::lock_guard<std::mutex> guard(_lock); |
177 | 8 | return _put_waiting; |
178 | 8 | } |
179 | | #endif |
180 | | |
181 | | // Returns the total amount of time threads have blocked in BlockingGet. |
182 | 1 | uint64_t total_get_wait_time() const { return _total_get_wait_time; }Unexecuted instantiation: _ZNK5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE19total_get_wait_timeEv _ZNK5doris13BlockingQueueIPN7RdKafka7MessageEE19total_get_wait_timeEv Line | Count | Source | 182 | 1 | uint64_t total_get_wait_time() const { return _total_get_wait_time; } |
|
183 | | |
184 | | // Returns the total amount of time threads have blocked in BlockingPut. |
185 | 1 | uint64_t total_put_wait_time() const { return _total_put_wait_time; }Unexecuted instantiation: _ZNK5doris13BlockingQueueINS_14WorkThreadPoolILb0EE4TaskEE19total_put_wait_timeEv _ZNK5doris13BlockingQueueIPN7RdKafka7MessageEE19total_put_wait_timeEv Line | Count | Source | 185 | 1 | uint64_t total_put_wait_time() const { return _total_put_wait_time; } |
|
186 | | |
187 | | private: |
188 | | static constexpr int64_t MAX_CV_WAIT_TIMEOUT_MS = 60 * 60 * 1000; // 1 hour |
189 | | bool _shutdown; |
190 | | const int _max_elements; |
191 | | std::condition_variable _get_cv; // 'get' callers wait on this |
192 | | std::condition_variable _put_cv; // 'put' callers wait on this |
193 | | // _lock guards access to _shutdown, _get_waiting, _put_waiting, _list, total_get_wait_time, and total_put_wait_time |
194 | | mutable std::mutex _lock; |
195 | | std::list<T> _list; |
196 | | std::atomic<uint64_t> _total_get_wait_time; |
197 | | std::atomic<uint64_t> _total_put_wait_time; |
198 | | // Number of threads currently inside the corresponding wait_for() call. The waiter that |
199 | | // increments a counter is also responsible for decrementing it after every wakeup reason. |
200 | | // Notifiers only inspect these counters and never consume a waiter's registration. |
201 | | size_t _get_waiting; |
202 | | size_t _put_waiting; |
203 | | }; |
204 | | #include "common/compile_check_end.h" |
205 | | } // namespace doris |