]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add cross platform mutex abstraction.
authorMichael Jerris <mike@jerris.com>
Wed, 28 Feb 2007 00:28:23 +0000 (00:28 +0000)
committerMichael Jerris <mike@jerris.com>
Wed, 28 Feb 2007 00:28:23 +0000 (00:28 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@4409 d0543943-73ff-0310-b7d9-9358b9ac24b2

libs/iax/libiax2.vcproj
libs/iax/src/Makefile.am
libs/iax/src/iax-mutex.c [new file with mode: 0644]
libs/iax/src/iax-mutex.h [new file with mode: 0644]

index 5a5a5fa4ce74e05789d99a4840417cb02e3d4792..ce7a04c30fdc08927709638c5b20e10e087af5e7 100644 (file)
                        Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"\r
                        UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"\r
                        >\r
+                       <File\r
+                               RelativePath=".\src\iax-mutex.c"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath=".\src\iax.c"\r
                                >\r
                                RelativePath=".\src\iax-client.h"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath=".\src\iax-mutex.h"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath=".\src\iax.h"\r
                                >\r
index ba87808bc0bb620a03d49049c0c6d47f46c62926..0f7f1b8b3eb8566b837e6eceb262aaa44ccf6096 100644 (file)
@@ -8,8 +8,8 @@ AM_CFLAGS += $(UCFLAGS)
 
 pkgdir = $(libdir)
 pkg_LTLIBRARIES=libiax.la
-libiax_la_SOURCES = iax2-parser.c iax.c md5.c jitterbuf.c
+libiax_la_SOURCES = iax2-parser.c iax.c md5.c jitterbuf.c iax-mutex.c
 library_includedir = $(prefix)/include/iax
-library_include_HEADERS = md5.h frame.h iax-client.h iax2.h iax2-parser.h iax.h
+library_include_HEADERS = md5.h frame.h iax-client.h iax2.h iax2-parser.h iax.h iax-mutex.h
 noinst_HEADERS = jitterbuf.h
 
diff --git a/libs/iax/src/iax-mutex.c b/libs/iax/src/iax-mutex.c
new file mode 100644 (file)
index 0000000..6fd82e7
--- /dev/null
@@ -0,0 +1,105 @@
+/* 
+ * Simple Mutex abstraction
+ * Copyright(C) 2007 Michael Jerris
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so.
+ *\r
+ * This work is provided under this license on an "as is" basis, without warranty of any kind,\r
+ * either expressed or implied, including, without limitation, warranties that the covered code\r
+ * is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire\r
+ * risk as to the quality and performance of the covered code is with you. Should any covered\r
+ * code prove defective in any respect, you (not the initial developer or any other contributor)\r
+ * assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty\r
+ * constitutes an essential part of this license. No use of any covered code is authorized hereunder\r
+ * except under this disclaimer. \r
+ *
+ */
+
+
+#include "iax-mutex.h"
+
+
+#ifdef WIN32
+#define _WIN32_WINNT 0x0400
+#include <windows.h>
+struct mutex {
+       CRITICAL_SECTION mutex;
+};
+
+#else
+
+#include <pthread.h>
+struct mutex {
+       pthread_mutex_t mutex;
+};
+
+#endif
+
+
+mutex_status_t mutex_create(mutex_t **mutex)\r
+{\r
+       mutex_t *check = NULL;\r
+\r
+       check = (mutex_t *)malloc(sizeof(**mutex));\r
+       if (!check)\r
+               return MUTEX_FAILURE;\r
+#ifdef WIN32\r
+       InitializeCriticalSection(&check->mutex);\r
+#else\r
+       if (pthread_mutex_init(&check->mutex, NULL))\r
+               return MUTEX_FAILURE;\r
+\r
+#endif\r
+\r
+       *mutex = check;\r
+\r
+       return MUTEX_SUCCESS;\r
+}\r
+\r
+mutex_status_t mutex_destroy(mutex_t *mutex)\r
+{\r
+#ifdef WIN32\r
+       DeleteCriticalSection(&mutex->mutex);\r
+#else\r
+       if (pthread_mutex_destroy(&mutex->mutex))\r
+               return MUTEX_FAILURE;\r
+#endif\r
+       free(mutex);\r
+       return MUTEX_SUCCESS;\r
+}\r
+\r
+mutex_status_t mutex_lock(mutex_t *mutex)\r
+{\r
+#ifdef WIN32\r
+       EnterCriticalSection(&mutex->mutex);\r
+#else\r
+       if (pthread_mutex_lock(&mutex->mutex))\r
+               return MUTEX_FAILURE;\r
+#endif\r
+       return MUTEX_SUCCESS;\r
+}\r
+\r
+mutex_status_t mutex_trylock(mutex_t *mutex)\r
+{\r
+#ifdef WIN32\r
+       if (!TryEnterCriticalSection(&mutex->mutex))\r
+               return MUTEX_FAILURE;\r
+#else\r
+       if (pthread_mutex_trylock(&mutex->mutex))\r
+               return MUTEX_FAILURE;\r
+#endif\r
+       return MUTEX_SUCCESS;\r
+}\r
+\r
+mutex_status_t mutex_unlock(mutex_t *mutex)\r
+{\r
+#ifdef WIN32\r
+       LeaveCriticalSection(&mutex->mutex);\r
+#else\r
+       if (pthread_mutex_unlock(&mutex->mutex))\r
+               return MUTEX_FAILURE;\r
+#endif\r
+       return MUTEX_SUCCESS;\r
+}\r
diff --git a/libs/iax/src/iax-mutex.h b/libs/iax/src/iax-mutex.h
new file mode 100644 (file)
index 0000000..1c0f407
--- /dev/null
@@ -0,0 +1,37 @@
+/* 
+ * Simple Mutex abstraction
+ * Copyright(C) 2007 Michael Jerris
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so.
+ *\r
+ * This work is provided under this license on an "as is" basis, without warranty of any kind,\r
+ * either expressed or implied, including, without limitation, warranties that the covered code\r
+ * is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire\r
+ * risk as to the quality and performance of the covered code is with you. Should any covered\r
+ * code prove defective in any respect, you (not the initial developer or any other contributor)\r
+ * assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty\r
+ * constitutes an essential part of this license. No use of any covered code is authorized hereunder\r
+ * except under this disclaimer. \r
+ *
+ */
+\r
+\r
+#ifndef _SIMPLE_ABSTRACT_MUTEX_H\r
+#define _SIMPLE_ABSTRACT_MUTEX_H\r
+\r
+typedef struct mutex mutex_t;\r
+\r
+typedef enum mutex_status {\r
+       MUTEX_SUCCESS,\r
+       MUTEX_FAILURE\r
+} mutex_status_t;\r
+\r
+mutex_status_t mutex_create(mutex_t **mutex);\r
+mutex_status_t mutex_destroy(mutex_t *mutex);\r
+mutex_status_t mutex_lock(mutex_t *mutex);\r
+mutex_status_t mutex_trylock(mutex_t *mutex);\r
+mutex_status_t mutex_unlock(mutex_t *mutex);\r
+\r
+#endif\r