/root/doris/be/src/util/thread_group.h
Line | Count | Source (jump to first uncovered line) |
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 <list> |
21 | | #include <shared_mutex> |
22 | | #include <thread> |
23 | | |
24 | | #include "common/status.h" |
25 | | |
26 | | namespace doris { |
27 | | class ThreadGroup { |
28 | | public: |
29 | 36 | ThreadGroup() {} |
30 | 36 | ~ThreadGroup() { |
31 | 214 | for (auto thrd : _threads) { |
32 | 214 | delete thrd; |
33 | 214 | } |
34 | 36 | } |
35 | | |
36 | 37 | bool is_this_thread_in() const { |
37 | 37 | std::thread::id id = std::this_thread::get_id(); |
38 | 37 | std::shared_lock rdlock(_mutex); |
39 | 215 | for (auto const& thrd : _threads) { |
40 | 215 | if (thrd->get_id() == id) { |
41 | 0 | return true; |
42 | 0 | } |
43 | 215 | } |
44 | 37 | return false; |
45 | 37 | } |
46 | | |
47 | 0 | bool is_thread_in(std::thread* thrd) const { |
48 | 0 | if (thrd) { |
49 | 0 | std::thread::id id = thrd->get_id(); |
50 | 0 | std::shared_lock rdlock(_mutex); |
51 | 0 | for (auto const& th : _threads) { |
52 | 0 | if (th->get_id() == id) { |
53 | 0 | return true; |
54 | 0 | } |
55 | 0 | } |
56 | 0 | return false; |
57 | 0 | } else { |
58 | 0 | return false; |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | | template <typename F> |
63 | 214 | std::thread* create_thread(F threadfunc) { |
64 | 214 | std::lock_guard<std::shared_mutex> wrlock(_mutex); |
65 | 214 | std::unique_ptr<std::thread> new_thread = std::make_unique<std::thread>(threadfunc); |
66 | 214 | _threads.push_back(new_thread.get()); |
67 | 214 | return new_thread.release(); |
68 | 214 | } _ZN5doris11ThreadGroup13create_threadISt12_Bind_resultIvFSt7_Mem_fnIMNS_14WorkThreadPoolILb0EEEFviEEPS5_iEEEEPSt6threadT_ Line | Count | Source | 63 | 112 | std::thread* create_thread(F threadfunc) { | 64 | 112 | std::lock_guard<std::shared_mutex> wrlock(_mutex); | 65 | 112 | std::unique_ptr<std::thread> new_thread = std::make_unique<std::thread>(threadfunc); | 66 | 112 | _threads.push_back(new_thread.get()); | 67 | 112 | return new_thread.release(); | 68 | 112 | } |
_ZN5doris11ThreadGroup13create_threadISt12_Bind_resultIvFSt7_Mem_fnIMNS_14WorkThreadPoolILb1EEEFviEEPS5_iEEEEPSt6threadT_ Line | Count | Source | 63 | 102 | std::thread* create_thread(F threadfunc) { | 64 | 102 | std::lock_guard<std::shared_mutex> wrlock(_mutex); | 65 | 102 | std::unique_ptr<std::thread> new_thread = std::make_unique<std::thread>(threadfunc); | 66 | 102 | _threads.push_back(new_thread.get()); | 67 | 102 | return new_thread.release(); | 68 | 102 | } |
|
69 | | |
70 | 0 | Status add_thread(std::thread* thrd) { |
71 | 0 | if (thrd) { |
72 | 0 | if (!is_thread_in(thrd)) { |
73 | 0 | std::lock_guard<std::shared_mutex> guard(_mutex); |
74 | 0 | _threads.push_back(thrd); |
75 | 0 | return Status::OK(); |
76 | 0 | } else { |
77 | 0 | return Status::InvalidArgument("trying to add a duplicated thread"); |
78 | 0 | } |
79 | 0 | } else { |
80 | 0 | return Status::InvalidArgument("trying to add a nullptr as thread"); |
81 | 0 | } |
82 | 0 | } |
83 | | |
84 | 0 | void remove_thread(std::thread* thrd) { |
85 | 0 | std::lock_guard<std::shared_mutex> wrlock(_mutex); |
86 | 0 | std::list<std::thread*>::const_iterator it = |
87 | 0 | std::find(_threads.begin(), _threads.end(), thrd); |
88 | 0 | if (it != _threads.end()) { |
89 | 0 | _threads.erase(it); |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | 37 | Status join_all() { |
94 | 37 | if (is_this_thread_in()) { |
95 | 0 | return Status::RuntimeError("trying joining itself"); |
96 | 0 | } |
97 | 37 | std::shared_lock rdlock(_mutex); |
98 | | |
99 | 215 | for (auto thrd : _threads) { |
100 | 215 | if (thrd->joinable()) { |
101 | 214 | thrd->join(); |
102 | 214 | } |
103 | 215 | } |
104 | 37 | return Status::OK(); |
105 | 37 | } |
106 | | |
107 | 0 | size_t size() const { |
108 | 0 | std::shared_lock rdlock(_mutex); |
109 | 0 | return _threads.size(); |
110 | 0 | } |
111 | | |
112 | | private: |
113 | | std::list<std::thread*> _threads; |
114 | | mutable std::shared_mutex _mutex; |
115 | | }; |
116 | | } // namespace doris |