From 727abcdec7ad92fc2fae2585c5c609e0663858a6 Mon Sep 17 00:00:00 2001 From: Mark Spencer Date: Wed, 9 Jun 2004 01:45:08 +0000 Subject: [PATCH] Merge FreeBSD locking fixes (bug #1411) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@3176 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- acl.c | 2 +- apps/app_intercom.c | 2 +- apps/app_meetme.c | 2 +- apps/app_queue.c | 2 +- apps/app_voicemail.c | 2 +- asterisk.c | 2 +- astmm.c | 4 +- autoservice.c | 2 +- cdr.c | 2 +- cdr/cdr_odbc.c | 2 +- cdr/cdr_pgsql.c | 2 +- channel.c | 2 +- channels/chan_agent.c | 4 +- channels/chan_alsa.c | 2 +- channels/chan_h323.c | 6 +- channels/chan_iax.c | 4 +- channels/chan_iax2.c | 6 +- channels/chan_local.c | 4 +- channels/chan_mgcp.c | 10 +-- channels/chan_modem.c | 6 +- channels/chan_modem_aopen.c | 2 +- channels/chan_modem_bestdata.c | 2 +- channels/chan_modem_i4l.c | 2 +- channels/chan_nbs.c | 2 +- channels/chan_oss.c | 2 +- channels/chan_phone.c | 6 +- channels/chan_sip.c | 26 +++--- channels/chan_skinny.c | 10 +-- channels/chan_vofr.c | 6 +- channels/chan_vpb.c | 8 +- channels/chan_zap.c | 6 +- cli.c | 5 +- codecs/codec_a_mu.c | 2 +- codecs/codec_adpcm.c | 2 +- codecs/codec_alaw.c | 2 +- codecs/codec_g723_1.c | 2 +- codecs/codec_g726.c | 2 +- codecs/codec_gsm.c | 2 +- codecs/codec_ilbc.c | 2 +- codecs/codec_lpc10.c | 2 +- codecs/codec_speex.c | 2 +- codecs/codec_ulaw.c | 2 +- db.c | 2 +- dns.c | 2 +- enum.c | 2 +- file.c | 2 +- formats/format_g723.c | 2 +- formats/format_g726.c | 2 +- formats/format_g729.c | 2 +- formats/format_gsm.c | 2 +- formats/format_h263.c | 2 +- formats/format_ilbc.c | 2 +- formats/format_pcm.c | 2 +- formats/format_pcm_alaw.c | 2 +- formats/format_vox.c | 2 +- formats/format_wav.c | 2 +- formats/format_wav_gsm.c | 2 +- frame.c | 2 +- image.c | 2 +- include/asterisk/linkedlists.h | 3 - include/asterisk/lock.h | 156 ++++++++++++++++++++++++++------- include/asterisk/module.h | 2 +- indications.c | 2 +- loader.c | 4 +- logger.c | 6 +- manager.c | 4 +- pbx.c | 11 +-- pbx/pbx_config.c | 2 +- pbx/pbx_gtkconsole.c | 2 +- res/res_crypto.c | 2 +- res/res_monitor.c | 2 +- res/res_musiconhold.c | 2 +- res/res_parking.c | 2 +- stdtime/localtime.c | 8 +- translate.c | 2 +- utils.c | 3 +- 76 files changed, 254 insertions(+), 158 deletions(-) diff --git a/acl.c b/acl.c index d696716dab..4feea75a7f 100755 --- a/acl.c +++ b/acl.c @@ -34,7 +34,7 @@ #include #include -static ast_mutex_t routeseq_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(routeseq_lock); #endif diff --git a/apps/app_intercom.c b/apps/app_intercom.c index a4d8636974..4462be1809 100755 --- a/apps/app_intercom.c +++ b/apps/app_intercom.c @@ -59,7 +59,7 @@ STANDARD_LOCAL_USER; LOCAL_USER_DECL; -static ast_mutex_t sound_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(sound_lock); static int sound = -1; static int write_audio(short *data, int len) diff --git a/apps/app_meetme.c b/apps/app_meetme.c index 591ea5af8b..84f2e1081e 100755 --- a/apps/app_meetme.c +++ b/apps/app_meetme.c @@ -125,7 +125,7 @@ struct ast_conf_user { #define ADMINFLAG_KICKME (1 << 2) /* User is kicked */ -static ast_mutex_t conflock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(conflock); static int admin_exec(struct ast_channel *chan, void *data); diff --git a/apps/app_queue.c b/apps/app_queue.c index 840c5fca7d..86047fdbed 100755 --- a/apps/app_queue.c +++ b/apps/app_queue.c @@ -218,7 +218,7 @@ struct ast_call_queue { }; static struct ast_call_queue *queues = NULL; -static ast_mutex_t qlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(qlock); static char *int2strat(int strategy) { diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c index fd47cac8c3..e2f3fbfc6b 100755 --- a/apps/app_voicemail.c +++ b/apps/app_voicemail.c @@ -207,7 +207,7 @@ static char *app2 = "VoiceMailMain"; static char *app3 = "MailboxExists"; -static ast_mutex_t vmlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(vmlock); struct ast_vm_user *users; struct ast_vm_user *usersl; struct vm_zone *zones = NULL; diff --git a/asterisk.c b/asterisk.c index e6e74efaaa..5894da1a8c 100755 --- a/asterisk.c +++ b/asterisk.c @@ -83,7 +83,7 @@ static struct ast_atexit { void (*func)(void); struct ast_atexit *next; } *atexits = NULL; -static ast_mutex_t atexitslock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(atexitslock); time_t ast_startuptime; time_t ast_lastreloadtime; diff --git a/astmm.c b/astmm.c index 6e507aaacf..6b98c557d1 100755 --- a/astmm.c +++ b/astmm.c @@ -58,8 +58,8 @@ static struct ast_region { #define HASH(a) \ (((unsigned long)(a)) % SOME_PRIME) -static ast_mutex_t reglock = AST_MUTEX_INITIALIZER; -static ast_mutex_t showmemorylock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(reglock); +AST_MUTEX_DEFINE_STATIC(showmemorylock); static inline void *__ast_alloc_region(size_t size, int which, const char *file, int lineno, const char *func) { diff --git a/autoservice.c b/autoservice.c index 3f24db8c19..2f28f35a7a 100755 --- a/autoservice.c +++ b/autoservice.c @@ -36,7 +36,7 @@ #define MAX_AUTOMONS 256 -static ast_mutex_t autolock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(autolock); struct asent { struct ast_channel *chan; diff --git a/cdr.c b/cdr.c index 67acf105de..5f43b12a42 100755 --- a/cdr.c +++ b/cdr.c @@ -30,7 +30,7 @@ int ast_default_amaflags = AST_CDR_DOCUMENTATION; char ast_default_accountcode[20] = ""; -static ast_mutex_t cdrlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(cdrlock); static struct ast_cdr_beitem { char name[20]; diff --git a/cdr/cdr_odbc.c b/cdr/cdr_odbc.c index 2bcd9d3f7a..acd23270ab 100755 --- a/cdr/cdr_odbc.c +++ b/cdr/cdr_odbc.c @@ -41,7 +41,7 @@ static char *dsn = NULL, *username = NULL, *password = NULL, *loguniqueid = NULL static int dsn_alloc = 0, username_alloc = 0, password_alloc = 0, loguniqueid_alloc = 0; static int connected = 0; -static ast_mutex_t odbc_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(odbc_lock); static int odbc_do_query(void); static int odbc_init(void); diff --git a/cdr/cdr_pgsql.c b/cdr/cdr_pgsql.c index bbbc2275cf..876ed7f8c8 100755 --- a/cdr/cdr_pgsql.c +++ b/cdr/cdr_pgsql.c @@ -41,7 +41,7 @@ static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbport_alloc = 0; static int connected = 0; -static ast_mutex_t pgsql_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(pgsql_lock); PGconn *conn; PGresult *result; diff --git a/channel.c b/channel.c index b3ba99743e..52a49aa45d 100755 --- a/channel.c +++ b/channel.c @@ -74,7 +74,7 @@ struct ast_channel *channels = NULL; /* Protect the channel list (highly unlikely that two things would change it at the same time, but still! */ -static ast_mutex_t chlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(chlock); int ast_check_hangup(struct ast_channel *chan) { diff --git a/channels/chan_agent.c b/channels/chan_agent.c index cc35c3ee58..f91c6324b8 100755 --- a/channels/chan_agent.c +++ b/channels/chan_agent.c @@ -91,10 +91,10 @@ static int wrapuptime; static int ackcall; static int usecnt =0; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); /* Protect the interface list (of sip_pvt's) */ -static ast_mutex_t agentlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(agentlock); static int recordagentcalls = 0; static char recordformat[AST_MAX_BUF]; diff --git a/channels/chan_alsa.c b/channels/chan_alsa.c index 8446a7a930..d78ee0f205 100755 --- a/channels/chan_alsa.c +++ b/channels/chan_alsa.c @@ -77,7 +77,7 @@ static int silencethreshold = 1000; static char digits[80] = ""; static char text2send[80] = ""; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); static char *type = "Console"; static char *desc = "ALSA Console Channel Driver"; diff --git a/channels/chan_h323.c b/channels/chan_h323.c index 20f8cd1ea6..6c49f227c2 100755 --- a/channels/chan_h323.c +++ b/channels/chan_h323.c @@ -141,15 +141,15 @@ static struct sched_context *sched; static struct io_context *io; /** Protect the interface list (of oh323_pvt's) */ -static ast_mutex_t iflock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(iflock); /** Usage counter and associated lock */ static int usecnt =0; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); /* Protect the monitoring thread, so only one process can kill or start it, and not when it's doing something critical. */ -static ast_mutex_t monlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(monlock); /* This is the thread for the monitor which checks for input on the channels which are not currently in use. */ diff --git a/channels/chan_iax.c b/channels/chan_iax.c index d1be191835..b8a48fed2d 100755 --- a/channels/chan_iax.c +++ b/channels/chan_iax.c @@ -80,7 +80,7 @@ #define GAMMA (0.01) #ifdef MYSQL_FRIENDS -static ast_mutex_t mysqllock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(mysqllock); static MYSQL *mysql; static char mydbuser[80]; static char mydbpass[80]; @@ -112,7 +112,7 @@ static int tos = 0; static int expirey = AST_DEFAULT_REG_EXPIRE; static int usecnt; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); int (*iax_regfunk)(char *username, int onoff) = NULL; diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c index 25097ac4a7..a89a0f5572 100755 --- a/channels/chan_iax2.c +++ b/channels/chan_iax2.c @@ -100,7 +100,7 @@ #define GAMMA (0.01) #ifdef MYSQL_FRIENDS -static ast_mutex_t mysqllock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(mysqllock); static MYSQL *mysql; static char mydbuser[80]; static char mydbpass[80]; @@ -138,7 +138,7 @@ static int expirey = IAX_DEFAULT_REG_EXPIRE; static int timingfd = -1; /* Timing file descriptor */ static int usecnt; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); int (*iax2_regfunk)(char *username, int onoff) = NULL; @@ -269,7 +269,7 @@ static struct iax2_trunk_peer { int calls; } *tpeers = NULL; -static ast_mutex_t tpeerlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(tpeerlock); struct iax_firmware { struct iax_firmware *next; diff --git a/channels/chan_local.c b/channels/chan_local.c index 86ed6d17ad..c6cb4c7cf5 100755 --- a/channels/chan_local.c +++ b/channels/chan_local.c @@ -49,12 +49,12 @@ static char *tdesc = "Local Proxy Channel Driver"; static int capability = -1; static int usecnt =0; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0) /* Protect the interface list (of sip_pvt's) */ -static ast_mutex_t locallock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(locallock); static struct local_pvt { ast_mutex_t lock; /* Channel private lock */ diff --git a/channels/chan_mgcp.c b/channels/chan_mgcp.c index 27adaccfa8..740731eabf 100755 --- a/channels/chan_mgcp.c +++ b/channels/chan_mgcp.c @@ -199,7 +199,7 @@ static int adsi = 0; static int usecnt =0; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); /* SC: transaction id should always be positive */ static unsigned int oseq; @@ -214,9 +214,9 @@ static int matchdigittimeout = 3000; /* Protect the monitoring thread, so only one process can kill or start it, and not when it's doing something critical. */ -static ast_mutex_t netlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(netlock); -static ast_mutex_t monlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(monlock); /* This is the thread for the monitor which checks for input on the channels which are not currently in use. */ @@ -419,10 +419,10 @@ static struct mgcp_gateway { struct mgcp_gateway *next; } *gateways; -static ast_mutex_t mgcp_reload_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(mgcp_reload_lock); static int mgcp_reloading = 0; -static ast_mutex_t gatelock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(gatelock); static int mgcpsock = -1; diff --git a/channels/chan_modem.c b/channels/chan_modem.c index 728aa361ad..905a15d283 100755 --- a/channels/chan_modem.c +++ b/channels/chan_modem.c @@ -72,14 +72,14 @@ static int baudrate = 115200; static int stripmsd = 0; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); /* Protect the interface list (of ast_modem_pvt's) */ -static ast_mutex_t iflock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(iflock); /* Protect the monitoring thread, so only one process can kill or start it, and not when it's doing something critical. */ -static ast_mutex_t monlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(monlock); /* This is the thread for the monitor which checks for input on the channels which are not currently in use. */ diff --git a/channels/chan_modem_aopen.c b/channels/chan_modem_aopen.c index 4222e85cdd..c123bfddff 100755 --- a/channels/chan_modem_aopen.c +++ b/channels/chan_modem_aopen.c @@ -34,7 +34,7 @@ static char *breakcmd = "\0x10\0x03"; static char *desc = "A/Open (Rockwell Chipset) ITU-2 VoiceModem Driver"; static int usecnt; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); static char *aopen_idents[] = { /* Identify A/Open Modem */ diff --git a/channels/chan_modem_bestdata.c b/channels/chan_modem_bestdata.c index 10732dca75..7eca765199 100755 --- a/channels/chan_modem_bestdata.c +++ b/channels/chan_modem_bestdata.c @@ -39,7 +39,7 @@ static char *breakcmd = "\020!"; static char *desc = "BestData (Conexant V.90 Chipset) VoiceModem Driver"; static int usecnt; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); static char *bestdata_idents[] = { /* Identify BestData Modem */ diff --git a/channels/chan_modem_i4l.c b/channels/chan_modem_i4l.c index a8d08a631a..a7c28b621d 100755 --- a/channels/chan_modem_i4l.c +++ b/channels/chan_modem_i4l.c @@ -34,7 +34,7 @@ static char *breakcmd = "\0x10\0x14\0x10\0x3"; static char *desc = "ISDN4Linux Emulated Modem Driver"; static int usecnt; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); static char *i4l_idents[] = { /* Identify ISDN4Linux Driver */ diff --git a/channels/chan_nbs.c b/channels/chan_nbs.c index b4e5392c42..700dcc643f 100755 --- a/channels/chan_nbs.c +++ b/channels/chan_nbs.c @@ -41,7 +41,7 @@ static int usecnt =0; /* Only linear is allowed */ static int prefformat = AST_FORMAT_SLINEAR; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); static char context[AST_MAX_EXTENSION] = "default"; diff --git a/channels/chan_oss.c b/channels/chan_oss.c index 731ae30eea..7bd7ddb8c5 100755 --- a/channels/chan_oss.c +++ b/channels/chan_oss.c @@ -70,7 +70,7 @@ static int silencesuppression = 0; static int silencethreshold = 1000; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); static char *type = "Console"; static char *desc = "OSS Console Channel Driver"; diff --git a/channels/chan_phone.c b/channels/chan_phone.c index 3e1a28b4b3..8b2261cbb0 100755 --- a/channels/chan_phone.c +++ b/channels/chan_phone.c @@ -76,14 +76,14 @@ static int silencesupression = 0; static int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); /* Protect the interface list (of phone_pvt's) */ -static ast_mutex_t iflock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(iflock); /* Protect the monitoring thread, so only one process can kill or start it, and not when it's doing something critical. */ -static ast_mutex_t monlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(monlock); /* This is the thread for the monitor which checks for input on the channels which are not currently in use. */ diff --git a/channels/chan_sip.c b/channels/chan_sip.c index 3c0dc2b56d..74d84bd89b 100755 --- a/channels/chan_sip.c +++ b/channels/chan_sip.c @@ -108,7 +108,7 @@ static int default_expiry = DEFAULT_DEFAULT_EXPIRY; /* MYSQL_FRIENDS: Check if peer exists in database and read some configuration from databse (not all options supported though) */ #ifdef MYSQL_FRIENDS -static ast_mutex_t mysqllock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(mysqllock); static MYSQL *mysql; static char mydbuser[80]; static char mydbpass[80]; @@ -155,16 +155,16 @@ static int globalrtptimeout = 0; static int globalrtpholdtimeout = 0; static int usecnt =0; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); /* Protect the interface list (of sip_pvt's) */ -static ast_mutex_t iflock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(iflock); /* Protect the monitoring thread, so only one process can kill or start it, and not when it's doing something critical. */ -static ast_mutex_t netlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(netlock); -static ast_mutex_t monlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(monlock); /* This is the thread for the monitor which checks for input on the channels which are not currently in use. */ @@ -430,7 +430,7 @@ struct sip_peer { struct sip_peer *next; }; -static ast_mutex_t sip_reload_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(sip_reload_lock); static int sip_reloading = 0; #define REG_STATE_UNREGISTERED 0 @@ -467,20 +467,20 @@ struct sip_registry { static struct ast_user_list { struct sip_user *users; ast_mutex_t lock; -} userl = { NULL, AST_MUTEX_INITIALIZER }; +} userl; /*--- The peer list: Peers and Friends ---*/ static struct ast_peer_list { struct sip_peer *peers; ast_mutex_t lock; -} peerl = { NULL, AST_MUTEX_INITIALIZER }; +} peerl; /*--- The register list: Other SIP proxys we register with and call ---*/ static struct ast_register_list { struct sip_registry *registrations; ast_mutex_t lock; int recheck; -} regl = { NULL, AST_MUTEX_INITIALIZER }; +} regl; #define REINVITE_INVITE 1 @@ -1368,6 +1368,7 @@ static void __sip_destroy(struct sip_pvt *p, int lockowner) ast_sched_del(sched, cp->retransid); free(cp); } + ast_mutex_destroy(&p->lock); free(p); } } @@ -2018,6 +2019,7 @@ static struct sip_pvt *sip_alloc(char *callid, struct sockaddr_in *sin, int useg struct sip_pvt *p; p = malloc(sizeof(struct sip_pvt)); + ast_mutex_init(&p->lock); if (!p) return NULL; /* Keep track of stuff */ @@ -2035,6 +2037,7 @@ static struct sip_pvt *sip_alloc(char *callid, struct sockaddr_in *sin, int useg p->ocseq = 101; if (!p->rtp) { ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno)); + ast_mutex_destroy(&p->lock); free(p); return NULL; } @@ -2049,7 +2052,6 @@ static struct sip_pvt *sip_alloc(char *callid, struct sockaddr_in *sin, int useg if (p->vrtp) ast_rtp_setnat(p->vrtp, p->nat); } - ast_mutex_init(&p->lock); if (sin) { memcpy(&p->sa, sin, sizeof(p->sa)); @@ -8039,6 +8041,10 @@ int load_module() int res; struct sip_peer *peer; struct sip_registry *reg; + + ast_mutex_init(&userl.lock); + ast_mutex_init(&peerl.lock); + ast_mutex_init(®l.lock); sched = sched_context_create(); if (!sched) { ast_log(LOG_WARNING, "Unable to create schedule context\n"); diff --git a/channels/chan_skinny.c b/channels/chan_skinny.c index 22b07c5977..bb8a9a9c8b 100755 --- a/channels/chan_skinny.c +++ b/channels/chan_skinny.c @@ -585,17 +585,17 @@ static struct io_context *io; /* usage count and locking */ static int usecnt = 0; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); /* Protect the monitoring thread, so only one process can kill or start it, and not when it's doing something critical. */ -static ast_mutex_t monlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(monlock); /* Protect the network socket */ -static ast_mutex_t netlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(netlock); /* Protect the session list */ -static ast_mutex_t sessionlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(sessionlock); /* Protect the device list */ -static ast_mutex_t devicelock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(devicelock); /* This is the thread for the monitor which checks for input on the channels which are not currently in use. */ diff --git a/channels/chan_vofr.c b/channels/chan_vofr.c index 444701fd7d..7ab952dc27 100755 --- a/channels/chan_vofr.c +++ b/channels/chan_vofr.c @@ -53,14 +53,14 @@ static char context[AST_MAX_EXTENSION] = "default"; static char language[MAX_LANGUAGE] = ""; static int usecnt =0; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); /* Protect the interface list (of vofr_pvt's) */ -static ast_mutex_t iflock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(iflock); /* Protect the monitoring thread, so only one process can kill or start it, and not when it's doing something critical. */ -static ast_mutex_t monlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(monlock); /* This is the thread for the monitor which checks for input on the channels which are not currently in use. */ diff --git a/channels/chan_vpb.c b/channels/chan_vpb.c index c578946e09..7528a0f209 100755 --- a/channels/chan_vpb.c +++ b/channels/chan_vpb.c @@ -69,14 +69,14 @@ static int gruntdetect_timeout = 3600000; /* Grunt detect timeout is 1hr. */ static const int prefformat = AST_FORMAT_SLINEAR; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); /* Protect the interface list (of vpb_pvt's) */ -static ast_mutex_t iflock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(iflock); /* Protect the monitoring thread, so only one process can kill or start it, and not when it's doing something critical. */ -static ast_mutex_t monlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(monlock); /* This is the thread for the monitor which checks for input on the channels which are not currently in use. */ @@ -171,7 +171,7 @@ typedef struct { static vpb_bridge_t * bridges; static int max_bridges = MAX_BRIDGES_V4PCI; -static ast_mutex_t bridge_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(bridge_lock); typedef enum { vpb_model_unknown = 0, diff --git a/channels/chan_zap.c b/channels/chan_zap.c index 58a67ddf04..076ad18a1b 100755 --- a/channels/chan_zap.c +++ b/channels/chan_zap.c @@ -241,16 +241,16 @@ static int gendigittimeout = 8000; static int matchdigittimeout = 3000; static int usecnt =0; -static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(usecnt_lock); /* Protect the interface list (of zt_pvt's) */ -static ast_mutex_t iflock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(iflock); static int ifcount = 0; /* Protect the monitoring thread, so only one process can kill or start it, and not when it's doing something critical. */ -static ast_mutex_t monlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(monlock); /* This is the thread for the monitor which checks for input on the channels which are not currently in use. */ diff --git a/cli.c b/cli.c index 6fa4e28fd3..69d9e67fbd 100755 --- a/cli.c +++ b/cli.c @@ -46,8 +46,7 @@ void ast_cli(int fd, char *fmt, ...) free(stuff); } -ast_mutex_t clilock = AST_MUTEX_INITIALIZER; - +AST_MUTEX_DEFINE_STATIC(clilock); struct ast_cli_entry *helpers = NULL; @@ -157,7 +156,7 @@ static int handle_unload(int fd, int argc, char *argv[]) #define MODLIST_FORMAT "%-25s %-40.40s %-10d\n" #define MODLIST_FORMAT2 "%-25s %-40.40s %-10s\n" -static ast_mutex_t climodentrylock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(climodentrylock); static int climodentryfd = -1; static int modlist_modentry(char *module, char *description, int usecnt) diff --git a/codecs/codec_a_mu.c b/codecs/codec_a_mu.c index 4b66342622..84d23a146c 100755 --- a/codecs/codec_a_mu.c +++ b/codecs/codec_a_mu.c @@ -27,7 +27,7 @@ #define BUFFER_SIZE 8096 /* size for the translation buffers */ -static ast_mutex_t localuser_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(localuser_lock); static int localusecnt = 0; static char *tdesc = "A-law and Mulaw direct Coder/Decoder"; diff --git a/codecs/codec_adpcm.c b/codecs/codec_adpcm.c index 6ca656a232..9aa0304c71 100755 --- a/codecs/codec_adpcm.c +++ b/codecs/codec_adpcm.c @@ -28,7 +28,7 @@ #define BUFFER_SIZE 8096 /* size for the translation buffers */ -static ast_mutex_t localuser_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(localuser_lock); static int localusecnt = 0; static char *tdesc = "Adaptive Differential PCM Coder/Decoder"; diff --git a/codecs/codec_alaw.c b/codecs/codec_alaw.c index 08baba4bd4..5574cd451f 100755 --- a/codecs/codec_alaw.c +++ b/codecs/codec_alaw.c @@ -26,7 +26,7 @@ #define BUFFER_SIZE 8096 /* size for the translation buffers */ -static ast_mutex_t localuser_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(localuser_lock); static int localusecnt = 0; static char *tdesc = "A-law Coder/Decoder"; diff --git a/codecs/codec_g723_1.c b/codecs/codec_g723_1.c index 0d793897f8..3692b56abd 100755 --- a/codecs/codec_g723_1.c +++ b/codecs/codec_g723_1.c @@ -56,7 +56,7 @@ #include "slin_g723_ex.h" #include "g723_slin_ex.h" -static ast_mutex_t localuser_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(localuser_lock); static int localusecnt=0; #ifdef ANNEX_B diff --git a/codecs/codec_g726.c b/codecs/codec_g726.c index ac32678cab..3dad026ddc 100755 --- a/codecs/codec_g726.c +++ b/codecs/codec_g726.c @@ -29,7 +29,7 @@ #define BUFFER_SIZE 8096 /* size for the translation buffers */ #define BUF_SHIFT 5 -static ast_mutex_t localuser_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(localuser_lock); static int localusecnt = 0; static char *tdesc = "ITU G.726-32kbps G726 Transcoder"; diff --git a/codecs/codec_gsm.c b/codecs/codec_gsm.c index f0efe1a825..8e37c70ec5 100755 --- a/codecs/codec_gsm.c +++ b/codecs/codec_gsm.c @@ -39,7 +39,7 @@ #include "slin_gsm_ex.h" #include "gsm_slin_ex.h" -static ast_mutex_t localuser_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(localuser_lock); static int localusecnt=0; static char *tdesc = "GSM/PCM16 (signed linear) Codec Translator"; diff --git a/codecs/codec_ilbc.c b/codecs/codec_ilbc.c index d3497ba41c..9479212492 100755 --- a/codecs/codec_ilbc.c +++ b/codecs/codec_ilbc.c @@ -35,7 +35,7 @@ #define USE_ILBC_ENHANCER 0 -static ast_mutex_t localuser_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(localuser_lock); static int localusecnt=0; static char *tdesc = "iLBC/PCM16 (signed linear) Codec Translator"; diff --git a/codecs/codec_lpc10.c b/codecs/codec_lpc10.c index ffd1ddc23d..2b054710a5 100755 --- a/codecs/codec_lpc10.c +++ b/codecs/codec_lpc10.c @@ -43,7 +43,7 @@ #define LPC10_BYTES_IN_COMPRESSED_FRAME (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8 -static ast_mutex_t localuser_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(localuser_lock); static int localusecnt=0; static char *tdesc = "LPC10 2.4kbps (signed linear) Voice Coder"; diff --git a/codecs/codec_speex.c b/codecs/codec_speex.c index ae731fafa3..4bd5155dde 100755 --- a/codecs/codec_speex.c +++ b/codecs/codec_speex.c @@ -37,7 +37,7 @@ #include "slin_speex_ex.h" #include "speex_slin_ex.h" -static ast_mutex_t localuser_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(localuser_lock); static int localusecnt=0; static char *tdesc = "Speex/PCM16 (signed linear) Codec Translator"; diff --git a/codecs/codec_ulaw.c b/codecs/codec_ulaw.c index 41101897d3..55ff81e1cc 100755 --- a/codecs/codec_ulaw.c +++ b/codecs/codec_ulaw.c @@ -26,7 +26,7 @@ #define BUFFER_SIZE 8096 /* size for the translation buffers */ -static ast_mutex_t localuser_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(localuser_lock); static int localusecnt = 0; static char *tdesc = "Mu-law Coder/Decoder"; diff --git a/db.c b/db.c index 372d22df0a..4842b71de9 100755 --- a/db.c +++ b/db.c @@ -38,7 +38,7 @@ #include "astconf.h" static DB *astdb; -static ast_mutex_t dblock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(dblock); static int dbinit(void) { diff --git a/dns.c b/dns.c index 0ec7d6d51a..dbd918d6f2 100755 --- a/dns.c +++ b/dns.c @@ -154,7 +154,7 @@ static int dns_parse_answer(void *context, #if defined(res_ninit) #define HAS_RES_NINIT #else -static ast_mutex_t res_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(res_lock); #if 0 #warning "Warning, res_ninit is missing... Could have reentrancy issues" #endif diff --git a/enum.c b/enum.c index a4ffe3fa35..b746d45ab4 100755 --- a/enum.c +++ b/enum.c @@ -53,7 +53,7 @@ static struct enum_search { static int enumver = 0; -static ast_mutex_t enumlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(enumlock); struct naptr { unsigned short order; diff --git a/file.c b/file.c index ad80d5272b..1e896e8cd1 100755 --- a/file.c +++ b/file.c @@ -79,7 +79,7 @@ struct ast_filestream { struct ast_channel *owner; }; -static ast_mutex_t formatlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(formatlock); static struct ast_format *formats = NULL; diff --git a/formats/format_g723.c b/formats/format_g723.c index 1b610f1f0c..cf5228f8be 100755 --- a/formats/format_g723.c +++ b/formats/format_g723.c @@ -42,7 +42,7 @@ struct ast_filestream { }; -static ast_mutex_t g723_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(g723_lock); static int glistcnt = 0; static char *name = "g723sf"; diff --git a/formats/format_g726.c b/formats/format_g726.c index c12b6275f7..acb44c728b 100755 --- a/formats/format_g726.c +++ b/formats/format_g726.c @@ -59,7 +59,7 @@ struct ast_filestream { unsigned char g726[FRAME_TIME * 5]; /* G.726 encoded voice */ }; -static ast_mutex_t g726_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(g726_lock); static int glistcnt = 0; static char *desc = "Raw G.726 (16/24/32/40kbps) data"; diff --git a/formats/format_g729.c b/formats/format_g729.c index 68dde216fd..9f7531d5f2 100755 --- a/formats/format_g729.c +++ b/formats/format_g729.c @@ -49,7 +49,7 @@ struct ast_filestream { }; -static ast_mutex_t g729_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(g729_lock); static int glistcnt = 0; static char *name = "g729"; diff --git a/formats/format_gsm.c b/formats/format_gsm.c index d58039e6d2..619786e56f 100755 --- a/formats/format_gsm.c +++ b/formats/format_gsm.c @@ -58,7 +58,7 @@ struct ast_filestream { }; -static ast_mutex_t gsm_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(gsm_lock); static int glistcnt = 0; static char *name = "gsm"; diff --git a/formats/format_h263.c b/formats/format_h263.c index 7a8866666c..dd8a21454c 100755 --- a/formats/format_h263.c +++ b/formats/format_h263.c @@ -50,7 +50,7 @@ struct ast_filestream { }; -static ast_mutex_t h263_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(h263_lock); static int glistcnt = 0; static char *name = "h263"; diff --git a/formats/format_ilbc.c b/formats/format_ilbc.c index d4863c7209..0d50900d90 100755 --- a/formats/format_ilbc.c +++ b/formats/format_ilbc.c @@ -51,7 +51,7 @@ struct ast_filestream { }; -static ast_mutex_t ilbc_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(ilbc_lock); static int glistcnt = 0; static char *name = "iLBC"; diff --git a/formats/format_pcm.c b/formats/format_pcm.c index bc548992ba..2ad29471ce 100755 --- a/formats/format_pcm.c +++ b/formats/format_pcm.c @@ -47,7 +47,7 @@ struct ast_filestream { }; -static ast_mutex_t pcm_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(pcm_lock); static int glistcnt = 0; static char *name = "pcm"; diff --git a/formats/format_pcm_alaw.c b/formats/format_pcm_alaw.c index db6b0c3d03..55911d8f39 100755 --- a/formats/format_pcm_alaw.c +++ b/formats/format_pcm_alaw.c @@ -54,7 +54,7 @@ struct ast_filestream { }; -static ast_mutex_t pcm_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(pcm_lock); static int glistcnt = 0; static char *name = "alaw"; diff --git a/formats/format_vox.c b/formats/format_vox.c index 20021fb733..095332a515 100755 --- a/formats/format_vox.c +++ b/formats/format_vox.c @@ -51,7 +51,7 @@ struct ast_filestream { }; -static ast_mutex_t vox_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(vox_lock); static int glistcnt = 0; static char *name = "vox"; diff --git a/formats/format_wav.c b/formats/format_wav.c index 52f1508abd..804c38c4eb 100755 --- a/formats/format_wav.c +++ b/formats/format_wav.c @@ -53,7 +53,7 @@ struct ast_filestream { }; -static ast_mutex_t wav_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(wav_lock); static int glistcnt = 0; static char *name = "wav"; diff --git a/formats/format_wav_gsm.c b/formats/format_wav_gsm.c index 6c988bdb8e..a9f9df838a 100755 --- a/formats/format_wav_gsm.c +++ b/formats/format_wav_gsm.c @@ -63,7 +63,7 @@ struct ast_filestream { }; -static ast_mutex_t wav_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(wav_lock); static int glistcnt = 0; static char *name = "wav49"; diff --git a/frame.c b/frame.c index 1e80f0b0d1..f8905ca959 100755 --- a/frame.c +++ b/frame.c @@ -29,7 +29,7 @@ #ifdef TRACE_FRAMES static int headers = 0; static struct ast_frame *headerlist = NULL; -static ast_mutex_t framelock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(framelock); #endif #define SMOOTHER_SIZE 8000 diff --git a/image.c b/image.c index 01590436ce..f0c21035f9 100755 --- a/image.c +++ b/image.c @@ -33,7 +33,7 @@ #include "astconf.h" static struct ast_imager *list; -static ast_mutex_t listlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(listlock); int ast_image_register(struct ast_imager *img) { diff --git a/include/asterisk/linkedlists.h b/include/asterisk/linkedlists.h index 01067d5f07..b752b3adb7 100755 --- a/include/asterisk/linkedlists.h +++ b/include/asterisk/linkedlists.h @@ -16,9 +16,6 @@ struct name { \ ast_mutex_t lock; \ } -#define AST_LIST_HEAD_INITIALIZER(head) \ - { NULL, AST_MUTEX_INITIALIZER } - #define AST_LIST_HEAD_SET(head,entry) do { \ (head)->first=(entry); \ ast_pthread_mutex_init(&(head)->lock,NULL); \ diff --git a/include/asterisk/lock.h b/include/asterisk/lock.h index c40a1ddcf8..4c82928e2a 100755 --- a/include/asterisk/lock.h +++ b/include/asterisk/lock.h @@ -28,6 +28,14 @@ 0x20 } } #endif +#ifdef __FreeBSD__ +#ifdef __GNUC__ +#define AST_MUTEX_INIT_W_CONSTRUCTORS +#else +#define AST_MUTEX_INIT_ON_FIRST_USE +#endif +#endif /* __FreeBSD__ */ + #ifdef DEBUG_THREADS #ifdef THREAD_CRASH @@ -41,9 +49,17 @@ /* From now on, Asterisk REQUIRES Recursive (not error checking) mutexes and will not run without them. */ +#ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP +#define AST_MUTEX_INIT_VAULE { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, NULL, 0, NULL, 0 } +#else +#define AST_MUTEX_INIT_VAULE { PTHREAD_MUTEX_INITIALIZER, NULL, 0, NULL, 0 } +#endif -#define AST_MUTEX_INITIALIZER { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, NULL, 0, NULL, 0 } +#ifdef PTHREAD_MUTEX_RECURSIVE_NP #define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE_NP +#else +#define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE +#endif struct ast_mutex_info { pthread_mutex_t mutex; @@ -55,38 +71,53 @@ struct ast_mutex_info { typedef struct ast_mutex_info ast_mutex_t; -static inline int ast_mutex_init(ast_mutex_t *t) { - static pthread_mutexattr_t attr; - static int init = 1; - int res; - extern int pthread_mutexattr_setkind_np(pthread_mutexattr_t *, int); - - if (init) { - pthread_mutexattr_init(&attr); - pthread_mutexattr_setkind_np(&attr, AST_MUTEX_KIND); - init = 0; - } - res = pthread_mutex_init(&t->mutex, &attr); - t->file = NULL; - t->lineno = 0; - t->func = 0; - t->thread = 0; - return res; -} - static inline int ast_pthread_mutex_init(ast_mutex_t *t, pthread_mutexattr_t *attr) { - int res; - res = pthread_mutex_init(&t->mutex, attr); t->file = NULL; t->lineno = 0; t->func = 0; t->thread = 0; - return res; + return pthread_mutex_init(&t->mutex, attr); } -static inline int __ast_pthread_mutex_lock(char *filename, int lineno, char *func, ast_mutex_t *t) { +static inline int ast_mutex_init(ast_mutex_t *t) +{ + static pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, AST_MUTEX_KIND); + return ast_pthread_mutex_init(t, &attr); +} + +#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) +/* if AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope + constrictors/destructors to create/destroy mutexes. */ +#define __AST_MUTEX_DEFINE(scope,mutex) \ + scope ast_mutex_t mutex = AST_MUTEX_INIT_VAULE; \ +static void __attribute__ ((constructor)) init_##mutex(void) \ +{ \ + ast_mutex_init(&mutex); \ +} \ +static void __attribute__ ((destructor)) fini_##mutex(void) \ +{ \ + ast_mutex_destroy(&mutex); \ +} +#elif defined(AST_MUTEX_INIT_ON_FIRST_USE) || !defined(AST_MUTEX_INIT_W_CONSTRUCTORS) +/* if AST_MUTEX_INIT_ON_FIRST_USE is defined, mutexes are created on + first use. The performance impact on FreeBSD should be small since + the pthreads library does this itself to initialize errror checking + (defaulty type) mutexes. If nither is defined, the pthreads librariy + does the initialization itself on first use. */ +#define __AST_MUTEX_DEFINE(scope,mutex) \ + scope ast_mutex_t mutex = AST_MUTEX_INIT_VAULE +#endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ + +static inline int __ast_pthread_mutex_lock(char *filename, int lineno, char *func, ast_mutex_t *t) +{ int res; +#ifdef AST_MUTEX_INIT_ON_FIRST_USE + if(*t->mutex == (ast_mutex_t)AST_MUTEX_KIND) + ast_mutex_init(t->mutex); +#endif res = pthread_mutex_lock(&t->mutex); if (!res) { t->file = filename; @@ -107,6 +138,10 @@ static inline int __ast_pthread_mutex_lock(char *filename, int lineno, char *fun static inline int __ast_pthread_mutex_trylock(char *filename, int lineno, char *func, ast_mutex_t *t) { int res; +#ifdef AST_MUTEX_INIT_ON_FIRST_USE + if(*t->mutex == (ast_mutex_t)AST_MUTEX_KIND) + ast_mutex_init(t->mutex); +#endif res = pthread_mutex_trylock(&t->mutex); if (!res) { t->file = filename; @@ -166,26 +201,83 @@ static inline int __ast_pthread_mutex_destroy(char *filename, int lineno, char * /* From now on, Asterisk REQUIRES Recursive (not error checking) mutexes and will not run without them. */ -#define AST_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP +#ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP +#define AST_MUTEX_INIT_VAULE PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP +#else +#define AST_MUTEX_INIT_VAULE PTHREAD_MUTEX_INITIALIZER +#endif + +#ifdef PTHREAD_MUTEX_RECURSIVE_NP #define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE_NP +#else +#define AST_MUTEX_KIND PTHREAD_MUTEX_RECURSIVE +#endif typedef pthread_mutex_t ast_mutex_t; -#define ast_mutex_lock(t) pthread_mutex_lock(t) -#define ast_mutex_unlock(t) pthread_mutex_unlock(t) -#define ast_mutex_trylock(t) pthread_mutex_trylock(t) -static inline int ast_mutex_init(ast_mutex_t *t) +static inline int ast_mutex_init(ast_mutex_t *pmutex) { pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, AST_MUTEX_KIND); - return pthread_mutex_init(t, &attr); + return pthread_mutex_init(pmutex, &attr); } -#define ast_pthread_mutex_init(t,a) pthread_mutex_init(t,a) -#define ast_mutex_destroy(t) pthread_mutex_destroy(t) +#define ast_pthread_mutex_init(pmutex,a) pthread_mutex_init(pmutex,a) +#define ast_mutex_unlock(pmutex) pthread_mutex_unlock(pmutex) +#define ast_mutex_destroy(pmutex) pthread_mutex_destroy(pmutex) + +#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) +/* if AST_MUTEX_INIT_W_CONSTRUCTORS is defined, use file scope + constrictors/destructors to create/destroy mutexes. */ +#define __AST_MUTEX_DEFINE(scope,mutex) \ + scope ast_mutex_t mutex = AST_MUTEX_INIT_VAULE; \ +static void __attribute__ ((constructor)) init_##mutex(void) \ +{ \ + ast_mutex_init(&mutex); \ +} \ +static void __attribute__ ((destructor)) fini_##mutex(void) \ +{ \ + ast_mutex_destroy(&mutex); \ +} + +#define ast_mutex_lock(pmutex) pthread_mutex_lock(pmutex) +#define ast_mutex_trylock(pmutex) pthread_mutex_trylock(pmutex) + +#elif defined(AST_MUTEX_INIT_ON_FIRST_USE) +/* if AST_MUTEX_INIT_ON_FIRST_USE is defined, mutexes are created on + first use. The performance impact on FreeBSD should be small since + the pthreads library does this itself to initialize errror checking + (defaulty type) mutexes.*/ +#define __AST_MUTEX_DEFINE(scope,mutex) \ + scope ast_mutex_t mutex = AST_MUTEX_INIT_VAULE + +static inline int ast_mutex_lock(ast_mutex_t *pmutex) +{ + if(*pmutex == (ast_mutex_t)AST_MUTEX_KIND) + ast_mutex_init(pmutex); + return pthread_mutex_lock(pmutex); +} +static inline int ast_mutex_trylock(ast_mutex_t *pmutex) +{ + if(*pmutex == (ast_mutex_t)AST_MUTEX_KIND) + ast_mutex_init(pmutex); + return pthread_mutex_trylock(pmutex); +} +#else +/* By default, use static initialization of mutexes.*/ +#define __AST_MUTEX_DEFINE(scope,mutex) \ + scope ast_mutex_t mutex = AST_MUTEX_INIT_VAULE +#define ast_mutex_lock(pmutex) pthread_mutex_lock(pmutex) +#define ast_mutex_trylock(pmutex) pthread_mutex_trylock(pmutex) +#endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */ #endif /* DEBUG_THREADS */ +#define AST_MUTEX_DEFINE_STATIC(mutex) __AST_MUTEX_DEFINE(static,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 gethostbyname __gethostbyname__is__not__reentrant__use__ast_gethostbyname__instead__ #endif diff --git a/include/asterisk/module.h b/include/asterisk/module.h index b7a5184e8f..aba72980d7 100755 --- a/include/asterisk/module.h +++ b/include/asterisk/module.h @@ -152,7 +152,7 @@ void ast_unregister_atexit(void (*func)(void)); struct localuser *next; \ } -#define LOCAL_USER_DECL static ast_mutex_t localuser_lock = AST_MUTEX_INITIALIZER; \ +#define LOCAL_USER_DECL AST_MUTEX_DEFINE_STATIC(localuser_lock); \ static struct localuser *localusers = NULL; \ static int localusecnt = 0; diff --git a/indications.c b/indications.c index 1f8b555558..25b98a0ca5 100755 --- a/indications.c +++ b/indications.c @@ -229,7 +229,7 @@ static struct tone_zone *current_tonezone; /* Protect the tone_zones list (highly unlikely that two things would change * it at the same time, but still! */ -ast_mutex_t tzlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_EXPORTED(tzlock); /* Set global indication country */ int ast_set_indication_country(const char *country) diff --git a/loader.c b/loader.c index 7451fd4664..b05cdf7668 100755 --- a/loader.c +++ b/loader.c @@ -98,8 +98,8 @@ static struct loadupdate { struct loadupdate *next; } *updaters = NULL; -static ast_mutex_t modlock = AST_MUTEX_INITIALIZER; -static ast_mutex_t reloadlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(modlock); +AST_MUTEX_DEFINE_STATIC(reloadlock); static struct module *module_list=NULL; diff --git a/logger.c b/logger.c index 331ca669f2..2b7f9f2c68 100755 --- a/logger.c +++ b/logger.c @@ -49,8 +49,8 @@ static int syslog_level_map[] = { #define MAX_MSG_QUEUE 200 static char dateformat[256] = "%b %e %T"; /* Original Asterisk Format */ -static ast_mutex_t msglist_lock = AST_MUTEX_INITIALIZER; -static ast_mutex_t loglock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(msglist_lock); +AST_MUTEX_DEFINE_STATIC(loglock); static int pending_logger_reload = 0; static struct msglist { @@ -228,7 +228,7 @@ static void init_logger_chain(void) } static FILE *qlog = NULL; -static ast_mutex_t qloglock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(qloglock); void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...) { diff --git a/manager.c b/manager.c index 9806da78a7..f5055d1244 100755 --- a/manager.c +++ b/manager.c @@ -59,7 +59,7 @@ static int enabled = 0; static int portno = DEFAULT_MANAGER_PORT; static int asock = -1; static pthread_t t; -static ast_mutex_t sessionlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(sessionlock); static int block_sockets = 0; static struct permalias { @@ -78,7 +78,7 @@ static struct permalias { static struct mansession *sessions = NULL; static struct manager_action *first_action = NULL; -static ast_mutex_t actionlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(actionlock); int ast_carefulwrite(int fd, char *s, int len, int timeoutms) { diff --git a/pbx.c b/pbx.c index 02acc3ded4..02c006b69d 100755 --- a/pbx.c +++ b/pbx.c @@ -176,7 +176,7 @@ int pbx_builtin_setvar(struct ast_channel *, void *); void pbx_builtin_setvar_helper(struct ast_channel *chan, char *name, char *value); char *pbx_builtin_getvar_helper(struct ast_channel *chan, char *name); -static struct varshead globals = AST_LIST_HEAD_INITIALIZER(varshead); +static struct varshead globals; static struct pbx_builtin { char name[AST_MAX_APP]; @@ -362,18 +362,18 @@ static struct pbx_builtin { }; /* Lock for the application list */ -static ast_mutex_t applock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(applock); static struct ast_context *contexts = NULL; /* Lock for the ast_context list */ -static ast_mutex_t conlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(conlock); static struct ast_app *apps = NULL; /* Lock for switches */ -static ast_mutex_t switchlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(switchlock); struct ast_switch *switches = NULL; /* Lock for extension state notifys */ -static ast_mutex_t hintlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(hintlock); static int stateid = 1; struct ast_hint *hints = NULL; struct ast_state_cb *statecbs = NULL; @@ -4654,6 +4654,7 @@ int load_pbx(void) ast_verbose( "Asterisk PBX Core Initializing\n"); ast_verbose( "Registering builtin applications:\n"); } + AST_LIST_HEAD_INIT(&globals); ast_cli_register(&show_applications_cli); ast_cli_register(&show_application_cli); ast_cli_register(&show_dialplan_cli); diff --git a/pbx/pbx_config.c b/pbx/pbx_config.c index 2d47c4db18..0c0cbd1b64 100755 --- a/pbx/pbx_config.c +++ b/pbx/pbx_config.c @@ -42,7 +42,7 @@ static char *registrar = "pbx_config"; static int static_config = 0; static int write_protect_config = 1; -static ast_mutex_t save_dialplan_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(save_dialplan_lock); static struct ast_context *local_contexts = NULL; diff --git a/pbx/pbx_gtkconsole.c b/pbx/pbx_gtkconsole.c index 86755d245b..fa2ac21acf 100755 --- a/pbx/pbx_gtkconsole.c +++ b/pbx/pbx_gtkconsole.c @@ -41,7 +41,7 @@ #include "../asterisk.h" #include "../astconf.h" -static ast_mutex_t verb_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(verb_lock); static pthread_t console_thread; diff --git a/res/res_crypto.c b/res/res_crypto.c index 2a2e5cb8b9..e0cb6233cd 100755 --- a/res/res_crypto.c +++ b/res/res_crypto.c @@ -59,7 +59,7 @@ static char base64[64]; static char b2a[256]; -static ast_mutex_t keylock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(keylock); #define KEY_NEEDS_PASSCODE (1 << 16) diff --git a/res/res_monitor.c b/res/res_monitor.c index 2f028ca935..43b6d728e4 100755 --- a/res/res_monitor.c +++ b/res/res_monitor.c @@ -23,7 +23,7 @@ #define AST_MONITOR_DIR AST_SPOOL_DIR "/monitor" -static ast_mutex_t monitorlock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(monitorlock); static unsigned long seq = 0; diff --git a/res/res_musiconhold.c b/res/res_musiconhold.c index d6bc6c760e..27519a7ea6 100755 --- a/res/res_musiconhold.c +++ b/res/res_musiconhold.c @@ -99,7 +99,7 @@ struct mohdata { static struct mohclass *mohclasses; -static ast_mutex_t moh_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(moh_lock); #define LOCAL_MPG_123 "/usr/local/bin/mpg123" #define MPG_123 "/usr/bin/mpg123" diff --git a/res/res_parking.c b/res/res_parking.c index e63f490cb2..29db04136d 100755 --- a/res/res_parking.c +++ b/res/res_parking.c @@ -84,7 +84,7 @@ struct parkeduser { static struct parkeduser *parkinglot; -static ast_mutex_t parking_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(parking_lock); static pthread_t parking_thread; diff --git a/stdtime/localtime.c b/stdtime/localtime.c index f0b67e67ee..c0058300e0 100755 --- a/stdtime/localtime.c +++ b/stdtime/localtime.c @@ -184,10 +184,10 @@ static struct state * gmtptr = NULL; static int gmt_is_set; #ifdef _THREAD_SAFE -static ast_mutex_t lcl_mutex = AST_MUTEX_INITIALIZER; -static ast_mutex_t tzset_mutex = AST_MUTEX_INITIALIZER; -static ast_mutex_t tzsetwall_mutex = AST_MUTEX_INITIALIZER; -static ast_mutex_t gmt_mutex = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(lcl_mutex); +AST_MUTEX_DEFINE_STATIC(tzset_mutex); +AST_MUTEX_DEFINE_STATIC(tzsetwall_mutex); +AST_MUTEX_DEFINE_STATIC(gmt_mutex); #endif /* diff --git a/translate.c b/translate.c index 1a9529542c..56f0e5e998 100755 --- a/translate.c +++ b/translate.c @@ -34,7 +34,7 @@ /* This could all be done more efficiently *IF* we chained packets together by default, but it would also complicate virtually every application. */ -static ast_mutex_t list_lock = AST_MUTEX_INITIALIZER; +AST_MUTEX_DEFINE_STATIC(list_lock); static struct ast_translator *list = NULL; struct ast_translator_dir { diff --git a/utils.c b/utils.c index a1ecb139bd..37ffcb0bd2 100755 --- a/utils.c +++ b/utils.c @@ -20,13 +20,14 @@ #define ERANGE 34 #undef gethostbyname +AST_MUTEX_DEFINE_STATIC(__mutex); + int gethostbyname_r (const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop) { int hsave; struct hostent *ph; - static ast_mutex_t __mutex = AST_MUTEX_INITIALIZER; ast_mutex_lock(&__mutex); /* begin critical area */ hsave = h_errno;