[asterisk-commits] russell: branch group/timing r122374 - in /team/group/timing: channels/ inclu...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jun 12 16:13:14 CDT 2008


Author: russell
Date: Thu Jun 12 16:13:14 2008
New Revision: 122374

URL: http://svn.digium.com/view/asterisk?view=rev&rev=122374
Log:
 * use a rwlock instead of a mutex around accessing the installed timer functions
 * add a set_rate() API call instead of giving the rate in the open() API call.
   There are times when we need to change the rate and turn it on and off

Modified:
    team/group/timing/channels/chan_iax2.c
    team/group/timing/include/asterisk/timing.h
    team/group/timing/main/timing.c
    team/group/timing/res/res_timing_dahdi.c

Modified: team/group/timing/channels/chan_iax2.c
URL: http://svn.digium.com/view/asterisk/team/group/timing/channels/chan_iax2.c?view=diff&rev=122374&r1=122373&r2=122374
==============================================================================
--- team/group/timing/channels/chan_iax2.c (original)
+++ team/group/timing/channels/chan_iax2.c Thu Jun 12 16:13:14 2008
@@ -12142,7 +12142,10 @@
 	if(set_config(config, 0) == -1)
 		return AST_MODULE_LOAD_DECLINE;
 
-	timingfd = ast_timer_open(trunkfreq);
+	timingfd = ast_timer_open();
+	if (timingfd > -1) {
+		ast_timer_set_rate(timingfd, trunkfreq);
+	}
 
  	if (ast_channel_register(&iax2_tech)) {
 		ast_log(LOG_ERROR, "Unable to register channel class %s\n", "IAX2");

Modified: team/group/timing/include/asterisk/timing.h
URL: http://svn.digium.com/view/asterisk/team/group/timing/include/asterisk/timing.h?view=diff&rev=122374&r1=122373&r2=122374
==============================================================================
--- team/group/timing/include/asterisk/timing.h (original)
+++ team/group/timing/include/asterisk/timing.h Thu Jun 12 16:13:14 2008
@@ -65,8 +65,9 @@
  * public API calls.
  */
 struct ast_timing_functions {
-	int (*timer_open)(unsigned int rate);
+	int (*timer_open)(void);
 	void (*timer_close)(int handle);
+	int (*timer_set_rate)(int handle, unsigned int rate);
 	void (*timer_ack)(int handle, unsigned int quantity);
 	int (*timer_enable_continuous)(int handle);
 	int (*timer_disable_continuous)(int handle);
@@ -97,12 +98,10 @@
 /*!
  * \brief Open a timing fd
  *
- * \arg rate number of timer ticks per second
- *
  * \retval -1 error, with errno set
  * \retval >=0 success
  */
-int ast_timer_open(unsigned int rate);
+int ast_timer_open(void);
 
 /*!
  * \brief Close an opened timing handle
@@ -112,6 +111,21 @@
  * \return nothing
  */
 void ast_timer_close(int handle);
+
+/*!
+ * \brief Set the timing tick rate
+ *
+ * \arg handle timing fd returned from timer_open()
+ * \arg rate ticks per second, 0 turns the ticks off if needed
+ *
+ * Use this function if you want the timing fd to show input at a certain
+ * rate.  The other alternative use of a timing fd, is using the continuous
+ * mode.
+ *
+ * \retval -1 error, with errno set
+ * \retval 0 success
+ */
+int ast_timer_set_rate(int handle, unsigned int rate);
 
 /*!
  * \brief Acknowledge a timer event

Modified: team/group/timing/main/timing.c
URL: http://svn.digium.com/view/asterisk/team/group/timing/main/timing.c?view=diff&rev=122374&r1=122373&r2=122374
==============================================================================
--- team/group/timing/main/timing.c (original)
+++ team/group/timing/main/timing.c Thu Jun 12 16:13:14 2008
@@ -30,7 +30,7 @@
 #include "asterisk/timing.h"
 #include "asterisk/lock.h"
 
-AST_MUTEX_DEFINE_STATIC(lock);
+AST_RWLOCK_DEFINE_STATIC(lock);
 
 static struct ast_timing_functions timer_funcs;
 
@@ -38,6 +38,7 @@
 {
 	if (!funcs->timer_open ||
 	    !funcs->timer_close ||
+		!funcs->timer_set_rate ||
 	    !funcs->timer_ack ||
 	    !funcs->timer_get_event ||
 	    !funcs->timer_enable_continuous ||
@@ -45,94 +46,113 @@
 		return NULL;
 	}
 
-	ast_mutex_lock(&lock);
+	ast_rwlock_wrlock(&lock);
 
 	if (timer_funcs.timer_open) {
-		ast_mutex_unlock(&lock);
+		ast_rwlock_unlock(&lock);
+		ast_log(LOG_NOTICE, "Multiple timing modules are loaded.  You should only load one.\n");
 		return NULL;
 	}
 	
 	timer_funcs = *funcs;
 
-	ast_mutex_unlock(&lock);
+	ast_rwlock_unlock(&lock);
 
 	return &timer_funcs;
 }
 
 void ast_uninstall_timing_functions(void *handle)
 {
-	ast_mutex_lock(&lock);
+	ast_rwlock_wrlock(&lock);
 
 	if (handle != &timer_funcs) {
-		ast_mutex_unlock(&lock);
+		ast_rwlock_unlock(&lock);
 		return;
 	}
 
 	memset(&timer_funcs, 0, sizeof(timer_funcs));
 
-	ast_mutex_unlock(&lock);
+	ast_rwlock_unlock(&lock);
 }
 
-int ast_timer_open(unsigned int rate)
+int ast_timer_open(void)
 {
 	int timer;
 
-	ast_mutex_lock(&lock);
+	ast_rwlock_rdlock(&lock);
 
 	if (!timer_funcs.timer_open) {
-		ast_mutex_unlock(&lock);
+		ast_rwlock_unlock(&lock);
 		return -1;
 	}
 
-	timer = timer_funcs.timer_open(rate);
+	timer = timer_funcs.timer_open();
 
-	ast_mutex_unlock(&lock);
+	ast_rwlock_unlock(&lock);
 
 	return timer;
 }
 
 void ast_timer_close(int timer)
 {
-	ast_mutex_lock(&lock);
+	ast_rwlock_rdlock(&lock);
 
 	if (!timer_funcs.timer_close) {
-		ast_mutex_unlock(&lock);
+		ast_rwlock_unlock(&lock);
 		return;
 	}
 
 	timer_funcs.timer_close(timer);
 
-	ast_mutex_unlock(&lock);
+	ast_rwlock_unlock(&lock);
+}
+
+int ast_timer_set_rate(int handle, unsigned int rate)
+{
+	int res;
+
+	ast_rwlock_rdlock(&lock);
+
+	if (!timer_funcs.timer_set_rate) {
+		ast_rwlock_unlock(&lock);
+		return -1;
+	}
+
+	res = timer_funcs.timer_set_rate(handle, rate);
+
+	ast_rwlock_unlock(&lock);
+
+	return res;
 }
 
 void ast_timer_ack(int handle, unsigned int quantity)
 {
-	ast_mutex_lock(&lock);
+	ast_rwlock_rdlock(&lock);
 
 	if (!timer_funcs.timer_ack) {
-		ast_mutex_unlock(&lock);
+		ast_rwlock_unlock(&lock);
 		return;
 	}
 
 	timer_funcs.timer_ack(handle, quantity);
 
-	ast_mutex_unlock(&lock);
+	ast_rwlock_unlock(&lock);
 }
 
 int ast_timer_enable_continuous(int handle)
 {
 	int result;
 
-	ast_mutex_lock(&lock);
+	ast_rwlock_rdlock(&lock);
 
 	if (!timer_funcs.timer_enable_continuous) {
-		ast_mutex_unlock(&lock);
+		ast_rwlock_unlock(&lock);
 		return -1;
 	}
 
 	result = timer_funcs.timer_enable_continuous(handle);
 
-	ast_mutex_unlock(&lock);
+	ast_rwlock_unlock(&lock);
 
 	return result;
 }
@@ -141,16 +161,16 @@
 {
 	int result;
 
-	ast_mutex_lock(&lock);
+	ast_rwlock_rdlock(&lock);
 
 	if (!timer_funcs.timer_disable_continuous) {
-		ast_mutex_unlock(&lock);
+		ast_rwlock_unlock(&lock);
 		return -1;
 	}
 
 	result = timer_funcs.timer_disable_continuous(handle);
 
-	ast_mutex_unlock(&lock);
+	ast_rwlock_unlock(&lock);
 
 	return result;
 }
@@ -159,16 +179,16 @@
 {
 	enum ast_timing_event result;
 
-	ast_mutex_lock(&lock);
+	ast_rwlock_rdlock(&lock);
 
 	if (!timer_funcs.timer_get_event) {
-		ast_mutex_unlock(&lock);
+		ast_rwlock_unlock(&lock);
 		return -1;
 	}
 
 	result = timer_funcs.timer_get_event(handle);
 
-	ast_mutex_unlock(&lock);
+	ast_rwlock_unlock(&lock);
 
 	return result;
 }

Modified: team/group/timing/res/res_timing_dahdi.c
URL: http://svn.digium.com/view/asterisk/team/group/timing/res/res_timing_dahdi.c?view=diff&rev=122374&r1=122373&r2=122374
==============================================================================
--- team/group/timing/res/res_timing_dahdi.c (original)
+++ team/group/timing/res/res_timing_dahdi.c Thu Jun 12 16:13:14 2008
@@ -43,8 +43,9 @@
 
 static void *timing_funcs_handle;
 
-static int dahdi_timer_open(unsigned int rate);
+static int dahdi_timer_open(void);
 static void dahdi_timer_close(int handle);
+static int dahdi_timer_set_rate(int handle, unsigned int rate);
 static void dahdi_timer_ack(int handle, unsigned int quantity);
 static int dahdi_timer_enable_continuous(int handle);
 static int dahdi_timer_disable_continuous(int handle);
@@ -53,40 +54,38 @@
 static struct ast_timing_functions dahdi_timing_functions = {
 	.timer_open = dahdi_timer_open,
 	.timer_close = dahdi_timer_close,
+	.timer_set_rate = dahdi_timer_set_rate,
 	.timer_ack = dahdi_timer_ack,
 	.timer_enable_continuous = dahdi_timer_enable_continuous,
 	.timer_disable_continuous = dahdi_timer_disable_continuous,
 	.timer_get_event = dahdi_timer_get_event,
 };
 
-static int dahdi_timer_open(unsigned int rate)
+static int dahdi_timer_open(void)
 {
-	int fd;
+	return open("/dev/dahdi/timer", O_RDWR);
+}
+
+static void dahdi_timer_close(int handle)
+{
+	close(handle);
+}
+
+static int dahdi_timer_set_rate(int handle, unsigned int rate)
+{
 	int samples;
-
-	fd = open("/dev/dahdi/timer", O_RDWR);
-
-	if (fd < 0) {
-		return -1;
-	}
 
 	/* DAHDI timers are configured using a number of samples,
 	 * based on an 8 kHz sample rate. */
 	samples = (unsigned int) roundf((8000.0 / ((float) rate)));
 
-	if (ioctl(fd, DAHDI_TIMERCONFIG, &samples)) {
+	if (ioctl(handle, DAHDI_TIMERCONFIG, &samples)) {
 		ast_log(LOG_ERROR, "Failed to configure DAHDI timing fd for %u sample timer ticks\n",
 			samples);
-		close(fd);
 		return -1;
 	}
 
-	return fd;
-}
-
-static void dahdi_timer_close(int handle)
-{
-	close(handle);
+	return 0;
 }
 
 static void dahdi_timer_ack(int handle, unsigned int quantity)




More information about the asterisk-commits mailing list