summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunyan He <junyan.he@intel.com>2016-04-21 19:21:49 +0800
committerJunyan He <junyan.he@intel.com>2016-04-21 19:21:49 +0800
commitddd623765716e767d47e50ce1967044ecb2f035b (patch)
treea16fdde8dfa914b7690d9125b282f95424a93a0d
parent677de86125364d11d012d6124108640d42d9266f (diff)
add deadlock check
-rw-r--r--libclapi/cl_mutex.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/libclapi/cl_mutex.c b/libclapi/cl_mutex.c
index f7540b5d..569f9eea 100644
--- a/libclapi/cl_mutex.c
+++ b/libclapi/cl_mutex.c
@@ -179,12 +179,67 @@ static int try_to_get_the_mutex(pthread_mutex_t* mutex)
return 1;
}
+static void handle_deadlock(pthread_mutex_t* mutex, char* file, int line)
+{
+ cl_mutex_log_item item = NULL;
+ int i;
+ int same_file;
+ int print_others;
+ int num;
+
+ 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;
+ }
+ }
+ printf("------------------------------------------------------\n");
+ if (item == NULL) {
+ printf("We may dead lock at a unknown mutex, call lock point is"
+ "file: %s, line: %d\n", file, line);
+ } else {
+ same_file = 0;
+ print_others = 1;
+ printf("We may dead lock at a mutex:%p, call lock point is"
+ "file: %s, line: %d.\nThe mutex is held by file: %s, line: %d\n",
+ mutex, file, line, item->holder.file, item->holder.line);
+ 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\n", item->waiter[i].file, item->waiter[i].line);
+ }
+
+ if (item->waiter[i].file == file)
+ same_file = 1;
+ }
+ assert(same_file);
+ }
+ printf("------------------------------------------------------\n");
+
+ num = 0;
+ for (i = 0; i < cl_mutex_log_map_size; i++) {
+ if (cl_mutex_log_map[i]) {
+ num++;
+ }
+ }
+ assert(num == cl_mutex_log_num);
+
+ exit(0);
+ pthread_mutex_unlock(&cl_mutex_log_lock);
+}
+
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.
+ handle_deadlock(mutex, file, line);
}
after_get_the_mutex(mutex, file, line, nth);