From 959b4cc9d1cb7acd01bf44e5b4614204c3b2bec6 Mon Sep 17 00:00:00 2001 From: cedric Date: Sat, 23 Oct 2010 16:56:36 +0000 Subject: [PATCH] * eina: improve threads and debugging support in one big mempool. git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@53815 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- src/modules/mp/one_big/eina_one_big.c | 95 ++++++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 29 deletions(-) diff --git a/src/modules/mp/one_big/eina_one_big.c b/src/modules/mp/one_big/eina_one_big.c index c0d8018..5feb70d 100644 --- a/src/modules/mp/one_big/eina_one_big.c +++ b/src/modules/mp/one_big/eina_one_big.c @@ -24,9 +24,11 @@ #include #ifdef EFL_HAVE_POSIX_THREADS -#include +# include #endif +#include + #ifdef EFL_HAVE_WIN32_THREADS # define WIN32_LEAN_AND_MEAN # include @@ -42,9 +44,7 @@ # include #endif -#ifdef DEBUG #include "eina_private.h" -#endif #ifdef INF #undef INF @@ -77,6 +77,9 @@ struct _One_Big #ifdef EFL_HAVE_THREADS # ifdef EFL_HAVE_POSIX_THREADS +# ifdef EFL_DEBUG_THREADS + pthread_t self; +# endif pthread_mutex_t mutex; # else HANDLE mutex; @@ -91,10 +94,17 @@ eina_one_big_malloc(void *data, __UNUSED__ unsigned int size) unsigned char *mem = NULL; #ifdef EFL_HAVE_THREADS + if (_threads_activated) + { # ifdef EFL_HAVE_POSIX_THREADS - pthread_mutex_lock(&pool->mutex); + pthread_mutex_lock(&pool->mutex); # else - WaitForSingleObject(pool->mutex, INFINITE); + WaitForSingleObject(pool->mutex, INFINITE); +# endif + } +# ifdef EFL_DEBUG_THREADS + else + assert(pthread_equal(pool->self, pthread_self())); # endif #endif @@ -147,11 +157,14 @@ eina_one_big_malloc(void *data, __UNUSED__ unsigned int size) on_exit: #ifdef EFL_HAVE_THREADS + if (_threads_activated) + { # ifdef EFL_HAVE_POSIX_THREADS - pthread_mutex_unlock(&pool->mutex); + pthread_mutex_unlock(&pool->mutex); # else - ReleaseMutex(pool->mutex); + ReleaseMutex(pool->mutex); # endif + } #endif #ifndef NVALGRIND @@ -166,10 +179,17 @@ eina_one_big_free(void *data, void *ptr) One_Big *pool = data; #ifdef EFL_HAVE_THREADS + if (_threads_activated) + { # ifdef EFL_HAVE_POSIX_THREADS - pthread_mutex_lock(&pool->mutex); + pthread_mutex_lock(&pool->mutex); # else - WaitForSingleObject(pool->mutex, INFINITE); + WaitForSingleObject(pool->mutex, INFINITE); +# endif + } +# ifdef EFL_DEBUG_THREADS + else + assert(pthread_equal(pool->self, pthread_self())); # endif #endif @@ -181,8 +201,20 @@ eina_one_big_free(void *data, void *ptr) } else { +#ifndef NDEBUG + Eina_Inlist *it; +#endif Eina_Inlist *il; + il = (Eina_Inlist *)(((unsigned char *)ptr) - sizeof(Eina_Inlist)); + +#ifndef NDEBUG + for (it = pool->over_list; it != NULL; it = it->next) + if (it == il) break; + + assert(it != NULL); +#endif + pool->over_list = eina_inlist_remove(pool->over_list, il); free(il); pool->over--; @@ -193,11 +225,14 @@ eina_one_big_free(void *data, void *ptr) #endif #ifdef EFL_HAVE_THREADS + if (_threads_activated) + { # ifdef EFL_HAVE_POSIX_THREADS - pthread_mutex_unlock(&pool->mutex); + pthread_mutex_unlock(&pool->mutex); # else - ReleaseMutex(pool->mutex); + ReleaseMutex(pool->mutex); # endif + } #endif } @@ -237,6 +272,9 @@ eina_one_big_init(const char *context, #ifdef EFL_HAVE_THREADS # ifdef EFL_HAVE_POSIX_THREADS +# ifdef EFL_DEBUG_THREADS + pool->self = pthread_self(); +# endif pthread_mutex_init(&pool->mutex, NULL); # else pool->mutex = CreateMutex(NULL, FALSE, NULL); @@ -257,10 +295,17 @@ eina_one_big_shutdown(void *data) if (!pool) return; #ifdef EFL_HAVE_THREADS + if (_threads_activated) + { # ifdef EFL_HAVE_POSIX_THREADS - pthread_mutex_lock(&pool->mutex); + pthread_mutex_lock(&pool->mutex); # else - WaitForSingleObject(pool->mutex, INFINITE); + WaitForSingleObject(pool->mutex, INFINITE); +# endif + } +# ifdef EFL_DEBUG_THREADS + else + assert(pthread_equal(pool->self, pthread_self())); # endif #endif @@ -292,32 +337,24 @@ eina_one_big_shutdown(void *data) "Pool [%s] still over by %i\n", pool->name, pool->over); } -#ifdef DEBUG - if (pool->usage > 0) - INF( - "Bad news we are destroying memory still referenced in mempool [%s]\n", - pool->name); - - if (pool->over > 0) - INF("Bad news we are losing track of pointer from mempool [%s]\n", - pool->name); - -#endif #ifndef NVALGRIND VALGRIND_DESTROY_MEMPOOL(pool); #endif if (pool->base) free(pool->base); - + #ifdef EFL_HAVE_THREADS + if (_threads_activated) + { # ifdef EFL_HAVE_POSIX_THREADS - pthread_mutex_unlock(&pool->mutex); - pthread_mutex_destroy(&pool->mutex); + pthread_mutex_unlock(&pool->mutex); + pthread_mutex_destroy(&pool->mutex); # else - ReleaseMutex(pool->mutex); - CloseHandle(pool->mutex); + ReleaseMutex(pool->mutex); + CloseHandle(pool->mutex); # endif + } #endif free(pool); } -- 2.7.4