eina: put global into namespace and add lock abort
authorcedric <cedric>
Sun, 24 Apr 2011 20:22:17 +0000 (20:22 +0000)
committercedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Sun, 24 Apr 2011 20:22:17 +0000 (20:22 +0000)
git-svn-id: http://svn.enlightenment.org/svn/e/trunk/eina@58875 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/include/eina_inline_lock_posix.x
src/lib/eina_main.c

index 72d3ab3..1477c93 100644 (file)
 
 typedef pthread_mutex_t Eina_Lock;
 
-EAPI extern Eina_Bool _threads_activated;
+EAPI extern Eina_Bool _eina_threads_activated;
+
+#ifdef EINA_HAVE_DEBUG_THREADS
+# include <sys/time.h>
+
+EAPI extern int _eina_threads_debug;
+#endif
 
 static inline Eina_Bool
 eina_lock_new(Eina_Lock *mutex)
 {
-   return (pthread_mutex_init(mutex, NULL) == 0) ? EINA_TRUE : EINA_FALSE;
+   pthread_mutexattr_t attr;
+
+   if (pthread_mutexattr_init(&attr) != 0)
+     return EINA_FALSE;
+   if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
+     return EINA_FALSE;
+   if (pthread_mutex_init(mutex, &attr) != 0)
+     return EINA_FALSE;
+
+   pthread_mutexattr_destroy(&attr);
+
+   return EINA_TRUE;
 }
 
 static inline void
@@ -40,15 +57,37 @@ eina_lock_free(Eina_Lock *mutex)
 static inline Eina_Bool
 eina_lock_take(Eina_Lock *mutex)
 {
-   if (_threads_activated)
-     return (pthread_mutex_lock(mutex) == 0) ? EINA_TRUE : EINA_FALSE;
+   if (_eina_threads_activated)
+      {
+#ifdef EINA_HAVE_DEBUG_THREADS
+         if (_eina_threads_debug)
+           {
+              struct timeval t0, t1;
+              int dt;
+
+              gettimeofday(&t0, NULL);
+              pthread_mutex_lock(&(x));
+              gettimeofday(&t1, NULL);
+
+              dt = (t1.tv_sec - t0.tv_sec) * 1000000;
+              if (t1.tv_usec > t0.tv_usec)
+                dt += (t1.tv_usec - t0.tv_usec);
+              else
+                dt -= t0.tv_usec - t1.tv_usec;
+              dt /= 1000;
+
+              if (dt > _eina_threads_debug) abort();
+           }
+#endif
+         return (pthread_mutex_lock(mutex) == 0) ? EINA_TRUE : EINA_FALSE;
+      }
    return EINA_FALSE;
 }
 
 static inline Eina_Bool
 eina_lock_take_try(Eina_Lock *mutex)
 {
-   if (_threads_activated)
+   if (_eina_threads_activated)
      return (pthread_mutex_trylock(mutex) == 0) ? EINA_TRUE : EINA_FALSE;
    return EINA_FALSE;
 }
@@ -56,7 +95,7 @@ eina_lock_take_try(Eina_Lock *mutex)
 static inline Eina_Bool
 eina_lock_release(Eina_Lock *mutex)
 {
-   if (_threads_activated)
+   if (_eina_threads_activated)
      return (pthread_mutex_unlock(mutex) == 0) ? EINA_TRUE : EINA_FALSE;
    return EINA_FALSE;
 }
index 101dbb2..26a33e5 100644 (file)
@@ -74,7 +74,11 @@ static int _eina_log_dom = -1;
 #endif
 #define DBG(...) EINA_LOG_DOM_DBG(_eina_log_dom, __VA_ARGS__)
 
-EAPI Eina_Bool _threads_activated = EINA_FALSE;
+EAPI Eina_Bool _eina_threads_activated = EINA_FALSE;
+
+#ifdef EINA_HAVE_DEBUG_THREADS
+EAPI int _eina_threads_debug = 0;
+#endif
 
 static Eina_Lock _mutex;
 
@@ -213,6 +217,10 @@ eina_init(void)
      }
 
    eina_lock_new(&_mutex);
+#ifdef EINA_HAVE_DEBUG_THREADS
+   if (getenv("EINA_DEBUG_THREADS"))
+     _eina_threads_debug = atoi(getenv("EINA_DEBUG_THREADS");
+#endif
 
    _eina_main_count = 1;
    return 1;
@@ -252,7 +260,7 @@ eina_threads_init(void)
 
    eina_share_common_threads_init();
    eina_log_threads_init();
-   _threads_activated = EINA_TRUE;
+   _eina_threads_activated = EINA_TRUE;
 
    eina_lock_release(&_mutex);
 
@@ -281,7 +289,7 @@ eina_threads_shutdown(void)
 
    eina_lock_release(&_mutex);
 
-   _threads_activated = EINA_FALSE;
+   _eina_threads_activated = EINA_FALSE;
 
    return ret;
 #else