AC_SUBST(EINA_CONFIGURE_HAVE_THREADS)
AM_CONDITIONAL([EINA_HAVE_THREADS], [! test "x${have_threads}" = "xno"])
+if ! test "x${have_debug_threads}" = "xno"; then
+ EINA_CONFIGURE_HAVE_DEBUG_THREADS="#define EINA_DEBUG_THREADS"
+fi
+AC_SUBST(EINA_CONFIGURE_HAVE_DEBUG_THREADS)
+AM_CONDITIONAL([EINA_DEBUG_THREADS], [! test "x${have_debug_threads}" = "xno"])
+
### Additional options to configure
# Magic debug
#endif
@EINA_CONFIGURE_HAVE_THREADS@
+#ifdef EINA_HAVE_DEBUG_THREADS
+# undef EINA_HAVE_DEBUG_THREADS
+#endif
+@EINA_CONFIGURE_HAVE_DEBUG_THREADS@
+
#ifdef EINA_SIZEOF_WCHAR_T
# undef EINA_SIZEOF_WCHAR_T
#endif
#include <pthread.h>
-
typedef pthread_mutex_t Eina_Lock;
+EAPI extern Eina_Bool _threads_activated;
+
static inline Eina_Bool
eina_lock_new(Eina_Lock *mutex)
{
static inline Eina_Bool
eina_lock_take(Eina_Lock mutex)
{
- return (pthread_mutex_lock(&mutex) == 0) ? EINA_TRUE : EINA_FALSE;
+ if (_threads_activated)
+ return (pthread_mutex_lock(&mutex) == 0) ? EINA_TRUE : EINA_FALSE;
+ return EINA_FALSE;
}
static inline Eina_Bool
eina_lock_take_try(Eina_Lock mutex)
{
- return (pthread_mutex_trylock(&mutex) == 0) ? EINA_TRUE : EINA_FALSE;
+ if (_threads_activated)
+ return (pthread_mutex_trylock(&mutex) == 0) ? EINA_TRUE : EINA_FALSE;
+ return EINA_FALSE;
}
static inline Eina_Bool
eina_lock_release(Eina_Lock mutex)
{
- return (pthread_mutex_unlock(&mutex) == 0) ? EINA_TRUE : EINA_FALSE;
+ if (_threads_activated)
+ return (pthread_mutex_unlock(&mutex) == 0) ? EINA_TRUE : EINA_FALSE;
+ return EINA_FALSE;
}
-
#endif
#include <windows.h>
-
typedef CRITICAL_SECTION Eina_Lock;
+EAPI extern Eina_Bool _threads_activated;
+
static inline Eina_Bool
eina_lock_new(Eina_Lock *mutex)
{
static inline Eina_Bool
eina_lock_take(Eina_Lock mutex)
{
+ if (!_threads_activated) return EINA_FALSE;
+
EnterCriticalSection(&mutex);
return EINA_TRUE;
static inline Eina_Bool
eina_lock_take_try(Eina_Lock mutex)
{
+ if (!_threads_activated) return EINA_FALSE;
+
return TryEnterCriticalSection(&mutex) == 0 ? EINA_FALSE : EINA_TRUE;
}
static inline Eina_Bool
eina_lock_release(Eina_Lock mutex)
{
+ if (!_threads_activated) return EINA_FALSE;
+
LeaveCriticalSection(&mutex);
return EINA_TRUE;
}
-
#endif
#include <windows.h>
+EAPI extern Eina_Bool _threads_activated;
typedef HANDLE Eina_Lock;
{
DWORD res;
+ if (!_threads_activated) return EINA_FALSE;
+
res = WaitForSingleObject(mutex, INFINITE);
if ((res == WAIT_ABANDONED) || (res == WAIT_FAILED))
return EINA_FALSE;
return EINA_TRUE;
}
-#define eina_lock_take_try(m) eina_lock_take(m)
+static inline Eina_Bool
+eina_lock_take_try(Eina_Lock mutex)
+{
+ return eina_lock_take(mutex);
+}
static inline Eina_Bool
eina_lock_release(Eina_Lock mutex)
{
+ if (!_threads_activated) return EINA_FALSE;
+
return ReleaseMutex(mutex);
}
* if not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef EINA_ARRAY_H_
-#define EINA_ARRAY_H_
+#ifndef EINA_LOCK_H_
+#define EINA_LOCK_H_
#include "eina_config.h"
# include "eina_inline_lock_void.x"
#endif
+static inline Eina_Bool eina_lock_new(Eina_Lock *mutex);
+static inline void eina_lock_free(Eina_Lock mutex);
+static inline Eina_Bool eina_lock_take(Eina_Lock mutex);
+static inline Eina_Bool eina_lock_take_try(Eina_Lock mutex);
+static inline Eina_Bool eina_lock_release(Eina_Lock mutex);
+
/**
* @}
*/
#include <stdio.h>
-#ifdef EFL_HAVE_POSIX_THREADS
-# include <pthread.h>
-#endif
-
#ifdef EFL_HAVE_WIN32_THREADS
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#include "eina_magic.h"
#include "eina_rectangle.h"
#include "eina_safety_checks.h"
+#include "eina_lock.h"
/*============================================================================*
* Local *
#endif
#define DBG(...) EINA_LOG_DOM_DBG(_eina_log_dom, __VA_ARGS__)
-Eina_Bool _threads_activated = EINA_FALSE;
+EAPI Eina_Bool _threads_activated = EINA_FALSE;
-#ifdef EFL_HAVE_THREADS
-# ifdef EFL_HAVE_POSIX_THREADS
-static pthread_mutex_t _mutex = PTHREAD_MUTEX_INITIALIZER;
-# define LOCK() if(_threads_activated) pthread_mutex_lock(&_mutex)
-# define UNLOCK() if(_threads_activated) pthread_mutex_unlock(&_mutex)
-# define UNLOCK_FORCE() pthread_mutex_unlock(&_mutex)
-# else /* EFL_HAVE_WIN32_THREADS */
-static HANDLE _mutex = NULL;
-# define LOCK() if(_threads_activated) WaitForSingleObject(_mutex, INFINITE)
-# define UNLOCK() if(_threads_activated) ReleaseMutex(_mutex)
-# define UNLOCK_FORCE() ReleaseMutex(_mutex)
-# endif
-#else
-# define LOCK() do {} while (0)
-# define UNLOCK() do {} while (0)
-# define UNLOCK_FORCE() do {} while (0)
-#endif
+static Eina_Lock _mutex;
/* place module init/shutdown functions here to avoid other modules
* calling them by mistake.
}
}
+ eina_lock_new(&_mutex);
+
_eina_main_count = 1;
return 1;
}
{
_eina_main_count--;
if (EINA_UNLIKELY(_eina_main_count == 0))
- _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
+ {
+ _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
+
+ eina_lock_free(_mutex);
+ }
return _eina_main_count;
}
#ifdef EFL_HAVE_THREADS
int ret;
-# ifdef EFL_HAVE_WIN32_THREADS
- if (!_mutex)
- _mutex = CreateMutex(NULL, FALSE, NULL);
-
- if (!_mutex)
- return 0;
-# endif
-
- LOCK();
+ eina_lock_take(_mutex);
++_eina_main_thread_count;
ret = _eina_main_thread_count;
if(_eina_main_thread_count > 1)
{
- UNLOCK();
+ eina_lock_release(_mutex);
return ret;
}
eina_log_threads_init();
_threads_activated = EINA_TRUE;
+ eina_lock_release(_mutex);
+
return ret;
#else
return 0;
#ifdef EFL_HAVE_THREADS
int ret;
- LOCK();
+ eina_lock_take(_mutex);
ret = --_eina_main_thread_count;
if(_eina_main_thread_count > 0)
{
- UNLOCK();
+ eina_lock_release(_mutex);
return ret;
}
eina_share_common_threads_shutdown();
eina_log_threads_shutdown();
- _threads_activated = EINA_FALSE;
-
- UNLOCK_FORCE();
+ eina_lock_release(_mutex);
-# ifdef EFL_HAVE_WIN32_THREADS
- if (_mutex)
- CloseHandle(_mutex);
-
-# endif
+ _threads_activated = EINA_FALSE;
return ret;
#else
#include "eina_mempool.h"
#include "eina_trash.h"
#include "eina_rbtree.h"
+#include "eina_lock.h"
#include "eina_private.h"
int alloc_size;
int group_size;
int usage;
-#ifdef EFL_HAVE_THREADS
-# ifdef EFL_HAVE_POSIX_THREADS
-# ifdef EFL_DEBUG_THREADS
+#ifdef EFL_DEBUG_THREADS
pthread_t self;
-# endif
- pthread_mutex_t mutex;
-# else
- HANDLE mutex;
-# endif
#endif
+ Eina_Lock mutex;
};
typedef struct _Chained_Pool Chained_Pool;
Chained_Pool *p = NULL;
void *mem;
-#ifdef EFL_HAVE_THREADS
- if (_threads_activated)
+ if (!eina_lock_take(pool->mutex))
{
-# ifdef EFL_HAVE_POSIX_THREADS
- pthread_mutex_lock(&pool->mutex);
-# else
- WaitForSingleObject(pool->mutex, INFINITE);
-# endif
- }
#ifdef EFL_DEBUG_THREADS
- else
- assert(pthread_equal(pool->self, pthread_self()));
-#endif
+ assert(pthread_equal(pool->self, pthread_self()));
#endif
+ }
// Either we have some free space in the first one, or there is no free space.
if (pool->first) p = EINA_INLIST_CONTAINER_GET(pool->first, Chained_Pool);
p = _eina_chained_mp_pool_new(pool);
if (!p)
{
-#ifdef EFL_HAVE_PTHREAD
- if (_threads_activated)
- {
-# ifdef EFL_HAVE_POSIX_THREADS
- pthread_mutex_unlock(&pool->mutex);
-# else
- ReleaseMutex(pool->mutex);
-# endif
- }
-#endif
+ eina_lock_release(pool->mutex);
return NULL;
}
mem = _eina_chained_mempool_alloc_in(pool, p);
-#ifdef EFL_HAVE_THREADS
- if (_threads_activated)
- {
-# ifdef EFL_HAVE_POSIX_THREADS
- pthread_mutex_unlock(&pool->mutex);
-# else
- ReleaseMutex(pool->mutex);
-# endif
- }
-#endif
+ eina_lock_release(pool->mutex);
return mem;
}
Chained_Pool *p;
// look 4 pool
-
-#ifdef EFL_HAVE_THREADS
- if (_threads_activated)
+ if (!eina_lock_take(pool->mutex))
{
-# ifdef EFL_HAVE_POSIX_THREADS
- pthread_mutex_lock(&pool->mutex);
-# else
- WaitForSingleObject(pool->mutex, INFINITE);
-# endif
- }
#ifdef EFL_DEBUG_THREADS
- else
- assert(pthread_equal(pool->self, pthread_self()));
-#endif
+ assert(pthread_equal(pool->self, pthread_self()));
#endif
+ }
// searching for the right mempool
r = eina_rbtree_inline_lookup(pool->root, ptr, 0, _eina_chained_mp_pool_key_cmp, NULL);
}
#endif
-#ifdef EFL_HAVE_THREADS
- if (_threads_activated)
- {
-# ifdef EFL_HAVE_POSIX_THREADS
- pthread_mutex_unlock(&pool->mutex);
-# else
- ReleaseMutex(pool->mutex);
-# endif
- }
-#endif
+ eina_lock_release(pool->mutex);
return;
}
Chained_Pool *tail;
/* FIXME: Improvement - per Chained_Pool lock */
-
-#ifdef EFL_HAVE_THREADS
- if (_threads_activated)
+ if (!eina_lock_take(pool->mutex))
{
-# ifdef EFL_HAVE_POSIX_THREADS
- pthread_mutex_lock(&pool->mutex);
-# else
- WaitForSingleObject(pool->mutex, INFINITE);
-# endif
- }
#ifdef EFL_DEBUG_THREADS
- else
- assert(pthread_equal(pool->self, pthread_self()));
-#endif
+ assert(pthread_equal(pool->self, pthread_self()));
#endif
+ }
pool->first = eina_inlist_sort(pool->first,
(Eina_Compare_Cb) _eina_chained_mempool_usage_cmp);
}
/* FIXME: improvement - reorder pool so that the most used one get in front */
-
-#ifdef EFL_HAVE_THREADS
- if (_threads_activated)
- {
-# ifdef EFL_HAVE_POSIX_THREADS
- pthread_mutex_unlock(&pool->mutex);
-# else
- ReleaseMutex(pool->mutex);
-# endif
- }
-#endif
+ eina_lock_release(pool->mutex);
}
static void *
VALGRIND_CREATE_MEMPOOL(mp, 0, 1);
#endif
-#ifdef EFL_HAVE_THREADS
-# ifdef EFL_HAVE_POSIX_THREADS
-# ifdef EFL_DEBUG_THREADS
+#ifdef EFL_DEBUG_THREADS
mp->self = pthread_self();
-# endif
- pthread_mutex_init(&mp->mutex, NULL);
-# else
- mp->mutex = CreateMutex(NULL, FALSE, NULL);
-# endif
#endif
+ eina_lock_new(&mp->mutex);
+
return mp;
}
VALGRIND_DESTROY_MEMPOOL(mp);
#endif
-#ifdef EFL_HAVE_THREADS
-# ifdef EFL_HAVE_POSIX_THREADS
-# ifdef EFL_DEBUG_THREADS
+ eina_lock_free(mp->mutex);
+
+#ifdef EFL_DEBUG_THREADS
assert(pthread_equal(mp->self, pthread_self()));
-# endif
- pthread_mutex_destroy(&mp->mutex);
-# else
- CloseHandle(mp->mutex);
-# endif
#endif
free(mp);