eina - add recursive mutex lock - apparently these are portable
authorCarsten Haitzler (Rasterman) <raster@rasterman.com>
Wed, 7 Sep 2016 14:12:02 +0000 (23:12 +0900)
committerCarsten Haitzler (Rasterman) <raster@rasterman.com>
Wed, 7 Sep 2016 15:09:31 +0000 (00:09 +0900)
new feature - recursive lock. same as normal lock just with recursive
attr set

@feature

src/lib/eina/eina_inline_lock_posix.x
src/lib/eina/eina_lock.h

index 20ff9ec..05babd5 100644 (file)
@@ -186,6 +186,35 @@ fail_release:
    return ok;
 }
 
+static inline Eina_Bool
+eina_lock_recursive_new(Eina_Lock *mutex)
+{
+   pthread_mutexattr_t attr;
+   Eina_Bool ok = EINA_FALSE;
+
+#ifdef EINA_HAVE_DEBUG_THREADS
+   if (!_eina_threads_activated)
+     assert(pthread_equal(_eina_main_loop, pthread_self()));
+#endif
+
+   if (pthread_mutexattr_init(&attr) != 0)
+     return EINA_FALSE;
+   if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
+     goto fail_release;
+#ifdef EINA_HAVE_DEBUG_THREADS
+   if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0)
+     goto fail_release;
+   memset(mutex, 0, sizeof(Eina_Lock));
+#endif
+   if (pthread_mutex_init(&(mutex->mutex), &attr) != 0)
+     goto fail_release;
+
+   ok = EINA_TRUE;
+fail_release:
+   pthread_mutexattr_destroy(&attr);
+   return ok;
+}
+
 static inline void
 eina_lock_free(Eina_Lock *mutex)
 {
index 7f53218..2a347ae 100644 (file)
@@ -120,6 +120,22 @@ EAPI extern Eina_Error EINA_ERROR_NOT_MAIN_LOOP;
 static inline Eina_Bool eina_lock_new(Eina_Lock *mutex);
 
 /**
+ * @brief Initializes a new #Eina_Lock that is recursive.
+ *
+ * @param[in] mutex The #Eina_Lock structure to be initialized
+ * @return #EINA_TRUE on success, #EINA_FALSE otherwise
+ *
+ * @details This function initializes an #Eina_Lock with appropriate values.
+ *          These values are platform dependent as is the structure of the
+ *          #Eina_Lock itself.
+ *
+ * @see eina_lock_new()
+ * @see eina_lock_free()
+ * @since 1.19
+ */
+static inline Eina_Bool eina_lock_recursive_new(Eina_Lock *mutex);
+
+/**
  * @brief Deallocates an #Eina_Lock
  *
  * @param[in] mutex The #Eina_Lock structure to be deallocated