MCPcopy Create free account
hub / github.com/apache/arrow / TestSafeCallIntoR

Function TestSafeCallIntoR

r/src/safe-call-into-r-impl.cpp:51–100  ·  view source on GitHub ↗

[[arrow::export]]

Source from the content-addressed store, hash-verified

49
50// [[arrow::export]]
51std::string TestSafeCallIntoR(cpp11::function r_fun_that_returns_a_string,
52 std::string opt) {
53 if (opt == "async_with_executor") {
54 std::thread thread;
55
56 auto result = RunWithCapturedR<std::string>([&thread, r_fun_that_returns_a_string]() {
57 auto fut = arrow::Future<std::string>::Make();
58 thread = std::thread([&fut, r_fun_that_returns_a_string]() {
59 auto result = SafeCallIntoR<std::string>(
60 [&] { return cpp11::as_cpp<std::string>(r_fun_that_returns_a_string()); });
61
62 fut.MarkFinished(result);
63 });
64
65 return fut;
66 });
67
68 if (thread.joinable()) {
69 thread.join();
70 }
71
72 return arrow::ValueOrStop(result);
73 } else if (opt == "async_without_executor") {
74 auto fut = arrow::Future<std::string>::Make();
75 std::thread thread([&fut, r_fun_that_returns_a_string]() {
76 auto result = SafeCallIntoR<std::string>(
77 [&] { return cpp11::as_cpp<std::string>(r_fun_that_returns_a_string()); });
78
79 if (result.ok()) {
80 fut.MarkFinished(result.ValueUnsafe());
81 } else {
82 fut.MarkFinished(result.status());
83 }
84 });
85
86 thread.join();
87
88 // We should be able to get this far, but fut will contain an error
89 // because it tried to evaluate R code from another thread
90 return arrow::ValueOrStop(fut.result());
91
92 } else if (opt == "on_main_thread") {
93 auto result = SafeCallIntoR<std::string>(
94 [&]() { return cpp11::as_cpp<std::string>(r_fun_that_returns_a_string()); });
95 arrow::StopIfNotOk(result.status());
96 return result.ValueUnsafe();
97 } else {
98 cpp11::stop("Unknown `opt`");
99 }
100}

Callers 1

_arrow_TestSafeCallIntoRFunction · 0.85

Calls 10

ValueOrStopFunction · 0.85
StopIfNotOkFunction · 0.85
stopFunction · 0.85
ValueUnsafeMethod · 0.80
MakeFunction · 0.50
MarkFinishedMethod · 0.45
joinMethod · 0.45
okMethod · 0.45
statusMethod · 0.45
resultMethod · 0.45

Tested by

no test coverage detected