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.
19 #include <folly/executors/thread_factory/ThreadFactory.h>
21 #include <folly/portability/SysResource.h>
22 #include <folly/portability/SysTime.h>
27 * A ThreadFactory that sets nice values for each thread. The main
28 * use case for this class is if there are multiple
29 * CPUThreadPoolExecutors in a single process, or between multiple
30 * processes, where some should have a higher priority than the others.
32 * Note that per-thread nice values are not POSIX standard, but both
33 * pthreads and linux support per-thread nice. The default linux
34 * scheduler uses these values to do smart thread prioritization.
35 * sched_priority function calls only affect real-time schedulers.
37 class PriorityThreadFactory : public ThreadFactory {
39 explicit PriorityThreadFactory(
40 std::shared_ptr<ThreadFactory> factory,
42 : factory_(std::move(factory)), priority_(priority) {}
44 std::thread newThread(Func&& func) override {
45 int priority = priority_;
46 return factory_->newThread([ priority, func = std::move(func) ]() mutable {
47 if (setpriority(PRIO_PROCESS, 0, priority) != 0) {
48 LOG(ERROR) << "setpriority failed (are you root?) with error " << errno,
56 std::shared_ptr<ThreadFactory> factory_;