eina: add Eina_Semaphore API.
authorcedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Fri, 30 Dec 2011 13:38:53 +0000 (13:38 +0000)
committercedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Fri, 30 Dec 2011 13:38:53 +0000 (13:38 +0000)
Patch by Vincent Torri.

git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@66693 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

ChangeLog
NEWS
src/include/eina_inline_lock_posix.x
src/include/eina_inline_lock_void.x
src/include/eina_inline_lock_win32.x
src/include/eina_inline_lock_wince.x
src/include/eina_lock.h

index a968f31..638b828 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
 2011-12-28  Cedric Bail
 
        * Fix NONNULL argument for eina_hash_find.
+
+2011-12-30  Vincent Torri
+
+       * Add Eina_Semaphore abstraction API.
diff --git a/NEWS b/NEWS
index ca39236..5e042e9 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,7 @@ Changes since Eina 1.1.0:
 Additions:
 
     * eina_mempool_calloc
+    * Eina_Semaphore abstraction API
 
 
 Eina 1.1.0
index 1dc59aa..64e049a 100644 (file)
 #ifndef EINA_INLINE_LOCK_POSIX_X_
 #define EINA_INLINE_LOCK_POSIX_X_
 
+#ifdef EINA_UNUSED
+# undef EINA_UNUSED
+#endif
+#ifdef __GNUC__
+# define EINA_UNUSED __attribute__((unused))
+#else
+# define EINA_UNUSED
+#endif
+
 #include <errno.h>
 #ifndef __USE_UNIX98
 # define __USE_UNIX98
@@ -28,6 +37,8 @@
 # include <pthread.h>
 #endif
 
+#include <semaphore.h>
+
 #include <sys/time.h>
 #include <stdio.h>
 
@@ -46,6 +57,7 @@ typedef struct _Eina_Lock Eina_Lock;
 typedef struct _Eina_RWLock Eina_RWLock;
 typedef struct _Eina_Condition Eina_Condition;
 typedef pthread_key_t Eina_TLS;
+typedef sem_t Eina_Semaphore;
 
 struct _Eina_Lock
 {
@@ -505,4 +517,40 @@ eina_tls_set(Eina_TLS key, const void *data)
    return EINA_TRUE;
 }
 
+static inline Eina_Bool
+eina_semaphore_new(Eina_Semaphore *sem, int count_init)
+{
+   if (!sem || (count_init <= 0))
+     return EINA_FALSE;
+
+   return (sem_init(sem, count_init, 1) == 0) ? EINA_TRUE : EINA_FALSE;
+}
+
+static inline Eina_Bool
+eina_semaphore_free(Eina_Semaphore *sem)
+{
+   if (!sem)
+     return EINA_FALSE;
+
+   return (sem_destroy(sem) == 0) ? EINA_TRUE : EINA_FALSE;
+}
+
+static inline Eina_Bool
+eina_semaphore_lock(Eina_Semaphore *sem)
+{
+   if (!sem)
+     return EINA_FALSE;
+
+   return (sem_wait(sem) == 0) ? EINA_TRUE : EINA_FALSE;
+}
+
+static inline Eina_Bool
+eina_semaphore_release(Eina_Semaphore *sem, int count_release EINA_UNUSED)
+{
+   if (!sem)
+     return EINA_FALSE;
+
+   return (sem_post(sem) == 0) ? EINA_TRUE : EINA_FALSE;
+}
+
 #endif
index 8cb9a49..64adcf7 100644 (file)
@@ -47,6 +47,7 @@ typedef void *Eina_Lock;
 typedef void *Eina_RWLock;
 typedef void *Eina_Condition;
 typedef void *Eina_TLS;
+typedef void *Eina_Semaphore;
 
 /**
  * @brief Create a new #Eina_Lock.
@@ -239,6 +240,31 @@ eina_tls_set(Eina_TLS key EINA_UNUSED, const void *data EINA_UNUSED)
    return EINA_FALSE;
 }
 
+static inline Eina_Bool
+eina_semaphore_new(Eina_Semaphore *sem EINA_UNUSED,
+                   int count_init EINA_UNUSED)
+{
+   return EINA_FALSE;
+}
+
+static inline Eina_Bool
+eina_semaphore_free(Eina_Semaphore *sem EINA_UNUSED)
+{
+   return EINA_FALSE;
+}
+
+static inline Eina_Bool
+eina_semaphore_lock(Eina_Semaphore *sem EINA_UNUSED)
+{
+   return EINA_FALSE;
+}
+
+static inline Eina_Bool
+eina_semaphore_release(Eina_Semaphore *sem EINA_UNUSED,
+                       int count_release EINA_UNUSED)
+{
+   return EINA_FALSE;
+}
 
 /**
  * @}
index 072095c..7677260 100644 (file)
@@ -58,6 +58,8 @@ struct _Eina_Win32_RWLock
 
 typedef DWORD     Eina_TLS;
 
+typedef HANDLE    Eina_Semaphore;
+
 EAPI extern Eina_Bool _eina_threads_activated;
 
 static inline Eina_Bool
@@ -463,4 +465,48 @@ eina_tls_set(Eina_TLS key, const void *data)
    return EINA_TRUE;
 }
 
+static inline Eina_Bool
+eina_semaphore_new(Eina_Semaphore *sem, int count_init)
+{
+   if (!sem || (count_init <= 0))
+     return EINA_FALSE;
+
+   *sem = CreateSemaphore(NULL, count_init, 32767, NULL);
+   if (!*sem)
+     return EINA_FALSE;
+}
+
+static inline Eina_Bool
+eina_semaphore_free(Eina_Semaphore *sem)
+{
+  if (!sem)
+     return EINA_FALSE;
+
+  CloseHandle(*sem);
+}
+
+static inline Eina_Bool
+eina_semaphore_lock(Eina_Semaphore *sem)
+{
+   DWORD res;
+
+   if (!sem)
+     return EINA_FALSE;
+
+   res = WaitForSingleObject(ev->shared->lock, 0L);
+   if (res == WAIT_OBJECT_0)
+     return EINA_TRUE;
+
+   return EINA_FALSE;
+}
+
+static inline Eina_Bool
+eina_semaphore_release(Eina_Semaphore *sem, int count_release)
+{
+   if (!sem)
+     return EINA_FALSE;
+
+   return ReleaseSemaphore(*sem, count_release, NULL) ? EINA_TRUE : EINA_FALSE;
+}
+
 #endif
index 965d475..1af1aac 100644 (file)
 #ifndef EINA_INLINE_LOCK_WIN32_X_
 #define EINA_INLINE_LOCK_WIN32_X_
 
+#ifdef EINA_UNUSED
+# undef EINA_UNUSED
+#endif
+#ifdef __GNUC__
+# define EINA_UNUSED __attribute__((unused))
+#else
+# define EINA_UNUSED
+#endif
+
 #include <windows.h>
 
 EAPI extern Eina_Bool _threads_activated;
@@ -26,6 +35,7 @@ EAPI extern Eina_Bool _threads_activated;
 typedef HANDLE    Eina_Lock;
 typedef Eina_Lock Eina_RWLock;
 typedef DWORD     Eina_TLS;
+typedef void *    Eina_Semaphore;
 
 static inline Eina_Bool
 eina_lock_new(Eina_Lock *mutex)
@@ -173,6 +183,30 @@ eina_tls_set(Eina_TLS key, const void *data)
    return EINA_TRUE;
 }
 
+static inline Eina_Bool
+eina_semaphore_new(Eina_Semaphore *sem EINA_UNUSED,
+                   int count_init EINA_UNUSED)
+{
+   return EINA_FALSE;
+}
 
+static inline Eina_Bool
+eina_semaphore_free(Eina_Semaphore *sem EINA_UNUSED)
+{
+   return EINA_FALSE;
+}
+
+static inline Eina_Bool
+eina_semaphore_lock(Eina_Semaphore *sem EINA_UNUSED)
+{
+   return EINA_FALSE;
+}
+
+static inline Eina_Bool
+eina_semaphore_release(Eina_Semaphore *sem EINA_UNUSED,
+                       int count_release EINA_UNUSED)
+{
+   return EINA_FALSE;
+}
 
 #endif
index 7c26dc0..16f4314 100644 (file)
@@ -81,6 +81,10 @@ static inline void eina_tls_free(Eina_TLS key);
 static inline void *eina_tls_get(Eina_TLS key);
 static inline Eina_Bool eina_tls_set(Eina_TLS key, const void *data);
 
+static inline Eina_Bool eina_semaphore_new(Eina_Semaphore *sem, int count_init);
+static inline Eina_Bool eina_semaphore_free(Eina_Semaphore *sem);
+static inline Eina_Bool eina_semaphore_lock(Eina_Semaphore *sem);
+static inline Eina_Bool eina_semaphore_release(Eina_Semaphore *sem, int count_release);
 
 #ifdef EINA_HAVE_DEBUG_THREADS
 # define EINA_MAIN_LOOP_CHECK_RETURN_VAL(val)                          \