From f30b48eb7ee1759d20c62eca8c04b5fecb8e9887 Mon Sep 17 00:00:00 2001
From: stepan <stepan.sindelar@oracle.com>
Date: Tue, 19 Jun 2018 17:41:57 +0200
Subject: [PATCH] Allow to turn off the native event loop in FastRConfig

---
 .../oracle/truffle/r/launcher/RCommand.java   | 17 ++++------
 .../builtin/fastr/FastRInitEventLoop.java     | 32 +++++++++++++++----
 2 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RCommand.java b/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RCommand.java
index c9a0a1b40e..261cc14678 100644
--- a/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RCommand.java
+++ b/com.oracle.truffle.r.launcher/src/com/oracle/truffle/r/launcher/RCommand.java
@@ -336,18 +336,13 @@ public class RCommand extends RAbstractLauncher {
      * @param executor
      */
     private static void initializeNativeEventLoop(Context context, final ExecutorService executor) {
-        Path tmpDir;
-        try {
-            tmpDir = Files.createTempDirectory("fastr-fifo");
-        } catch (Exception e1) {
-            throw fatal("Cannot create temporary directory for event loop fifo");
-        }
-        final String fifoInPath = tmpDir.resolve("event-loop-fifo-in").toString();
-        final String fifoOutPath = tmpDir.resolve("event-loop-fifo-out").toString();
-        final int res = context.eval(Source.newBuilder("R", ".fastr.initEventLoop", "<init-event-loop>").internal(true).buildLiteral()).execute(fifoInPath, fifoOutPath).asInt();
-        if (res != 0) {
-            System.out.println("WARNING: Native event loop unavailable");
+        Value result = context.eval(Source.newBuilder("R", ".fastr.initEventLoop", "<init-event-loop>").internal(true).buildLiteral()).execute();
+        if (result.isNull()) {
+            return; // event loop is not configured to be run
+        } else if (result.getMember("result").asInt() != 0) {
+            System.out.println("WARNING: Native event loop unavailable. Error code: " + result.getMember("result").asInt());
         } else {
+            final String fifoInPath = result.getMember("fifoInPath").asString();
             Thread t = new Thread() {
                 @Override
                 public void run() {
diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRInitEventLoop.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRInitEventLoop.java
index 87cb281849..b1ca10a33f 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRInitEventLoop.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/fastr/FastRInitEventLoop.java
@@ -25,10 +25,16 @@ package com.oracle.truffle.r.nodes.builtin.fastr;
 import static com.oracle.truffle.r.runtime.builtins.RBehavior.COMPLEX;
 import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.PRIMITIVE;
 
+import java.nio.file.Files;
+import java.nio.file.Path;
+
 import com.oracle.truffle.api.dsl.Specialization;
 import com.oracle.truffle.r.nodes.builtin.RBuiltinNode;
+import com.oracle.truffle.r.runtime.FastRConfig;
 import com.oracle.truffle.r.runtime.builtins.RBuiltin;
 import com.oracle.truffle.r.runtime.context.RContext;
+import com.oracle.truffle.r.runtime.data.RDataFactory;
+import com.oracle.truffle.r.runtime.data.RNull;
 import com.oracle.truffle.r.runtime.ffi.BaseRFFI;
 
 /**
@@ -44,19 +50,31 @@ import com.oracle.truffle.r.runtime.ffi.BaseRFFI;
  * uses the <code>fifoOut</code> named pipe to release the native event loop. The described
  * procedure ensures that the native event handlers handle events within a single FastR context
  */
-@RBuiltin(name = ".fastr.initEventLoop", kind = PRIMITIVE, behavior = COMPLEX, parameterNames = {"fifoInPath", "fifoOutPath"})
-public abstract class FastRInitEventLoop extends RBuiltinNode.Arg2 {
+@RBuiltin(name = ".fastr.initEventLoop", kind = PRIMITIVE, behavior = COMPLEX, parameterNames = {})
+public abstract class FastRInitEventLoop extends RBuiltinNode.Arg0 {
 
     static {
-        Casts casts = new Casts(FastRInitEventLoop.class);
-        casts.arg("fifoInPath").asStringVector().findFirst();
-        casts.arg("fifoOutPath").asStringVector().findFirst();
+        Casts.noCasts(FastRInitEventLoop.class);
     }
 
     @Child private BaseRFFI.InitEventLoopNode initEventLoopNode = BaseRFFI.InitEventLoopNode.create();
 
     @Specialization
-    public Object initEventLoop(String fifoInPath, String fifoOutPath) {
-        return initEventLoopNode.execute(fifoInPath, fifoOutPath);
+    public Object initEventLoop() {
+        if (FastRConfig.UseNativeEventLoop) {
+            Path tmpDir;
+            try {
+                tmpDir = Files.createTempDirectory("fastr-fifo");
+            } catch (Exception e) {
+                return RDataFactory.createList(new Object[]{1}, RDataFactory.createStringVector("result"));
+            }
+            String fifoInPath = tmpDir.resolve("event-loop-fifo-in").toString();
+            String fifoOutPath = tmpDir.resolve("event-loop-fifo-out").toString();
+            int result = initEventLoopNode.execute(fifoInPath, fifoOutPath);
+            return RDataFactory.createList(new Object[]{result, fifoInPath, fifoOutPath},
+                            RDataFactory.createStringVector(new String[]{"result", "fifoInPath", "fifoOutPath"}, RDataFactory.COMPLETE_VECTOR));
+        } else {
+            return RNull.instance;
+        }
     }
 }
-- 
GitLab