[Freeswitch-svn] [commit] r12675 - freeswitch/trunk/src
FreeSWITCH SVN
anthm at freeswitch.org
Thu Mar 19 18:51:50 PDT 2009
Author: anthm
Date: Thu Mar 19 20:51:50 2009
New Revision: 12675
Log:
use apr_pool_mutex_set to make pools thread safe
Modified:
freeswitch/trunk/src/switch_core_memory.c
Modified: freeswitch/trunk/src/switch_core_memory.c
==============================================================================
--- freeswitch/trunk/src/switch_core_memory.c (original)
+++ freeswitch/trunk/src/switch_core_memory.c Thu Mar 19 20:51:50 2009
@@ -39,13 +39,17 @@
//#define DEBUG_ALLOC2
//#define DESTROY_POOLS
//#define INSTANTLY_DESTROY_POOLS
-/*#define LOCK_MORE*/
+//#define LOCK_MORE
+//#define USE_MEM_LOCK
+//#define SWITCH_POOL_RECYCLE
#ifndef SWITCH_POOL_RECYCLE
#define PER_POOL_LOCK 1
#endif
static struct {
+#ifdef USE_MEM_LOCK
switch_mutex_t *mem_lock;
+#endif
switch_queue_t *pool_queue; /* 8 ball break */
switch_queue_t *pool_recycle_queue;
switch_memory_pool_t *memory_pool;
@@ -69,8 +73,10 @@
switch_assert(session->pool != NULL);
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_lock(memory_manager.mem_lock);
#endif
+#endif
#ifdef DEBUG_ALLOC
if (memory > 500)
@@ -83,8 +89,10 @@
memset(ptr, 0, memory);
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_unlock(memory_manager.mem_lock);
#endif
+#endif
return ptr;
}
@@ -98,8 +106,10 @@
switch_assert(memory_manager.memory_pool != NULL);
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_lock(memory_manager.mem_lock);
#endif
+#endif
#ifdef DEBUG_ALLOC
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Perm Allocate %d\n", (int) memory);
@@ -111,8 +121,10 @@
memset(ptr, 0, memory);
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_unlock(memory_manager.mem_lock);
#endif
+#endif
return ptr;
}
@@ -127,8 +139,10 @@
return NULL;
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_lock(memory_manager.mem_lock);
#endif
+#endif
len = strlen(todup) + 1;
duped = apr_pstrmemdup(memory_manager.memory_pool, todup, len);
@@ -139,8 +153,10 @@
#endif
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_unlock(memory_manager.mem_lock);
#endif
+#endif
return duped;
}
@@ -151,8 +167,10 @@
char *result = NULL;
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_lock(memory_manager.mem_lock);
#endif
+#endif
switch_assert(session != NULL);
switch_assert(session->pool != NULL);
@@ -163,8 +181,10 @@
va_end(ap);
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_unlock(memory_manager.mem_lock);
#endif
+#endif
return result;
}
@@ -177,8 +197,10 @@
switch_assert(pool != NULL);
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_lock(memory_manager.mem_lock);
#endif
+#endif
va_start(ap, fmt);
@@ -187,8 +209,10 @@
va_end(ap);
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_unlock(memory_manager.mem_lock);
#endif
+#endif
return result;
}
@@ -196,31 +220,37 @@
SWITCH_DECLARE(char *) switch_core_perform_session_strdup(switch_core_session_t *session, const char *todup, const char *file, const char *func, int line)
{
char *duped = NULL;
- switch_size_t len;
switch_assert(session != NULL);
switch_assert(session->pool != NULL);
+#ifdef DEBUG_ALLOC
+ switch_size_t len;
+#endif
if (!todup) {
return NULL;
}
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_lock(memory_manager.mem_lock);
#endif
+#endif
- len = strlen(todup) + 1;
+
#ifdef DEBUG_ALLOC
+ len = strlen(todup);
if (len > 500)
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_CONSOLE, "Sess Strdup Allocate %d\n", (int) len);
#endif
- duped = apr_pstrmemdup(session->pool, todup, len);
+ duped = strdup(todup);
switch_assert(duped != NULL);
-
-
+
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_unlock(memory_manager.mem_lock);
#endif
+#endif
return duped;
}
@@ -235,8 +265,10 @@
return NULL;
}
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_lock(memory_manager.mem_lock);
#endif
+#endif
len = strlen(todup) + 1;
@@ -249,8 +281,10 @@
switch_assert(duped != NULL);
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_unlock(memory_manager.mem_lock);
#endif
+#endif
return duped;
}
@@ -275,7 +309,9 @@
void *pop = NULL;
#endif
+#ifdef USE_MEM_LOCK
switch_mutex_lock(memory_manager.mem_lock);
+#endif
switch_assert(pool != NULL);
#ifndef PER_POOL_LOCK
@@ -286,21 +322,22 @@
#ifdef PER_POOL_LOCK
if ((apr_allocator_create(&my_allocator)) != APR_SUCCESS) {
- return SWITCH_STATUS_MEMERR;
+ abort();
}
if ((apr_pool_create_ex(pool, NULL, NULL, my_allocator)) != APR_SUCCESS) {
- apr_allocator_destroy(my_allocator);
- my_allocator = NULL;
- return SWITCH_STATUS_MEMERR;
+ abort();
}
- if ((apr_thread_mutex_create(&my_mutex, APR_THREAD_MUTEX_DEFAULT, *pool)) != APR_SUCCESS) {
- return SWITCH_STATUS_MEMERR;
+ if ((apr_thread_mutex_create(&my_mutex, APR_THREAD_MUTEX_NESTED, *pool)) != APR_SUCCESS) {
+ abort();
}
apr_allocator_mutex_set(my_allocator, my_mutex);
apr_allocator_owner_set(my_allocator, *pool);
+
+ apr_pool_mutex_set(*pool, my_mutex);
+
#else
apr_pool_create(pool, NULL);
switch_assert(*pool != NULL);
@@ -313,7 +350,11 @@
#endif
tmp = switch_core_sprintf(*pool, "%s:%d", func, line);
apr_pool_tag(*pool, tmp);
+
+
+#ifdef USE_MEM_LOCK
switch_mutex_unlock(memory_manager.mem_lock);
+#endif
return SWITCH_STATUS_SUCCESS;
}
@@ -327,10 +368,22 @@
#endif
#ifdef INSTANTLY_DESTROY_POOLS
+#ifdef USE_MEM_LOCK
+ switch_mutex_lock(memory_manager.mem_lock);
+#endif
apr_pool_destroy(*pool);
+#ifdef USE_MEM_LOCK
+ switch_mutex_unlock(memory_manager.mem_lock);
+#endif
#else
if ((memory_manager.pool_thread_running != 1) || (switch_queue_push(memory_manager.pool_queue, *pool) != SWITCH_STATUS_SUCCESS)) {
+#ifdef USE_MEM_LOCK
+ switch_mutex_lock(memory_manager.mem_lock);
+#endif
apr_pool_destroy(*pool);
+#ifdef USE_MEM_LOCK
+ switch_mutex_unlock(memory_manager.mem_lock);
+#endif
}
#endif
@@ -346,8 +399,10 @@
switch_assert(pool != NULL);
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_lock(memory_manager.mem_lock);
#endif
+#endif
#ifdef DEBUG_ALLOC
if (memory > 500)
@@ -360,8 +415,10 @@
memset(ptr, 0, memory);
#ifdef LOCK_MORE
+#ifdef USE_MEM_LOCK
switch_mutex_unlock(memory_manager.mem_lock);
#endif
+#endif
return ptr;
}
@@ -379,7 +436,13 @@
if (!pool) {
break;
}
+#ifdef USE_MEM_LOCK
+ switch_mutex_lock(memory_manager.mem_lock);
+#endif
apr_pool_destroy(pool);
+#ifdef USE_MEM_LOCK
+ switch_mutex_unlock(memory_manager.mem_lock);
+#endif
}
#endif
return;
@@ -396,7 +459,9 @@
int x = len, done = 0;
switch_yield(1000000);
+#ifdef USE_MEM_LOCK
switch_mutex_lock(memory_manager.mem_lock);
+#endif
while (x > 0) {
void *pop = NULL;
if (switch_queue_pop(memory_manager.pool_queue, &pop) != SWITCH_STATUS_SUCCESS || !pop) {
@@ -405,16 +470,31 @@
}
#if defined(PER_POOL_LOCK) || defined(DESTROY_POOLS)
+#ifdef USE_MEM_LOCK
+ switch_mutex_lock(memory_manager.mem_lock);
+#endif
apr_pool_destroy(pop);
+#ifdef USE_MEM_LOCK
+ switch_mutex_unlock(memory_manager.mem_lock);
+#endif
#else
apr_pool_clear(pop);
if (switch_queue_trypush(memory_manager.pool_recycle_queue, pop) != SWITCH_STATUS_SUCCESS) {
+#ifdef USE_MEM_LOCK
+ switch_mutex_lock(memory_manager.mem_lock);
+#endif
apr_pool_destroy(pop);
+#ifdef USE_MEM_LOCK
+ switch_mutex_unlock(memory_manager.mem_lock);
+#endif
+
}
#endif
x--;
}
+#ifdef USE_MEM_LOCK
switch_mutex_unlock(memory_manager.mem_lock);
+#endif
if (done) {
goto done;
}
@@ -429,8 +509,15 @@
{
void *pop = NULL;
while (switch_queue_trypop(memory_manager.pool_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
+#ifdef USE_MEM_LOCK
+ switch_mutex_lock(memory_manager.mem_lock);
+#endif
apr_pool_destroy(pop);
pop = NULL;
+#ifdef USE_MEM_LOCK
+ switch_mutex_unlock(memory_manager.mem_lock);
+#endif
+
}
}
@@ -439,14 +526,17 @@
return NULL;
}
+#ifndef INSTANTLY_DESTROY_POOLS
static switch_thread_t *pool_thread_p = NULL;
+#endif
void switch_core_memory_stop(void)
{
+#ifndef INSTANTLY_DESTROY_POOLS
switch_status_t st;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping memory pool queue.\n");
-#ifndef INSTANTLY_DESTROY_POOLS
+
memory_manager.pool_thread_running = -1;
switch_thread_join(&st, pool_thread_p);
@@ -490,7 +580,9 @@
switch_assert(memory_manager.memory_pool != NULL);
#endif
+#ifdef USE_MEM_LOCK
switch_mutex_init(&memory_manager.mem_lock, SWITCH_MUTEX_NESTED, memory_manager.memory_pool);
+#endif
#ifdef INSTANTLY_DESTROY_POOLS
{
More information about the Freeswitch-svn
mailing list