From c8ae2569a4d90e79b9af83acdd50c1be2ecab9f0 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Sun, 30 May 2010 04:01:57 +0200 Subject: improved concurrent load test (disabled by default) and confirmed that cl programs can't be built concurrently. CLProgram uses ReentrantLock to put synchronous and asynchronous calls to clBuildProgram(...) in a squence. --- src/com/jogamp/opencl/CLProgram.java | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'src/com') diff --git a/src/com/jogamp/opencl/CLProgram.java b/src/com/jogamp/opencl/CLProgram.java index b6c1d7a2..7ccc3bc8 100644 --- a/src/com/jogamp/opencl/CLProgram.java +++ b/src/com/jogamp/opencl/CLProgram.java @@ -15,6 +15,7 @@ import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Set; import java.util.Map; +import java.util.concurrent.locks.ReentrantLock; import static com.jogamp.opencl.CLException.*; import static com.jogamp.opencl.CL.*; @@ -30,7 +31,7 @@ import static com.jogamp.common.nio.Buffers.*; */ public class CLProgram extends CLObject implements CLResource { -// private final static Object buildLock = new Object(); + private final static ReentrantLock buildLock = new ReentrantLock(); private final Set kernels; private Map buildStatusMap; @@ -317,6 +318,7 @@ public class CLProgram extends CLObject implements CLResource { if(listener != null) { callback = new BuildProgramCallback() { public void buildFinished(long cl_program) { + buildLock.unlock(); listener.buildFinished(CLProgram.this); } }; @@ -324,10 +326,21 @@ public class CLProgram extends CLObject implements CLResource { // Build the program int ret = 0; - // building programs is not threadsafe -// synchronized(buildLock) { - ret = cl.clBuildProgram(ID, count, deviceIDs, options, callback); -// } + + // spec: building programs is not threadsafe, we are locking the API call to + // make sure only one thread calls it at a time until it completes (asynchronous or synchronously). + { + buildLock.lock(); + boolean exception = true; + try{ + ret = cl.clBuildProgram(ID, count, deviceIDs, options, callback); + exception = false; + }finally{ + if(callback == null || exception) { + buildLock.unlock(); + } + } + } if(ret != CL_SUCCESS) { throw newException(ret, "\n"+getBuildLog()); -- cgit v1.2.3