[asterisk-commits] branch file/iax2-multithreading r12201 - /team/file/iax2-multithreading/chann...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon Mar 6 17:25:11 MST 2006


Author: file
Date: Mon Mar  6 18:25:09 2006
New Revision: 12201

URL: http://svn.digium.com/view/asterisk?rev=12201&view=rev
Log:
Add my modifications, and get rid of the poll that plagued the emulation implementation we have.

Modified:
    team/file/iax2-multithreading/channels/chan_iax2.c

Modified: team/file/iax2-multithreading/channels/chan_iax2.c
URL: http://svn.digium.com/view/asterisk/team/file/iax2-multithreading/channels/chan_iax2.c?rev=12201&r1=12200&r2=12201&view=diff
==============================================================================
--- team/file/iax2-multithreading/channels/chan_iax2.c (original)
+++ team/file/iax2-multithreading/channels/chan_iax2.c Mon Mar  6 18:25:09 2006
@@ -240,6 +240,8 @@
 
 static pthread_t netthreadid = AST_PTHREADT_NULL;
 static pthread_t schedthreadid = AST_PTHREADT_NULL;
+AST_MUTEX_DEFINE_STATIC(sched_lock);
+static int sched_halt = 0;static ast_cond_t sched_cond;
 
 enum {
 	IAX_STATE_STARTED = 		(1 << 0),
@@ -696,6 +698,8 @@
 	int iores;
 	int iofd;
 	time_t checktime;
+	ast_mutex_t lock;
+	ast_cond_t cond;
 };
 
 struct iax2_thread_list {
@@ -836,7 +840,7 @@
 #ifdef DEBUG_SCHED_MULTITHREAD
 		ast_copy_string(thread->curfunc, funcname, sizeof(thread->curfunc));
 #endif
-		pthread_kill(thread->threadid, SIGURG);
+		ast_cond_signal(&thread->cond);
 		return 0;
 	}
 	time(&t);
@@ -2283,7 +2287,9 @@
     }
 
     pvt->jbid = ast_sched_add(sched, when, get_from_jb, (void *)pvt);
-	pthread_kill(schedthreadid, SIGURG);
+
+    /* Signal scheduler thread */
+    ast_cond_signal(&sched_cond);
 }
 
 static void __get_from_jb(void *p) 
@@ -2617,7 +2623,7 @@
 		if (option_debug && iaxdebug)
 			ast_log(LOG_DEBUG, "schedule_delivery: Scheduling delivery in %d ms\n", delay);
 		fr->retrans = ast_sched_add(sched, delay, do_deliver, fr);
-		pthread_kill(schedthreadid, SIGURG);
+		ast_cond_signal(&sched_cond);
 	}
 #endif
 	if (tsout)
@@ -2653,7 +2659,7 @@
 	ast_mutex_unlock(&iaxq.lock);
 	/* Wake up the network and scheduler thread */
 	pthread_kill(netthreadid, SIGURG);
-	pthread_kill(schedthreadid, SIGURG);
+	ast_cond_signal(&sched_cond);
 	return 0;
 }
 
@@ -6516,7 +6522,7 @@
 #ifdef DEBUG_SCHED_MULTITHREAD
 		ast_copy_string(thread->curfunc, "socket_process", sizeof(thread->curfunc));
 #endif
-		pthread_kill(thread->threadid, SIGURG);
+		ast_cond_signal(&thread->cond);
 	} else {
 		time(&t);
 		if (t != last_errtime)
@@ -7892,12 +7898,12 @@
 static void *iax2_process_thread(void *data)
 {
 	struct iax2_thread *thread_copy, *thread = data;
-	struct timeval tv;
+
 	for(;;) {
-		/* Sleep for up to 1 second */
-		tv.tv_sec = 1;
-		tv.tv_usec = 0;
-		select(0, NULL, NULL, NULL, &tv);
+		/* Wait for something to signal us to be awake */
+		ast_mutex_lock(&thread->lock);
+		ast_cond_wait(&thread->cond, &thread->lock);
+		ast_mutex_unlock(&thread->lock);
 		/* Unlink from idlelist / activelist if there*/
 		ASTOBJ_CONTAINER_UNLINK(&idlelist, thread);
 		ASTOBJ_CONTAINER_UNLINK(&activelist, thread);
@@ -8250,19 +8256,24 @@
 {
 	int count;
 	int res;
+	struct timespec ts;
+
 	for (;;) {
 		res = ast_sched_wait(sched);
 		if ((res > 1000) || (res < 0))
 			res = 1000;
-		res = poll(NULL, 0, res);
-		if (res < 0) {
-			if ((errno != EAGAIN) && (errno != EINTR))
-				ast_log(LOG_WARNING, "poll failed: %s\n", strerror(errno));
-		}
+		ts.tv_sec = res;
+		ast_mutex_lock(&sched_lock);
+		ast_cond_timedwait(&sched_cond, &sched_lock, &ts);
+		if (sched_halt == 1) {
+			break;
+		}
+		ast_mutex_unlock(&sched_lock);
 		count = ast_sched_runq(sched);
 		if (count >= 20)
 			ast_log(LOG_DEBUG, "chan_iax2: ast_sched_runq ran %d scheduled tasks all at once\n", count);
 	}
+	ast_mutex_unlock(&sched_lock);
 	return NULL;
 }
 
@@ -8306,7 +8317,7 @@
 					/* We need reliable delivery.  Schedule a retransmission */
 					f->retries++;
 					f->retrans = ast_sched_add(sched, f->retrytime, attempt_transmit, f);
-					pthread_kill(schedthreadid, SIGURG);
+					ast_cond_signal(&sched_cond);
 				}
 			}
 			f = f->next;
@@ -8338,6 +8349,8 @@
 		if (thread) {
 			ASTOBJ_INIT(thread);
 			thread->threadnum = ++threadcount;
+			ast_mutex_init(&thread->lock);
+			ast_cond_init(&thread->cond, NULL);
 			if (ast_pthread_create(&thread->threadid, NULL, iax2_process_thread, thread)) {
 				ast_log(LOG_WARNING, "Failed to create new thread!\n");
 				free(thread);
@@ -9868,16 +9881,20 @@
 	}
 	if (schedthreadid != AST_PTHREADT_NULL) {
 		pthread_cancel(schedthreadid);
+		ast_mutex_lock(&sched_lock);
+		sched_halt = 1;
+		ast_mutex_unlock(&sched_lock);
+		ast_cond_signal(&sched_cond);
 		pthread_join(schedthreadid, NULL);
 	}
 	while (idlelist.head || activelist.head) {
 		ASTOBJ_CONTAINER_TRAVERSE(&idlelist, 1, {
 			iterator->halt = 1;
-			pthread_kill(iterator->threadid, SIGURG);
+			ast_cond_signal(&iterator->cond);
 		});
 		ASTOBJ_CONTAINER_TRAVERSE(&activelist, 1, {
 			iterator->halt = 1;
-			pthread_kill(iterator->threadid, SIGURG);
+                        ast_cond_signal(&iterator->cond);
 		});
 		usleep(100000);
 	}



More information about the asterisk-commits mailing list