From: Junyan He <[email protected]> Make it a MT-safe call.
Signed-off-by: Junyan He <[email protected]> --- src/cl_api.c | 25 ------------------------- src/cl_api_mem.c | 14 ++++++++++++++ src/cl_mem.c | 43 +++++++++++++++++++++++++++++++++---------- src/cl_mem.h | 9 ++++++--- 4 files changed, 53 insertions(+), 38 deletions(-) diff --git a/src/cl_api.c b/src/cl_api.c index 605c853..ac6f695 100644 --- a/src/cl_api.c +++ b/src/cl_api.c @@ -979,31 +979,6 @@ clGetImageInfo(cl_mem mem, param_value_size_ret); } -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 clCreateSampler(cl_context context, cl_bool normalized, diff --git a/src/cl_api_mem.c b/src/cl_api_mem.c index 054c37a..5e3814d 100644 --- a/src/cl_api_mem.c +++ b/src/cl_api_mem.c @@ -22,6 +22,20 @@ #include "cl_event.h" #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); +} + void * clEnqueueMapBuffer(cl_command_queue command_queue, cl_mem buffer, diff --git a/src/cl_mem.c b/src/cl_mem.c index 2b783b9..e662d8f 100644 --- a/src/cl_mem.c +++ b/src/cl_mem.c @@ -311,6 +311,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; @@ -618,6 +619,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; @@ -774,6 +776,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; @@ -1375,6 +1378,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) @@ -1390,6 +1395,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) { @@ -1427,16 +1440,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; @@ -2624,3 +2627,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 5a384ed..71e55a8 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*/ @@ -354,5 +355,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__ */ -- 2.7.4 _______________________________________________ Beignet mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/beignet
