mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-05 12:16:00 +00:00
Make lock logging reentrant when DEBUG_THREADS enabled
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6555 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
@@ -76,15 +76,17 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define AST_MUTEX_INIT_VALUE { PTHREAD_MUTEX_INIT_VALUE, NULL, 0, 0, NULL, 0 }
|
#define AST_MUTEX_INIT_VALUE { PTHREAD_MUTEX_INIT_VALUE, { NULL }, { 0 }, 0, { NULL }, { 0 } }
|
||||||
|
|
||||||
|
#define AST_MAX_REENTRANCY 10
|
||||||
|
|
||||||
struct ast_mutex_info {
|
struct ast_mutex_info {
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
const char *file;
|
const char *file[AST_MAX_REENTRANCY];
|
||||||
int lineno;
|
int lineno[AST_MAX_REENTRANCY];
|
||||||
int reentrancy;
|
int reentrancy;
|
||||||
const char *func;
|
const char *func[AST_MAX_REENTRANCY];
|
||||||
pthread_t thread;
|
pthread_t thread[AST_MAX_REENTRANCY];
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct ast_mutex_info ast_mutex_t;
|
typedef struct ast_mutex_info ast_mutex_t;
|
||||||
@@ -108,10 +110,10 @@ static inline int __ast_pthread_mutex_init_attr(const char *filename, int lineno
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
t->file = filename;
|
t->file[0] = filename;
|
||||||
t->lineno = lineno;
|
t->lineno[0] = lineno;
|
||||||
t->func = func;
|
t->func[0] = func;
|
||||||
t->thread = 0;
|
t->thread[0] = 0;
|
||||||
t->reentrancy = 0;
|
t->reentrancy = 0;
|
||||||
|
|
||||||
return pthread_mutex_init(&t->mutex, attr);
|
return pthread_mutex_init(&t->mutex, attr);
|
||||||
@@ -154,7 +156,7 @@ static inline int __ast_pthread_mutex_destroy(const char *filename, int lineno,
|
|||||||
__ast_mutex_logger("%s line %d (%s): Error: attempt to destroy locked mutex '%s'.\n",
|
__ast_mutex_logger("%s line %d (%s): Error: attempt to destroy locked mutex '%s'.\n",
|
||||||
filename, lineno, func, mutex_name);
|
filename, lineno, func, mutex_name);
|
||||||
__ast_mutex_logger("%s line %d (%s): Error: '%s' was locked here.\n",
|
__ast_mutex_logger("%s line %d (%s): Error: '%s' was locked here.\n",
|
||||||
t->file, t->lineno, t->func, mutex_name);
|
t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,9 +167,9 @@ static inline int __ast_pthread_mutex_destroy(const char *filename, int lineno,
|
|||||||
else
|
else
|
||||||
t->mutex = PTHREAD_MUTEX_INIT_VALUE;
|
t->mutex = PTHREAD_MUTEX_INIT_VALUE;
|
||||||
#endif
|
#endif
|
||||||
t->file = filename;
|
t->file[0] = filename;
|
||||||
t->lineno = lineno;
|
t->lineno[0] = lineno;
|
||||||
t->func = func;
|
t->func[0] = func;
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -238,11 +240,16 @@ static inline int __ast_pthread_mutex_lock(const char *filename, int lineno, con
|
|||||||
#endif /* DETECT_DEADLOCKS */
|
#endif /* DETECT_DEADLOCKS */
|
||||||
|
|
||||||
if (!res) {
|
if (!res) {
|
||||||
t->reentrancy++;
|
if (t->reentrancy < AST_MAX_REENTRANCY) {
|
||||||
t->file = filename;
|
t->file[t->reentrancy] = filename;
|
||||||
t->lineno = lineno;
|
t->lineno[t->reentrancy] = lineno;
|
||||||
t->func = func;
|
t->func[t->reentrancy] = func;
|
||||||
t->thread = pthread_self();
|
t->thread[t->reentrancy] = pthread_self();
|
||||||
|
t->reentrancy++;
|
||||||
|
} else {
|
||||||
|
__ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
|
||||||
|
filename, lineno, func, mutex_name);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
__ast_mutex_logger("%s line %d (%s): Error obtaining mutex: %s\n",
|
__ast_mutex_logger("%s line %d (%s): Error obtaining mutex: %s\n",
|
||||||
filename, lineno, func, strerror(errno));
|
filename, lineno, func, strerror(errno));
|
||||||
@@ -258,11 +265,11 @@ static inline int __ast_pthread_mutex_trylock(const char *filename, int lineno,
|
|||||||
const char* mutex_name, ast_mutex_t *t)
|
const char* mutex_name, ast_mutex_t *t)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
|
int canlog = strcmp(filename, "logger.c");
|
||||||
|
|
||||||
#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) || defined(AST_MUTEX_INIT_ON_FIRST_USE)
|
#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) || defined(AST_MUTEX_INIT_ON_FIRST_USE)
|
||||||
if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
|
if ((t->mutex) == ((pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER)) {
|
||||||
#ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
|
#ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
|
||||||
int canlog = strcmp(filename, "logger.c");
|
|
||||||
|
|
||||||
__ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
|
__ast_mutex_logger("%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
|
||||||
filename, lineno, func, mutex_name);
|
filename, lineno, func, mutex_name);
|
||||||
@@ -272,11 +279,16 @@ static inline int __ast_pthread_mutex_trylock(const char *filename, int lineno,
|
|||||||
#endif /* defined(AST_MUTEX_INIT_W_CONSTRUCTORS) || defined(AST_MUTEX_INIT_ON_FIRST_USE) */
|
#endif /* defined(AST_MUTEX_INIT_W_CONSTRUCTORS) || defined(AST_MUTEX_INIT_ON_FIRST_USE) */
|
||||||
|
|
||||||
if (!(res = pthread_mutex_trylock(&t->mutex))) {
|
if (!(res = pthread_mutex_trylock(&t->mutex))) {
|
||||||
t->reentrancy++;
|
if (t->reentrancy < AST_MAX_REENTRANCY) {
|
||||||
t->file = filename;
|
t->file[t->reentrancy] = filename;
|
||||||
t->lineno = lineno;
|
t->lineno[t->reentrancy] = lineno;
|
||||||
t->func = func;
|
t->func[t->reentrancy] = func;
|
||||||
t->thread = pthread_self();
|
t->thread[t->reentrancy] = pthread_self();
|
||||||
|
t->reentrancy++;
|
||||||
|
} else {
|
||||||
|
__ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
|
||||||
|
filename, lineno, func, mutex_name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
@@ -295,11 +307,11 @@ static inline int __ast_pthread_mutex_unlock(const char *filename, int lineno, c
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (t->reentrancy && (t->thread != pthread_self())) {
|
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",
|
__ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
|
||||||
filename, lineno, func, mutex_name);
|
filename, lineno, func, mutex_name);
|
||||||
__ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
|
__ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
|
||||||
t->file, t->lineno, t->func, mutex_name);
|
t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
|
||||||
#ifdef THREAD_CRASH
|
#ifdef THREAD_CRASH
|
||||||
DO_THREAD_CRASH;
|
DO_THREAD_CRASH;
|
||||||
#endif
|
#endif
|
||||||
@@ -311,11 +323,11 @@ static inline int __ast_pthread_mutex_unlock(const char *filename, int lineno, c
|
|||||||
t->reentrancy = 0;
|
t->reentrancy = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!t->reentrancy) {
|
if (t->reentrancy < AST_MAX_REENTRANCY) {
|
||||||
t->file = NULL;
|
t->file[t->reentrancy] = NULL;
|
||||||
t->lineno = 0;
|
t->lineno[t->reentrancy] = 0;
|
||||||
t->func = NULL;
|
t->func[t->reentrancy] = NULL;
|
||||||
t->thread = 0;
|
t->thread[t->reentrancy] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = pthread_mutex_unlock(&t->mutex))) {
|
if ((res = pthread_mutex_unlock(&t->mutex))) {
|
||||||
@@ -342,11 +354,11 @@ static inline int __ast_pthread_cond_wait(const char *filename, int lineno, cons
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (t->reentrancy && (t->thread != pthread_self())) {
|
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",
|
__ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
|
||||||
filename, lineno, func, mutex_name);
|
filename, lineno, func, mutex_name);
|
||||||
__ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
|
__ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
|
||||||
t->file, t->lineno, t->func, mutex_name);
|
t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
|
||||||
#ifdef THREAD_CRASH
|
#ifdef THREAD_CRASH
|
||||||
DO_THREAD_CRASH;
|
DO_THREAD_CRASH;
|
||||||
#endif
|
#endif
|
||||||
@@ -358,11 +370,11 @@ static inline int __ast_pthread_cond_wait(const char *filename, int lineno, cons
|
|||||||
t->reentrancy = 0;
|
t->reentrancy = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!t->reentrancy) {
|
if (t->reentrancy < AST_MAX_REENTRANCY) {
|
||||||
t->file = NULL;
|
t->file[t->reentrancy] = NULL;
|
||||||
t->lineno = 0;
|
t->lineno[t->reentrancy] = 0;
|
||||||
t->func = NULL;
|
t->func[t->reentrancy] = NULL;
|
||||||
t->thread = 0;
|
t->thread[t->reentrancy] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = pthread_cond_wait(cond, &t->mutex))) {
|
if ((res = pthread_cond_wait(cond, &t->mutex))) {
|
||||||
@@ -372,11 +384,16 @@ static inline int __ast_pthread_cond_wait(const char *filename, int lineno, cons
|
|||||||
DO_THREAD_CRASH;
|
DO_THREAD_CRASH;
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
t->reentrancy++;
|
if (t->reentrancy < AST_MAX_REENTRANCY) {
|
||||||
t->file = filename;
|
t->file[t->reentrancy] = filename;
|
||||||
t->lineno = lineno;
|
t->lineno[t->reentrancy] = lineno;
|
||||||
t->func = func;
|
t->func[t->reentrancy] = func;
|
||||||
t->thread = pthread_self();
|
t->thread[t->reentrancy] = pthread_self();
|
||||||
|
t->reentrancy++;
|
||||||
|
} else {
|
||||||
|
__ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
|
||||||
|
filename, lineno, func, mutex_name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
@@ -396,11 +413,11 @@ static inline int __ast_pthread_cond_timedwait(const char *filename, int lineno,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (t->reentrancy && (t->thread != pthread_self())) {
|
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",
|
__ast_mutex_logger("%s line %d (%s): attempted unlock mutex '%s' without owning it!\n",
|
||||||
filename, lineno, func, mutex_name);
|
filename, lineno, func, mutex_name);
|
||||||
__ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
|
__ast_mutex_logger("%s line %d (%s): '%s' was locked here.\n",
|
||||||
t->file, t->lineno, t->func, mutex_name);
|
t->file[t->reentrancy-1], t->lineno[t->reentrancy-1], t->func[t->reentrancy-1], mutex_name);
|
||||||
#ifdef THREAD_CRASH
|
#ifdef THREAD_CRASH
|
||||||
DO_THREAD_CRASH;
|
DO_THREAD_CRASH;
|
||||||
#endif
|
#endif
|
||||||
@@ -412,11 +429,11 @@ static inline int __ast_pthread_cond_timedwait(const char *filename, int lineno,
|
|||||||
t->reentrancy = 0;
|
t->reentrancy = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!t->reentrancy) {
|
if (t->reentrancy < AST_MAX_REENTRANCY) {
|
||||||
t->file = NULL;
|
t->file[t->reentrancy] = NULL;
|
||||||
t->lineno = 0;
|
t->lineno[t->reentrancy] = 0;
|
||||||
t->func = NULL;
|
t->func[t->reentrancy] = NULL;
|
||||||
t->thread = 0;
|
t->thread[t->reentrancy] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = pthread_cond_timedwait(cond, &t->mutex, abstime))) {
|
if ((res = pthread_cond_timedwait(cond, &t->mutex, abstime))) {
|
||||||
@@ -426,11 +443,16 @@ static inline int __ast_pthread_cond_timedwait(const char *filename, int lineno,
|
|||||||
DO_THREAD_CRASH;
|
DO_THREAD_CRASH;
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
t->reentrancy++;
|
if (t->reentrancy < AST_MAX_REENTRANCY) {
|
||||||
t->file = filename;
|
t->file[t->reentrancy] = filename;
|
||||||
t->lineno = lineno;
|
t->lineno[t->reentrancy] = lineno;
|
||||||
t->func = func;
|
t->func[t->reentrancy] = func;
|
||||||
t->thread = pthread_self();
|
t->thread[t->reentrancy] = pthread_self();
|
||||||
|
t->reentrancy++;
|
||||||
|
} else {
|
||||||
|
__ast_mutex_logger("%s line %d (%s): '%s' really deep reentrancy!\n",
|
||||||
|
filename, lineno, func, mutex_name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
Reference in New Issue
Block a user