Skip to content

Commit a026a10

Browse files
otfacebook-github-bot
authored andcommitted
Test thread priority inheritance on add() on non-dynamic CPUThreadPoolExecutor
Summary: If `CPUThreadPoolExecutor` is configured without dynamic thread creation, it is expected that no new threads are created on `add()`. This is important because `add()` may come from threads with different priority, and that would be inherited by the newly created threads. Using static size protects from that behavior. This is widely depended on, so we should have a test for it. Reviewed By: uvdn7 Differential Revision: D79378022 fbshipit-source-id: 3ccbd305a001b0cdd09e7142a6ee0ddb1a44c73a
1 parent 6f0cc6d commit a026a10

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

third-party/folly/src/folly/executors/test/ThreadPoolExecutorTest.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,31 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include <folly/CPortability.h>
18-
#include <folly/DefaultKeepAliveExecutor.h>
19-
#include <folly/executors/CPUThreadPoolExecutor.h>
20-
#include <folly/executors/ThreadPoolExecutor.h>
21-
#include <folly/lang/Keep.h>
22-
#include <folly/synchronization/Latch.h>
23-
2417
#include <atomic>
2518
#include <memory>
2619
#include <thread>
2720

2821
#include <boost/thread.hpp>
29-
22+
#include <folly/CPortability.h>
23+
#include <folly/DefaultKeepAliveExecutor.h>
3024
#include <folly/Exception.h>
3125
#include <folly/container/F14Map.h>
26+
#include <folly/executors/CPUThreadPoolExecutor.h>
3227
#include <folly/executors/EDFThreadPoolExecutor.h>
3328
#include <folly/executors/FutureExecutor.h>
3429
#include <folly/executors/IOThreadPoolExecutor.h>
30+
#include <folly/executors/ThreadPoolExecutor.h>
3531
#include <folly/executors/VirtualExecutor.h>
3632
#include <folly/executors/task_queue/LifoSemMPMCQueue.h>
3733
#include <folly/executors/task_queue/UnboundedBlockingQueue.h>
3834
#include <folly/executors/thread_factory/InitThreadFactory.h>
3935
#include <folly/executors/thread_factory/PriorityThreadFactory.h>
36+
#include <folly/lang/Keep.h>
4037
#include <folly/portability/GMock.h>
4138
#include <folly/portability/GTest.h>
4239
#include <folly/portability/PThread.h>
4340
#include <folly/portability/SysResource.h>
41+
#include <folly/synchronization/Latch.h>
4442
#include <folly/synchronization/detail/Spin.h>
4543
#include <folly/system/ThreadId.h>
4644

@@ -626,6 +624,29 @@ TEST(ThreadPoolExecutorTest, BlockingQueue) {
626624
EXPECT_EQ(5, c);
627625
}
628626

627+
TEST(ThreadPoolExecutorTest, NoThreadPriorityInheritance) {
628+
constexpr size_t kNumThreads = 16;
629+
const auto initialPriority = getpriority(PRIO_PROCESS, 0);
630+
631+
// If minThreads == maxThreads, no threads should be created on add(), and
632+
// thus they should not inherit the parent thread's priority. Instead, all
633+
// threads should inherit the priority at the time of construction.
634+
CPUThreadPoolExecutor exe{std::make_pair(kNumThreads, kNumThreads)};
635+
636+
// Nice the current thread.
637+
setpriority(PRIO_PROCESS, 0, 19);
638+
639+
Latch ready{kNumThreads + 1};
640+
for (size_t i = 0; i < kNumThreads; ++i) {
641+
exe.add([&] {
642+
EXPECT_EQ(getpriority(PRIO_PROCESS, 0), initialPriority);
643+
ready.arrive_and_wait();
644+
});
645+
}
646+
ready.arrive_and_wait();
647+
exe.join();
648+
}
649+
629650
TEST(PriorityThreadFactoryTest, ThreadPriority) {
630651
errno = 0;
631652
auto currentPriority = getpriority(PRIO_PROCESS, 0);

0 commit comments

Comments
 (0)