paleolimbot commented on a change in pull request #12558:
URL: https://github.com/apache/arrow/pull/12558#discussion_r837833137



##########
File path: r/src/safe-call-into-r-impl.cpp
##########
@@ -0,0 +1,123 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// 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 "./arrow_types.h"
+#if defined(ARROW_R_WITH_ARROW)
+
+#include <functional>
+#include <thread>
+#include "./safe-call-into-r.h"
+
+static MainRThread main_r_thread;
+
+MainRThread* GetMainRThread() { return &main_r_thread; }
+
+arrow::Status MainRThread::RunTask(Task* task) {
+  if (IsMainThread()) {
+    // If we're on the main thread, run the task immediately
+    try {
+      ARROW_RETURN_NOT_OK(task->run());
+      return arrow::Status::OK();
+    } catch (cpp11::unwind_exception& e) {
+      SetError(e.token);
+      return arrow::Status::UnknownError("R code execution error");
+    }
+  } else if (executor_ != nullptr) {
+    // If we are not on the main thread and have an Executor
+    // use it to run the task on the main R thread.
+    auto fut = executor_->Submit([task]() { return task->run(); });
+    ARROW_RETURN_NOT_OK(fut);
+    ARROW_RETURN_NOT_OK(fut.ValueUnsafe().result());
+    return arrow::Status::OK();
+  } else {
+    return arrow::Status::NotImplemented(
+        "Call to R from a non-R thread without an event loop");
+  }
+}
+
+// [[arrow::export]]
+void InitializeMainRThread() { main_r_thread.Initialize(); }
+
+// [[arrow::export]]
+std::string TestSafeCallIntoR(cpp11::function r_fun_that_returns_a_string,
+                              std::string opt) {
+  if (opt == "async_with_executor") {
+    std::thread* thread_ptr;
+
+    auto result =
+        RunWithCapturedR<std::string>([&thread_ptr, 
r_fun_that_returns_a_string]() {
+          auto fut = arrow::Future<std::string>::Make();
+          thread_ptr = new std::thread([fut, r_fun_that_returns_a_string]() 
mutable {
+            auto result = SafeCallIntoR<std::string>([&] {
+              return cpp11::as_cpp<std::string>(r_fun_that_returns_a_string());
+            });
+
+            if (result.ok()) {
+              fut.MarkFinished(result.ValueUnsafe());
+            } else {
+              fut.MarkFinished(result.status());
+            }
+          });
+
+          return fut;
+        });
+
+    thread_ptr->join();
+    delete thread_ptr;
+
+    // Stop for any R execution errors that may have occurred
+    GetMainRThread()->ClearError();

Review comment:
       Good point...I moved it to `RunWithCapturedR()`.




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to