* eina/m4/efl_pthread.m4,

* eina/src/modules/mp/chained_pool/Makefile.am,
	* eina/src/modules/mp/chained_pool/eina_chained_mempool.c: Now chained mempool are thread safe.



SVN revision: 39058
This commit is contained in:
Cedric BAIL 2009-02-17 14:18:14 +00:00
parent a98287fc07
commit 55cfc46ee2
3 changed files with 68 additions and 10 deletions

View File

@ -13,7 +13,7 @@ AC_DEFUN([EFL_CHECK_PTHREAD],
dnl configure option
AC_ARG_ENABLE([pthread],
[AC_HELP_STRING([--enable-pthread], [enable POSIX threads code @<:@default=no@:>@])],
[AC_HELP_STRING([--disable-pthread], [enable POSIX threads code @<:@default=no@:>@])],
[
if test "x${enableval}" = "xyes" ; then
_efl_enable_pthread="yes"
@ -21,7 +21,7 @@ AC_ARG_ENABLE([pthread],
_efl_enable_pthread="no"
fi
],
[_efl_enable_pthread="no"]
[_efl_enable_pthread="yes"]
)
AC_MSG_CHECKING([whether to build POSIX threads code])
AC_MSG_RESULT([${_efl_enable_pthread}])
@ -42,18 +42,36 @@ if test "x${_efl_enable_pthread}" = "xyes" ; then
]],
[[]])
],
[_efl_have_pthread="yes"
EFL_PTHREAD_FLAGS="-pthread"]
[
_efl_have_pthread="yes"
EFL_PTHREAD_CFLAGS="-pthread"
EFL_PTHREAD_LIBS="-pthread"
]
)
AC_LANG_POP([C])
CFLAGS=${SAVE_CFLAGS}
fi
AC_MSG_CHECKING([whether POSIX threads are supported])
AC_MSG_CHECKING([whether compiler need -pthread POSIX for threads support])
AC_MSG_RESULT([${_efl_have_pthread}])
AC_SUBST(EFL_PTHREAD_FLAGS)
if test "x${_efl_have_pthread}" = "xno" -a "x${_efl_enable_pthread}" = "xyes" ; then
AC_CHECK_HEADER(pthread.h,
[
_efl_have_pthread="yes"
EFL_PTHREAD_LIBS="-lpthread"
],
[
_efl_have_pthread="no"
])
AC_MSG_CHECKING([whether system support POSIX threads])
AC_MSG_RESULT([${_efl_enable_pthread}])
fi
AC_SUBST(EFL_PTHREAD_CFLAGS)
AC_SUBST(EFL_PTHREAD_LIBS)
if test "x${_efl_have_pthread}" = "xyes" ; then
AC_DEFINE(EFL_HAVE_PTHREAD, 1, [Define to mention that POSIX threads are supported])

View File

@ -4,7 +4,7 @@ AM_CPPFLAGS = \
-I. \
-I$(top_srcdir)/src/include \
-I$(top_builddir)/src/include \
@EINA_CFLAGS@
@EINA_CFLAGS@ @EFL_PTHREAD_CFLAGS@
if !EINA_STATIC_BUILD_CHAINED_POOL
@ -14,7 +14,7 @@ controller_LTLIBRARIES = eina_chained_mempool.la
eina_chained_mempool_la_SOURCES = \
eina_chained_mempool.c
eina_chained_mempool_la_LIBADD = $(top_builddir)/src/lib/libeina.la @EINA_LIBS@
eina_chained_mempool_la_LIBADD = $(top_builddir)/src/lib/libeina.la @EINA_LIBS@ @EFL_PTHREAD_LIBS@
eina_chained_mempool_la_LDFLAGS = -no-undefined @lt_enable_auto_import@ -module -avoid-version
eina_chained_mempool_la_LIBTOOLFLAGS = --tag=disable-static

View File

@ -26,6 +26,10 @@
#include <stdlib.h>
#include <string.h>
#ifdef EFL_HAVE_PTHREAD
#include <pthread.h>
#endif
#include "eina_inlist.h"
#include "eina_error.h"
#include "eina_module.h"
@ -41,6 +45,9 @@ struct _Chained_Mempool
int item_size;
int pool_size;
int usage;
#ifdef EFL_HAVE_PTHREAD
pthread_mutex_t mutex;
#endif
};
typedef struct _Chained_Pool Chained_Pool;
@ -85,6 +92,10 @@ eina_chained_mempool_malloc(void *data, __UNUSED__ unsigned int size)
Chained_Pool *p = NULL;
void *mem;
#ifdef EFL_HAVE_PTHREAD
pthread_mutex_lock(&pool->mutex);
#endif
// look 4 pool from 2nd bucket on
EINA_INLIST_FOREACH(pool->first, p)
{
@ -95,11 +106,18 @@ eina_chained_mempool_malloc(void *data, __UNUSED__ unsigned int size)
break;
}
}
// we have reached the end of the list - no free pools
if (!p)
{
p = _eina_chained_mp_pool_new(pool);
if (!p) return NULL;
if (!p)
{
#ifdef EFL_HAVE_PTHREAD
pthread_mutex_unlock(&pool->mutex);
#endif
return NULL;
}
pool->first = eina_inlist_prepend(pool->first, EINA_INLIST_GET(p));
}
// this points to the next free block - so take it
@ -108,9 +126,15 @@ eina_chained_mempool_malloc(void *data, __UNUSED__ unsigned int size)
p->base = *((void **)mem);
// move to end - it just filled up
if (!p->base)
pool->first = eina_inlist_demote(pool->first, EINA_INLIST_GET(p));
{
pool->first = eina_inlist_demote(pool->first, EINA_INLIST_GET(p));
}
p->usage++;
pool->usage++;
#ifdef EFL_HAVE_PTHREAD
pthread_mutex_unlock(&pool->mutex);
#endif
return mem;
}
@ -126,6 +150,10 @@ eina_chained_mempool_free(void *data, void *ptr)
psize = item_alloc * pool->pool_size;
// look 4 pool
#ifdef EFL_HAVE_PTHREAD
pthread_mutex_lock(&pool->mutex);
#endif
EINA_INLIST_FOREACH(pool->first, p)
{
// pool mem base
@ -153,6 +181,10 @@ eina_chained_mempool_free(void *data, void *ptr)
break;
}
}
#ifdef EFL_HAVE_PTHREAD
pthread_mutex_unlock(&pool->mutex);
#endif
}
static void*
@ -181,6 +213,10 @@ eina_chained_mempool_init(const char *context, __UNUSED__ const char *option, va
memcpy((char*) mp->name, context, length);
}
#ifdef EFL_HAVE_PTHREAD
pthread_mutex_init(&mp->mutex, NULL);
#endif
return mp;
}
@ -204,6 +240,10 @@ eina_chained_mempool_shutdown(void *data)
_eina_chained_mp_pool_free(p);
}
#ifdef EFL_HAVE_PTHREAD
pthread_mutex_destroy(&mp->mutex);
#endif
free(mp);
}