summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunyan He <junyan.he@intel.com>2016-04-22 18:04:05 +0800
committerJunyan He <junyan.he@intel.com>2016-04-22 18:04:05 +0800
commita56c1573f8c25ebe6491413ef606e9c4692acb1e (patch)
tree4273a63496bd3d91a4f18a02ace9921e76479b7f
parenteeae92e76a9c4d12c09f7c749c355f4b7d156619 (diff)
lock
-rw-r--r--libclapi/cl_context.c4
-rw-r--r--libclapi/cl_event.c89
2 files changed, 47 insertions, 46 deletions
diff --git a/libclapi/cl_context.c b/libclapi/cl_context.c
index dbc71503..abd0ee1a 100644
--- a/libclapi/cl_context.c
+++ b/libclapi/cl_context.c
@@ -146,8 +146,8 @@ static cl_context cl_context_new(const cl_context_properties *properties,
ctx->props = *props;
ctx->magic = CL_MAGIC_CONTEXT_HEADER;
cl_ref_set_val(&ctx->ref_n, 1);
- PTHREAD_MUTEX_INIT(&ctx->lock);
- PTHREAD_COND_INIT(&ctx->cond);
+ CL_MUTEX_INIT(&ctx->lock);
+ CL_COND_INIT(&ctx->cond);
memcpy(ctx->devices, devices, sizeof(cl_device_id)*num_devices);
ctx->device_num = num_devices;
diff --git a/libclapi/cl_event.c b/libclapi/cl_event.c
index a83d19eb..ffb58df2 100644
--- a/libclapi/cl_event.c
+++ b/libclapi/cl_event.c
@@ -19,6 +19,7 @@
#include <string.h>
#include <assert.h>
#include "cl_alloc.h"
+#include "cl_mutex.h"
#include "cl_driver.h"
#include "cl_event.h"
#include "cl_driver.h"
@@ -40,17 +41,17 @@ cl_int cl_event_set_status(cl_event event, cl_int status)
assert(!IS_MARKER_EVENT(event));
assert(!IS_BARRIER_EVENT(event));
- pthread_mutex_lock(&event->lock);
+ CL_MUTEX_LOCK(&event->lock);
if (event->user_event) {
assert(event->status != CL_RUNNING && event->status != CL_QUEUED);
if (event->status <= CL_COMPLETE) { // Already set
- pthread_mutex_unlock(&event->lock);
+ CL_MUTEX_UNLOCK(&event->lock);
return CL_INVALID_OPERATION;
}
} else {
assert(event->queue);
if (status >= event->status) { //Should never go back.
- pthread_mutex_unlock(&event->lock);
+ CL_MUTEX_UNLOCK(&event->lock);
return CL_INVALID_OPERATION;
}
}
@@ -70,13 +71,13 @@ cl_int cl_event_set_status(cl_event event, cl_int status)
user_cb->pfn_notify(event, event->status, user_cb->user_data);
user_cb = user_cb->next;
}
- pthread_mutex_unlock(&event->lock);
+ CL_MUTEX_UNLOCK(&event->lock);
if (status > CL_COMPLETE) // For event still pending, nothing to do further.
return CL_SUCCESS;
/* Now begin to notify the others. Someone may wait on the event. */
- pthread_mutex_lock(&event->ctx->lock);
+ CL_MUTEX_LOCK(&event->ctx->lock);
/* First notify the one wait on context. */
pthread_cond_broadcast(&event->ctx->cond);
@@ -92,12 +93,12 @@ cl_int cl_event_set_status(cl_event event, cl_int status)
if (queue_num == 0) {
/* Nothing to do further. */
- pthread_mutex_unlock(&event->ctx->lock);
+ CL_MUTEX_UNLOCK(&event->ctx->lock);
return CL_SUCCESS;
}
queue_array = CL_CALLOC(queue_num, sizeof(void*));
if (queue_array == NULL) {
- pthread_mutex_unlock(&event->ctx->lock);
+ CL_MUTEX_UNLOCK(&event->ctx->lock);
return CL_OUT_OF_HOST_MEMORY;
}
@@ -112,7 +113,7 @@ cl_int cl_event_set_status(cl_event event, cl_int status)
q = q->next;
}
- pthread_mutex_unlock(&event->ctx->lock);
+ CL_MUTEX_UNLOCK(&event->ctx->lock);
for (i = 0; i < queue_num; i++) {
if (queue_array[i]) {
@@ -141,9 +142,9 @@ static cl_int cl_get_event_info(cl_event event, cl_event_info param_name, size_t
} else if (param_name == CL_EVENT_COMMAND_TYPE) {
FILL_GETINFO_RET(cl_command_type, 1, &event->type, CL_SUCCESS);
} else if (param_name == CL_EVENT_COMMAND_EXECUTION_STATUS) {
- pthread_mutex_lock(&event->lock);
+ CL_MUTEX_LOCK(&event->lock);
status = event->status;
- pthread_mutex_unlock(&event->lock);
+ CL_MUTEX_UNLOCK(&event->lock);
FILL_GETINFO_RET(cl_int, 1, &status, CL_SUCCESS);
} else if (param_name == CL_EVENT_REFERENCE_COUNT) {
cl_uint ref = cl_ref_get_val(&event->ref_n);
@@ -229,12 +230,12 @@ static cl_event cl_event_new(cl_context ctx, cl_command_queue queue,
assert(ctx);
cl_retain_context(ctx);
e->ctx = ctx;
- pthread_mutex_lock(&ctx->lock);
+ CL_MUTEX_LOCK(&ctx->lock);
e->next = ctx->events;
if (ctx->events != NULL)
ctx->events->prev = e;
ctx->events = e;
- pthread_mutex_unlock(&ctx->lock);
+ CL_MUTEX_UNLOCK(&ctx->lock);
if (queue) {
cl_retain_command_queue(queue);
@@ -258,14 +259,14 @@ static void cl_event_delete(cl_event e)
e->queue = NULL;
}
- pthread_mutex_lock(&e->ctx->lock);
+ CL_MUTEX_LOCK(&e->ctx->lock);
if (e->prev)
e->prev->next = e->next;
if (e->next)
e->next->prev = e->prev;
if (e->ctx->events == e)
e->ctx->events = e->next;
- pthread_mutex_unlock(&e->ctx->lock);
+ CL_MUTEX_UNLOCK(&e->ctx->lock);
cl_release_context(e->ctx);
if (e->wait_events) {
@@ -316,13 +317,13 @@ LOCAL cl_int cl_event_check_waitlist(cl_uint num_events_in_wait_list, const cl_e
/* check the event and context */
for (i = 0; i < num_events_in_wait_list; i++) {
CHECK_EVENT(event_wait_list[i]);
- pthread_mutex_lock(&event_wait_list[i]->lock);
+ CL_MUTEX_LOCK(&event_wait_list[i]->lock);
if (event_wait_list[i]->status < CL_COMPLETE) {
- pthread_mutex_unlock(&event_wait_list[i]->lock);
+ CL_MUTEX_UNLOCK(&event_wait_list[i]->lock);
err = CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST;
goto exit;
}
- pthread_mutex_unlock(&event_wait_list[i]->lock);
+ CL_MUTEX_UNLOCK(&event_wait_list[i]->lock);
if (event && event == &event_wait_list[i]) // It's ourself?
goto error;
@@ -413,10 +414,10 @@ LOCAL cl_event* cl_check_context_barrier_events(cl_context context, cl_int* even
cl_bool completed = CL_FALSE;
assert(context);
- pthread_mutex_lock(&context->lock);
+ CL_MUTEX_LOCK(&context->lock);
if (context->barrier_events == NULL) {
assert(context->barrier_events_num == 0);
- pthread_mutex_unlock(&context->lock);
+ CL_MUTEX_UNLOCK(&context->lock);
if (err)
*err = CL_SUCCESS;
return NULL;
@@ -431,16 +432,16 @@ LOCAL cl_event* cl_check_context_barrier_events(cl_context context, cl_int* even
assert(!IS_BARRIER_EVENT(e->wait_events[j]));
assert(!IS_MARKER_EVENT(e->wait_events[j]));
- pthread_mutex_lock(&e->lock);
+ CL_MUTEX_LOCK(&e->lock);
if (e->wait_events[j]->status < CL_COMPLETE) { // error
completed = CL_TRUE;
- pthread_mutex_unlock(&e->lock);
+ CL_MUTEX_UNLOCK(&e->lock);
break;
}
if (e->wait_events[j]->status > CL_COMPLETE) {
completed = CL_FALSE;
}
- pthread_mutex_unlock(&e->lock);
+ CL_MUTEX_UNLOCK(&e->lock);
}
if (completed) {
@@ -458,7 +459,7 @@ LOCAL cl_event* cl_check_context_barrier_events(cl_context context, cl_int* even
} else if (new_num != context->barrier_events_num) { // Need to shrink.
new_list = CL_MALLOC(sizeof(cl_event)*new_num);
if (new_list == NULL) {
- pthread_mutex_unlock(&context->lock);
+ CL_MUTEX_UNLOCK(&context->lock);
if (err)
*err = CL_OUT_OF_HOST_MEMORY;
return NULL;
@@ -488,7 +489,7 @@ LOCAL cl_event* cl_check_context_barrier_events(cl_context context, cl_int* even
if (e_num) {
e_list = CL_MALLOC(sizeof(cl_event)*e_num);
if (e_list == NULL) {
- pthread_mutex_unlock(&context->lock);
+ CL_MUTEX_UNLOCK(&context->lock);
if (err)
*err = CL_OUT_OF_HOST_MEMORY;
return NULL;
@@ -505,7 +506,7 @@ LOCAL cl_event* cl_check_context_barrier_events(cl_context context, cl_int* even
}
assert(k == e_num);
- pthread_mutex_unlock(&context->lock);
+ CL_MUTEX_UNLOCK(&context->lock);
if (old_list) { // Need to free all the completed events without lock.
assert(old_num > 0);
@@ -602,7 +603,7 @@ static cl_int cl_event_set_callback(cl_event event , cl_int exec_type,
cb->status = exec_type;
cb->executed = CL_FALSE;
- pthread_mutex_lock(&event->lock);
+ CL_MUTEX_LOCK(&event->lock);
if(event->status > exec_type) {
cb->next = event->user_cb;
event->user_cb = cb;
@@ -611,7 +612,7 @@ static cl_int cl_event_set_callback(cl_event event , cl_int exec_type,
exec_imm = CL_TRUE;
cb->pfn_notify(event, event->status, cb->user_data);
}
- pthread_mutex_unlock(&event->lock);
+ CL_MUTEX_UNLOCK(&event->lock);
if (exec_imm) {
CL_FREE(cb);
@@ -636,7 +637,7 @@ static cl_int cl_enqueue_marker_or_barrier_with_waitlist(cl_command_queue queue,
if (event_wait_list == NULL) { // We need to collect all the queue related events.
assert(num_events == 0);
- pthread_mutex_lock(&queue->ctx->lock);
+ CL_MUTEX_LOCK(&queue->ctx->lock);
e = queue->ctx->events;
while(e) {
if (e->queue == queue) {
@@ -648,7 +649,7 @@ static cl_int cl_enqueue_marker_or_barrier_with_waitlist(cl_command_queue queue,
if (num_events) {
ctx_event_list = CL_CALLOC(num_events, sizeof(cl_event));
if (ctx_event_list == NULL) {
- pthread_mutex_unlock(&queue->ctx->lock);
+ CL_MUTEX_UNLOCK(&queue->ctx->lock);
err = CL_OUT_OF_HOST_MEMORY;
goto error;
}
@@ -661,13 +662,13 @@ static cl_int cl_enqueue_marker_or_barrier_with_waitlist(cl_command_queue queue,
continue;
}
- pthread_mutex_lock(&e->lock);
+ CL_MUTEX_LOCK(&e->lock);
if (e->status <= CL_COMPLETE) { // Already completed events, do not consider.
- pthread_mutex_unlock(&e->lock);
+ CL_MUTEX_UNLOCK(&e->lock);
e = e->next;
continue;
}
- pthread_mutex_unlock(&e->lock);
+ CL_MUTEX_UNLOCK(&e->lock);
ctx_event_list[i] = e;
i++;
@@ -678,7 +679,7 @@ static cl_int cl_enqueue_marker_or_barrier_with_waitlist(cl_command_queue queue,
num_events = i;
}
- pthread_mutex_unlock(&queue->ctx->lock);
+ CL_MUTEX_UNLOCK(&queue->ctx->lock);
e = cl_create_event(queue->ctx, queue, CL_FALSE, num_events, ctx_event_list, &err);
} else {
@@ -693,19 +694,19 @@ static cl_int cl_enqueue_marker_or_barrier_with_waitlist(cl_command_queue queue,
if (is_barrier) {
SET_BARRIER_EVENT(e);
- pthread_mutex_lock(&queue->ctx->lock);
+ CL_MUTEX_LOCK(&queue->ctx->lock);
queue->ctx->barrier_events_num++;
queue->ctx->barrier_events = CL_REALLOC(queue->ctx->barrier_events,
sizeof(cl_event)*queue->ctx->barrier_events_num);
if (queue->ctx->barrier_events == NULL) {
- pthread_mutex_unlock(&queue->ctx->lock);
+ CL_MUTEX_UNLOCK(&queue->ctx->lock);
err = CL_OUT_OF_HOST_MEMORY;
goto error;
}
cl_retain_event(e);
queue->ctx->barrier_events[queue->ctx->barrier_events_num - 1] = e;
- pthread_mutex_unlock(&queue->ctx->lock);
+ CL_MUTEX_UNLOCK(&queue->ctx->lock);
} else {
SET_MARKER_EVENT(e);
}
@@ -755,21 +756,21 @@ static cl_int cl_event_wait_for_events(cl_uint num_events, const cl_event *event
}
/* Begin to wait for something. */
- pthread_mutex_lock(&new_list[0]->ctx->lock);// Add the event should have same context.
+ CL_MUTEX_LOCK(&new_list[0]->ctx->lock);// Add the event should have same context.
while(1) {
w_event = NULL;
/* first pass. iterate the list, some one is error, return error status immediately. */
for (i = 0; i < new_num; i++) {
- pthread_mutex_lock(&new_list[i]->lock);
+ CL_MUTEX_LOCK(&new_list[i]->lock);
if (new_list[i]->status < CL_COMPLETE) { // Error, cancel all.
- pthread_mutex_unlock(&new_list[i]->lock);
- pthread_mutex_unlock(&new_list[0]->ctx->lock);
+ CL_MUTEX_UNLOCK(&new_list[i]->lock);
+ CL_MUTEX_UNLOCK(&new_list[0]->ctx->lock);
err = CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST;
goto error;
} else if (new_list[i]->status > CL_COMPLETE) {
w_event = new_list[i]; // Find some candidate.
}
- pthread_mutex_unlock(&new_list[i]->lock);
+ CL_MUTEX_UNLOCK(&new_list[i]->lock);
}
if (w_event == NULL) // We finish wait.
@@ -779,19 +780,19 @@ static cl_int cl_event_wait_for_events(cl_uint num_events, const cl_event *event
it, but this just cause the _wait_for function return immediately and we
continue this loop. */
if (w_event->user_event == CL_FALSE) {
- pthread_mutex_unlock(&new_list[0]->ctx->lock);// Add the event should have same context.
+ CL_MUTEX_UNLOCK(&new_list[0]->ctx->lock);// Add the event should have same context.
err = w_event->queue->device->driver->wait_for_events(1, &w_event, w_event->queue);
if (err != CL_SUCCESS) {
goto error;
}
- pthread_mutex_lock(&new_list[0]->ctx->lock);// Add the event should have same context.
+ CL_MUTEX_LOCK(&new_list[0]->ctx->lock);// Add the event should have same context.
} else {
/* user event can also have change it status, but because we hold context->lock, the
signal should not be broadcasted. */
pthread_cond_wait(&new_list[0]->ctx->cond, &new_list[0]->ctx->lock);
}
}
- pthread_mutex_unlock(&new_list[0]->ctx->lock);// Add the event should have same context.
+ CL_MUTEX_UNLOCK(&new_list[0]->ctx->lock);// Add the event should have same context.
if (barrier_list)
CL_FREE(barrier_list);
if (new_list && new_list != event_wait_list)