[asterisk-commits] russell: branch russell/sched_thread r140292 - in /team/russell/sched_thread:...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Aug 27 07:42:50 CDT 2008


Author: russell
Date: Wed Aug 27 07:42:49 2008
New Revision: 140292

URL: http://svn.digium.com/view/asterisk?view=rev&rev=140292
Log:
Merge a bit of code I wrote this morning.  It adds a common implementation of
a scheduler thread.

Modified:
    team/russell/sched_thread/include/asterisk/sched.h
    team/russell/sched_thread/main/sched.c

Modified: team/russell/sched_thread/include/asterisk/sched.h
URL: http://svn.digium.com/view/asterisk/team/russell/sched_thread/include/asterisk/sched.h?view=diff&rev=140292&r1=140291&r2=140292
==============================================================================
--- team/russell/sched_thread/include/asterisk/sched.h (original)
+++ team/russell/sched_thread/include/asterisk/sched.h Wed Aug 27 07:42:49 2008
@@ -267,6 +267,20 @@
 	} \
 } while(0)
 
+/*!
+ * \brief An opaque type representing a scheduler thread
+ */
+struct sched_thread;
+
+struct sched_thread *sched_thread_create(void);
+
+struct sched_thread *sched_thread_destroy(struct sched_thread *st);
+
+int sched_thread_add(struct sched_thread *st, int when, ast_sched_cb cb,
+		const void *data);
+
+int sched_thread_del(struct sched_thread *st, int id);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

Modified: team/russell/sched_thread/main/sched.c
URL: http://svn.digium.com/view/asterisk/team/russell/sched_thread/main/sched.c?view=diff&rev=140292&r1=140291&r2=140292
==============================================================================
--- team/russell/sched_thread/main/sched.c (original)
+++ team/russell/sched_thread/main/sched.c Wed Aug 27 07:42:49 2008
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2005, Digium, Inc.
+ * Copyright (C) 1999 - 2008, Digium, Inc.
  *
  * Mark Spencer <markster at digium.com>
  *
@@ -70,6 +70,128 @@
 #endif
 };
 
+struct sched_thread {
+	pthread_t thread;
+	ast_mutex_t lock;
+	ast_cond_t cond;
+	struct sched_context *context;
+	unsigned int stop:1;
+};
+
+static void *sched_run(void *data)
+{
+	struct sched_thread *st = data;
+
+	while (!st->stop) {
+		int ms;
+		struct timespec ts = {
+			.tv_sec = 0,	
+		};
+
+		ast_mutex_lock(&st->lock);
+
+		if (st->stop) {
+			ast_mutex_unlock(&st->lock);
+			return NULL;
+		}
+
+		ms = ast_sched_wait(st->context);
+
+		if (ms == -1) {
+			ast_cond_wait(&st->cond, &st->lock);
+		} else {	
+			struct timeval tv;
+			tv = ast_tvadd(ast_tvnow(), ast_samp2tv(ms, 1000));
+			ts.tv_sec = tv.tv_sec;
+			ts.tv_nsec = tv.tv_usec * 1000;
+			ast_cond_timedwait(&st->cond, &st->lock, &ts);
+		}
+
+		ast_mutex_unlock(&st->lock);
+
+		if (st->stop) {
+			return NULL;
+		}
+
+		ast_sched_runq(st->context);
+	}
+
+	return NULL;
+}
+
+struct sched_thread *sched_thread_destroy(struct sched_thread *st)
+{
+	if (st->thread != AST_PTHREADT_NULL) {
+		ast_mutex_lock(&st->lock);
+		st->stop = 1;
+		ast_cond_signal(&st->cond);
+		ast_mutex_unlock(&st->lock);
+		pthread_join(st->thread, NULL);
+		st->thread = AST_PTHREADT_NULL;
+	}
+
+	ast_mutex_destroy(&st->lock);
+	ast_cond_destroy(&st->cond);
+
+	if (st->context) {
+		sched_context_destroy(st->context);
+		st->context = NULL;
+	}
+
+	ast_free(st);
+
+	return NULL;
+}
+
+struct sched_thread *sched_thread_create(void)
+{
+	struct sched_thread *st;
+
+	if (!(st = ast_calloc(1, sizeof(*st)))) {
+		return NULL;
+	}
+
+	ast_mutex_init(&st->lock);
+	ast_cond_init(&st->cond, NULL);
+
+	st->thread = AST_PTHREADT_NULL;
+
+	if (!(st->context = sched_context_create())) {
+		ast_log(LOG_ERROR, "Failed to create scheduler\n");
+		sched_thread_destroy(st);
+		return NULL;
+	}
+	
+	if (ast_pthread_create_background(&st->thread, NULL, sched_run, st)) {
+		ast_log(LOG_ERROR, "Failed to create scheduler thread\n");
+		sched_thread_destroy(st);
+		return NULL;
+	}
+
+	return st;
+}
+
+int sched_thread_add(struct sched_thread *st, int when, ast_sched_cb cb,
+		const void *data)
+{
+	int res;
+
+	res = ast_sched_add(st->context, when, cb, data);
+
+	if (res != -1) {
+		ast_mutex_lock(&st->lock);
+		ast_cond_signal(&st->cond);
+		ast_mutex_unlock(&st->lock);
+	}
+
+	return res;
+}
+
+int sched_thread_del(struct sched_thread *st, int id)
+{
+	AST_SCHED_DEL(st->context, id);
+	return 0;
+}
 
 /* hash routines for sched */
 




More information about the asterisk-commits mailing list