proxygen
ThreadedExecutor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <chrono>
20 
21 #include <glog/logging.h>
22 
25 
26 namespace folly {
27 
28 template <typename F>
29 static auto with_unique_lock(std::mutex& m, F&& f) -> decltype(f()) {
30  std::unique_lock<std::mutex> lock(m);
31  return f();
32 }
33 
34 ThreadedExecutor::ThreadedExecutor(std::shared_ptr<ThreadFactory> threadFactory)
35  : threadFactory_(std::move(threadFactory)) {
36  controlt_ = std::thread([this] { control(); });
37 }
38 
40  stopping_.store(true, std::memory_order_release);
41  notify();
42  controlt_.join();
43  CHECK(running_.empty());
44  CHECK(finished_.empty());
45 }
46 
48  CHECK(!stopping_.load(std::memory_order_acquire));
49  with_unique_lock(enqueuedm_, [&] { enqueued_.push_back(std::move(func)); });
50  notify();
51 }
52 
53 std::shared_ptr<ThreadFactory> ThreadedExecutor::newDefaultThreadFactory() {
54  return std::make_shared<NamedThreadFactory>("Threaded");
55 }
56 
58  with_unique_lock(controlm_, [&] { controls_ = true; });
59  controlc_.notify_one();
60 }
61 
63  folly::setThreadName("ThreadedCtrl");
64  auto looping = true;
65  while (looping) {
66  controlWait();
67  looping = controlPerformAll();
68  }
69 }
70 
72  constexpr auto kMaxWait = std::chrono::seconds(10);
73  std::unique_lock<std::mutex> lock(controlm_);
74  controlc_.wait_for(lock, kMaxWait, [&] { return controls_; });
75  controls_ = false;
76 }
77 
79  func();
80  auto id = std::this_thread::get_id();
81  with_unique_lock(finishedm_, [&] { finished_.push_back(id); });
82  notify();
83 }
84 
86  std::deque<std::thread::id> finishedt;
87  with_unique_lock(finishedm_, [&] { std::swap(finishedt, finished_); });
88  for (auto id : finishedt) {
89  running_[id].join();
90  running_.erase(id);
91  }
92 }
93 
95  std::deque<Func> enqueuedt;
96  with_unique_lock(enqueuedm_, [&] { std::swap(enqueuedt, enqueued_); });
97  for (auto& f : enqueuedt) {
98  auto th = threadFactory_->newThread(
99  [this, f = std::move(f)]() mutable { work(f); });
100  auto id = th.get_id();
101  running_[id] = std::move(th);
102  }
103 }
104 
106  auto stopping = stopping_.load(std::memory_order_acquire);
109  return !stopping || !running_.empty();
110 }
111 } // namespace folly
std::deque< Func > enqueued_
auto f
std::condition_variable controlc_
static std::shared_ptr< ThreadFactory > newDefaultThreadFactory()
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static auto with_unique_lock(std::mutex &m, F &&f) -> decltype(f())
STL namespace.
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::deque< std::thread::id > finished_
std::shared_ptr< ThreadFactory > threadFactory_
void add(Func func) override
auto lock(Synchronized< D, M > &synchronized, Args &&...args)
static map< string, int > m
std::atomic< bool > stopping_
bool setThreadName(std::thread::id tid, StringPiece name)
Definition: ThreadName.cpp:109
std::mutex mutex
std::map< std::thread::id, std::thread > running_
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
ThreadedExecutor(std::shared_ptr< ThreadFactory > threadFactory=newDefaultThreadFactory())