diff options
author | Junyan He <junyan.he@intel.com> | 2016-04-21 18:40:02 +0800 |
---|---|---|
committer | Junyan He <junyan.he@intel.com> | 2016-04-21 18:40:02 +0800 |
commit | 2c2b38fa0cb61cdafe490129fe12938485af08ef (patch) | |
tree | 1208cd275863b39571be431c07c7c7affc7c8757 | |
parent | 322ffb40ce887305f1a629464608f8542051eec0 (diff) |
add lock
-rw-r--r-- | libclapi/CMakeLists.txt | 2 | ||||
-rw-r--r-- | libclapi/cl_mutex.c | 134 |
2 files changed, 130 insertions, 6 deletions
diff --git a/libclapi/CMakeLists.txt b/libclapi/CMakeLists.txt index 4f644e7c..4ec11279 100644 --- a/libclapi/CMakeLists.txt +++ b/libclapi/CMakeLists.txt @@ -19,5 +19,5 @@ set(LIBCLAPI_SRC ) add_library(clapi SHARED ${LIBCLAPI_SRC}) -target_link_libraries(clapi dl) +target_link_libraries(clapi dl rt) install (TARGETS clapi LIBRARY DESTINATION ${BEIGNET_INSTALL_DIR}) diff --git a/libclapi/cl_mutex.c b/libclapi/cl_mutex.c index e9502c7f..b18990c2 100644 --- a/libclapi/cl_mutex.c +++ b/libclapi/cl_mutex.c @@ -28,13 +28,133 @@ static unsigned int cl_mutex_log_num; typedef struct _cl_mutex_log_item { pthread_mutex_t* mutex; - char* file; - int line; + struct { + char* file; + int line; + } holder; + struct { + char* file; + int line; + } waiter[8]; } _cl_mutex_log_item; typedef struct _cl_mutex_log_item* cl_mutex_log_item; +static cl_mutex_log_item* cl_mutex_log_map; +static int cl_mutex_log_map_size; -static int try_to_get_the_lock(pthread_mutex_t* mutex) +static int before_get_the_mutex(pthread_mutex_t* mutex, char* file, int line) +{ + int i; + int ret = -1; + cl_mutex_log_item item = NULL; + + pthread_mutex_lock(&cl_mutex_log_lock); + for (i = 0; i < cl_mutex_log_map_size; i++) { + if (cl_mutex_log_map[i]->mutex == mutex) { + item = cl_mutex_log_map[i]; + break; + } + } + + if (item == NULL) { + for (i = 0; i < cl_mutex_log_map_size; i++) { + if (cl_mutex_log_map[i] == NULL) { + break; + } + } + + if (i == cl_mutex_log_map_size) { + cl_mutex_log_map = realloc(cl_mutex_log_map, 2*cl_mutex_log_map_size*sizeof(cl_mutex_log_item)); + memset(cl_mutex_log_map + cl_mutex_log_map_size, 0, cl_mutex_log_map_size*sizeof(cl_mutex_log_item)); + cl_mutex_log_map_size = cl_mutex_log_map_size*2; + } + + item = malloc(sizeof(_cl_mutex_log_item)); + assert(item); + memset(item, 0, sizeof(_cl_mutex_log_item)); + item->waiter[0].file = file; + item->waiter[0].line = line; + cl_mutex_log_map[i] = item; + ret = 0; + cl_mutex_log_num++; + } else { + for (i = 0; i < 8; i++) { + if (item->waiter[i].file == NULL) { + item->waiter[i].file = file; + item->waiter[i].line = line; + ret = i; + break; + } + } + /* If no waiter slot avaible, just skip, do not record. */ + } + + pthread_mutex_unlock(&cl_mutex_log_lock); + return ret; +} + +static void after_get_the_mutex(pthread_mutex_t* mutex, char* file, int line, int nth) +{ + int i; + cl_mutex_log_item item = NULL; + + pthread_mutex_lock(&cl_mutex_log_lock); + for (i = 0; i < cl_mutex_log_map_size; i++) { + if (cl_mutex_log_map[i]->mutex == mutex) { + item = cl_mutex_log_map[i]; + break; + } + } + assert(item); + + if (nth >= 0) { + assert(nth < 8); + item->waiter[nth].file = NULL; + item->waiter[nth].line = 0; + } + + item->holder.file = file; + item->holder.line = line; + + pthread_mutex_unlock(&cl_mutex_log_lock); +} + +static void after_release_the_mutex(pthread_mutex_t* mutex) +{ + int i, j; + cl_mutex_log_item item = NULL; + int still_used; + + pthread_mutex_lock(&cl_mutex_log_lock); + for (i = 0; i < cl_mutex_log_map_size; i++) { + if (cl_mutex_log_map[i]->mutex == mutex) { + item = cl_mutex_log_map[i]; + break; + } + } + + assert(item); + item->holder.file = NULL; + item->holder.line = 0; + + still_used = 0; + for (j = 0; j < 8; j++) { + if (item->waiter[j].file != NULL) { + still_used = 1; + break; + } + } + + if (still_used == 0) { + cl_mutex_log_map[i] = NULL; + free(item); + cl_mutex_log_num--; + } + + pthread_mutex_unlock(&cl_mutex_log_lock); +} + +static int try_to_get_the_mutex(pthread_mutex_t* mutex) { struct timespec abs_time; clock_gettime(CLOCK_REALTIME , &abs_time); @@ -46,10 +166,14 @@ static int try_to_get_the_lock(pthread_mutex_t* mutex) return 1; } -LOCAL void cl_mutex_lock(pthread_mutex_t* mutex) +LOCAL void cl_mutex_lock(pthread_mutex_t* mutex, char* file, int line) { assert(mutex); + int nth = before_get_the_mutex(mutex, file, line); + if (try_to_get_the_mutex(mutex) == 0) { + // Consider to be dead locked. + } - + after_get_the_mutex(mutex, file, line, nth); } |