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
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
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
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
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.
|
|
|
|
*/
|
|
|
|
|
2018-10-20 01:30:13 +08:00
|
|
|
#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"
|
|
|
|
|
2020-10-08 12:58:24 +08:00
|
|
|
class ThreadPool final : public IThreadPool, public ReferenceCounted<ThreadPool> {
|
2017-05-26 04:48:44 +08:00
|
|
|
struct Thread {
|
2021-03-11 02:06:03 +08:00
|
|
|
ThreadPool* pool;
|
2017-05-26 04:48:44 +08:00
|
|
|
IThreadPoolReceiver* userObject;
|
2021-03-11 05:24:55 +08:00
|
|
|
THREAD_HANDLE handle; // Owned by main thread
|
2017-05-26 04:48:44 +08:00
|
|
|
static thread_local IThreadPoolReceiver* threadUserObject;
|
2021-03-11 02:06:03 +08:00
|
|
|
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();
|
2021-03-11 02:06:03 +08:00
|
|
|
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);
|
|
|
|
}
|
2020-06-21 21:17:55 +08:00
|
|
|
delete userObject;
|
|
|
|
userObject = nullptr;
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
static void dispatch(PThreadAction action) { (*action)(threadUserObject); }
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
2021-03-11 02:06:03 +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;
|
2021-03-11 02:06:03 +08:00
|
|
|
enum Mode { Run = 0, Shutdown = 2 };
|
2017-05-26 04:48:44 +08:00
|
|
|
volatile int mode;
|
2020-06-23 16:32:08 +08:00
|
|
|
int stackSize;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
struct ActionWrapper {
|
|
|
|
PThreadAction action;
|
|
|
|
ActionWrapper(PThreadAction action) : action(action) {}
|
2021-03-11 02:06:03 +08:00
|
|
|
// 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
|
|
|
};
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
public:
|
2020-06-23 16:32:08 +08:00
|
|
|
ThreadPool(int stackSize) : dontstop(ios), mode(Run), stackSize(stackSize) {}
|
2021-01-26 09:55:43 +08:00
|
|
|
~ThreadPool() override {}
|
2020-10-08 06:55:11 +08:00
|
|
|
Future<Void> stop(Error const& e = success()) override {
|
2021-03-11 02:06:03 +08:00
|
|
|
if (mode == Shutdown)
|
|
|
|
return Void();
|
2017-05-26 04:48:44 +08:00
|
|
|
ReferenceCounted<ThreadPool>::addref();
|
|
|
|
ios.stop(); // doesn't work?
|
|
|
|
mode = Shutdown;
|
2021-03-11 02:06:03 +08:00
|
|
|
for (int i = 0; i < threads.size(); i++) {
|
2021-03-11 05:24:55 +08:00
|
|
|
waitThread(threads[i]->handle);
|
2017-05-26 04:48:44 +08:00
|
|
|
delete threads[i];
|
|
|
|
}
|
|
|
|
ReferenceCounted<ThreadPool>::delref();
|
|
|
|
return Void();
|
|
|
|
}
|
2021-03-10 13:08:58 +08:00
|
|
|
|
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 {
|
2021-03-11 05:24:55 +08:00
|
|
|
if (ReferenceCounted<ThreadPool>::delref_no_destroy()) {
|
2021-03-11 02:06:03 +08:00
|
|
|
stop();
|
2021-03-11 05:24:55 +08:00
|
|
|
delete this;
|
|
|
|
}
|
2020-10-08 06:55:11 +08:00
|
|
|
}
|
2021-03-10 13:08:58 +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));
|
2021-03-11 05:24:55 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +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;
|