[svn-commits] tilghman: branch tilghman/sched-fu r137333 - /team/tilghman/sched-fu/main/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Aug 12 15:31:12 CDT 2008


Author: tilghman
Date: Tue Aug 12 15:31:11 2008
New Revision: 137333

URL: http://svn.digium.com/view/asterisk?view=rev&rev=137333
Log:
A few optimizations

Modified:
    team/tilghman/sched-fu/main/sched.c

Modified: team/tilghman/sched-fu/main/sched.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/sched-fu/main/sched.c?view=diff&rev=137333&r1=137332&r2=137333
==============================================================================
--- team/tilghman/sched-fu/main/sched.c (original)
+++ team/tilghman/sched-fu/main/sched.c Tue Aug 12 15:31:11 2008
@@ -59,7 +59,7 @@
 
 struct sched_context {
 	int last_executed;
-	ast_mutex_t lock;
+	ast_rwlock_t lock;
 	unsigned int eventcnt;                  /*!< Number of events processed */
 	unsigned int schedcnt;                  /*!< Number of outstanding schedule events */
 	unsigned int highwater;					/*!< highest count so far */
@@ -197,8 +197,10 @@
 				aoi = ao2_iterator_init(con->sched[i], 0);
 				while ((s = ao2_iterator_next(&aoi))) {
 					int tmp;
-					if ((tmp = ast_tvdiff_ms(s->when, now)) < ms) {
+					if ((tmp = ast_tvdiff_ms(s->when, now)) < ms && tmp < 60000) {
+						ao2_ref(s, -1);
 						ms = tmp;
+						break;
 					}
 					ao2_ref(s, -1);
 				}
@@ -210,8 +212,10 @@
 				aoi = ao2_iterator_init(con->sched[i], 0);
 				while ((s = ao2_iterator_next(&aoi))) {
 					int tmp;
-					if ((tmp = ast_tvdiff_ms(s->when, now)) < ms) {
+					if ((tmp = ast_tvdiff_ms(s->when, now)) < ms && tmp < 60000) {
+						ao2_ref(s, -1);
 						ms = tmp;
+						break;
 					}
 					ao2_ref(s, -1);
 				}
@@ -236,9 +240,16 @@
 {
 	int slot = CALC_SLOT(s->when);
 
+	ast_rwlock_rdlock(&con->lock);
 	if (!con->sched[slot]) {
-		con->sched[slot] = ao2_container_alloc(1, null_hash_fn, ao2_match_by_addr);
-	}
+		ast_rwlock_unlock(&con->lock);
+		ast_rwlock_wrlock(&con->lock);
+		if (!con->sched[slot]) {
+			con->sched[slot] = ao2_container_alloc(1, null_hash_fn, ao2_match_by_addr);
+		}
+	}
+	ast_rwlock_unlock(&con->lock);
+
 	ao2_link(con->sched[slot], s);
 	if (!ast_hashtab_insert_safe(con->schedq_ht, s)) {
 		ast_log(LOG_WARNING, "Schedule Queue entry %d is already in table!\n", s->id);
@@ -285,7 +296,7 @@
 		ast_log(LOG_NOTICE, "Scheduled event in 0 ms?\n");
 		return -1;
 	}
-	ast_mutex_lock(&con->lock);
+	ast_rwlock_rdlock(&con->lock);
 	if ((tmp = sched_alloc(con))) {
 		tmp->id = con->eventcnt++;
 		tmp->callback = callback;
@@ -295,17 +306,20 @@
 		tmp->when = ast_tv(0, 0);
 		if (sched_settime(&tmp->when, when)) {
 			sched_release(con, tmp);
+			ast_rwlock_unlock(&con->lock);
 		} else {
+			ast_rwlock_unlock(&con->lock);
 			schedule(con, tmp);
 			res = tmp->id;
 		}
+	} else {
+		ast_rwlock_unlock(&con->lock);
 	}
 #ifdef DUMP_SCHEDULER
 	/* Dump contents of the context while we have the lock so nothing gets screwed up by accident. */
 	if (option_debug)
 		ast_sched_dump(con);
 #endif
-	ast_mutex_unlock(&con->lock);
 	return res;
 }
 
@@ -346,7 +360,7 @@
 
 	DEBUG(ast_debug(1, "ast_sched_del(%d)\n", id));
 	
-	ast_mutex_lock(&con->lock);
+	ast_rwlock_rdlock(&con->lock);
 
 	/* OK, this is the heart of the sched performance upgrade.
 	   If we have 4700 peers, we can have 4700+ entries in the
@@ -365,7 +379,10 @@
 		if (!ast_hashtab_remove_this_object(con->schedq_ht, s)) {
 			ast_log(LOG_WARNING, "Found sched entry %d, then couldn't remove it?\n", s->id);
 		}
+		ast_rwlock_unlock(&con->lock);
 		sched_release(con, s);
+	} else {
+		ast_rwlock_unlock(&con->lock);
 	}
 	
 #ifdef DUMP_SCHEDULER
@@ -373,7 +390,6 @@
 	if (option_debug)
 		ast_sched_dump(con);
 #endif
-	ast_mutex_unlock(&con->lock);
 
 	if (!s) {
 		ast_debug(1, "Attempted to delete nonexistent schedule entry %d!\n", id);




More information about the svn-commits mailing list