@ -17,7 +17,10 @@
*/
/*! \file
* \ brief General Asterisk channel locking definitions .
* \ brief Asterisk locking - related definitions :
* - ast_mutext_t , ast_rwlock_t and related functions ;
* - atomic arithmetic instructions ;
* - wrappers for channel locking .
*
* - See \ ref LockDef
*/
@ -46,8 +49,6 @@
# define _ASTERISK_LOCK_H
# include <pthread.h>
// #include <netdb.h>
# include <time.h>
# include <sys/param.h>
# include "asterisk/logger.h"
@ -88,6 +89,13 @@
# define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE
# endif /* PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP */
/*
* Definition of ast_mutex_t , ast_cont_d and related functions with / without debugging
* ( search for DEBUG_THREADS to find the start / end of the sections ) .
*
* The non - debug code contains just wrappers for the corresponding pthread functions .
* The debug code tracks usage and tries to identify deadlock situations .
*/
# ifdef DEBUG_THREADS
# define __ast_mutex_logger(...) do { if (canlog) ast_log(LOG_ERROR, __VA_ARGS__); else fprintf(stderr, __VA_ARGS__); } while (0)
@ -662,8 +670,7 @@ static inline int __ast_cond_timedwait(const char *filename, int lineno, const c
typedef pthread_mutex_t ast_mutex_t ;
# define AST_MUTEX_INIT_VALUE ((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
# define AST_MUTEX_INIT_VALUE_NOTRACKING \
( ( ast_mutex_t ) PTHREAD_MUTEX_INIT_VALUE )
# define AST_MUTEX_INIT_VALUE_NOTRACKING ((ast_mutex_t) PTHREAD_MUTEX_INIT_VALUE)
# define ast_mutex_init_notracking(m) ast_mutex_init(m)
@ -737,8 +744,10 @@ static inline int ast_cond_timedwait(ast_cond_t *cond, ast_mutex_t *t, const str
# endif /* !DEBUG_THREADS */
# if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
/* If AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope
destructors to destroy mutexes and create it on the fly . */
/*
* If AST_MUTEX_INIT_W_CONSTRUCTORS is defined , use file scope constructors
* and destructors to create / destroy global mutexes .
*/
# define __AST_MUTEX_DEFINE(scope, mutex, init_val, track) \
scope ast_mutex_t mutex = init_val ; \
static void __attribute__ ( ( constructor ) ) init_ # # mutex ( void ) \
@ -748,14 +757,14 @@ static void __attribute__ ((constructor)) init_##mutex(void) \
else \
ast_mutex_init_notracking ( & mutex ) ; \
} \
\
static void __attribute__ ( ( destructor ) ) fini_ # # mutex ( void ) \
{ \
ast_mutex_destroy ( & mutex ) ; \
}
# else /* !AST_MUTEX_INIT_W_CONSTRUCTORS */
/* By default, use static initialization of mutexes. */
# define __AST_MUTEX_DEFINE(scope, mutex, init_val, track) \
scope ast_mutex_t mutex = init_val
# define __AST_MUTEX_DEFINE(scope, mutex, init_val, track) scope ast_mutex_t mutex = init_val
# endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */
# define pthread_mutex_t use_ast_mutex_t_instead_of_pthread_mutex_t
@ -783,6 +792,12 @@ static void __attribute__ ((destructor)) fini_##mutex(void) \
# define pthread_create __use_ast_pthread_create_instead__
# endif
/*
* Same as above , definitions of ast_rwlock_t for the various cases :
* simple wrappers for the pthread equivalent in the non - debug case ,
* more sophisticated tracking in the debug case .
*/
typedef pthread_rwlock_t ast_rwlock_t ;
# ifdef HAVE_PTHREAD_RWLOCK_INITIALIZER
@ -794,6 +809,12 @@ typedef pthread_rwlock_t ast_rwlock_t;
# ifdef DEBUG_THREADS
# define ast_rwlock_init(rwlock) __ast_rwlock_init(__FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
# define ast_rwlock_destroy(rwlock) __ast_rwlock_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
# define ast_rwlock_unlock(a) _ast_rwlock_unlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
# define ast_rwlock_rdlock(a) _ast_rwlock_rdlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
# define ast_rwlock_wrlock(a) _ast_rwlock_wrlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
# define ast_rwlock_tryrdlock(a) _ast_rwlock_tryrdlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
# define ast_rwlock_trywrlock(a) _ast_rwlock_trywrlock(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
static inline int __ast_rwlock_init ( const char * filename , int lineno , const char * func , const char * rwlock_name , ast_rwlock_t * prwlock )
@ -820,7 +841,6 @@ static inline int __ast_rwlock_init(const char *filename, int lineno, const char
return res ;
}
# define ast_rwlock_destroy(rwlock) __ast_rwlock_destroy(__FILE__, __LINE__, __PRETTY_FUNCTION__, #rwlock, rwlock)
static inline int __ast_rwlock_destroy ( const char * filename , int lineno , const char * func , const char * rwlock_name , ast_rwlock_t * prwlock )
{
@ -842,8 +862,6 @@ static inline int __ast_rwlock_destroy(const char *filename, int lineno, const c
return res ;
}
# define ast_rwlock_unlock(a) \
_ast_rwlock_unlock ( a , # a , __FILE__ , __LINE__ , __PRETTY_FUNCTION__ )
static inline int _ast_rwlock_unlock ( ast_rwlock_t * lock , const char * name ,
const char * file , int line , const char * func )
@ -869,8 +887,6 @@ static inline int _ast_rwlock_unlock(ast_rwlock_t *lock, const char *name,
return res ;
}
# define ast_rwlock_rdlock(a) \
_ast_rwlock_rdlock ( a , # a , __FILE__ , __LINE__ , __PRETTY_FUNCTION__ )
static inline int _ast_rwlock_rdlock ( ast_rwlock_t * lock , const char * name ,
const char * file , int line , const char * func )
@ -902,8 +918,6 @@ static inline int _ast_rwlock_rdlock(ast_rwlock_t *lock, const char *name,
return res ;
}
# define ast_rwlock_wrlock(a) \
_ast_rwlock_wrlock ( a , # a , __FILE__ , __LINE__ , __PRETTY_FUNCTION__ )
static inline int _ast_rwlock_wrlock ( ast_rwlock_t * lock , const char * name ,
const char * file , int line , const char * func )
@ -935,8 +949,6 @@ static inline int _ast_rwlock_wrlock(ast_rwlock_t *lock, const char *name,
return res ;
}
# define ast_rwlock_tryrdlock(a) \
_ast_rwlock_tryrdlock ( a , # a , __FILE__ , __LINE__ , __PRETTY_FUNCTION__ )
static inline int _ast_rwlock_tryrdlock ( ast_rwlock_t * lock , const char * name ,
const char * file , int line , const char * func )
@ -968,8 +980,6 @@ static inline int _ast_rwlock_tryrdlock(ast_rwlock_t *lock, const char *name,
return res ;
}
# define ast_rwlock_trywrlock(a) \
_ast_rwlock_trywrlock ( a , # a , __FILE__ , __LINE__ , __PRETTY_FUNCTION__ )
static inline int _ast_rwlock_trywrlock ( ast_rwlock_t * lock , const char * name ,
const char * file , int line , const char * func )
@ -1059,19 +1069,19 @@ static void __attribute__ ((constructor)) init_##rwlock(void) \
{ \
ast_rwlock_init ( & rwlock ) ; \
} \
\
static void __attribute__ ( ( destructor ) ) fini_ # # rwlock ( void ) \
{ \
ast_rwlock_destroy ( & rwlock ) ; \
}
# else
# define __AST_RWLOCK_DEFINE(scope, rwlock) \
scope ast_rwlock_t rwlock = AST_RWLOCK_INIT_VALUE
# define __AST_RWLOCK_DEFINE(scope, rwlock) scope ast_rwlock_t rwlock = AST_RWLOCK_INIT_VALUE
# endif
# define AST_RWLOCK_DEFINE_STATIC(rwlock) __AST_RWLOCK_DEFINE(static, rwlock)
/*
* Initial s upport for atomic instructions .
* S upport for atomic instructions .
* For platforms that have it , use the native cpu instruction to
* implement them . For other platforms , resort to a ' slow ' version
* ( defined in utils . c ) that protects the atomic instruction with