Eina: Add Eina_Lock API
authorcaro <caro>
Fri, 22 Apr 2011 21:26:36 +0000 (21:26 +0000)
committercaro <caro@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Fri, 22 Apr 2011 21:26:36 +0000 (21:26 +0000)
Eina_Lock API is a small set of functions to manage in a
cross platform way mutual exclusion objects

git-svn-id: http://svn.enlightenment.org/svn/e/trunk/eina@58835 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

ChangeLog
configure.ac
src/include/Eina.h
src/include/Makefile.am
src/include/eina_config.h.in
src/include/eina_inline_lock_posix.x [new file with mode: 0644]
src/include/eina_inline_lock_void.x [new file with mode: 0644]
src/include/eina_inline_lock_win32.x [new file with mode: 0644]
src/include/eina_inline_lock_wince.x [new file with mode: 0644]
src/include/eina_lock.h [new file with mode: 0644]

index 13e66e4..b68181b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
 
        * Add Simple XML parser API.
 
-2011-05-11  Cedric Bail
+2011-04-11  Cedric Bail
 
        * Add eina_inlist_sort.
        * Add eina_mempool_repack.
        * Add Eina_Object API.
 
-2011-05-13  Cedric Bail & Vincent Torri
+2011-04-13  Cedric Bail & Vincent Torri
 
        * Add Eina_File API.
 
+2011-04-22  Vincent Torri
+
+        * Add Eina_Lock API
+
index c260d03..a837459 100644 (file)
@@ -110,6 +110,12 @@ EFL_CHECK_THREADS(
    [have_threads="no"])
 EFL_CHECK_SPINLOCK([have_posix_threads_spinlock="yes"], [have_posix_threads_spinlock="no"])
 
+if ! test "x${have_threads}" = "xno" ; then
+   EINA_CONFIGURE_HAVE_THREADS="#define EINA_HAVE_THREADS"
+fi
+AC_SUBST(EINA_CONFIGURE_HAVE_THREADS)
+AM_CONDITIONAL([EINA_HAVE_THREADS], [! test "x${have_threads}" = "xno"])
+
 ### Additional options to configure
 
 # Magic debug
@@ -364,6 +370,7 @@ AC_C_INLINE
 AC_C___ATTRIBUTE__
 AC_PROG_CC_STDC
 
+have_wince="no"
 have_win32="no"
 EINA_CPPFLAGS=""
 EINA_CFLAGS=""
@@ -372,6 +379,7 @@ case "$host_os" in
       EINA_CPPFLAGS="-D_WIN32_WCE=0x0420"
       EINA_CFLAGS="${EVIL_CFLAGS}"
       have_win32="yes"
+      have_wince="yes"
       ;;
    mingw*)
       EINA_CPPFLAGS="-D_WIN32_WINNT=0x0501"
@@ -379,6 +387,7 @@ case "$host_os" in
       have_win32="yes"
       ;;
 esac
+AM_CONDITIONAL([EINA_HAVE_WINCE], [test "x$have_wince" = "xyes"])
 AM_CONDITIONAL([EINA_HAVE_WIN32], [test "x$have_win32" = "xyes"])
 
 m4_ifdef([v_mic],
index 7c2dfc2..efeefc4 100644 (file)
@@ -159,6 +159,7 @@ extern "C" {
 #include "eina_quadtree.h"
 #include "eina_simple_xml_parser.h"
 #include "eina_object.h"
+#include "eina_lock.h"
 
 #ifdef __cplusplus
 }
index 5bfbab1..5da8fe8 100644 (file)
@@ -54,7 +54,22 @@ eina_ustrbuf.h \
 eina_unicode.h \
 eina_quadtree.h \
 eina_simple_xml_parser.h \
-eina_object.h
+eina_object.h \
+eina_lock.h
+
+if EINA_HAVE_THREADS
+if EINA_HAVE_WINCE
+EINAHEADERS += eina_inline_lock_wince.x
+else
+if EINA_HAVE_WIN32
+EINAHEADERS += eina_inline_lock_win32.x
+else
+EINAHEADERS += eina_inline_lock_posix.x
+endif
+endif
+else
+EINAHEADERS += eina_inline_lock_void.x
+endif
 
 installed_mainheaderdir = $(includedir)/eina-@VMAJ@
 dist_installed_mainheader_DATA = Eina.h eina_config.h
index 56331bb..6a94f95 100644 (file)
 #endif
 @EINA_CONFIGURE_HAVE_STDINT_H@
 
+#ifdef EINA_HAVE_THREADS
+# undef EINA_HAVE_THREADS
+#endif
+@EINA_CONFIGURE_HAVE_THREADS@
+
 #ifdef EINA_SIZEOF_WCHAR_T
 # undef EINA_SIZEOF_WCHAR_T
 #endif
diff --git a/src/include/eina_inline_lock_posix.x b/src/include/eina_inline_lock_posix.x
new file mode 100644 (file)
index 0000000..8151a28
--- /dev/null
@@ -0,0 +1,58 @@
+/* EINA - EFL data type library
+ * Copyright (C) 2011 Vincent Torri
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library;
+ * if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef EINA_INLINE_LOCK_POSIX_X_
+#define EINA_INLINE_LOCK_POSIX_X_
+
+#include <pthread.h>
+
+
+typedef pthread_mutex_t Eina_Lock;
+
+static inline Eina_Bool
+eina_lock_new(Eina_Lock *mutex)
+{
+   return (pthread_mutex_init(mutex, NULL) == 0) ? EINA_TRUE : EINA_FALSE;
+}
+
+static inline void
+eina_lock_free(Eina_Lock mutex)
+{
+   pthread_mutex_destroy(&mutex);
+}
+
+static inline Eina_Bool
+eina_lock_take(Eina_Lock mutex)
+{
+   return (pthread_mutex_lock(&mutex) == 0) ? EINA_TRUE : EINA_FALSE;
+}
+
+static inline Eina_Bool
+eina_lock_take_try(Eina_Lock mutex)
+{
+   return (pthread_mutex_trylock(&mutex) == 0) ? EINA_TRUE : EINA_FALSE;
+}
+
+static inline Eina_Bool
+eina_lock_release(Eina_Lock mutex)
+{
+   return (pthread_mutex_unlock(&mutex) == 0) ? EINA_TRUE : EINA_FALSE;
+}
+
+
+#endif
diff --git a/src/include/eina_inline_lock_void.x b/src/include/eina_inline_lock_void.x
new file mode 100644 (file)
index 0000000..2d605a4
--- /dev/null
@@ -0,0 +1,131 @@
+/* EINA - EFL data type library
+ * Copyright (C) 2011 Vincent Torri
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library;
+ * if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef EINA_INLINE_LOCK_VOID_X_
+#define EINA_INLINE_LOCK_VOID_X_
+
+
+/**
+ * @addtogroup Eina_Lock_Group Lock
+ *
+ * @brief These functions provide Mutual Exclusion objects management.
+ *
+ * @note On Windows XP, critical sections are used, while on Windows
+ * CE, standard Mutex objects are used.
+ *
+ * @{
+ */
+
+/**
+ * @typedef Eina_Lock
+ * Abtract type for a mutual exclusive object.
+ */
+typedef void *Eina_Lock;
+
+/**
+ * @brief Create a new #Eina_Lock.
+ *
+ * @param mutex A pointer to the lock object.
+ * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
+ *
+ * This function creates a new #Eina_Lock object and stores it in the
+ * @p mutex buffer. On success, this function returns #EINA_TRUE and
+ * #EINA_FALSE otherwise. To free the resources allocated by this
+ * function, use eina_lock_free(). For performance reasons, no check
+ * is done on @p mutex.
+ */
+static inline Eina_Bool
+eina_lock_new(Eina_Lock *mutex)
+{
+   return EINA_FALSE;
+}
+
+/**
+ * @brief Free the ressources of the given lock object.
+ *
+ * @param mutex The lock object to free.
+ *
+ * This function frees the resources of @p mutex allocated by
+ * eina_lock_new(). For performance reasons, no check is done on
+ * @p mutex.
+ */
+static inline void
+eina_lock_free(Eina_Lock mutex)
+{
+}
+
+/**
+ * @brief Lock the given mutual exclusion object.
+ *
+ * @param mutex The lock object to lock.
+ * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
+ *
+ * This function locks @p mutex. @p mutex must have been created by
+ * eina_lock_new(). On success, this function returns #EINA_TRUE and
+ * #EINA_FALSE otherwise. For performance reasons, no check is done on
+ * @p mutex.
+ */
+static inline Eina_Bool
+eina_lock_take(Eina_Lock mutex)
+{
+   return EINA_FALSE;
+}
+
+/**
+ * @brief Try to lock the given mutual exclusion object.
+ *
+ * @param mutex The lock object to try to lock.
+ * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
+ *
+ * This function tries to lock @p mutex. @p mutex must have been created by
+ * eina_lock_new(). If @p mutex can be locked, this function returns
+ * #EINA_TRUE; if @p mutex can not be locked, or is already locked, it
+ * returns #EINA_FALSE. This function does not block and returns
+ * immediatly. For performance reasons, no check is done on
+ * @p mutex.
+ *
+ * @note On Windows CE, this function is actually eina_lock_take().
+ */
+static inline Eina_Bool
+eina_lock_take_try(Eina_Lock mutex)
+{
+   return EINA_FALSE;
+}
+
+/**
+ * @brief Unlock the given mutual exclusion object.
+ *
+ * @param mutex The lock object to unlock.
+ * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
+ *
+ * This function unlocks @p mutex. @p mutex must have been created by
+ * eina_lock_new(). On success, this function returns #EINA_TRUE and
+ * #EINA_FALSE otherwise. For performance reasons, no check is done on
+ * @p mutex.
+ */
+static inline Eina_Bool
+eina_lock_release(Eina_Lock mutex)
+{
+   return EINA_FALSE;
+}
+
+/**
+ * @}
+ */
+
+#endif
diff --git a/src/include/eina_inline_lock_win32.x b/src/include/eina_inline_lock_win32.x
new file mode 100644 (file)
index 0000000..65bc177
--- /dev/null
@@ -0,0 +1,64 @@
+/* EINA - EFL data type library
+ * Copyright (C) 2011 Vincent Torri
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library;
+ * if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef EINA_INLINE_LOCK_WIN32_X_
+#define EINA_INLINE_LOCK_WIN32_X_
+
+#include <windows.h>
+
+
+typedef CRITICAL_SECTION Eina_Lock;
+
+static inline Eina_Bool
+eina_lock_new(Eina_Lock *mutex)
+{
+   InitializeCriticalSection(m);
+
+   return EINA_TRUE;
+}
+
+static inline void
+eina_lock_free(Eina_Lock mutex)
+{
+   DeleteCriticalSection(&mutex);
+}
+
+static inline Eina_Bool
+eina_lock_take(Eina_Lock mutex)
+{
+   EnterCriticalSection(&mutex);
+
+   return EINA_TRUE;
+}
+
+static inline Eina_Bool
+eina_lock_take_try(Eina_Lock mutex)
+{
+   return TryEnterCriticalSection(&mutex) == 0 ? EINA_FALSE : EINA_TRUE;
+}
+
+static inline Eina_Bool
+eina_lock_release(Eina_Lock mutex)
+{
+   LeaveCriticalSection(&mutex);
+
+   return EINA_TRUE;
+}
+
+
+#endif
diff --git a/src/include/eina_inline_lock_wince.x b/src/include/eina_inline_lock_wince.x
new file mode 100644 (file)
index 0000000..c9ab082
--- /dev/null
@@ -0,0 +1,64 @@
+/* EINA - EFL data type library
+ * Copyright (C) 2011 Vincent Torri
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library;
+ * if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef EINA_INLINE_LOCK_WIN32_X_
+#define EINA_INLINE_LOCK_WIN32_X_
+
+#include <windows.h>
+
+
+typedef HANDLE Eina_Lock;
+
+static inline Eina_Bool
+eina_lock_new(Eina_Lock *mutex)
+{
+   Eina_Lock m;
+
+   m = CreateMutex(NULL, FALSE, NULL);
+   if (m) *mutex = m;
+   return (m != NULL);
+}
+
+static inline void
+eina_lock_free(Eina_Lock mutex)
+{
+   CloseHandle(mutex);
+}
+
+static inline Eina_Bool
+eina_lock_take(Eina_Lock mutex)
+{
+   DWORD res;
+
+   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_release(Eina_Lock mutex)
+{
+   return ReleaseMutex(mutex);
+}
+
+
+#endif
diff --git a/src/include/eina_lock.h b/src/include/eina_lock.h
new file mode 100644 (file)
index 0000000..45133fc
--- /dev/null
@@ -0,0 +1,56 @@
+/* EINA - EFL data type library
+ * Copyright (C) 2011 Vincent Torri
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library;
+ * if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef EINA_ARRAY_H_
+#define EINA_ARRAY_H_
+
+#include "eina_config.h"
+
+/**
+ * @addtogroup Eina_Tools_Group Tools
+ *
+ * @{
+ */
+
+/**
+ * @defgroup Eina_File_Group File
+ *
+ * @{
+ */
+
+#ifdef EINA_HAVE_THREADS
+# ifdef _WIN32_WCE
+#  include "eina_inline_lock_wince.x"
+# elif _WIN32
+#  include "eina_inline_lock_win32.x"
+# else
+#  include "eina_inline_lock_posix.x"
+# endif
+#else
+# include "eina_inline_lock_void.x"
+#endif
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+#endif