[asterisk-commits] mnicholson: trunk r314960 - in /trunk: ./ channels/chan_agent.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Apr 22 16:33:44 CDT 2011


Author: mnicholson
Date: Fri Apr 22 16:33:42 2011
New Revision: 314960

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=314960
Log:
Merged revisions 314959 via svnmerge from 
https://origsvn.digium.com/svn/asterisk/branches/1.8

................
  r314959 | mnicholson | 2011-04-22 16:20:08 -0500 (Fri, 22 Apr 2011) | 24 lines
  
  Merged revisions 314958 via svnmerge from 
  https://origsvn.digium.com/svn/asterisk/branches/1.6.2
  
  ................
    r314958 | mnicholson | 2011-04-22 15:49:45 -0500 (Fri, 22 Apr 2011) | 17 lines
    
    Merged revisions 311203,314908 via svnmerge from 
    https://origsvn.digium.com/svn/asterisk/branches/1.4
    
    ........
      r311203 | mnicholson | 2011-03-17 14:14:37 -0500 (Thu, 17 Mar 2011) | 4 lines
      
      Don't hold the pvt lock while streaming a file.
      
      ABE-2756
    ........
      r314908 | mnicholson | 2011-04-22 15:01:48 -0500 (Fri, 22 Apr 2011) | 4 lines
      
      Prevent the login thread and the app threads from using the asterisk channel at the same time.
      
      ABE-2756
    ........
  ................
................

Modified:
    trunk/   (props changed)
    trunk/channels/chan_agent.c

Propchange: trunk/
------------------------------------------------------------------------------
Binary property 'branch-1.8-merged' - no diff available.

Modified: trunk/channels/chan_agent.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/chan_agent.c?view=diff&rev=314960&r1=314959&r2=314960
==============================================================================
--- trunk/channels/chan_agent.c (original)
+++ trunk/channels/chan_agent.c Fri Apr 22 16:33:42 2011
@@ -269,9 +269,9 @@
 	char agent[AST_MAX_AGENT];     /*!< Agent ID */
 	char password[AST_MAX_AGENT];  /*!< Password for Agent login */
 	char name[AST_MAX_AGENT];
-	ast_mutex_t app_lock;          /**< Synchronization between owning applications */
 	int app_lock_flag;
 	ast_cond_t app_complete_cond;
+	ast_cond_t login_wait_cond;
 	volatile int app_sleep_cond;   /**< Sleep condition for the login app */
 	struct ast_channel *owner;     /**< Agent */
 	char logincallerid[80];        /**< Caller ID they had when they logged in */
@@ -429,8 +429,8 @@
 			return NULL;
 		ast_copy_string(p->agent, agt, sizeof(p->agent));
 		ast_mutex_init(&p->lock);
-		ast_mutex_init(&p->app_lock);
 		ast_cond_init(&p->app_complete_cond, NULL);
+		ast_cond_init(&p->login_wait_cond, NULL);
 		p->app_lock_flag = 0;
 		p->app_sleep_cond = 1;
 		p->group = group;
@@ -483,20 +483,23 @@
  */
 static int agent_cleanup(struct agent_pvt *p)
 {
-	struct ast_channel *chan = p->owner;
+	struct ast_channel *chan = NULL;
+	ast_mutex_lock(&p->lock);
+	chan = p->owner;
 	p->owner = NULL;
 	chan->tech_pvt = NULL;
+	/* Release ownership of the agent to other threads (presumably running the login app). */
 	p->app_sleep_cond = 1;
-	/* Release ownership of the agent to other threads (presumably running the login app). */
 	p->app_lock_flag = 0;
 	ast_cond_signal(&p->app_complete_cond);
 	if (chan) {
 		chan = ast_channel_release(chan);
 	}
 	if (p->dead) {
+		ast_mutex_unlock(&p->lock);
 		ast_mutex_destroy(&p->lock);
-		ast_mutex_destroy(&p->app_lock);
 		ast_cond_destroy(&p->app_complete_cond);
+		ast_cond_destroy(&p->login_wait_cond);
 		ast_free(p);
         }
 	return 0;
@@ -773,30 +776,42 @@
 	struct agent_pvt *p = ast->tech_pvt;
 	int res = -1;
 	int newstate=0;
+	struct ast_channel *chan;
+
 	ast_mutex_lock(&p->lock);
 	p->acknowledged = 0;
+
+	if (p->pending) {
+		ast_log(LOG_DEBUG, "Pretending to dial on pending agent\n");
+		ast_mutex_unlock(&p->lock);
+		ast_setstate(ast, AST_STATE_DIALING);
+		return 0;
+	}
+
 	if (!p->chan) {
-		if (p->pending) {
-			ast_debug(1, "Pretending to dial on pending agent\n");
-			newstate = AST_STATE_DIALING;
-			res = 0;
-		} else {
-			ast_log(LOG_NOTICE, "Whoa, they hung up between alloc and call...  what are the odds of that?\n");
-			res = -1;
-		}
+		ast_log(LOG_DEBUG, "Agent disconnected while we were connecting the call\n");
 		ast_mutex_unlock(&p->lock);
-		if (newstate)
-			ast_setstate(ast, newstate);
 		return res;
 	}
 	ast_verb(3, "agent_call, call to agent '%s' call on '%s'\n", p->agent, p->chan->name);
 	ast_debug(3, "Playing beep, lang '%s'\n", p->chan->language);
-	res = ast_streamfile(p->chan, beep, p->chan->language);
+	
+	chan = p->chan;
+	ast_mutex_unlock(&p->lock);
+
+	res = ast_streamfile(chan, beep, chan->language);
 	ast_debug(3, "Played beep, result '%d'\n", res);
 	if (!res) {
-		res = ast_waitstream(p->chan, "");
+		res = ast_waitstream(chan, "");
 		ast_debug(3, "Waited for stream, result '%d'\n", res);
 	}
+	
+	ast_mutex_lock(&p->lock);
+	if (!p->chan) {
+		/* chan went away while we were streaming, this shouldn't be possible */
+		res = -1;
+	}
+
 	if (!res) {
 		struct ast_format tmpfmt;
 		res = ast_set_read_format_from_cap(p->chan, p->chan->nativeformats);
@@ -879,6 +894,10 @@
 	ast->tech_pvt = NULL;
 	p->app_sleep_cond = 1;
 	p->acknowledged = 0;
+
+	/* Release ownership of the agent to other threads (presumably running the login app). */
+	p->app_lock_flag = 0;
+	ast_cond_signal(&p->app_complete_cond);
 
 	/* if they really are hung up then set start to 0 so the test
 	 * later if we're called on an already downed channel
@@ -930,8 +949,8 @@
 		p->abouttograb = 0;
 	} else if (p->dead) {
 		ast_mutex_destroy(&p->lock);
-		ast_mutex_destroy(&p->app_lock);
 		ast_cond_destroy(&p->app_complete_cond);
+		ast_cond_destroy(&p->login_wait_cond);
 		ast_free(p);
 	} else {
 		if (p->chan) {
@@ -941,9 +960,6 @@
 			p->lastdisc = ast_tvadd(ast_tvnow(), ast_samp2tv(p->wrapuptime, 1000));
 			ast_mutex_unlock(&p->lock);
 		}
-		/* Release ownership of the agent to other threads (presumably running the login app). */
-		p->app_lock_flag = 0;
-		ast_cond_signal(&p->app_complete_cond);
 	}
 	return 0;
 }
@@ -1030,7 +1046,6 @@
 static struct ast_channel *agent_new(struct agent_pvt *p, int state, const char *linkedid)
 {
 	struct ast_channel *tmp;
-	int alreadylocked;
 #if 0
 	if (!p->chan) {
 		ast_log(LOG_WARNING, "No channel? :(\n");
@@ -1068,38 +1083,6 @@
 	tmp->tech_pvt = p;
 	p->owner = tmp;
 	tmp->priority = 1;
-	/* Wake up and wait for other applications (by definition the login app)
-	 * to release this channel). Takes ownership of the agent channel
-	 * to this thread only.
-	 * For signalling the other thread, ast_queue_frame is used until we
-	 * can safely use signals for this purpose. The pselect() needs to be
-	 * implemented in the kernel for this.
-	 */
-	p->app_sleep_cond = 0;
-
-	alreadylocked = p->app_lock_flag;
-	p->app_lock_flag = 1;
-
-	if (alreadylocked) {
-		if (p->chan) {
-			ast_queue_frame(p->chan, &ast_null_frame);
-			ast_mutex_unlock(&p->lock);	/* For other thread to read the condition. */
-			p->app_lock_flag = 1;
-			ast_mutex_lock(&p->lock);
-		} else {
-			ast_log(LOG_WARNING, "Agent disconnected while we were connecting the call\n");
-			p->owner = NULL;
-			tmp->tech_pvt = NULL;
-			p->app_sleep_cond = 1;
-			tmp = ast_channel_release(tmp);
-			ast_mutex_unlock(&p->lock);	/* For other thread to read the condition. */
-			p->app_lock_flag = 0;
-			ast_cond_signal(&p->app_complete_cond);
-			return NULL;
-		}
-	} 
-	if (p->chan)
-		ast_indicate(p->chan, AST_CONTROL_UNHOLD);
 	return tmp;
 }
 
@@ -1257,8 +1240,8 @@
 			if (!p->owner) {
 				if (!p->chan) {
 					ast_mutex_destroy(&p->lock);
-					ast_mutex_destroy(&p->app_lock);
 					ast_cond_destroy(&p->app_complete_cond);
+					ast_cond_destroy(&p->login_wait_cond);
 					ast_free(p);
 				} else {
 					/* Cause them to hang up */
@@ -1459,6 +1442,45 @@
 	}
 	*cause = hasagent ? AST_CAUSE_BUSY : AST_CAUSE_UNREGISTERED;
 	AST_LIST_UNLOCK(&agents);
+
+	if (chan) {
+		ast_mutex_lock(&p->lock);
+		if (p->pending) {
+			ast_mutex_unlock(&p->lock);
+			return chan;
+		}
+
+		if (!p->chan) {
+			ast_log(LOG_DEBUG, "Agent disconnected while we were connecting the call\n");
+			*cause = AST_CAUSE_UNREGISTERED;
+			ast_mutex_unlock(&p->lock);
+			agent_hangup(chan);
+			return NULL;
+		}
+
+		/* we need to take control of the channel from the login app
+		 * thread */
+		p->app_sleep_cond = 0;
+		p->app_lock_flag = 1;
+
+		ast_queue_frame(p->chan, &ast_null_frame);
+		ast_cond_wait(&p->login_wait_cond, &p->lock);
+
+		if (!p->chan) {
+			ast_log(LOG_DEBUG, "Agent disconnected while we were connecting the call\n");
+			p->app_sleep_cond = 1;
+			p->app_lock_flag = 0;
+			ast_cond_signal(&p->app_complete_cond);
+			ast_mutex_unlock(&p->lock);
+			*cause = AST_CAUSE_UNREGISTERED;
+			agent_hangup(chan);
+			return NULL;
+		}
+
+		ast_indicate(p->chan, AST_CONTROL_UNHOLD);
+		ast_mutex_unlock(&p->lock);
+	}
+
 	return chan;
 }
 
@@ -2070,13 +2092,13 @@
 							}
 							ast_mutex_unlock(&p->lock);
 							AST_LIST_UNLOCK(&agents);
+
 							/*	Synchronize channel ownership between call to agent and itself. */
-							ast_mutex_lock(&p->app_lock);
+							ast_mutex_lock(&p->lock);
 							if (p->app_lock_flag == 1) {
-								ast_cond_wait(&p->app_complete_cond, &p->app_lock);
+								ast_cond_signal(&p->login_wait_cond);
+								ast_cond_wait(&p->app_complete_cond, &p->lock);
 							}
-							ast_mutex_unlock(&p->app_lock);
-							ast_mutex_lock(&p->lock);
 							ast_mutex_unlock(&p->lock);
 							if (p->ackcall) {
 								res = agent_ack_sleep(p);
@@ -2094,12 +2116,20 @@
 							sched_yield();
 						}
 						ast_mutex_lock(&p->lock);
-						if (res && p->owner) 
-							ast_log(LOG_WARNING, "Huh?  We broke out when there was still an owner?\n");
 						/* Log us off if appropriate */
 						if (p->chan == chan) {
 							p->chan = NULL;
 						}
+
+						/* Synchronize channel ownership between call to agent and itself. */
+						if (p->app_lock_flag == 1) {
+							ast_cond_signal(&p->login_wait_cond);
+							ast_cond_wait(&p->app_complete_cond, &p->lock);
+						}
+
+						if (res && p->owner)
+							ast_log(LOG_WARNING, "Huh?  We broke out when there was still an owner?\n");
+
 						p->acknowledged = 0;
 						logintime = time(NULL) - p->loginstart;
 						p->loginstart = 0;
@@ -2115,8 +2145,8 @@
 						ast_devstate_changed(AST_DEVICE_UNAVAILABLE, "Agent/%s", p->agent);
 						if (p->dead && !p->owner) {
 							ast_mutex_destroy(&p->lock);
-							ast_mutex_destroy(&p->app_lock);
 							ast_cond_destroy(&p->app_complete_cond);
+							ast_cond_destroy(&p->login_wait_cond);
 							ast_free(p);
 						}
 					}




More information about the asterisk-commits mailing list