From 0e3893e7e3e270f8231b89eaf89537cf01a43052 Mon Sep 17 00:00:00 2001
From: Michael Bien <mbien@fh-landshut.de>
Date: Thu, 2 Sep 2010 22:50:00 +0200
Subject: CLEvent callbacks for HLB and LLB.

---
 src/com/jogamp/opencl/CLEvent.java              | 19 +++++++++++++++++++
 src/com/jogamp/opencl/CLEventListener.java      | 15 +++++++++++++++
 src/com/jogamp/opencl/impl/CLEventCallback.java | 15 +++++++++++++++
 src/com/jogamp/opencl/impl/CLImpl.java          | 11 +++++++++++
 4 files changed, 60 insertions(+)
 create mode 100644 src/com/jogamp/opencl/CLEventListener.java
 create mode 100644 src/com/jogamp/opencl/impl/CLEventCallback.java

(limited to 'src/com/jogamp/opencl')

diff --git a/src/com/jogamp/opencl/CLEvent.java b/src/com/jogamp/opencl/CLEvent.java
index b7df1465..8423e7c6 100644
--- a/src/com/jogamp/opencl/CLEvent.java
+++ b/src/com/jogamp/opencl/CLEvent.java
@@ -1,7 +1,10 @@
 package com.jogamp.opencl;
 
+import com.jogamp.opencl.impl.CLEventCallback;
+import java.util.List;
 import com.jogamp.common.nio.PointerBuffer;
 import java.nio.Buffer;
+import java.util.ArrayList;
 
 import static com.jogamp.opencl.CL.*;
 import static com.jogamp.opencl.CLException.*;
@@ -25,6 +28,22 @@ public class CLEvent extends CLObject implements CLResource {
         this.eventProfilingInfo = new CLEventProfilingInfoAccessor();
     }
 
+    /**
+     * Registers a callback which will be called when the event terminates (COMPLETE or ERROR).
+     */
+    public void registerCallback(final CLEventListener callback) {
+        this.registerCallback(callback, ExecutionStatus.COMPLETE);
+    }
+
+    // apparently only ExecutionStatus.COMPLETE is allowed -> private
+    private void registerCallback(final CLEventListener callback, ExecutionStatus trigger) {
+        cl.clSetEventCallback(ID, trigger.STATUS, new CLEventCallback() {
+            public void eventStateChanged(long event, int status) {
+                callback.eventStateChanged(CLEvent.this, status);
+            }
+        });
+    }
+
     public void release() {
         int ret = cl.clReleaseEvent(ID);
         checkForError(ret, "can not release event");
diff --git a/src/com/jogamp/opencl/CLEventListener.java b/src/com/jogamp/opencl/CLEventListener.java
new file mode 100644
index 00000000..76911008
--- /dev/null
+++ b/src/com/jogamp/opencl/CLEventListener.java
@@ -0,0 +1,15 @@
+/*
+ * Created on Thursday, September 02 2010 22:01
+ */
+package com.jogamp.opencl;
+
+/**
+ * A callback for a specific command execution status.
+ * @author Michael Bien
+ * @see CLEvent#registerCallback(com.jogamp.opencl.CLEventListener) 
+ */
+public interface CLEventListener {
+
+    public void eventStateChanged(CLEvent event, int status);
+
+}
\ No newline at end of file
diff --git a/src/com/jogamp/opencl/impl/CLEventCallback.java b/src/com/jogamp/opencl/impl/CLEventCallback.java
new file mode 100644
index 00000000..db4d5b35
--- /dev/null
+++ b/src/com/jogamp/opencl/impl/CLEventCallback.java
@@ -0,0 +1,15 @@
+/*
+ * Created on Tuesday, July 06 2010 00:46
+ */
+
+package com.jogamp.opencl.impl;
+
+/**
+ * A callback for a specific command execution status.
+ * @author Michael Bien
+ */
+public interface CLEventCallback {
+
+    public void eventStateChanged(long event, int status);
+
+}
diff --git a/src/com/jogamp/opencl/impl/CLImpl.java b/src/com/jogamp/opencl/impl/CLImpl.java
index abec3597..b9dd9835 100644
--- a/src/com/jogamp/opencl/impl/CLImpl.java
+++ b/src/com/jogamp/opencl/impl/CLImpl.java
@@ -123,6 +123,17 @@ public class CLImpl extends CLAbstractImpl {
     /** Entry point to C language function: <code> int32_t clBuildProgram(cl_program, uint32_t, cl_device_id * , const char * , void * ); </code>    */
     private native int clBuildProgram0(long program, int deviceCount, Object deviceList, int deviceListOffset, String options, BuildProgramCallback cb, long address);
 
+
+    public int clSetEventCallback(long event, int trigger, CLEventCallback callback) {
+        final long address = addressTable._addressof_clSetEventCallback;
+        if (address == 0) {
+            throw new UnsupportedOperationException("Method not available");
+        }
+        return clSetEventCallback0(event, trigger, callback, address);
+    }
+
+    private native int clSetEventCallback0(long event, int type, CLEventCallback cb, long address);
+
     /** Interface to C language function: <br> <code> void *  {@native clEnqueueMapImage}(cl_command_queue command_queue, cl_mem image, uint32_t blocking_map, uint64_t map_flags, const size_t * , const size_t * , size_t *  image_row_pitch, size_t *  image_slice_pitch, uint32_t num_events_in_wait_list, cl_event *  event_wait_list, cl_event *  event, int32_t *  errcode_ret); </code>
     @param origin a direct {@link com.jogamp.common.nio.PointerBuffer}
     @param range a direct {@link com.jogamp.common.nio.PointerBuffer}
-- 
cgit v1.2.3