diff options
author | Benny Prijono <bennylp@teluu.com> | 2006-02-26 21:18:42 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2006-02-26 21:18:42 +0000 |
commit | 47c1f09504fce4a8f980e8a407f40dbfaacc5a6b (patch) | |
tree | 470e12f1f956b2eb08cbd10cc6ba084d61961a34 /pjlib | |
parent | 9cb2e01ffb6e99bd29a09040064a74ee35714ab6 (diff) |
Added mutex protection for caching pool
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@232 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib')
-rw-r--r-- | pjlib/include/pj/pool.h | 10 | ||||
-rw-r--r-- | pjlib/src/pj/pool_caching.c | 25 |
2 files changed, 34 insertions, 1 deletions
diff --git a/pjlib/include/pj/pool.h b/pjlib/include/pj/pool.h index 93d67a90..885b275e 100644 --- a/pjlib/include/pj/pool.h +++ b/pjlib/include/pj/pool.h @@ -542,6 +542,16 @@ struct pj_caching_pool * List of pools currently allocated by applications. */ pj_list used_list; + + /** + * Internal pool. + */ + pj_pool_t *pool; + + /** + * Mutex. + */ + pj_mutex_t *mutex; }; diff --git a/pjlib/src/pj/pool_caching.c b/pjlib/src/pj/pool_caching.c index 5f5200f4..6114160e 100644 --- a/pjlib/src/pj/pool_caching.c +++ b/pjlib/src/pj/pool_caching.c @@ -56,6 +56,10 @@ PJ_DEF(void) pj_caching_pool_init( pj_caching_pool *cp, cp->factory.create_pool = &cpool_create_pool; cp->factory.release_pool = &cpool_release_pool; cp->factory.dump_status = &cpool_dump_status; + + cp->pool = pj_pool_create_int(&cp->factory, "cachingpool", 128, + 0, NULL); + i = pj_mutex_create_simple(cp->pool, "cachingpool", &cp->mutex); } PJ_DEF(void) pj_caching_pool_destroy( pj_caching_pool *cp ) @@ -84,6 +88,8 @@ PJ_DEF(void) pj_caching_pool_destroy( pj_caching_pool *cp ) pj_pool_destroy_int(pool); pool = next; } + + pj_mutex_destroy(cp->mutex); } static pj_pool_t* cpool_create_pool(pj_pool_factory *pf, @@ -98,6 +104,8 @@ static pj_pool_t* cpool_create_pool(pj_pool_factory *pf, PJ_CHECK_STACK(); + pj_mutex_lock(cp->mutex); + /* Use pool factory's policy when callback is NULL */ if (callback == NULL) { callback = pf->policy.callback; @@ -123,8 +131,10 @@ static pj_pool_t* cpool_create_pool(pj_pool_factory *pf, /* Create new pool */ pool = pj_pool_create_int(&cp->factory, name, initial_size, increment_sz, callback); - if (!pool) + if (!pool) { + pj_mutex_unlock(cp->mutex); return NULL; + } } else { /* Get one pool from the list. */ @@ -145,6 +155,8 @@ static pj_pool_t* cpool_create_pool(pj_pool_factory *pf, /* Increment used count. */ ++cp->used_count; + + pj_mutex_unlock(cp->mutex); return pool; } @@ -155,6 +167,8 @@ static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool) PJ_CHECK_STACK(); + pj_mutex_lock(cp->mutex); + /* Erase from the used list. */ pj_list_erase(pool); @@ -169,6 +183,7 @@ static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool) cp->capacity + pool->capacity > cp->max_capacity) { pj_pool_destroy_int(pool); + pj_mutex_unlock(cp->mutex); return; } @@ -188,17 +203,23 @@ static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool) if (i == PJ_CACHING_POOL_ARRAY_SIZE) { /* Something has gone wrong with the pool. */ pj_pool_destroy_int(pool); + pj_mutex_unlock(cp->mutex); return; } pj_list_insert_after(&cp->free_list[i], pool); cp->capacity += pool->capacity; + + pj_mutex_unlock(cp->mutex); } static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail ) { #if PJ_LOG_MAX_LEVEL >= 3 pj_caching_pool *cp = (pj_caching_pool*)factory; + + pj_mutex_lock(cp->mutex); + PJ_LOG(3,("cachpool", " Dumping caching pool:")); PJ_LOG(3,("cachpool", " Capacity=%u, max_capacity=%u, used_cnt=%u", \ cp->capacity, cp->max_capacity, cp->used_count)); @@ -218,6 +239,8 @@ static void cpool_dump_status(pj_pool_factory *factory, pj_bool_t detail ) total_used, total_capacity, total_used * 100 / total_capacity)); } + + pj_mutex_unlock(cp->mutex); #else PJ_UNUSED_ARG(factory); PJ_UNUSED_ARG(detail); |