westonpace commented on a change in pull request #9892:
URL: https://github.com/apache/arrow/pull/9892#discussion_r608243354
##########
File path: cpp/src/arrow/util/thread_pool_test.cc
##########
@@ -123,6 +123,125 @@ class AddTester {
std::vector<int> outs_;
};
+template <typename T = arrow::detail::Empty>
+struct TerminalCallback {
+ void operator()() {
+ auto result = std::move(callback)();
+ std::move(finish_signal)(result);
+ }
+
+ FnOnce<Result<T>()> callback;
+ SerialExecutor::FinishSignal<T> finish_signal;
+};
+
+template <>
+struct TerminalCallback<arrow::detail::Empty> {
+ void operator()() {
+ auto st = std::move(callback)();
+ if (!st.ok()) {
+ std::move(finish_signal)(st);
+ } else {
+ std::move(finish_signal)(arrow::detail::Empty());
+ }
+ }
+
+ FnOnce<Status()> callback;
+ SerialExecutor::FinishSignal<> finish_signal;
+};
+
+TEST(TestSerialExecutor, Create) {
+ bool task_ran = false;
+ SerialExecutor::Scheduler<> task = [&](Executor* executor,
+ SerialExecutor::FinishSignal<>
finish_signal) {
+ EXPECT_TRUE(executor != nullptr);
+ task_ran = true;
+ std::move(finish_signal)(arrow::detail::Empty());
+ return Status::OK();
+ };
+ ASSERT_OK(SerialExecutor::RunInSerialExecutor(std::move(task)));
+ EXPECT_TRUE(task_ran);
+}
+
+TEST(TestSerialExecutor, SpawnNested) {
+ bool nested_ran = false;
+ SerialExecutor::Scheduler<> scheduler =
+ [&](Executor* executor, SerialExecutor::FinishSignal<> finish_signal) {
+ return executor->Spawn(TerminalCallback<>{[&] {
+ nested_ran = true;
+ return Status::OK();
+ },
+ std::move(finish_signal)});
+ };
+ ASSERT_OK(SerialExecutor::RunInSerialExecutor(std::move(scheduler)));
+ EXPECT_TRUE(nested_ran);
+}
+
+TEST(TestSerialExecutor, WithResult) {
+ SerialExecutor::Scheduler<int> scheduler =
+ [&](Executor* executor, SerialExecutor::FinishSignal<int> finish_signal)
{
+ return executor->Spawn(
+ TerminalCallback<int>{[] { return 42; },
std::move(finish_signal)});
+ };
+ ASSERT_OK_AND_EQ(42,
SerialExecutor::RunInSerialExecutor(std::move(scheduler)));
+}
+
+TEST(TestSerialExecutor, StopToken) {
+ bool nested_ran = false;
+ StopSource stop_source;
+ SerialExecutor::Scheduler<> scheduler =
+ [&](Executor* executor, SerialExecutor::FinishSignal<> finish_signal) {
+ RETURN_NOT_OK(executor->Spawn([&] { nested_ran = true; },
stop_source.token()));
+ RETURN_NOT_OK(executor->Spawn(
+ TerminalCallback<>{[&] { return Status::OK(); },
std::move(finish_signal)}));
+ stop_source.RequestStop(Status::Invalid("XYZ"));
+ return Status::OK();
+ };
+ ASSERT_OK(SerialExecutor::RunInSerialExecutor(std::move(scheduler)));
+ EXPECT_FALSE(nested_ran);
+}
+
+TEST(TestSerialExecutor, ContinueAfterExternal) {
+ bool continuation_ran = false;
+ EXPECT_OK_AND_ASSIGN(auto mockIoPool, ThreadPool::Make(1));
Review comment:
Thanks. Fixed.
##########
File path: cpp/src/arrow/util/thread_pool.cc
##########
@@ -44,6 +44,63 @@ struct Task {
} // namespace
+struct SerialExecutor::State {
+ std::queue<Task> task_queue;
+ std::mutex mutex;
+ std::condition_variable wait_for_tasks;
+};
+
+SerialExecutor::SerialExecutor() : state_(new State()) {}
+SerialExecutor::~SerialExecutor() {}
+
+Status SerialExecutor::SpawnReal(TaskHints hints, FnOnce<void()> task,
+ StopToken stop_token, StopCallback&&
stop_callback) {
+ // The serial task queue is truly serial (no mutex needed) but SpawnReal may
be called
+ // from external threads (e.g. when transferring back from blocking I/O
threads) so a
+ // mutex is needed
+ {
+ std::lock_guard<std::mutex> lg(state_->mutex);
+ state_->task_queue.push(
+ Task{std::move(task), std::move(stop_token),
std::move(stop_callback)});
+ }
+ state_->wait_for_tasks.notify_one();
+ return Status::OK();
+}
+
+void SerialExecutor::MarkFinished(bool& finished) {
Review comment:
Fixed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]