diff options
author | Junyan He <junyan.he@intel.com> | 2016-04-15 16:36:17 +0800 |
---|---|---|
committer | Junyan He <junyan.he@intel.com> | 2016-04-15 16:36:17 +0800 |
commit | b69169db6601031f987213df13dab6c968925e6e (patch) | |
tree | 9ecc0bc2e6351db46ea75d837e2205b8a753ceb3 | |
parent | a946bd9ecadcf3dc428d4ba0c80427e8a991ee5b (diff) |
wait
-rw-r--r-- | include/cl_context.h | 1 | ||||
-rw-r--r-- | libclapi/cl_context.c | 2 | ||||
-rw-r--r-- | libclapi/cl_event.c | 60 | ||||
-rw-r--r-- | src/cl_event.c | 3 |
4 files changed, 60 insertions, 6 deletions
diff --git a/include/cl_context.h b/include/cl_context.h index 4a624dcb..27efd063 100644 --- a/include/cl_context.h +++ b/include/cl_context.h @@ -59,6 +59,7 @@ typedef struct _cl_context { cl_sampler samplers; /* All sampler object currently allocated */ cl_event events; /* All event object currently allocated */ pthread_mutex_t lock; /* The lock to protect the context. */ + pthread_cond_t cond; /* The condition we wait on. */ struct _cl_context_prop props; cl_context_properties *prop_user; /* a copy of user passed context properties when create context */ cl_uint prop_len; /* count of the properties */ diff --git a/libclapi/cl_context.c b/libclapi/cl_context.c index a76ae220..738244f9 100644 --- a/libclapi/cl_context.c +++ b/libclapi/cl_context.c @@ -146,6 +146,7 @@ static cl_context cl_context_new(const cl_context_properties *properties, ctx->magic = CL_MAGIC_CONTEXT_HEADER; cl_ref_set_val(&ctx->ref_n, 1); pthread_mutex_init(&ctx->lock, NULL); + pthread_cond_init(&ctx->cond, NULL); memcpy(ctx->devices, devices, sizeof(cl_device_id)*num_devices); ctx->device_num = num_devices; @@ -168,6 +169,7 @@ static void cl_context_delete(cl_context ctx) assert(ctx); pthread_mutex_destroy(&ctx->lock); + pthread_cond_destroy(&ctx->cond); /* delete refs to devices. */ for (i = 0; i < ctx->device_num; i++) { cl_release_device_id(ctx->devices[i]); diff --git a/libclapi/cl_event.c b/libclapi/cl_event.c index 00cef2bd..58432dd9 100644 --- a/libclapi/cl_event.c +++ b/libclapi/cl_event.c @@ -288,9 +288,13 @@ cl_int cl_event_set_status(cl_event event, cl_int status) } pthread_mutex_unlock(&event->lock); - /* We need to notify all the queue attached to the context, ref them temporary. */ - queue_num = 0; pthread_mutex_lock(&event->ctx->lock); + /* First notify the one wait on context. */ + pthread_cond_broadcast(&event->ctx->cond); + + /*Then, we need to notify all the queue attached to the context, ref them temporary. */ + queue_num = 0; + q = event->ctx->queues; while (q) { queue_num++; @@ -333,11 +337,59 @@ cl_int cl_event_set_status(cl_event event, cl_int status) return CL_SUCCESS; } -static cl_int cl_event_wait_events(cl_uint num_events_in_wait_list, - const cl_event *event_wait_list, cl_command_queue queue) +static cl_int cl_event_wait_for_events(cl_uint num_events, const cl_event *event_wait_list) { + cl_int i; + cl_int j; + cl_int num = 0; + cl_event *list = NULL; + cl_int err = CL_SUCCESS; + cl_bool need_to_wait = CL_FALSE; + + /* First, wait for all user events. */ + for (i = 0; i < num_events; i++) { + if (event_wait_list[i]->user_event) + num++; + } + + if (num) { + while(1) { + need_to_wait = CL_FALSE; + + for (i = 0; i < num_events; i++) { + if (event_wait_list[i]->user_event == 0) + continue; + + pthread_mutex_lock(&event_wait_list[i]->lock); + if (event_wait_list[i]->status > CL_COMPLETE) { + pthread_mutex_unlock(&event_wait_list[i]->lock); + need_to_wait = CL_TRUE; + break; + } + + if (event_wait_list[i]->status < CL_COMPLETE) { // Error, cancel all. + pthread_mutex_unlock(&event_wait_list[i]->lock); + err = CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST; + goto error; + } + pthread_mutex_unlock(&event_wait_list[i]->lock); + } + + if (need_to_wait) { + pthread_mutex_lock(&event_wait_list[0]->ctx->lock);// Add the event should have same context. + pthread_cond_wait(&event_wait_list[0]->ctx->cond, &event_wait_list[0]->ctx->lock); + pthread_mutex_unlock(&event_wait_list[0]->ctx->lock); + } else { + break; // break the while(1) + } + } + } +error: + if (list) + cl_free(list); + return err; } /************************************************************************************** diff --git a/src/cl_event.c b/src/cl_event.c index d2aee1ef..3aa788fa 100644 --- a/src/cl_event.c +++ b/src/cl_event.c @@ -260,8 +260,7 @@ error: goto exit; } -cl_int cl_event_wait_events(cl_uint num_events_in_wait_list, const cl_event *event_wait_list, - cl_command_queue queue) +cl_int cl_event_wait_for_events(cl_uint num_events_in_wait_list, const cl_event *event_wait_list) { cl_int i; |