ed46dd3549eabab0c8b974fd1707a782ca36f311
[folly.git] / folly / executors / PriorityThreadFactory.h
1 /*
2  * Copyright 2017 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
17 #pragma once
18
19 #include <folly/executors/ThreadFactory.h>
20
21 #include <folly/portability/SysResource.h>
22 #include <folly/portability/SysTime.h>
23
24 namespace folly {
25
26 /**
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.
31  *
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.
36  */
37 class PriorityThreadFactory : public ThreadFactory {
38  public:
39   explicit PriorityThreadFactory(
40       std::shared_ptr<ThreadFactory> factory,
41       int priority)
42       : factory_(std::move(factory)), priority_(priority) {}
43
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,
49             strerror(errno);
50       }
51       func();
52     });
53   }
54
55  private:
56   std::shared_ptr<ThreadFactory> factory_;
57   int priority_;
58 };
59
60 } // folly