foundationdb/flow/IThreadPool.cpp

122 lines
3.7 KiB
C++
Raw Normal View History

2017-05-26 04:48:44 +08:00
/*
* IThreadPool.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
*
2017-05-26 04:48:44 +08:00
* 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
*
2017-05-26 04:48:44 +08:00
* http://www.apache.org/licenses/LICENSE-2.0
*
2017-05-26 04:48:44 +08:00
* 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 "flow/IThreadPool.h"
2017-05-26 04:48:44 +08:00
#include <algorithm>
#define BOOST_SYSTEM_NO_LIB
#define BOOST_DATE_TIME_NO_LIB
#define BOOST_REGEX_NO_LIB
#include "boost/asio.hpp"
#include "boost/bind.hpp"
class ThreadPool final : public IThreadPool, public ReferenceCounted<ThreadPool> {
2017-05-26 04:48:44 +08:00
struct Thread {
ThreadPool* pool;
2017-05-26 04:48:44 +08:00
IThreadPoolReceiver* userObject;
THREAD_HANDLE handle; // Owned by main thread
2017-05-26 04:48:44 +08:00
static thread_local IThreadPoolReceiver* threadUserObject;
explicit Thread(ThreadPool* pool, IThreadPoolReceiver* userObject) : pool(pool), userObject(userObject) {}
2018-04-23 18:37:27 +08:00
~Thread() { ASSERT_ABORT(!userObject); }
2017-05-26 04:48:44 +08:00
void run() {
deprioritizeThread();
threadUserObject = userObject;
try {
userObject->init();
while (pool->ios.run_one() && (pool->mode == Mode::Run))
;
2017-05-26 04:48:44 +08:00
} catch (Error& e) {
TraceEvent(SevError, "ThreadPoolError").error(e);
}
delete userObject;
userObject = nullptr;
2017-05-26 04:48:44 +08:00
}
static void dispatch(PThreadAction action) { (*action)(threadUserObject); }
2017-05-26 04:48:44 +08:00
};
THREAD_FUNC start(void* p) {
2017-05-26 04:48:44 +08:00
((Thread*)p)->run();
THREAD_RETURN;
}
std::vector<Thread*> threads;
boost::asio::io_service ios;
boost::asio::io_service::work dontstop;
enum Mode { Run = 0, Shutdown = 2 };
2017-05-26 04:48:44 +08:00
volatile int mode;
int stackSize;
2017-05-26 04:48:44 +08:00
struct ActionWrapper {
PThreadAction action;
ActionWrapper(PThreadAction action) : action(action) {}
// HACK: Boost won't use move constructors, so we just assume the last copy made is the one that will be called
// or cancelled
ActionWrapper(ActionWrapper const& r) : action(r.action) { const_cast<ActionWrapper&>(r).action = nullptr; }
void operator()() {
Thread::dispatch(action);
action = nullptr;
}
~ActionWrapper() {
if (action) {
action->cancel();
}
}
ActionWrapper& operator=(ActionWrapper const&) = delete;
2017-05-26 04:48:44 +08:00
};
2017-05-26 04:48:44 +08:00
public:
ThreadPool(int stackSize) : dontstop(ios), mode(Run), stackSize(stackSize) {}
~ThreadPool() override {}
2020-10-08 06:55:11 +08:00
Future<Void> stop(Error const& e = success()) override {
if (mode == Shutdown)
return Void();
2017-05-26 04:48:44 +08:00
ReferenceCounted<ThreadPool>::addref();
ios.stop(); // doesn't work?
mode = Shutdown;
for (int i = 0; i < threads.size(); i++) {
waitThread(threads[i]->handle);
2017-05-26 04:48:44 +08:00
delete threads[i];
}
ReferenceCounted<ThreadPool>::delref();
return Void();
}
2020-10-08 06:55:11 +08:00
Future<Void> getError() const override { return Never(); } // FIXME
void addref() override { ReferenceCounted<ThreadPool>::addref(); }
void delref() override {
if (ReferenceCounted<ThreadPool>::delref_no_destroy()) {
stop();
delete this;
}
2020-10-08 06:55:11 +08:00
}
void addThread(IThreadPoolReceiver* userData, const char* name) override {
2017-05-26 04:48:44 +08:00
threads.push_back(new Thread(this, userData));
threads.back()->handle = startThread(start, threads.back(), stackSize);
2017-05-26 04:48:44 +08:00
}
2020-10-08 06:55:11 +08:00
void post(PThreadAction action) override { ios.post(ActionWrapper(action)); }
2017-05-26 04:48:44 +08:00
};
Reference<IThreadPool> createGenericThreadPool(int stackSize) {
return Reference<IThreadPool>(new ThreadPool(stackSize));
2017-05-26 04:48:44 +08:00
}
thread_local IThreadPoolReceiver* ThreadPool::Thread::threadUserObject;