* eina: improve threads and debugging support in one big mempool.

SVN revision: 53815
This commit is contained in:
Cedric BAIL 2010-10-23 16:56:36 +00:00
parent 32cf20c60e
commit 4b1e2abc23
1 changed files with 66 additions and 29 deletions

View File

@ -24,9 +24,11 @@
#include <string.h>
#ifdef EFL_HAVE_POSIX_THREADS
#include <pthread.h>
# include <pthread.h>
#endif
#include <assert.h>
#ifdef EFL_HAVE_WIN32_THREADS
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
@ -42,9 +44,7 @@
# include <valgrind/memcheck.h>
#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);
}