Code formatting cleanups in utils.c and include/asterisk/lock.h

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@3278 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
James Golovich
2004-06-22 19:53:36 +00:00
parent f5c18f62a1
commit 024f486810
2 changed files with 102 additions and 95 deletions

View File

@@ -72,11 +72,11 @@ struct ast_mutex_info {
typedef struct ast_mutex_info ast_mutex_t; typedef struct ast_mutex_info ast_mutex_t;
static inline int __ast_pthread_mutex_init_attr(char *filename, int lineno, char *func, static inline int __ast_pthread_mutex_init_attr(char *filename, int lineno, char *func,
char* mutex_name, ast_mutex_t *t, char* mutex_name, ast_mutex_t *t,
pthread_mutexattr_t *attr) pthread_mutexattr_t *attr)
{ {
#ifdef AST_MUTEX_INIT_W_CONSTRUCTORS #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
if((t->mutex) != ((pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER)) { if ((t->mutex) != ((pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER)) {
fprintf(stderr, "%s line %d (%s): Error: mutex '%s' is already initialized.\n", fprintf(stderr, "%s line %d (%s): Error: mutex '%s' is already initialized.\n",
filename, lineno, func, mutex_name); filename, lineno, func, mutex_name);
fprintf(stderr, "%s line %d (%s): Error: previously initialization of mutex '%s'.\n", fprintf(stderr, "%s line %d (%s): Error: previously initialization of mutex '%s'.\n",
@@ -84,8 +84,8 @@ static inline int __ast_pthread_mutex_init_attr(char *filename, int lineno, char
#ifdef THREAD_CRASH #ifdef THREAD_CRASH
DO_THREAD_CRASH; DO_THREAD_CRASH;
#endif #endif
return 0; return 0;
} }
#endif #endif
t->file = filename; t->file = filename;
t->lineno = lineno; t->lineno = lineno;
@@ -95,10 +95,10 @@ static inline int __ast_pthread_mutex_init_attr(char *filename, int lineno, char
} }
static inline int __ast_pthread_mutex_init(char *filename, int lineno, char *func, static inline int __ast_pthread_mutex_init(char *filename, int lineno, char *func,
char* mutex_name, ast_mutex_t *t) char *mutex_name, ast_mutex_t *t)
{ {
static pthread_mutexattr_t attr; static pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr); pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, AST_MUTEX_KIND); pthread_mutexattr_settype(&attr, AST_MUTEX_KIND);
return __ast_pthread_mutex_init_attr(filename, lineno, func, mutex_name, t, &attr); return __ast_pthread_mutex_init_attr(filename, lineno, func, mutex_name, t, &attr);
} }
@@ -107,38 +107,38 @@ static inline int __ast_pthread_mutex_init(char *filename, int lineno, char *fun
#define ast_pthread_mutex_init(pmutex,attr) __ast_pthread_mutex_init_attr(__FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex, attr) #define ast_pthread_mutex_init(pmutex,attr) __ast_pthread_mutex_init_attr(__FILE__, __LINE__, __PRETTY_FUNCTION__, #pmutex, pmutex, attr)
static inline int __ast_pthread_mutex_destroy(char *filename, int lineno, char *func, static inline int __ast_pthread_mutex_destroy(char *filename, int lineno, char *func,
char* mutex_name, ast_mutex_t *t) char *mutex_name, ast_mutex_t *t)
{ {
int res; int res;
#ifdef AST_MUTEX_INIT_W_CONSTRUCTORS #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
if((t->mutex) == ((pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER)) { if ((t->mutex) == ((pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER)) {
fprintf(stderr, "%s line %d (%s): Error: mutex '%s' is uninitialized.\n", fprintf(stderr, "%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
filename, lineno, func, mutex_name); filename, lineno, func, mutex_name);
} }
#endif #endif
res = pthread_mutex_trylock(&t->mutex); res = pthread_mutex_trylock(&t->mutex);
switch( res ) { switch (res) {
case 0: case 0:
pthread_mutex_unlock(&t->mutex); pthread_mutex_unlock(&t->mutex);
break; break;
case EINVAL: case EINVAL:
fprintf(stderr, "%s line %d (%s): Error: attempt to destroy invalid mutex '%s'.\n", fprintf(stderr, "%s line %d (%s): Error: attempt to destroy invalid mutex '%s'.\n",
filename, lineno, func, mutex_name); filename, lineno, func, mutex_name);
break; break;
case EBUSY: case EBUSY:
fprintf(stderr, "%s line %d (%s): Error: attemp to destroy locked mutex '%s'.\n", fprintf(stderr, "%s line %d (%s): Error: attemp to destroy locked mutex '%s'.\n",
filename, lineno, func, mutex_name); filename, lineno, func, mutex_name);
fprintf(stderr, "%s line %d (%s): Error: '%s' was locked here.\n", fprintf(stderr, "%s line %d (%s): Error: '%s' was locked here.\n",
t->file, t->lineno, t->func, mutex_name); t->file, t->lineno, t->func, mutex_name);
break; break;
} }
res = pthread_mutex_destroy(&t->mutex); res = pthread_mutex_destroy(&t->mutex);
if (res) if (res)
fprintf(stderr, "%s line %d (%s): Error destroying mutex: %s\n", fprintf(stderr, "%s line %d (%s): Error destroying mutex: %s\n",
filename, lineno, func, strerror(res)); filename, lineno, func, strerror(res));
#ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
else else
t->mutex = PTHREAD_MUTEX_INIT_VALUE; t->mutex = PTHREAD_MUTEX_INIT_VALUE;
#endif #endif
t->file = filename; t->file = filename;
t->lineno = lineno; t->lineno = lineno;
@@ -182,32 +182,32 @@ static inline int __ast_pthread_mutex_lock(char *filename, int lineno, char *fun
{ {
int res; int res;
#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
fprintf(stderr, "%s line %d (%s): Error: mutex '%s' is uninitialized.\n", fprintf(stderr, "%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
filename, lineno, func, mutex_name); filename, lineno, func, mutex_name);
#endif #endif
ast_mutex_init(t); ast_mutex_init(t);
} }
#endif /* definded(AST_MUTEX_INIT_W_CONSTRUCTORS) || defined(AST_MUTEX_INIT_ON_FIRST_USE) */ #endif /* definded(AST_MUTEX_INIT_W_CONSTRUCTORS) || defined(AST_MUTEX_INIT_ON_FIRST_USE) */
#ifdef DETECT_DEADLOCKS #ifdef DETECT_DEADLOCKS
{ {
time_t seconds seconds = time(NULL); time_t seconds seconds = time(NULL);
do { do {
res = pthread_mutex_trylock(&t->mutex); res = pthread_mutex_trylock(&t->mutex);
if(res == EBUSY) { if (res == EBUSY) {
if((time(NULL) - seconds) % 5) { if ((time(NULL) - seconds) % 5) {
fprintf(stderr, "%s line %d (%s): Deadlock? waited %d sec for mutex '%s'?\n", fprintf(stderr, "%s line %d (%s): Deadlock? waited %d sec for mutex '%s'?\n",
filename, lineno, func, (time(NULL) - seconds), mutex_name); filename, lineno, func, (time(NULL) - seconds), mutex_name);
fprintf(stderr, "%s line %d (%s): '%s' was locked here.\n", fprintf(stderr, "%s line %d (%s): '%s' was locked here.\n",
t->file, t->lineno, t->func, mutex_name); t->file, t->lineno, t->func, mutex_name);
} }
usleep(200); usleep(200);
} }
} while (res == EBUSY); } while (res == EBUSY);
} }
#else #else
res = pthread_mutex_lock(&t->mutex); res = pthread_mutex_lock(&t->mutex);
#endif /* DETECT_DEADLOCKS */ #endif /* DETECT_DEADLOCKS */
if (!res) { if (!res) {
t->file = filename; t->file = filename;
@@ -231,13 +231,13 @@ static inline int __ast_pthread_mutex_trylock(char *filename, int lineno, char *
{ {
int res; int res;
#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
fprintf(stderr, "%s line %d (%s): Error: mutex '%s' is uninitialized.\n", fprintf(stderr, "%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
filename, lineno, func, mutex_name); filename, lineno, func, mutex_name);
#endif #endif
ast_mutex_init(t); ast_mutex_init(t);
} }
#endif /* definded(AST_MUTEX_INIT_W_CONSTRUCTORS) || defined(AST_MUTEX_INIT_ON_FIRST_USE) */ #endif /* definded(AST_MUTEX_INIT_W_CONSTRUCTORS) || defined(AST_MUTEX_INIT_ON_FIRST_USE) */
res = pthread_mutex_trylock(&t->mutex); res = pthread_mutex_trylock(&t->mutex);
if (!res) { if (!res) {
@@ -252,13 +252,13 @@ static inline int __ast_pthread_mutex_trylock(char *filename, int lineno, char *
#define ast_mutex_trylock(a) __ast_pthread_mutex_trylock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a) #define ast_mutex_trylock(a) __ast_pthread_mutex_trylock(__FILE__, __LINE__, __PRETTY_FUNCTION__, #a, a)
static inline int __ast_pthread_mutex_unlock(char *filename, int lineno, char *func, static inline int __ast_pthread_mutex_unlock(char *filename, int lineno, char *func,
char* mutex_name, ast_mutex_t *t) { char* mutex_name, ast_mutex_t *t) {
int res; int res;
#ifdef AST_MUTEX_INIT_W_CONSTRUCTORS #ifdef AST_MUTEX_INIT_W_CONSTRUCTORS
if((t->mutex) == ((pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER)) { if ((t->mutex) == ((pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER)) {
fprintf(stderr, "%s line %d (%s): Error: mutex '%s' is uninitialized.\n", fprintf(stderr, "%s line %d (%s): Error: mutex '%s' is uninitialized.\n",
filename, lineno, func, mutex_name); filename, lineno, func, mutex_name);
} }
#endif #endif
/* Assumes lock is actually held */ /* Assumes lock is actually held */
t->file = NULL; t->file = NULL;
@@ -331,15 +331,15 @@ static void __attribute__ ((destructor)) fini_##mutex(void) \
static inline int ast_mutex_lock(ast_mutex_t *pmutex) static inline int ast_mutex_lock(ast_mutex_t *pmutex)
{ {
if(*pmutex == (ast_mutex_t)AST_MUTEX_KIND) if (*pmutex == (ast_mutex_t)AST_MUTEX_KIND)
ast_mutex_init(pmutex); ast_mutex_init(pmutex);
return pthread_mutex_lock(pmutex); return pthread_mutex_lock(pmutex);
} }
static inline int ast_mutex_trylock(ast_mutex_t *pmutex) static inline int ast_mutex_trylock(ast_mutex_t *pmutex)
{ {
if(*pmutex == (ast_mutex_t)AST_MUTEX_KIND) if (*pmutex == (ast_mutex_t)AST_MUTEX_KIND)
ast_mutex_init(pmutex); ast_mutex_init(pmutex);
return pthread_mutex_trylock(pmutex); return pthread_mutex_trylock(pmutex);
} }
#else #else
/* By default, use static initialization of mutexes.*/ /* By default, use static initialization of mutexes.*/
@@ -354,7 +354,6 @@ static inline int ast_mutex_trylock(ast_mutex_t *pmutex)
#define AST_MUTEX_DEFINE_STATIC(mutex) __AST_MUTEX_DEFINE(static,mutex) #define AST_MUTEX_DEFINE_STATIC(mutex) __AST_MUTEX_DEFINE(static,mutex)
#define AST_MUTEX_DEFINE_EXPORTED(mutex) __AST_MUTEX_DEFINE(/**/,mutex) #define AST_MUTEX_DEFINE_EXPORTED(mutex) __AST_MUTEX_DEFINE(/**/,mutex)
#define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__ #define AST_MUTEX_INITIALIZER __use_AST_MUTEX_DEFINE_STATIC_rather_than_AST_MUTEX_INITIALIZER__
#define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__ #define gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__

84
utils.c
View File

@@ -158,46 +158,54 @@ static int test_errors = 0;
static void *test_thread_body(void *data) static void *test_thread_body(void *data)
{ {
ast_mutex_lock(&test_lock); ast_mutex_lock(&test_lock);
lock_count += 10; lock_count += 10;
if(lock_count != 10) test_errors++; if (lock_count != 10)
ast_mutex_lock(&test_lock); test_errors++;
lock_count += 10; ast_mutex_lock(&test_lock);
if(lock_count != 20) test_errors++; lock_count += 10;
ast_mutex_lock(&test_lock2); if (lock_count != 20)
ast_mutex_unlock(&test_lock); test_errors++;
lock_count -= 10; ast_mutex_lock(&test_lock2);
if(lock_count != 10) test_errors++; ast_mutex_unlock(&test_lock);
ast_mutex_unlock(&test_lock); lock_count -= 10;
lock_count -= 10; if (lock_count != 10)
ast_mutex_unlock(&test_lock2); test_errors++;
if(lock_count != 0) test_errors++; ast_mutex_unlock(&test_lock);
return NULL; lock_count -= 10;
ast_mutex_unlock(&test_lock2);
if (lock_count != 0)
test_errors++;
return NULL;
} }
int test_for_thread_safety(void) int test_for_thread_safety(void)
{ {
ast_mutex_lock(&test_lock2); ast_mutex_lock(&test_lock2);
ast_mutex_lock(&test_lock); ast_mutex_lock(&test_lock);
lock_count += 1; lock_count += 1;
ast_mutex_lock(&test_lock); ast_mutex_lock(&test_lock);
lock_count += 1; lock_count += 1;
pthread_create(&test_thread, NULL, test_thread_body, NULL); pthread_create(&test_thread, NULL, test_thread_body, NULL);
pthread_yield(); pthread_yield();
usleep(100); usleep(100);
if(lock_count != 2) test_errors++; if (lock_count != 2)
ast_mutex_unlock(&test_lock); test_errors++;
lock_count -= 1; ast_mutex_unlock(&test_lock);
pthread_yield(); lock_count -= 1;
usleep(100); pthread_yield();
if(lock_count != 1) test_errors++; usleep(100);
ast_mutex_unlock(&test_lock); if (lock_count != 1)
lock_count -= 1; test_errors++;
if(lock_count != 0) test_errors++; ast_mutex_unlock(&test_lock);
ast_mutex_unlock(&test_lock2); lock_count -= 1;
pthread_yield(); if (lock_count != 0)
usleep(100); test_errors++;
if(lock_count != 0) test_errors++; ast_mutex_unlock(&test_lock2);
pthread_join(test_thread, NULL); pthread_yield();
return(test_errors); /* return 0 on success. */ usleep(100);
if (lock_count != 0)
test_errors++;
pthread_join(test_thread, NULL);
return(test_errors); /* return 0 on success. */
} }