eina: starting to use eina_lock.
authorcedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Sun, 24 Apr 2011 15:54:09 +0000 (15:54 +0000)
committercedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Sun, 24 Apr 2011 15:54:09 +0000 (15:54 +0000)
git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@58869 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

configure.ac
src/include/eina_config.h.in
src/include/eina_inline_lock_posix.x
src/include/eina_inline_lock_win32.x
src/include/eina_inline_lock_wince.x
src/include/eina_lock.h
src/lib/eina_main.c
src/modules/mp/chained_pool/eina_chained_mempool.c

index a837459..168478b 100644 (file)
@@ -116,6 +116,12 @@ fi
 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
index 6a94f95..cf7af51 100644 (file)
 #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
index 8151a28..c746d91 100644 (file)
 
 #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)
 {
@@ -39,20 +40,25 @@ eina_lock_free(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
index 65bc177..26a32aa 100644 (file)
 
 #include <windows.h>
 
-
 typedef CRITICAL_SECTION Eina_Lock;
 
+EAPI extern Eina_Bool _threads_activated;
+
 static inline Eina_Bool
 eina_lock_new(Eina_Lock *mutex)
 {
@@ -41,6 +42,8 @@ eina_lock_free(Eina_Lock mutex)
 static inline Eina_Bool
 eina_lock_take(Eina_Lock mutex)
 {
+   if (!_threads_activated) return EINA_FALSE;
+
    EnterCriticalSection(&mutex);
 
    return EINA_TRUE;
@@ -49,16 +52,19 @@ eina_lock_take(Eina_Lock mutex)
 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
index c9ab082..8be980d 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <windows.h>
 
+EAPI extern Eina_Bool _threads_activated;
 
 typedef HANDLE Eina_Lock;
 
@@ -45,6 +46,8 @@ eina_lock_take(Eina_Lock mutex)
 {
    DWORD res;
 
+   if (!_threads_activated) return EINA_FALSE;
+
    res = WaitForSingleObject(mutex, INFINITE);
    if ((res == WAIT_ABANDONED) || (res == WAIT_FAILED))
      return EINA_FALSE;
@@ -52,11 +55,17 @@ eina_lock_take(Eina_Lock mutex)
    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);
 }
 
index 45133fc..90bbf63 100644 (file)
@@ -16,8 +16,8 @@
  * 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);
+
 /**
  * @}
  */
index 78a94c3..f439326 100644 (file)
 
 #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>
@@ -50,6 +46,7 @@
 #include "eina_magic.h"
 #include "eina_rectangle.h"
 #include "eina_safety_checks.h"
+#include "eina_lock.h"
 
 /*============================================================================*
 *                                  Local                                     *
@@ -77,25 +74,9 @@ static int _eina_log_dom = -1;
 #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.
@@ -231,6 +212,8 @@ eina_init(void)
           }
      }
 
+   eina_lock_new(&_mutex);
+
    _eina_main_count = 1;
    return 1;
 }
@@ -240,7 +223,11 @@ eina_shutdown(void)
 {
    _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;
 }
@@ -252,22 +239,14 @@ eina_threads_init(void)
 #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;
      }
 
@@ -275,6 +254,8 @@ eina_threads_init(void)
    eina_log_threads_init();
    _threads_activated = EINA_TRUE;
 
+   eina_lock_release(_mutex);
+
    return ret;
 #else
    return 0;
@@ -287,26 +268,20 @@ eina_threads_shutdown(void)
 #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
index 160530a..a8f90f9 100644 (file)
@@ -43,6 +43,7 @@
 #include "eina_mempool.h"
 #include "eina_trash.h"
 #include "eina_rbtree.h"
+#include "eina_lock.h"
 
 #include "eina_private.h"
 
@@ -73,16 +74,10 @@ struct _Chained_Mempool
    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;
@@ -246,20 +241,12 @@ eina_chained_mempool_malloc(void *data, __UNUSED__ unsigned int size)
    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);
@@ -280,16 +267,7 @@ eina_chained_mempool_malloc(void *data, __UNUSED__ unsigned int size)
         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;
           }
 
@@ -300,16 +278,7 @@ eina_chained_mempool_malloc(void *data, __UNUSED__ unsigned int size)
 
    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;
 }
@@ -322,21 +291,12 @@ eina_chained_mempool_free(void *data, void *ptr)
    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);
@@ -362,16 +322,7 @@ eina_chained_mempool_free(void *data, void *ptr)
      }
 #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;
 }
 
@@ -385,21 +336,12 @@ eina_chained_mempool_repack(void *data,
   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);
@@ -467,17 +409,7 @@ eina_chained_mempool_repack(void *data,
      }
 
    /* 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 *
@@ -520,17 +452,12 @@ eina_chained_mempool_init(const char *context,
    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;
 }
 
@@ -567,15 +494,10 @@ eina_chained_mempool_shutdown(void *data)
    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);