From 3493673db88c5c6b39146d2909ca3228d7485a57 Mon Sep 17 00:00:00 2001
From: Tomas Stupka <tomas.stupka@oracle.com>
Date: Thu, 7 Dec 2017 17:04:23 +0100
Subject: [PATCH] initialize RGridGraphicsAdapter on each RContext/REngine
 initialization

---
 .../com/oracle/truffle/r/engine/REngine.java   |  2 +-
 .../truffle/r/library/graphics/RGraphics.java  | 18 ++++++++++--------
 .../truffle/r/runtime/context/RContext.java    |  1 +
 3 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/REngine.java b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/REngine.java
index ce6a668a1d..3fc8de06e5 100644
--- a/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/REngine.java
+++ b/com.oracle.truffle.r.engine/src/com/oracle/truffle/r/engine/REngine.java
@@ -168,7 +168,7 @@ final class REngine implements Engine, Engine.Timings {
         REnvironment.baseInitialize(baseFrame, globalFrame);
         context.getStateRFFI().initializeVariables(context);
         RBuiltinPackages.loadBase(context.getLanguage(), baseFrame);
-        RGraphics.initialize();
+        RGraphics.initialize(context);
         if (FastROptions.LoadProfiles.getBooleanValue()) {
             StartupTiming.timestamp("Before Profiles Loaded");
             /*
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/RGraphics.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/RGraphics.java
index 657822c217..edb375298a 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/RGraphics.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/RGraphics.java
@@ -18,6 +18,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 
 import com.oracle.truffle.r.library.fastrGrid.graphics.RGridGraphicsAdapter;
 import com.oracle.truffle.r.runtime.FastROptions;
+import com.oracle.truffle.r.runtime.context.RContext;
 import com.oracle.truffle.r.runtime.ffi.CallRFFI;
 import com.oracle.truffle.r.runtime.ffi.DLL;
 import com.oracle.truffle.r.runtime.ffi.NativeCallInfo;
@@ -30,16 +31,17 @@ import com.oracle.truffle.r.runtime.ffi.NativeCallInfo;
 public class RGraphics {
     private static final AtomicBoolean initialized = new AtomicBoolean();
 
-    public static void initialize() {
-        if (initialized.compareAndSet(false, true)) {
-            if (FastROptions.UseInternalGridGraphics.getBooleanValue()) {
+    public static void initialize(RContext context) {
+        if (FastROptions.UseInternalGridGraphics.getBooleanValue()) {
+            if (!context.internalGraphicsInitialized) {
                 RGridGraphicsAdapter.initialize();
-            } else if (FastROptions.LoadPackagesNativeCode.getBooleanValue()) {
-                DLL.DLLInfo dllInfo = DLL.findLibraryContainingSymbol("InitGraphics");
-                DLL.SymbolHandle symbolHandle = DLL.findSymbol("InitGraphics", dllInfo);
-                assert symbolHandle != DLL.SYMBOL_NOT_FOUND;
-                CallRFFI.InvokeVoidCallRootNode.create().getCallTarget().call(new NativeCallInfo("InitGraphics", symbolHandle, dllInfo), new Object[0]);
             }
+        } else if (initialized.compareAndSet(false, true) && FastROptions.LoadPackagesNativeCode.getBooleanValue()) {
+            DLL.DLLInfo dllInfo = DLL.findLibraryContainingSymbol("InitGraphics");
+            DLL.SymbolHandle symbolHandle = DLL.findSymbol("InitGraphics", dllInfo);
+            assert symbolHandle != DLL.SYMBOL_NOT_FOUND;
+            CallRFFI.InvokeVoidCallRootNode.create().getCallTarget().call(new NativeCallInfo("InitGraphics", symbolHandle, dllInfo), new Object[0]);
         }
+        context.internalGraphicsInitialized = true;
     }
 }
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/RContext.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/RContext.java
index 1f9abdcb98..28c057b810 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/RContext.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/context/RContext.java
@@ -354,6 +354,7 @@ public final class RContext {
     // Context specific state required for libraries, the initialization is handled lazily by the
     // concrete library.
     public Object gridContext = null;
+    public boolean internalGraphicsInitialized = false;
 
     public final WeakHashMap<String, WeakReference<String>> stringMap = new WeakHashMap<>();
     public final WeakHashMap<Source, REnvironment> sourceRefEnvironments = new WeakHashMap<>();
-- 
GitLab