Commit e66fcd54 authored by Lara Lu's avatar Lara Lu Committed by Facebook Github Bot

executor with priority

Summary: make an adapter executor that forwards add to addWithPriority of the underlying executor

Reviewed By: andriigrynenko

Differential Revision: D13788642

fbshipit-source-id: 5b4af43f4a3a3931817a2425890558f811152225
parent 65e5d94a
/*
* Copyright 2019-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/executors/ExecutorWithPriority.h>
#include <glog/logging.h>
namespace folly {
Executor::KeepAlive<ExecutorWithPriority> ExecutorWithPriority::create(
KeepAlive<Executor> executor,
int8_t priority) {
return makeKeepAlive<ExecutorWithPriority>(
new ExecutorWithPriority(std::move(executor), priority));
}
void ExecutorWithPriority::add(Func func) {
executor_->addWithPriority(std::move(func), priority_);
}
bool ExecutorWithPriority::keepAliveAcquire() {
auto keepAliveCounter =
keepAliveCounter_.fetch_add(1, std::memory_order_relaxed);
DCHECK(keepAliveCounter > 0);
return true;
}
void ExecutorWithPriority::keepAliveRelease() {
auto keepAliveCounter =
keepAliveCounter_.fetch_sub(1, std::memory_order_acq_rel);
DCHECK(keepAliveCounter > 0);
if (keepAliveCounter == 1) {
delete this;
}
}
} // namespace folly
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/Executor.h>
#include <atomic>
namespace folly {
class ExecutorWithPriority : public virtual Executor {
public:
ExecutorWithPriority(ExecutorWithPriority const&) = delete;
ExecutorWithPriority& operator=(ExecutorWithPriority const&) = delete;
ExecutorWithPriority(ExecutorWithPriority&&) = delete;
ExecutorWithPriority& operator=(ExecutorWithPriority&&) = delete;
static Executor::KeepAlive<ExecutorWithPriority> create(
KeepAlive<Executor> executor,
int8_t priority);
void add(Func func) override;
protected:
bool keepAliveAcquire() override;
void keepAliveRelease() override;
private:
ExecutorWithPriority(KeepAlive<Executor> executor, int8_t priority)
: executor_(std::move(executor)), priority_(priority) {}
std::atomic<ssize_t> keepAliveCounter_{1};
KeepAlive<Executor> executor_;
int8_t priority_;
};
} // namespace folly
/*
* Copyright 2019-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/executors/ExecutorWithPriority.h>
#include <folly/executors/CPUThreadPoolExecutor.h>
#include <folly/portability/GTest.h>
using namespace folly;
TEST(ExecutorWithPriorityTest, addWithCorrectPriorityTest) {
bool tookLopri = false;
auto completed = 0;
auto hipri = [&] {
EXPECT_FALSE(tookLopri);
completed++;
};
auto lopri = [&] {
tookLopri = true;
completed++;
};
auto pool = std::make_shared<CPUThreadPoolExecutor>(
0 /*numThreads*/, 2 /*numPriorities*/);
{
auto loPriExecutor = ExecutorWithPriority::create(
getKeepAliveToken(pool.get()), Executor::LO_PRI);
auto hiPriExecutor = ExecutorWithPriority::create(
getKeepAliveToken(pool.get()), Executor::HI_PRI);
for (int i = 0; i < 50; i++) {
loPriExecutor->add(lopri);
}
for (int i = 0; i < 50; i++) {
hiPriExecutor->add(hipri);
}
pool->setNumThreads(1);
}
pool->join();
EXPECT_EQ(100, completed);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment