summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYang Rong <rong.r.yang@intel.com>2016-12-16 18:39:17 +0800
committerYang Rong <rong.r.yang@intel.com>2016-12-16 20:37:03 +0800
commit05bdfda145ceeb8a5f352d1eda8bdaddb89ecbe5 (patch)
tree4d2be06c6f4f78f8eaedec87b07ab0b182cf3c89 /src
parent2256747a06fc323025fb81d50bcd5b2f45e98e32 (diff)
Refine clSetMemObjectDestructorCallback API.
Signed-off-by: Junyan He <junyan.he@intel.com> Reviewed-by: Yang Rong <rong.r.yang@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/cl_api.c27
-rw-r--r--src/cl_api_mem.c14
-rw-r--r--src/cl_mem.c43
-rw-r--r--src/cl_mem.h9
4 files changed, 54 insertions, 39 deletions
diff --git a/src/cl_api.c b/src/cl_api.c
index 2d97c251..d7b5434e 100644
--- a/src/cl_api.c
+++ b/src/cl_api.c
@@ -233,7 +233,7 @@ clCreateCommandQueueWithProperties(cl_context context,
if(queue_sz == 0xFFFFFFFF) queue_sz = device->queue_on_device_preferred_size;
INVALID_VALUE_IF (queue_sz > device->queue_on_device_max_size);
- queue = cl_context_create_queue(context, device, prop, &err);
+ queue = cl_create_command_queue(context, device, prop, &err);
queue->size = queue_sz;
error:
if (errcode_ret)
@@ -795,31 +795,6 @@ error:
return err;
}
-cl_int
-clSetMemObjectDestructorCallback(cl_mem memobj,
- void (CL_CALLBACK *pfn_notify) (cl_mem, void*),
- void * user_data)
-{
- cl_int err = CL_SUCCESS;
- CHECK_MEM(memobj);
- INVALID_VALUE_IF (pfn_notify == 0);
-
- cl_mem_dstr_cb *cb = (cl_mem_dstr_cb*)malloc(sizeof(cl_mem_dstr_cb));
- if (!cb) {
- err = CL_OUT_OF_HOST_MEMORY;
- goto error;
- }
-
- memset(cb, 0, sizeof(cl_mem_dstr_cb));
- cb->pfn_notify = pfn_notify;
- cb->user_data = user_data;
- cb->next = memobj->dstr_cb;
- memobj->dstr_cb = cb;
-
-error:
- return err;
-}
-
cl_sampler
clCreateSamplerWithProperties(cl_context context,
const cl_sampler_properties *sampler_properties,
diff --git a/src/cl_api_mem.c b/src/cl_api_mem.c
index 6dae06e4..07be7060 100644
--- a/src/cl_api_mem.c
+++ b/src/cl_api_mem.c
@@ -23,6 +23,20 @@
#include "CL/cl.h"
cl_int
+clSetMemObjectDestructorCallback(cl_mem memobj,
+ void(CL_CALLBACK *pfn_notify)(cl_mem, void *),
+ void *user_data)
+{
+ if (!CL_OBJECT_IS_MEM(memobj))
+ return CL_INVALID_MEM_OBJECT;
+
+ if (pfn_notify == NULL)
+ return CL_INVALID_VALUE;
+
+ return cl_mem_set_destructor_callback(memobj, pfn_notify, user_data);
+}
+
+cl_int
clGetMemObjectInfo(cl_mem memobj,
cl_mem_info param_name,
size_t param_value_size,
diff --git a/src/cl_mem.c b/src/cl_mem.c
index b5f9451b..d105bff5 100644
--- a/src/cl_mem.c
+++ b/src/cl_mem.c
@@ -143,6 +143,7 @@ cl_mem_allocate(enum cl_mem_type type,
}
CL_OBJECT_INIT_BASE(mem, CL_OBJECT_MEM_MAGIC);
+ list_init(&mem->dstr_cb_head);
mem->type = type;
mem->flags = flags;
mem->is_userptr = 0;
@@ -450,6 +451,7 @@ cl_mem_new_sub_buffer(cl_mem buffer,
mem = &sub_buf->base;
CL_OBJECT_INIT_BASE(mem, CL_OBJECT_MEM_MAGIC);
+ list_init(&mem->dstr_cb_head);
mem->type = CL_MEM_SUBBUFFER_TYPE;
mem->flags = flags;
mem->offset = buffer->offset;
@@ -606,6 +608,7 @@ void* cl_mem_svm_allocate(cl_context ctx, cl_svm_mem_flags flags,
mem->type = CL_MEM_SVM_TYPE;
CL_OBJECT_INIT_BASE(mem, CL_OBJECT_MEM_MAGIC);
+ list_init(&mem->dstr_cb_head);
mem->flags = flags | CL_MEM_USE_HOST_PTR;
mem->is_userptr = 0;
mem->is_svm = 0;
@@ -1207,6 +1210,8 @@ LOCAL void
cl_mem_delete(cl_mem mem)
{
cl_int i;
+ cl_mem_dstr_cb cb = NULL;
+
if (UNLIKELY(mem == NULL))
return;
if (CL_OBJECT_DEC_REF(mem) > 1)
@@ -1222,6 +1227,14 @@ cl_mem_delete(cl_mem mem)
cmrt_destroy_memory(mem);
#endif
+ /* First, call all the callbacks registered by user. */
+ while (!list_empty(&mem->dstr_cb_head)) {
+ cb = list_entry(mem->dstr_cb_head.next, _cl_mem_dstr_cb, node);
+ list_del(&cb->node);
+ cb->pfn_notify(mem, cb->user_data);
+ cl_free(cb);
+ }
+
/* iff we are a image, delete the 1d buffer if has. */
if (IS_IMAGE(mem)) {
if (cl_mem_image(mem)->buffer_1d) {
@@ -1259,16 +1272,6 @@ cl_mem_delete(cl_mem mem)
if (mem->mapped_ptr)
free(mem->mapped_ptr);
- if (mem->dstr_cb) {
- cl_mem_dstr_cb *cb = mem->dstr_cb;
- while (mem->dstr_cb) {
- cb = mem->dstr_cb;
- cb->pfn_notify(mem, cb->user_data);
- mem->dstr_cb = cb->next;
- free(cb);
- }
- }
-
/* Iff we are sub, do nothing for bo release. */
if (mem->type == CL_MEM_SUBBUFFER_TYPE) {
struct _cl_mem_buffer* buffer = (struct _cl_mem_buffer*)mem;
@@ -2456,3 +2459,23 @@ error:
*mem_ptr = NULL;
return err;
}
+
+LOCAL cl_int
+cl_mem_set_destructor_callback(cl_mem memobj,
+ void(CL_CALLBACK *pfn_notify)(cl_mem, void *), void *user_data)
+{
+ cl_mem_dstr_cb cb = cl_calloc(1, sizeof(_cl_mem_dstr_cb));
+ if (cb == NULL) {
+ return CL_OUT_OF_HOST_MEMORY;
+ }
+
+ memset(cb, 0, sizeof(_cl_mem_dstr_cb));
+ list_init(&cb->node);
+ cb->pfn_notify = pfn_notify;
+ cb->user_data = user_data;
+
+ CL_OBJECT_LOCK(memobj);
+ list_add(&cb->node, &memobj->dstr_cb_head, memobj->dstr_cb_head.next);
+ CL_OBJECT_UNLOCK(memobj);
+ return CL_SUCCESS;
+}
diff --git a/src/cl_mem.h b/src/cl_mem.h
index 8b96268b..dbe4becd 100644
--- a/src/cl_mem.h
+++ b/src/cl_mem.h
@@ -64,10 +64,11 @@ typedef struct _cl_mapped_ptr {
}cl_mapped_ptr;
typedef struct _cl_mem_dstr_cb {
- struct _cl_mem_dstr_cb * next;
+ list_head node; /* Mem callback list node */
void (CL_CALLBACK *pfn_notify)(cl_mem memobj, void *user_data);
void *user_data;
-}cl_mem_dstr_cb;
+} _cl_mem_dstr_cb;
+typedef _cl_mem_dstr_cb* cl_mem_dstr_cb;
/* Used for buffers and images */
enum cl_mem_type {
@@ -94,7 +95,7 @@ typedef struct _cl_mem {
int mapped_ptr_sz; /* The array size of mapped_ptr. */
int map_ref; /* The mapped count. */
uint8_t mapped_gtt; /* This object has mapped gtt, for unmap. */
- cl_mem_dstr_cb *dstr_cb; /* The destroy callback. */
+ list_head dstr_cb_head; /* All destroy callbacks. */
uint8_t is_userptr; /* CL_MEM_USE_HOST_PTR is enabled */
cl_bool is_svm; /* This object is svm */
size_t offset; /* offset of host_ptr to the page beginning, only for CL_MEM_USE_HOST_PTR*/
@@ -363,5 +364,7 @@ extern cl_mem cl_mem_new_image_from_fd(cl_context ctx,
extern cl_int cl_mem_record_map_mem(cl_mem mem, void *ptr, void **mem_ptr, size_t offset,
size_t size, const size_t *origin, const size_t *region);
+extern cl_int cl_mem_set_destructor_callback(cl_mem memobj,
+ void(CL_CALLBACK *pfn_notify)(cl_mem, void *), void *user_data);
#endif /* __CL_MEM_H__ */