From 101fd38e09cc31eebd3597571e6b0c3ab60b4ff6 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthony.minessale@gmail.com>
Date: Fri, 30 Mar 2007 17:25:48 +0000
Subject: [PATCH] fix race

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@4810 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 src/switch_core.c       |  1 +
 src/switch_core_sqldb.c | 10 +++++++---
 src/switch_event.c      |  5 +++--
 3 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/switch_core.c b/src/switch_core.c
index ceb502bfb5..62e4de2dd7 100644
--- a/src/switch_core.c
+++ b/src/switch_core.c
@@ -482,6 +482,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(char *console, const char **err
 	assert(runtime.memory_pool != NULL);
 	switch_log_init(runtime.memory_pool);
 	switch_event_init(runtime.memory_pool);
+
 	switch_core_sqldb_start(runtime.memory_pool);
 	switch_rtp_init(runtime.memory_pool);
 	runtime.running = 1;
diff --git a/src/switch_core_sqldb.c b/src/switch_core_sqldb.c
index 1313048a2a..6ceb193eac 100644
--- a/src/switch_core_sqldb.c
+++ b/src/switch_core_sqldb.c
@@ -39,6 +39,7 @@ static struct {
 	switch_core_db_t *event_db;
 	switch_queue_t *sql_queue;
 	switch_memory_pool_t *memory_pool;
+	int thread_running;
 } runtime;
 
 static switch_status_t switch_core_db_persistant_execute_trans(switch_core_db_t *db, char *sql, uint32_t retries)
@@ -159,9 +160,8 @@ static void *SWITCH_THREAD_FUNC switch_core_sql_thread(switch_thread_t * thread,
 	if (!runtime.event_db) {
 		runtime.event_db = switch_core_db_handle();
 	}
-	switch_queue_create(&runtime.sql_queue, SWITCH_SQL_QUEUE_LEN, runtime.memory_pool);
-
 
+	runtime.thread_running = 1;
 
 	for (;;) {
 		if (switch_queue_trypop(runtime.sql_queue, &pop) == SWITCH_STATUS_SUCCESS) {
@@ -398,11 +398,15 @@ void switch_core_sqldb_start(switch_memory_pool_t *pool)
 		}
 	}
 
+	switch_queue_create(&runtime.sql_queue, SWITCH_SQL_QUEUE_LEN, runtime.memory_pool);
+	
 	switch_threadattr_create(&thd_attr, runtime.memory_pool);
 	switch_threadattr_detach_set(thd_attr, 1);
 	switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
 	switch_thread_create(&thread, thd_attr, switch_core_sql_thread, NULL, runtime.memory_pool);
-
+	while (!runtime.thread_running) {
+		switch_yield(10000);
+	}
 }
 
 void switch_core_sqldb_stop(void)
diff --git a/src/switch_event.c b/src/switch_event.c
index eef532181f..a3a02770e8 100644
--- a/src/switch_event.c
+++ b/src/switch_event.c
@@ -398,13 +398,14 @@ SWITCH_DECLARE(switch_status_t) switch_event_init(switch_memory_pool_t *pool)
 	switch_threadattr_detach_set(thd_attr, 1);
 
 	assert(pool != NULL);
-	RUNTIME_POOL = pool;
-
+	THRUNTIME_POOL = RUNTIME_POOL = pool;
 
+	/*
 	if (switch_core_new_memory_pool(&THRUNTIME_POOL) != SWITCH_STATUS_SUCCESS) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Could not allocate memory pool\n");
 		return SWITCH_STATUS_MEMERR;
 	}
+	*/
 	/*
 	   if (switch_core_new_memory_pool(&BPOOL) != SWITCH_STATUS_SUCCESS) {
 	   switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Could not allocate memory pool\n");