From 6e142c0d2361e9be62b494509a5339237317cf11 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Wed, 1 Nov 2017 14:11:27 -0700 Subject: [PATCH] Fix unsynchronized accesses in IOThreadPoolExecutor::getEventBase Summary: [Folly] Fix unsynchronized accesses in `IOThreadPoolExecutor::getEventBase`. `getEventBase` may be invoked concurrently from two threads - RMWs to `nextThread_` must be synchronized with each other. `getEventBase` may be invoked concurrently with `setNumThreads` - the former's reads of `threadList_.vec_` must be synchronized with the latter's writes to it. Reviewed By: kennyyu Differential Revision: D6206916 fbshipit-source-id: 8bfae158effb5896ab478d0c20310293b037c892 --- folly/executors/IOThreadPoolExecutor.cpp | 3 ++- folly/executors/IOThreadPoolExecutor.h | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/folly/executors/IOThreadPoolExecutor.cpp b/folly/executors/IOThreadPoolExecutor.cpp index 7e16e80a..63601fdc 100644 --- a/folly/executors/IOThreadPoolExecutor.cpp +++ b/folly/executors/IOThreadPoolExecutor.cpp @@ -119,11 +119,12 @@ IOThreadPoolExecutor::pickThread() { if (n == 0) { return me; } - auto thread = ths[nextThread_++ % n]; + auto thread = ths[nextThread_.fetch_add(1, std::memory_order_relaxed) % n]; return std::static_pointer_cast(thread); } EventBase* IOThreadPoolExecutor::getEventBase() { + RWSpinLock::ReadHolder r{&threadListLock_}; return pickThread()->eventBase; } diff --git a/folly/executors/IOThreadPoolExecutor.h b/folly/executors/IOThreadPoolExecutor.h index cf52822c..b7c6688d 100644 --- a/folly/executors/IOThreadPoolExecutor.h +++ b/folly/executors/IOThreadPoolExecutor.h @@ -16,6 +16,8 @@ #pragma once +#include + #include #include #include @@ -86,7 +88,7 @@ class IOThreadPoolExecutor : public ThreadPoolExecutor, public IOExecutor { void stopThreads(size_t n) override; uint64_t getPendingTaskCountImpl(const RWSpinLock::ReadHolder&) override; - size_t nextThread_; + std::atomic nextThread_; folly::ThreadLocal> thisThread_; folly::EventBaseManager* eventBaseManager_; }; -- 2.34.1