Index: include/asterisk/lock.h =================================================================== --- include/asterisk/lock.h (revision 85353) +++ include/asterisk/lock.h (working copy) @@ -103,21 +103,37 @@ #include #include -#define AST_MUTEX_INIT_VALUE { PTHREAD_MUTEX_INIT_VALUE, 1, { NULL }, { 0 }, 0, { NULL }, { 0 } } +/* These correspond to the properties of the ast_mutex_info structure */ +#define AST_MUTEX_INIT_VALUE { PTHREAD_MUTEX_INIT_VALUE, 1, 0, {} } + #define AST_MUTEX_INIT_VALUE_NOTRACKING \ - { PTHREAD_MUTEX_INIT_VALUE, 0, { NULL }, { 0 }, 0, { NULL }, { 0 } } +{ PTHREAD_MUTEX_INIT_VALUE, 0, 0, {} } #define AST_MAX_REENTRANCY 10 + +/** +* the elements that used to be array items in the ast_mutex_info +*/ +struct ast_mutex_reentrant_info { + const char *file; + int lineno; + const char *func; + pthread_t thread; +}; + struct ast_mutex_info { pthread_mutex_t mutex; - /*! Track which thread holds this lock */ - unsigned int track:1; - const char *file[AST_MAX_REENTRANCY]; - int lineno[AST_MAX_REENTRANCY]; + unsigned int track:1; /*! Track which thread holds this lock */ int reentrancy; - const char *func[AST_MAX_REENTRANCY]; - pthread_t thread[AST_MAX_REENTRANCY]; + + /* replace the array of fields with an array of structure */ + struct ast_mutex_reentrant_info reentrancy_info[AST_MAX_REENTRANCY]; + + /*const char *file[AST_MAX_REENTRANCY];*/ + /*int lineno[AST_MAX_REENTRANCY];*/ + /*const char *func[AST_MAX_REENTRANCY];*/ + /*pthread_t thread[AST_MAX_REENTRANCY];*/ }; typedef struct ast_mutex_info ast_mutex_t; @@ -171,6 +187,7 @@ pthread_mutexattr_t *attr) { int i; + struct ast_mutex_reentrant_info* reentrancy_info; #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS int canlog = strcmp(filename, "logger.c"); @@ -178,8 +195,9 @@ if ((t->mutex) != (empty_mutex)) { __ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is already initialized.\n", filename, lineno, func, mutex_name); + reentrancy_info = &t->reentrancy_info[0]; __ast_mutex_logger("%s line %d (%s): Error: previously initialization of mutex '%s'.\n", - t->file[0], t->lineno[0], t->func[0], mutex_name); + reentrancy_info->file, reentrancy_info->lineno, reentrancy_info->func, mutex_name); DO_THREAD_CRASH; return 0; } @@ -187,14 +205,22 @@ #endif for (i = 0; i < AST_MAX_REENTRANCY; i++) { - t->file[i] = NULL; - t->lineno[i] = 0; - t->func[i] = NULL; - t->thread[i] = 0; + reentrancy_info = &t->reentrancy_info[i]; + reentrancy_info->file = NULL; + reentrancy_info->lineno = 0; + reentrancy_info->func = NULL; + reentrancy_info->thread = 0; } t->reentrancy = 0; t->track = track; + // old initialization +/* reentrancy_info = &t->reentrancy_info[0]; + reentrancy_info->file = filename; + reentrancy_info->lineno = lineno; + reentrancy_info->func = func; + reentrancy_info->thread = 0; +*/ return pthread_mutex_init(&t->mutex, attr); } @@ -212,11 +238,13 @@ #define ast_mutex_init_notracking(pmutex) \ __ast_pthread_mutex_init(0, __FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex) + static inline int __ast_pthread_mutex_destroy(const char *filename, int lineno, const char *func, const char *mutex_name, ast_mutex_t *t) { int res; int canlog = strcmp(filename, "logger.c"); + struct ast_mutex_reentrant_info* reentrancy_info; #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) { @@ -237,21 +265,26 @@ case EBUSY: __ast_mutex_logger("%s line %d (%s): Error: attempt to destroy locked mutex '%s'.\n", filename, lineno, func, mutex_name); + + reentrancy_info = &t->reentrancy_info[t->reentrancy-1]; __ast_mutex_logger("%s line %d (%s): Error: '%s' was locked here.\n", - t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name); + reentrancy_info->file, reentrancy_info->lineno, reentrancy_info->func, mutex_name); break; } - if ((res = pthread_mutex_destroy(&t->mutex))) + if ((res = pthread_mutex_destroy(&t->mutex))) { __ast_mutex_logger("%s line %d (%s): Error destroying mutex: %s\n", filename, lineno, func, strerror(res)); + } #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP - else + else { t->mutex = PTHREAD_MUTEX_INIT_VALUE; + } #endif - t->file[0] = filename; - t->lineno[0] = lineno; - t->func[0] = func; + reentrancy_info = &t->reentrancy_info[0]; + reentrancy_info->file = filename; + reentrancy_info->lineno = lineno; + reentrancy_info->func = func; return res; } @@ -261,9 +294,10 @@ { int res; int canlog = strcmp(filename, "logger.c"); - - if (t->track) + struct ast_mutex_reentrant_info *reentrancy_info; + if (t->track) { ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, &t->mutex); + } #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) { @@ -290,9 +324,11 @@ if ((current - seconds) && (!((current - seconds) % 5))) { __ast_mutex_logger("%s line %d (%s): Deadlock? waited %d sec for mutex '%s'?\n", filename, lineno, func, (int)(current - seconds), mutex_name); + + reentrancy_info = &t->reentrancy_info[t->reentrancy-1]; __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n", - t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], - t->func[t->reentrancy-1], mutex_name); + reentrancy_info->file, reentrancy_info->lineno, + reentrancy_info->func, mutex_name); } usleep(200); } @@ -309,21 +345,29 @@ #endif /* DETECT_DEADLOCKS */ if (!res) { - if (t->track) + if (t->track) { ast_mark_lock_acquired(); + } + if (t->reentrancy < AST_MAX_REENTRANCY) { - t->file[t->reentrancy] = filename; - t->lineno[t->reentrancy] = lineno; - t->func[t->reentrancy] = func; - t->thread[t->reentrancy] = pthread_self(); + reentrancy_info = &t->reentrancy_info[t->reentrancy]; + reentrancy_info->file = filename; + reentrancy_info->lineno = lineno; + reentrancy_info->func = func; + reentrancy_info->thread = pthread_self(); + //__ast_mutex_logger("%s line %d (%s): obtained lock %d for mutex '%s'\n", + // filename, lineno, func, t->reentrancy, mutex_name); + t->reentrancy++; + } else { __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n", filename, lineno, func, mutex_name); } } else { - if (t->track) + if (t->track) { ast_remove_lock_info(&t->mutex); + } __ast_mutex_logger("%s line %d (%s): Error obtaining mutex: %s\n", filename, lineno, func, strerror(res)); DO_THREAD_CRASH; @@ -337,6 +381,7 @@ { int res; int canlog = strcmp(filename, "logger.c"); + struct ast_mutex_reentrant_info* reentrancy_info; #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) { @@ -350,13 +395,17 @@ ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, &t->mutex); if (!(res = pthread_mutex_trylock(&t->mutex))) { - if (t->track) + + if (t->track) { ast_mark_lock_acquired(); + } + if (t->reentrancy < AST_MAX_REENTRANCY) { - t->file[t->reentrancy] = filename; - t->lineno[t->reentrancy] = lineno; - t->func[t->reentrancy] = func; - t->thread[t->reentrancy] = pthread_self(); + reentrancy_info = &t->reentrancy_info[t->reentrancy]; + reentrancy_info->file = filename; + reentrancy_info->lineno = lineno; + reentrancy_info->func = func; + reentrancy_info->thread = pthread_self(); t->reentrancy++; } else { __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n", @@ -374,6 +423,8 @@ { int res; int canlog = strcmp(filename, "logger.c"); + int current_reentrancy; + struct ast_mutex_reentrant_info* reentrancy_info; #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) { @@ -382,13 +433,18 @@ } #endif - if (t->reentrancy && (t->thread[t->reentrancy-1] != pthread_self())) { - __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n", + current_reentrancy = t->reentrancy; + if (current_reentrancy > 0) { + reentrancy_info = &t->reentrancy_info[current_reentrancy-1]; + + if (reentrancy_info->thread != pthread_self()) { + __ast_mutex_logger("%s line %d (%s): __ast_pthread_mutex_unlock() : attempted unlock mutex '%s' without owning it!\n", filename, lineno, func, mutex_name); __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n", - t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name); + reentrancy_info->file, reentrancy_info->lineno, reentrancy_info->func, mutex_name); DO_THREAD_CRASH; } + } if (--t->reentrancy < 0) { __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n", @@ -396,13 +452,19 @@ t->reentrancy = 0; } - if (t->reentrancy < AST_MAX_REENTRANCY) { - t->file[t->reentrancy] = NULL; - t->lineno[t->reentrancy] = 0; - t->func[t->reentrancy] = NULL; - t->thread[t->reentrancy] = 0; + current_reentrancy = t->reentrancy; + if (current_reentrancy < 0) { + current_reentrancy = 0; } + if (current_reentrancy < AST_MAX_REENTRANCY) { + reentrancy_info = &t->reentrancy_info[current_reentrancy]; + reentrancy_info->file = NULL; + reentrancy_info->lineno = 0; + reentrancy_info->func = NULL; + reentrancy_info->thread = 0; + } + if (t->track) ast_remove_lock_info(&t->mutex); @@ -445,6 +507,8 @@ { int res; int canlog = strcmp(filename, "logger.c"); + int current_reentrancy; + struct ast_mutex_reentrant_info* reentrancy_info; #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) { @@ -453,13 +517,18 @@ } #endif - if (t->reentrancy && (t->thread[t->reentrancy-1] != pthread_self())) { - __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n", + current_reentrancy = t->reentrancy; + if (current_reentrancy > 0) { + reentrancy_info = &t->reentrancy_info[current_reentrancy-1]; + if (reentrancy_info->thread != pthread_self()) { + __ast_mutex_logger("%s line %d (%s): __ast_cond_wait(): attempted unlock mutex '%s' without owning it!\n", filename, lineno, func, mutex_name); + __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n", - t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name); + reentrancy_info->file, reentrancy_info->lineno, reentrancy_info->func, mutex_name); DO_THREAD_CRASH; } + } if (--t->reentrancy < 0) { __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n", @@ -467,29 +536,39 @@ t->reentrancy = 0; } - if (t->reentrancy < AST_MAX_REENTRANCY) { - t->file[t->reentrancy] = NULL; - t->lineno[t->reentrancy] = 0; - t->func[t->reentrancy] = NULL; - t->thread[t->reentrancy] = 0; + current_reentrancy = t->reentrancy; + if (current_reentrancy < 0) { + current_reentrancy = 0; } - if (t->track) + if (current_reentrancy < AST_MAX_REENTRANCY) { + reentrancy_info = &t->reentrancy_info[current_reentrancy]; + reentrancy_info->file = NULL; + reentrancy_info->lineno = 0; + reentrancy_info->func = NULL; + reentrancy_info->thread = 0; + } + + if (t->track) { ast_remove_lock_info(&t->mutex); + } if ((res = pthread_cond_wait(cond, &t->mutex))) { __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n", filename, lineno, func, strerror(res)); DO_THREAD_CRASH; } else { - if (t->track) + if (t->track) { ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, &t->mutex); + } - if (t->reentrancy < AST_MAX_REENTRANCY) { - t->file[t->reentrancy] = filename; - t->lineno[t->reentrancy] = lineno; - t->func[t->reentrancy] = func; - t->thread[t->reentrancy] = pthread_self(); + current_reentrancy = t->reentrancy; + if (current_reentrancy < AST_MAX_REENTRANCY) { + reentrancy_info = &t->reentrancy_info[current_reentrancy]; + reentrancy_info->file = filename; + reentrancy_info->lineno = lineno; + reentrancy_info->func = func; + reentrancy_info->thread = pthread_self(); t->reentrancy++; } else { __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n", @@ -506,6 +585,8 @@ { int res; int canlog = strcmp(filename, "logger.c"); + int current_reentrancy; + struct ast_mutex_reentrant_info* reentrancy_info; #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) { @@ -514,13 +595,17 @@ } #endif - if (t->reentrancy && (t->thread[t->reentrancy-1] != pthread_self())) { - __ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n", + current_reentrancy = t->reentrancy; + if (current_reentrancy > 0) { + reentrancy_info = &t->reentrancy_info[current_reentrancy-1]; + if (reentrancy_info->thread != pthread_self()) { + __ast_mutex_logger("%s line %d (%s): __ast_cond_timedwait(): attempted unlock mutex '%s' without owning it!\n", filename, lineno, func, mutex_name); __ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n", - t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name); + reentrancy_info->file, reentrancy_info->lineno, reentrancy_info->func, mutex_name); DO_THREAD_CRASH; } + } if (--t->reentrancy < 0) { __ast_mutex_logger("%s line %d (%s): mutex '%s' freed more times than we've locked!\n", @@ -528,29 +613,42 @@ t->reentrancy = 0; } - if (t->reentrancy < AST_MAX_REENTRANCY) { - t->file[t->reentrancy] = NULL; - t->lineno[t->reentrancy] = 0; - t->func[t->reentrancy] = NULL; - t->thread[t->reentrancy] = 0; + current_reentrancy = t->reentrancy; + if (current_reentrancy < 0) { + current_reentrancy = 0; } - if (t->track) + if (current_reentrancy < AST_MAX_REENTRANCY) { + reentrancy_info = &t->reentrancy_info[current_reentrancy]; + reentrancy_info->file = NULL; + reentrancy_info->lineno = 0; + reentrancy_info->func = NULL; + reentrancy_info->thread = 0; + } + + if (t->track) { ast_remove_lock_info(&t->mutex); + } if ((res = pthread_cond_timedwait(cond, &t->mutex, abstime)) && (res != ETIMEDOUT)) { __ast_mutex_logger("%s line %d (%s): Error waiting on condition mutex '%s'\n", filename, lineno, func, strerror(res)); DO_THREAD_CRASH; } else { - if (t->track) + if (t->track) { ast_store_lock_info(AST_MUTEX, filename, lineno, func, mutex_name, &t->mutex); + } - if (t->reentrancy < AST_MAX_REENTRANCY) { - t->file[t->reentrancy] = filename; - t->lineno[t->reentrancy] = lineno; - t->func[t->reentrancy] = func; - t->thread[t->reentrancy] = pthread_self(); + + current_reentrancy = t->reentrancy; + if (current_reentrancy < AST_MAX_REENTRANCY) { + reentrancy_info = &t->reentrancy_info[current_reentrancy]; + + reentrancy_info->file = filename; + reentrancy_info->lineno = lineno; + reentrancy_info->func = func; + reentrancy_info->thread = pthread_self(); + t->reentrancy++; } else { __ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",