Permalink
Browse files

Pimpl idiom for Mutex class.

+ Added implementation for Windows with critical sections.
+ Added implementation with pthreads (for Unix like platforms).
  • Loading branch information...
1 parent 1379d1d commit 182e839fb1e2179d376a3cd70c7248ba3a6ad6e8 @dacap committed May 10, 2010
Showing with 155 additions and 6 deletions.
  1. +2 −1 include/Vaca/Mutex.h
  2. +13 −5 src/Mutex.cpp
  3. +67 −0 src/unix/MutexImpl.h
  4. +73 −0 src/win32/MutexImpl.h
View
3 include/Vaca/Mutex.h
@@ -54,7 +54,8 @@ namespace Vaca {
*/
class VACA_DLL Mutex : private NonCopyable
{
- CRITICAL_SECTION m_cs;
+ class MutexImpl;
+ MutexImpl* m_pimpl;
public:
View
18 src/Mutex.cpp
@@ -31,6 +31,14 @@
#include "Vaca/Mutex.h"
+#if defined(VACA_ON_WINDOWS)
+ #include "win32/MutexImpl.h"
+#elif defined(VACA_ON_UNIXLIKE)
+ #include "unix/MutexImpl.h"
+#else
+ #error Your platform does not support mutexes
+#endif
+
using namespace Vaca;
/**
@@ -42,7 +50,7 @@ using namespace Vaca;
*/
Mutex::Mutex()
{
- InitializeCriticalSection(&m_cs);
+ m_pimpl = new MutexImpl();
}
/**
@@ -54,7 +62,7 @@ Mutex::Mutex()
*/
Mutex::~Mutex()
{
- DeleteCriticalSection(&m_cs);
+ delete m_pimpl;
}
/**
@@ -71,7 +79,7 @@ Mutex::~Mutex()
*/
void Mutex::lock()
{
- EnterCriticalSection(&m_cs);
+ return m_pimpl->lock();
}
/**
@@ -85,7 +93,7 @@ void Mutex::lock()
*/
bool Mutex::tryLock()
{
- return TryEnterCriticalSection(&m_cs) ? true: false;
+ return m_pimpl->tryLock();
}
/**
@@ -103,5 +111,5 @@ bool Mutex::tryLock()
*/
void Mutex::unlock()
{
- LeaveCriticalSection(&m_cs);
+ return m_pimpl->unlock();
}
View
67 src/unix/MutexImpl.h
@@ -0,0 +1,67 @@
+// Vaca - Visual Application Components Abstraction
+// Copyright (c) 2005-2009 David Capello
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions
+// are met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in
+// the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of the author nor the names of its contributors
+// may be used to endorse or promote products derived from this
+// software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+// OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include <pthread.h>
+#include <errno.h>
+
+class Vaca::Mutex::MutexImpl
+{
+ pthread_mutex_t m_handle;
+
+public:
+
+ MutexImpl()
+ {
+ pthread_mutex_init(&m_handle, NULL);
+ }
+
+ ~MutexImpl()
+ {
+ pthread_mutex_destroy(&m_handle);
+ }
+
+ void lock()
+ {
+ pthread_mutex_lock(&m_handle);
+ }
+
+ bool tryLock()
+ {
+ return pthread_mutex_trylock(&m_handle) != EBUSY;
+ }
+
+ void unlock()
+ {
+ pthread_mutex_unlock(&m_handle);
+ }
+
+};
+
View
73 src/win32/MutexImpl.h
@@ -0,0 +1,73 @@
+// Vaca - Visual Application Components Abstraction
+// Copyright (c) 2005-2009 David Capello
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions
+// are met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in
+// the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of the author nor the names of its contributors
+// may be used to endorse or promote products derived from this
+// software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+// OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0400
+#endif
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+class Vaca::Mutex::MutexImpl
+{
+ CRITICAL_SECTION m_handle;
+
+public:
+
+ MutexImpl()
+ {
+ InitializeCriticalSection(&m_handle);
+ }
+
+ ~MutexImpl()
+ {
+ DeleteCriticalSection(&m_handle);
+ }
+
+ void lock()
+ {
+ EnterCriticalSection(&m_handle);
+ }
+
+ bool tryLock()
+ {
+#if(_WIN32_WINNT >= 0x0400)
+ return TryEnterCriticalSection(&m_handle) ? true: false;
+#else
+ return false;
+#endif
+ }
+
+ void unlock()
+ {
+ LeaveCriticalSection(&m_handle);
+ }
+
+};

0 comments on commit 182e839

Please sign in to comment.