version 0.1.3
[fms.git] / src / pthreadwrapper / mutex.cpp
diff --git a/src/pthreadwrapper/mutex.cpp b/src/pthreadwrapper/mutex.cpp
new file mode 100644 (file)
index 0000000..ca554b1
--- /dev/null
@@ -0,0 +1,42 @@
+#include "../../include/pthreadwrapper/mutex.h"\r
+#include <exception>\r
+\r
+#ifdef XMEM\r
+       #include <xmem.h>\r
+#endif\r
+\r
+namespace PThread\r
+{\r
+\r
+Mutex::Mutex()\r
+{\r
+       pthread_mutexattr_init(&m_attr);\r
+       pthread_mutexattr_settype(&m_attr,PTHREAD_MUTEX_ERRORCHECK);\r
+       pthread_mutex_init(&m_mutex,&m_attr);\r
+}\r
+\r
+Mutex::~Mutex()\r
+{\r
+       pthread_mutex_destroy(&m_mutex);\r
+       pthread_mutexattr_destroy(&m_attr);\r
+}\r
+\r
+void Mutex::Acquire() throw(std::exception)\r
+{\r
+       int rval=0;\r
+       if((rval=pthread_mutex_lock(&m_mutex))!=0)\r
+       {\r
+               // deadlock - throw exception\r
+               if(rval==EDEADLK)\r
+               {\r
+                       throw std::exception("Mutex would deadlock!");\r
+               }\r
+       }\r
+}\r
+\r
+void Mutex::Release()\r
+{\r
+       pthread_mutex_unlock(&m_mutex);\r
+}\r
+\r
+}      // namespace\r