diff options
author | Junyan He <junyan.he@intel.com> | 2016-04-22 17:54:53 +0800 |
---|---|---|
committer | Junyan He <junyan.he@intel.com> | 2016-04-22 17:54:53 +0800 |
commit | cc89ee2e688601b97e280bf0edd75e2eda80fbd2 (patch) | |
tree | e2cd961d574777a26eb83baafcc67e4b33a16529 | |
parent | 6688fd1c52dc77b9f019133d5755b8732c0bac3d (diff) |
thread
-rw-r--r-- | libclapi/cl_context.c | 5 | ||||
-rw-r--r-- | libclapi/cl_mutex.c | 69 | ||||
-rw-r--r-- | libclapi/cl_mutex.h | 9 | ||||
-rw-r--r-- | libclapi/cl_platform_id.c | 2 |
4 files changed, 83 insertions, 2 deletions
diff --git a/libclapi/cl_context.c b/libclapi/cl_context.c index 5d0f2b6c..dbc71503 100644 --- a/libclapi/cl_context.c +++ b/libclapi/cl_context.c @@ -26,6 +26,7 @@ #include "cl_device_id.h" #include "cl_context.h" #include "cl_alloc.h" +#include "cl_mutex.h" #include "cl_internals.h" #include "cl_driver.h" @@ -145,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, NULL); - pthread_cond_init(&ctx->cond, NULL); + PTHREAD_MUTEX_INIT(&ctx->lock); + PTHREAD_COND_INIT(&ctx->cond); memcpy(ctx->devices, devices, sizeof(cl_device_id)*num_devices); ctx->device_num = num_devices; diff --git a/libclapi/cl_mutex.c b/libclapi/cl_mutex.c index e25022ba..adffacbc 100644 --- a/libclapi/cl_mutex.c +++ b/libclapi/cl_mutex.c @@ -184,6 +184,7 @@ static void before_release_the_mutex(pthread_mutex_t* mutex, char* file, int li printf("At unlock point file: %s, line: %d, we can not find the locked mutex:%p item, fatal\n", file, line, mutex); assert(0); + pthread_mutex_unlock(&cl_mutex_log_lock); return; } @@ -191,6 +192,7 @@ static void before_release_the_mutex(pthread_mutex_t* mutex, char* file, int li printf("At unlock point file: %s, line: %d, we can not find the locked mutex:%p info, fatal\n", file, line, mutex); assert(0); + pthread_mutex_unlock(&cl_mutex_log_lock); return; } @@ -219,6 +221,65 @@ static void before_release_the_mutex(pthread_mutex_t* mutex, char* file, int li pthread_mutex_unlock(&cl_mutex_log_lock); } +static void before_destroy_mutex(pthread_mutex_t* mutex, char* file, int line) +{ + cl_mutex_log_item item = NULL; + int i; + int print_others; + pthread_t tid = pthread_self(); + + 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) { + pthread_mutex_unlock(&cl_mutex_log_lock); + return; + } + + print_others = 1; + printf("When we destroy a mutex:%p at file: %s, line: %d, thread id is %d.\n", + mutex, file, line, (int)tid); + + if (item->holder.file) { + printf(" The mutex is still held by: file: %s, line: %d, thread id is %d\n", + item->holder.file, item->holder.line, (int)item->holder.tid); + } + + for (i = 0; i < 8; i++) { + if (item->waiter[i].file) { + if (print_others) { + print_others = 0; + printf(" There are other waiters:\n"); + } + + printf(" file: %s, line: %d, thread id: %d\n", + item->waiter[i].file, item->waiter[i].line, (int)item->waiter[i].tid); + } + } + + print_others = 0; + for (i = 0; i < 8; i++) { + if (item->sleeper[i].file) { + if (print_others) { + print_others = 0; + printf(" There are other sleepers:\n"); + } + + printf(" file: %s, line: %d, thread id: %d, cond: %p\n", + item->sleeper[i].file, item->sleeper[i].line, + (int)item->sleeper[i].tid, item->sleeper[i].cond); + } + } + + assert(0); + pthread_mutex_unlock(&cl_mutex_log_lock); +} + static int before_sleep_on_the_mutex(pthread_cond_t* cond, pthread_mutex_t* mutex, char* file, int line) { int i; @@ -238,6 +299,7 @@ static int before_sleep_on_the_mutex(pthread_cond_t* cond, pthread_mutex_t* mute printf("At unlock point file: %s, line: %d, we can not find the locked mutex:%p item, fatal\n", file, line, mutex); assert(0); + pthread_mutex_unlock(&cl_mutex_log_lock); return -1; } @@ -245,6 +307,7 @@ static int before_sleep_on_the_mutex(pthread_cond_t* cond, pthread_mutex_t* mute printf("At unlock point file: %s, line: %d, we can not find the locked mutex:%p info, fatal\n", file, line, mutex); assert(0); + pthread_mutex_unlock(&cl_mutex_log_lock); return -1; } @@ -350,6 +413,12 @@ static void handle_deadlock(pthread_mutex_t* mutex, char* file, int line) pthread_mutex_unlock(&cl_mutex_log_lock); } +LOCAL void cl_mutex_destory(pthread_mutex_t* mutex, char* file, int line) +{ + before_destroy_mutex(mutex, file, line); + pthread_mutex_destroy(mutex); +} + LOCAL void cl_mutex_lock(pthread_mutex_t* mutex, char* file, int line) { assert(mutex); diff --git a/libclapi/cl_mutex.h b/libclapi/cl_mutex.h index 3c903a46..b22484b1 100644 --- a/libclapi/cl_mutex.h +++ b/libclapi/cl_mutex.h @@ -22,10 +22,14 @@ #include <stdlib.h> #include <pthread.h> +#define CL_DEBUG_MUTEX 1 #ifdef CL_DEBUG_MUTEX extern void cl_mutex_debug_init(void); #define CL_MUTEX_DEBUG_INIT() cl_mutex_debug_init() +extern void cl_mutex_destory(pthread_mutex_t* mutex, char* file, int line); +#define CL_MUTEX_DESTORY(mutex) cl_mutex_destory(mutex, __FILE__, __LINE__) + extern void cl_mutex_lock(pthread_mutex_t* mutex, char* file, int line); #define CL_MUTEX_LOCK(mutex) cl_mutex_lock(mutex, __FILE__, __LINE__) @@ -45,6 +49,7 @@ void cl_mutex_report(void); #else #define CL_MUTEX_DEBUG_INIT() +#define CL_MUTEX_DESTORY(mutex) pthread_mutex_destory(mutex) #define CL_MUTEX_LOCK(mutex) pthread_mutex_lock(mutex) #define CL_MUTEX_UNLOCK(mutex) pthread_mutex_unlock(mutex) #define CL_COND_WAIT(cond, mutex) pthread_cond_wait(cond, mutex) @@ -53,4 +58,8 @@ void cl_mutex_report(void); #endif /* End of CL_DEBUG_MUTEX */ #define PTHREAD_MUTEX_INIT(mutex) pthread_mutex_init(mutex, NULL) +#define PTHREAD_COND_INIT(cond) pthread_cond_init(cond, NULL) +#define PTHREAD_MUTEX_DESTROY(mutex) pthread_mutex_destroy(mutex) +#define PTHREAD_COND_DESTROY(cond) pthread_cond_destroy(cond) + #endif /* __CL_MUTEX_H__ */ diff --git a/libclapi/cl_platform_id.c b/libclapi/cl_platform_id.c index f5f870dd..493b3957 100644 --- a/libclapi/cl_platform_id.c +++ b/libclapi/cl_platform_id.c @@ -24,6 +24,7 @@ #include "cl_extension.h" #include "cl_internals.h" #include "cl_alloc.h" +#include "cl_mutex.h" #define _STR(x) #x #define _JOINT(x, y) _STR(x) "." _STR(y) @@ -113,6 +114,7 @@ void static cl_init_platform(void) if (!inited) { CL_ALLOC_DEBUG_INIT(); + CL_MUTEX_DEBUG_INIT(); cl_platform_extension_init(intel_platform.all_extensions, intel_platform.extensions, CL_MAX_EXTENSION_LENGTH); intel_platform.extensions_sz = strlen(intel_platform.extensions); |