update mutex handling

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@1319 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.0
Jeremy McNamara 22 years ago
parent 5b3ad85c96
commit 5047f6f219

@ -86,7 +86,7 @@ static char secret[50];
/** Private structure of a OpenH323 channel */ /** Private structure of a OpenH323 channel */
struct oh323_pvt { struct oh323_pvt {
pthread_mutex_t lock; /* Channel private lock */ ast_mutex_t lock; /* Channel private lock */
call_options_t calloptions; /* Options to be used during call setup */ call_options_t calloptions; /* Options to be used during call setup */
int alreadygone; /* Whether or not we've already been destroyed by or peer */ int alreadygone; /* Whether or not we've already been destroyed by or peer */
int needdestroy; /* if we need to be destroyed */ int needdestroy; /* if we need to be destroyed */
@ -111,17 +111,17 @@ struct oh323_pvt {
static struct ast_user_list { static struct ast_user_list {
struct oh323_user *users; struct oh323_user *users;
pthread_mutex_t lock; ast_mutex_t lock;
} userl = { NULL, AST_MUTEX_INITIALIZER }; } userl = { NULL, AST_MUTEX_INITIALIZER };
static struct ast_peer_list { static struct ast_peer_list {
struct oh323_peer *peers; struct oh323_peer *peers;
pthread_mutex_t lock; ast_mutex_t lock;
} peerl = { NULL, AST_MUTEX_INITIALIZER }; } peerl = { NULL, AST_MUTEX_INITIALIZER };
static struct ast_alias_list { static struct ast_alias_list {
struct oh323_alias *aliases; struct oh323_alias *aliases;
pthread_mutex_t lock; ast_mutex_t lock;
} aliasl = { NULL, AST_MUTEX_INITIALIZER }; } aliasl = { NULL, AST_MUTEX_INITIALIZER };
/** Asterisk RTP stuff*/ /** Asterisk RTP stuff*/
@ -129,15 +129,15 @@ static struct sched_context *sched;
static struct io_context *io; static struct io_context *io;
/** Protect the interface list (of oh323_pvt's) */ /** Protect the interface list (of oh323_pvt's) */
static pthread_mutex_t iflock = AST_MUTEX_INITIALIZER; static ast_mutex_t iflock = AST_MUTEX_INITIALIZER;
/** Usage counter and associated lock */ /** Usage counter and associated lock */
static int usecnt =0; static int usecnt =0;
static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
/* Protect the monitoring thread, so only one process can kill or start it, and not /* Protect the monitoring thread, so only one process can kill or start it, and not
when it's doing something critical. */ when it's doing something critical. */
static pthread_mutex_t monlock = AST_MUTEX_INITIALIZER; static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
/* This is the thread for the monitor which checks for input on the channels /* This is the thread for the monitor which checks for input on the channels
which are not currently in use. */ which are not currently in use. */
@ -155,10 +155,10 @@ static void __oh323_destroy(struct oh323_pvt *p)
/* Unlink us from the owner if we have one */ /* Unlink us from the owner if we have one */
if (p->owner) { if (p->owner) {
ast_pthread_mutex_lock(&p->owner->lock); ast_mutex_lock(&p->owner->lock);
ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name); ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
p->owner->pvt->pvt = NULL; p->owner->pvt->pvt = NULL;
ast_pthread_mutex_unlock(&p->owner->lock); ast_mutex_unlock(&p->owner->lock);
} }
cur = iflist; cur = iflist;
while(cur) { while(cur) {
@ -180,9 +180,9 @@ static void __oh323_destroy(struct oh323_pvt *p)
static void oh323_destroy(struct oh323_pvt *p) static void oh323_destroy(struct oh323_pvt *p)
{ {
ast_pthread_mutex_lock(&iflock); ast_mutex_lock(&iflock);
__oh323_destroy(p); __oh323_destroy(p);
ast_pthread_mutex_unlock(&iflock); ast_mutex_unlock(&iflock);
} }
static struct oh323_alias *build_alias(char *name, struct ast_variable *v) static struct oh323_alias *build_alias(char *name, struct ast_variable *v)
@ -280,7 +280,7 @@ static struct oh323_peer *build_peer(char *name, struct ast_variable *v)
int found=0; int found=0;
prev = NULL; prev = NULL;
ast_pthread_mutex_lock(&peerl.lock); ast_mutex_lock(&peerl.lock);
peer = peerl.peers; peer = peerl.peers;
while(peer) { while(peer) {
@ -299,9 +299,9 @@ static struct oh323_peer *build_peer(char *name, struct ast_variable *v)
} else { } else {
peerl.peers = peer->next; peerl.peers = peer->next;
} }
ast_pthread_mutex_unlock(&peerl.lock); ast_mutex_unlock(&peerl.lock);
} else { } else {
ast_pthread_mutex_unlock(&peerl.lock); ast_mutex_unlock(&peerl.lock);
peer = malloc(sizeof(struct oh323_peer)); peer = malloc(sizeof(struct oh323_peer));
memset(peer, 0, sizeof(struct oh323_peer)); memset(peer, 0, sizeof(struct oh323_peer));
} }
@ -452,11 +452,11 @@ static int oh323_hangup(struct ast_channel *c)
ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n"); ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
return 0; return 0;
} }
ast_pthread_mutex_lock(&p->lock); ast_mutex_lock(&p->lock);
/* Determine how to disconnect */ /* Determine how to disconnect */
if (p->owner != c) { if (p->owner != c) {
ast_log(LOG_WARNING, "Huh? We aren't the owner?\n"); ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
ast_pthread_mutex_unlock(&p->lock); ast_mutex_unlock(&p->lock);
return 0; return 0;
} }
if (!c || (c->_state != AST_STATE_UP)) if (!c || (c->_state != AST_STATE_UP))
@ -480,14 +480,14 @@ static int oh323_hangup(struct ast_channel *c)
} }
/* Update usage counter */ /* Update usage counter */
ast_pthread_mutex_lock(&usecnt_lock); ast_mutex_lock(&usecnt_lock);
usecnt--; usecnt--;
if (usecnt < 0) if (usecnt < 0)
ast_log(LOG_WARNING, "Usecnt < 0\n"); ast_log(LOG_WARNING, "Usecnt < 0\n");
ast_pthread_mutex_unlock(&usecnt_lock); ast_mutex_unlock(&usecnt_lock);
ast_update_use_count(); ast_update_use_count();
ast_pthread_mutex_unlock(&p->lock); ast_mutex_unlock(&p->lock);
return 0; return 0;
} }
@ -528,9 +528,9 @@ static struct ast_frame *oh323_read(struct ast_channel *c)
{ {
struct ast_frame *fr; struct ast_frame *fr;
struct oh323_pvt *p = c->pvt->pvt; struct oh323_pvt *p = c->pvt->pvt;
ast_pthread_mutex_lock(&p->lock); ast_mutex_lock(&p->lock);
fr = oh323_rtp_read(p); fr = oh323_rtp_read(p);
ast_pthread_mutex_unlock(&p->lock); ast_mutex_unlock(&p->lock);
return fr; return fr;
} }
@ -553,11 +553,11 @@ static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
} }
} }
if (p) { if (p) {
ast_pthread_mutex_lock(&p->lock); ast_mutex_lock(&p->lock);
if (p->rtp) { if (p->rtp) {
res = ast_rtp_write(p->rtp, frame); res = ast_rtp_write(p->rtp, frame);
} }
ast_pthread_mutex_unlock(&p->lock); ast_mutex_unlock(&p->lock);
} }
return res; return res;
} }
@ -605,13 +605,13 @@ static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
{ {
struct oh323_pvt *p = newchan->pvt->pvt; struct oh323_pvt *p = newchan->pvt->pvt;
ast_pthread_mutex_lock(&p->lock); ast_mutex_lock(&p->lock);
if (p->owner != oldchan) { if (p->owner != oldchan) {
ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner); ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
return -1; return -1;
} }
p->owner = newchan; p->owner = newchan;
ast_pthread_mutex_unlock(&p->lock); ast_mutex_unlock(&p->lock);
return 0; return 0;
} }
@ -661,9 +661,9 @@ static struct ast_channel *oh323_new(struct oh323_pvt *i, int state, const char
/* Set the owner of this channel */ /* Set the owner of this channel */
i->owner = tmp; i->owner = tmp;
ast_pthread_mutex_lock(&usecnt_lock); ast_mutex_lock(&usecnt_lock);
usecnt++; usecnt++;
ast_pthread_mutex_unlock(&usecnt_lock); ast_mutex_unlock(&usecnt_lock);
ast_update_use_count(); ast_update_use_count();
strncpy(tmp->context, i->context, sizeof(tmp->context)-1); strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
strncpy(tmp->exten, i->exten, sizeof(tmp->exten)-1); strncpy(tmp->exten, i->exten, sizeof(tmp->exten)-1);
@ -706,7 +706,7 @@ static struct oh323_pvt *oh323_alloc(int callid)
return NULL; return NULL;
} }
ast_rtp_settos(p->rtp, tos); ast_rtp_settos(p->rtp, tos);
ast_pthread_mutex_init(&p->lock); ast_mutex_init(&p->lock);
p->cd.call_reference = callid; p->cd.call_reference = callid;
p->bridge = 1; p->bridge = 1;
@ -716,10 +716,10 @@ static struct oh323_pvt *oh323_alloc(int callid)
p->nonCodecCapability |= AST_RTP_DTMF; p->nonCodecCapability |= AST_RTP_DTMF;
/* Add to interface list */ /* Add to interface list */
ast_pthread_mutex_lock(&iflock); ast_mutex_lock(&iflock);
p->next = iflist; p->next = iflist;
iflist = p; iflist = p;
ast_pthread_mutex_unlock(&iflock); ast_mutex_unlock(&iflock);
return p; return p;
} }
@ -727,18 +727,18 @@ static struct oh323_pvt *find_call(int call_reference)
{ {
struct oh323_pvt *p; struct oh323_pvt *p;
ast_pthread_mutex_lock(&iflock); ast_mutex_lock(&iflock);
p = iflist; p = iflist;
while(p) { while(p) {
if (p->cd.call_reference == call_reference) { if (p->cd.call_reference == call_reference) {
/* Found the call */ /* Found the call */
ast_pthread_mutex_unlock(&iflock); ast_mutex_unlock(&iflock);
return p; return p;
} }
p = p->next; p = p->next;
} }
ast_pthread_mutex_unlock(&iflock); ast_mutex_unlock(&iflock);
return NULL; return NULL;
} }
@ -1180,7 +1180,7 @@ static void *do_monitor(void *data)
for(;;) { for(;;) {
/* Check for interfaces needing to be killed */ /* Check for interfaces needing to be killed */
ast_pthread_mutex_lock(&iflock); ast_mutex_lock(&iflock);
restartsearch: restartsearch:
oh323 = iflist; oh323 = iflist;
while(oh323) { while(oh323) {
@ -1190,7 +1190,7 @@ restartsearch:
} }
oh323 = oh323->next; oh323 = oh323->next;
} }
ast_pthread_mutex_unlock(&iflock); ast_mutex_unlock(&iflock);
pthread_testcancel(); pthread_testcancel();
@ -1199,10 +1199,10 @@ restartsearch:
if ((res < 0) || (res > 1000)) if ((res < 0) || (res > 1000))
res = 1000; res = 1000;
res = ast_io_wait(io, res); res = ast_io_wait(io, res);
ast_pthread_mutex_lock(&monlock); ast_mutex_lock(&monlock);
if (res >= 0) if (res >= 0)
ast_sched_runq(sched); ast_sched_runq(sched);
ast_pthread_mutex_unlock(&monlock); ast_mutex_unlock(&monlock);
} }
/* Never reached */ /* Never reached */
return NULL; return NULL;
@ -1214,12 +1214,12 @@ static int restart_monitor(void)
/* If we're supposed to be stopped -- stay stopped */ /* If we're supposed to be stopped -- stay stopped */
if (monitor_thread == -2) if (monitor_thread == -2)
return 0; return 0;
if (ast_pthread_mutex_lock(&monlock)) { if (ast_mutex_lock(&monlock)) {
ast_log(LOG_WARNING, "Unable to lock monitor\n"); ast_log(LOG_WARNING, "Unable to lock monitor\n");
return -1; return -1;
} }
if (monitor_thread == pthread_self()) { if (monitor_thread == pthread_self()) {
ast_pthread_mutex_unlock(&monlock); ast_mutex_unlock(&monlock);
ast_log(LOG_WARNING, "Cannot kill myself\n"); ast_log(LOG_WARNING, "Cannot kill myself\n");
return -1; return -1;
} }
@ -1229,12 +1229,12 @@ static int restart_monitor(void)
} else { } else {
/* Start a new monitor */ /* Start a new monitor */
if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) { if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
ast_pthread_mutex_unlock(&monlock); ast_mutex_unlock(&monlock);
ast_log(LOG_ERROR, "Unable to start monitor thread.\n"); ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
return -1; return -1;
} }
} }
ast_pthread_mutex_unlock(&monlock); ast_mutex_unlock(&monlock);
return 0; return 0;
} }
@ -1442,26 +1442,26 @@ int reload_config()
if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) { if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
user = build_user(cat, ast_variable_browse(cfg, cat)); user = build_user(cat, ast_variable_browse(cfg, cat));
if (user) { if (user) {
ast_pthread_mutex_lock(&userl.lock); ast_mutex_lock(&userl.lock);
user->next = userl.users; user->next = userl.users;
userl.users = user; userl.users = user;
ast_pthread_mutex_unlock(&userl.lock); ast_mutex_unlock(&userl.lock);
} }
} else if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) { } else if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
peer = build_peer(cat, ast_variable_browse(cfg, cat)); peer = build_peer(cat, ast_variable_browse(cfg, cat));
if (peer) { if (peer) {
ast_pthread_mutex_lock(&peerl.lock); ast_mutex_lock(&peerl.lock);
peer->next = peerl.peers; peer->next = peerl.peers;
peerl.peers = peer; peerl.peers = peer;
ast_pthread_mutex_unlock(&peerl.lock); ast_mutex_unlock(&peerl.lock);
} }
} else if (!strcasecmp(utype, "h323")) { } else if (!strcasecmp(utype, "h323")) {
alias = build_alias(cat, ast_variable_browse(cfg, cat)); alias = build_alias(cat, ast_variable_browse(cfg, cat));
if (alias) { if (alias) {
ast_pthread_mutex_lock(&aliasl.lock); ast_mutex_lock(&aliasl.lock);
alias->next = aliasl.aliases; alias->next = aliasl.aliases;
aliasl.aliases = alias; aliasl.aliases = alias;
ast_pthread_mutex_unlock(&aliasl.lock); ast_mutex_unlock(&aliasl.lock);
} }
} else { } else {
ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config); ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
@ -1497,21 +1497,21 @@ void delete_users(void)
struct oh323_peer *peer; struct oh323_peer *peer;
/* Delete all users */ /* Delete all users */
ast_pthread_mutex_lock(&userl.lock); ast_mutex_lock(&userl.lock);
for (user=userl.users;user;) { for (user=userl.users;user;) {
userlast = user; userlast = user;
user=user->next; user=user->next;
free(userlast); free(userlast);
} }
userl.users=NULL; userl.users=NULL;
ast_pthread_mutex_unlock(&userl.lock); ast_mutex_unlock(&userl.lock);
ast_pthread_mutex_lock(&peerl.lock); ast_mutex_lock(&peerl.lock);
for (peer=peerl.peers;peer;) { for (peer=peerl.peers;peer;) {
/* Assume all will be deleted, and we'll find out for sure later */ /* Assume all will be deleted, and we'll find out for sure later */
peer->delme = 1; peer->delme = 1;
peer = peer->next; peer = peer->next;
} }
ast_pthread_mutex_unlock(&peerl.lock); ast_mutex_unlock(&peerl.lock);
} }
void delete_aliases(void) void delete_aliases(void)
@ -1519,21 +1519,21 @@ void delete_aliases(void)
struct oh323_alias *alias, *aliaslast; struct oh323_alias *alias, *aliaslast;
/* Delete all users */ /* Delete all users */
ast_pthread_mutex_lock(&aliasl.lock); ast_mutex_lock(&aliasl.lock);
for (alias=aliasl.aliases;alias;) { for (alias=aliasl.aliases;alias;) {
aliaslast = alias; aliaslast = alias;
alias=alias->next; alias=alias->next;
free(aliaslast); free(aliaslast);
} }
aliasl.aliases=NULL; aliasl.aliases=NULL;
ast_pthread_mutex_unlock(&aliasl.lock); ast_mutex_unlock(&aliasl.lock);
} }
void prune_peers(void) void prune_peers(void)
{ {
/* Prune peers who still are supposed to be deleted */ /* Prune peers who still are supposed to be deleted */
struct oh323_peer *peer, *peerlast, *peernext; struct oh323_peer *peer, *peerlast, *peernext;
ast_pthread_mutex_lock(&peerl.lock); ast_mutex_lock(&peerl.lock);
peerlast = NULL; peerlast = NULL;
for (peer=peerl.peers;peer;) { for (peer=peerl.peers;peer;) {
peernext = peer->next; peernext = peer->next;
@ -1547,7 +1547,7 @@ void prune_peers(void)
peerlast = peer; peerlast = peer;
peer=peernext; peer=peernext;
} }
ast_pthread_mutex_unlock(&peerl.lock); ast_mutex_unlock(&peerl.lock);
} }
int reload(void) int reload(void)
@ -1693,7 +1693,7 @@ int unload_module()
{ {
struct oh323_pvt *p, *pl; struct oh323_pvt *p, *pl;
if (!ast_pthread_mutex_lock(&iflock)) { if (!ast_mutex_lock(&iflock)) {
/* hangup all interfaces if they have an owner */ /* hangup all interfaces if they have an owner */
p = iflist; p = iflist;
while(p) { while(p) {
@ -1702,13 +1702,13 @@ int unload_module()
p = p->next; p = p->next;
} }
iflist = NULL; iflist = NULL;
ast_pthread_mutex_unlock(&iflock); ast_mutex_unlock(&iflock);
} else { } else {
ast_log(LOG_WARNING, "Unable to lock the interface list\n"); ast_log(LOG_WARNING, "Unable to lock the interface list\n");
return -1; return -1;
} }
if (!ast_pthread_mutex_lock(&iflock)) { if (!ast_mutex_lock(&iflock)) {
/* destroy all the interfaces and free their memory */ /* destroy all the interfaces and free their memory */
p = iflist; p = iflist;
while(p) { while(p) {
@ -1718,7 +1718,7 @@ int unload_module()
free(pl); free(pl);
} }
iflist = NULL; iflist = NULL;
ast_pthread_mutex_unlock(&iflock); ast_mutex_unlock(&iflock);
} else { } else {
ast_log(LOG_WARNING, "Unable to lock the interface list\n"); ast_log(LOG_WARNING, "Unable to lock the interface list\n");
return -1; return -1;
@ -1747,9 +1747,9 @@ int unload_module()
int usecount() int usecount()
{ {
int res; int res;
ast_pthread_mutex_lock(&usecnt_lock); ast_mutex_lock(&usecnt_lock);
res = usecnt; res = usecnt;
ast_pthread_mutex_unlock(&usecnt_lock); ast_mutex_unlock(&usecnt_lock);
return res; return res;
} }

Loading…
Cancel
Save