[asterisk-commits] mmichelson: branch mmichelson/timeout_fixes r373836 - in /team/mmichelson/tim...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Sep 26 13:36:45 CDT 2012


Author: mmichelson
Date: Wed Sep 26 13:36:41 2012
New Revision: 373836

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=373836
Log:
Fix timeout logic used in apps directory.

This also includes some changes in the core

* New function ast_remaining_ms() can be used to determine
the amount of time remaining, given when timing started and
what the time limit is.

* ast_waitfor() has been changed to be able to return
a negative return result if a negative timeout is passed
to it.


Modified:
    team/mmichelson/timeout_fixes/apps/app_dial.c
    team/mmichelson/timeout_fixes/apps/app_jack.c
    team/mmichelson/timeout_fixes/apps/app_meetme.c
    team/mmichelson/timeout_fixes/apps/app_queue.c
    team/mmichelson/timeout_fixes/apps/app_record.c
    team/mmichelson/timeout_fixes/apps/app_waitforring.c
    team/mmichelson/timeout_fixes/include/asterisk/time.h
    team/mmichelson/timeout_fixes/main/channel.c
    team/mmichelson/timeout_fixes/main/utils.c

Modified: team/mmichelson/timeout_fixes/apps/app_dial.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/timeout_fixes/apps/app_dial.c?view=diff&rev=373836&r1=373835&r2=373836
==============================================================================
--- team/mmichelson/timeout_fixes/apps/app_dial.c (original)
+++ team/mmichelson/timeout_fixes/apps/app_dial.c Wed Sep 26 13:36:41 2012
@@ -1055,6 +1055,7 @@
 	int is_cc_recall;
 	int cc_frame_received = 0;
 	int num_ringing = 0;
+	struct ast_timeval start = ast_tvnow();
 
 	ast_party_connected_line_init(&connected_caller);
 	if (single) {
@@ -1127,7 +1128,9 @@
 			}
 			return NULL;
 		}
+		*to = ast_remaining_ms(start, orig);
 		winner = ast_waitfor_n(watchers, pos, to);
+		*to = ast_remaining_ms(start, orig);
 		for (o = outgoing; o; o = o->next) {
 			struct ast_frame *f;
 			struct ast_channel *c = o->chan;

Modified: team/mmichelson/timeout_fixes/apps/app_jack.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/timeout_fixes/apps/app_jack.c?view=diff&rev=373836&r1=373835&r2=373836
==============================================================================
--- team/mmichelson/timeout_fixes/apps/app_jack.c (original)
+++ team/mmichelson/timeout_fixes/apps/app_jack.c Wed Sep 26 13:36:41 2012
@@ -768,7 +768,9 @@
 	while (!jack_data->stop) {
 		struct ast_frame *f;
 
-		ast_waitfor(chan, -1);
+		if (ast_waitfor(chan, -1) < 0) {
+			break;
+		}
 
 		f = ast_read(chan);
 		if (!f) {

Modified: team/mmichelson/timeout_fixes/apps/app_meetme.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/timeout_fixes/apps/app_meetme.c?view=diff&rev=373836&r1=373835&r2=373836
==============================================================================
--- team/mmichelson/timeout_fixes/apps/app_meetme.c (original)
+++ team/mmichelson/timeout_fixes/apps/app_meetme.c Wed Sep 26 13:36:41 2012
@@ -1869,7 +1869,7 @@
 		/* when no frames are available, this will wait
 		   for 1 millisecond maximum
 		*/
-		while (ast_waitfor(chan, 1)) {
+		while (ast_waitfor(chan, 1) > 0) {
 			f = ast_read(chan);
 			if (f)
 				ast_frfree(f);

Modified: team/mmichelson/timeout_fixes/apps/app_queue.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/timeout_fixes/apps/app_queue.c?view=diff&rev=373836&r1=373835&r2=373836
==============================================================================
--- team/mmichelson/timeout_fixes/apps/app_queue.c (original)
+++ team/mmichelson/timeout_fixes/apps/app_queue.c Wed Sep 26 13:36:41 2012
@@ -3473,6 +3473,7 @@
 #endif
 	struct ast_party_connected_line connected_caller;
 	char *inchan_name;
+	struct timeval start = ast_tvnow();
 
 	ast_party_connected_line_init(&connected_caller);
 
@@ -3531,7 +3532,9 @@
 		}
 
 		/* Poll for events from both the incoming channel as well as any outgoing channels */
+		*to = ast_remaining_ms(start, orig);
 		winner = ast_waitfor_n(watchers, pos, to);
+		*to = ast_remaining_ms(start, orig);
 
 		/* Service all of the outgoing channels */
 		for (o = start; o; o = o->call_next) {

Modified: team/mmichelson/timeout_fixes/apps/app_record.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/timeout_fixes/apps/app_record.c?view=diff&rev=373836&r1=373835&r2=373836
==============================================================================
--- team/mmichelson/timeout_fixes/apps/app_record.c (original)
+++ team/mmichelson/timeout_fixes/apps/app_record.c Wed Sep 26 13:36:41 2012
@@ -169,6 +169,7 @@
 		AST_APP_ARG(maxduration);
 		AST_APP_ARG(options);
 	);
+	struct timeval start;
 	
 	/* The next few lines of code parse out the filename and header from the input string */
 	if (ast_strlen_zero(data)) { /* no data implies no filename or anything is present */
@@ -330,14 +331,20 @@
 	if (maxduration <= 0)
 		maxduration = -1;
 
-	while ((waitres = ast_waitfor(chan, maxduration)) > -1) {
-		if (maxduration > 0) {
-			if (waitres == 0) {
+	start = ast_tvnow();
+	while (ast_tvdiff_ms(ast_tvnow(), start) < maxduration || maxduration < 0) {
+		int ms = ast_remaining_ms(start, maxduration);
+
+		waitres = ast_waitfor(chan, ms);
+		if (waitres < 0) {
+			break;
+		}
+
+		if (ms > 0 && waitres == 0) {
 				gottimeout = 1;
 				pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "TIMEOUT");
 				break;
 			}
-			maxduration = waitres;
 		}
 
 		f = ast_read(chan);

Modified: team/mmichelson/timeout_fixes/apps/app_waitforring.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/timeout_fixes/apps/app_waitforring.c?view=diff&rev=373836&r1=373835&r2=373836
==============================================================================
--- team/mmichelson/timeout_fixes/apps/app_waitforring.c (original)
+++ team/mmichelson/timeout_fixes/apps/app_waitforring.c Wed Sep 26 13:36:41 2012
@@ -63,10 +63,16 @@
 	struct ast_silence_generator *silgen = NULL;
 	int res = 0;
 	double s;
-	int ms;
+	int timeout_ms;
+	struct timeval start = ast_tvnow();
 
 	if (!data || (sscanf(data, "%30lg", &s) != 1)) {
 		ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
+		return 0;
+	}
+
+	if (s < 0.0) {
+		ast_log(LOG_WARNING, "Invalid timeout provided for WaitForRing (%lg)\n", s);
 		return 0;
 	}
 
@@ -74,8 +80,10 @@
 		silgen = ast_channel_start_silence_generator(chan);
 	}
 
-	ms = s * 1000.0;
-	while (ms > 0) {
+	timeout_ms = s * 1000.0;
+	while (ast_tvdiff_ms(ast_tvnow(), start) < timeout_ms || timeout_ms < 0) {
+		int ms = ast_remaining_ms(start, timeout_ms);
+
 		ms = ast_waitfor(chan, ms);
 		if (ms < 0) {
 			res = ms;
@@ -95,14 +103,13 @@
 	}
 	/* Now we're really ready for the ring */
 	if (!res) {
-		ms = 99999999;
-		while(ms > 0) {
-			ms = ast_waitfor(chan, ms);
-			if (ms < 0) {
-				res = ms;
+		for (;;) {
+			int wait_res = ast_waitfor(chan, -1);
+			if (wait_res < 0) {
+				res = -1;
 				break;
 			}
-			if (ms > 0) {
+			if (wait_res > 0) {
 				f = ast_read(chan);
 				if (!f) {
 					res = -1;

Modified: team/mmichelson/timeout_fixes/include/asterisk/time.h
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/timeout_fixes/include/asterisk/time.h?view=diff&rev=373836&r1=373835&r2=373836
==============================================================================
--- team/mmichelson/timeout_fixes/include/asterisk/time.h (original)
+++ team/mmichelson/timeout_fixes/include/asterisk/time.h Wed Sep 26 13:36:41 2012
@@ -152,6 +152,20 @@
 struct timeval ast_tvsub(struct timeval a, struct timeval b);
 
 /*!
+ * \brief Calculate remaining milliseconds given a starting timestamp
+ * and upper bound
+ *
+ * If the upper bound is negative, then this indicates that there is no
+ * upper bound on the amount of time to wait. This will result in a
+ * negative return.
+ *
+ * \param start When timing started being calculated
+ * \param max_ms The maximum number of milliseconds to wait from start. May be negative.
+ * \return The number of milliseconds left to wait for. May be negative.
+ */
+int ast_remaining_ms(struct timeval start, int max_ms);
+
+/*!
  * \brief Returns a timeval from sec, usec
  */
 AST_INLINE_API(

Modified: team/mmichelson/timeout_fixes/main/channel.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/timeout_fixes/main/channel.c?view=diff&rev=373836&r1=373835&r2=373836
==============================================================================
--- team/mmichelson/timeout_fixes/main/channel.c (original)
+++ team/mmichelson/timeout_fixes/main/channel.c Wed Sep 26 13:36:41 2012
@@ -3507,11 +3507,7 @@
 
 int ast_waitfor(struct ast_channel *c, int ms)
 {
-	int oldms = ms;	/* -1 if no timeout */
-
 	ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
-	if ((ms < 0) && (oldms < 0))
-		ms = 0;
 	return ms;
 }
 
@@ -3579,17 +3575,8 @@
 	 */
 	while (ast_tvdiff_ms(ast_tvnow(), start) < timeout_ms || timeout_ms < 0) {
 		struct ast_channel *rchan;
-		int outfd=-1;
-		int ms;
-
-		if (timeout_ms < 0) {
-			ms = timeout_ms;
-		} else {
-			ms = timeout_ms - ast_tvdiff_ms(ast_tvnow(), start);
-			if (ms < 0) {
-				ms = 0;
-			}
-		}
+		int outfd = -1;
+		int ms = ast_remaining_ms(start, timeout_ms);
 
 		errno = 0;
 		/* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,

Modified: team/mmichelson/timeout_fixes/main/utils.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/timeout_fixes/main/utils.c?view=diff&rev=373836&r1=373835&r2=373836
==============================================================================
--- team/mmichelson/timeout_fixes/main/utils.c (original)
+++ team/mmichelson/timeout_fixes/main/utils.c Wed Sep 26 13:36:41 2012
@@ -1432,6 +1432,23 @@
 	}
 	return a;
 }
+
+int ast_remaining_ms(struct timeval start, int max_ms)
+{
+	int ms;
+
+	if (max_ms < 0) {
+		ms = max_ms;
+	} else {
+		ms = max_ms - ast_tvdiff_ms(ast_tvnow(), start);
+		if (ms < 0) {
+			ms = 0;
+		}
+	}
+
+	return ms;
+}
+
 #undef ONE_MILLION
 
 /*! \brief glibc puts a lock inside random(3), so that the results are thread-safe.




More information about the asterisk-commits mailing list