From 31050a5a9e96fb46004668b219e501d1846f6a09 Mon Sep 17 00:00:00 2001 From: IjzerenHein Date: Fri, 15 Dec 2017 11:44:17 +0100 Subject: [PATCH] Made RNGLContext.java thread-safe (fixes rare exception when a shader is used in multiple surfaces at the same time) + can potentionally explain/fix other witnessed crashes --- .../projectseptember/RNGL/RNGLContext.java | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/android/src/main/java/com/projectseptember/RNGL/RNGLContext.java b/android/src/main/java/com/projectseptember/RNGL/RNGLContext.java index 5817372..ec02e0e 100644 --- a/android/src/main/java/com/projectseptember/RNGL/RNGLContext.java +++ b/android/src/main/java/com/projectseptember/RNGL/RNGLContext.java @@ -38,29 +38,43 @@ public class RNGLContext extends ReactContextBaseJavaModule { } public GLShaderData getShader (Integer id) { - return shaders.get(id); + GLShaderData data; + synchronized (this) { + data = shaders.get(id); + } + return data; } @ReactMethod public void addShader (final Integer id, final ReadableMap config, final Callback onCompile) { final String frag = config.getString("frag"); final String name = config.getString("name"); - shaders.put(id, new GLShaderData(name, STATIC_VERT, frag)); - if (onCompile != null) { - onCompileCallbacks.put(id, onCompile); + GLShaderData data = new GLShaderData(name, STATIC_VERT, frag); + synchronized (this) { + shaders.put(id, data); + if (onCompile != null) { + onCompileCallbacks.put(id, onCompile); + } } } @ReactMethod public void removeShader (final Integer id) { - GLShaderData shader = shaders.remove(id); + GLShaderData shader; + synchronized (this) { + shader = shaders.remove(id); + } if (shader == null) { throw new Error("removeShader("+id+"): shader does not exist"); } } public void shaderFailedToCompile(Integer id, GLShaderCompilationFailed e) { - Callback onCompile = onCompileCallbacks.get(id); + Callback onCompile; + synchronized (this) { + onCompile = onCompileCallbacks.get(id); + onCompileCallbacks.remove(id); + } if (onCompile == null) { Log.e("RNGLContext", e.getMessage()); } @@ -70,8 +84,11 @@ public class RNGLContext extends ReactContextBaseJavaModule { } public void shaderSucceedToCompile(Integer id, Map uniformTypes) { - Callback onCompile = onCompileCallbacks.get(id); - onCompileCallbacks.remove(id); + Callback onCompile; + synchronized (this) { + onCompile = onCompileCallbacks.get(id); + onCompileCallbacks.remove(id); + } if (onCompile != null) { WritableMap res = Arguments.createMap(); WritableMap uniforms = Arguments.createMap(); -- 2.26.2