2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "TestExecutor.h"
23 TestExecutor::TestExecutor() {
24 const auto kWorkers = std::max(1U, thread::hardware_concurrency());
25 for (auto idx = 0U; idx < kWorkers; ++idx) {
26 workers_.emplace_back([this] {
30 unique_lock<mutex> lk(m_);
31 cv_.wait(lk, [this] { return !workItems_.empty(); });
32 work = std::move(workItems_.front());
44 TestExecutor::~TestExecutor() {
45 for (auto& worker : workers_) {
49 for (auto& worker : workers_) {
54 void TestExecutor::add(Func f) {
56 addImpl(std::move(f));
60 uint32_t TestExecutor::numThreads() const {
61 return workers_.size();
64 void TestExecutor::addImpl(Func f) {
66 lock_guard<mutex> g(m_);
67 workItems_.push(std::move(f));