Index Source Files Annotated Class List Alphabetical Class List Class Hierarchy Graphical Class Hierarchy

# Mutex.h

Go to the documentation of this file.

    00001 /* -*- C++ -*- */
    00002
    00003 /****************************************************************************
    00004 ** Copyright (c) quickfixengine.org  All rights reserved.
    00005 **
    00006 ** This file is part of the QuickFIX FIX Engine
    00007 **
    00008 ** This file may be distributed under the terms of the quickfixengine.org
    00009 ** license as defined by quickfixengine.org and appearing in the file
    00010 ** LICENSE included in the packaging of this file.
    00011 **
    00012 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    00013 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    00014 **
    00015 ** See http://www.quickfixengine.org/LICENSE for licensing information.
    00016 **
    00017 ** Contact ask@quickfixengine.org if any conditions of this licensing are
    00018 ** not clear to you.
    00019 **
    00020 ****************************************************************************/
    00021
    00022 #ifndef FIX_MUTEX_H
    00023 #define FIX_MUTEX_H
    00024
    00025 #include "Utility.h"
    00026
    00027 namespace FIX
    00028 {
    00030 class Mutex
    00031 {
    00032 public:
    00033   Mutex()
    00034   {
    00035 #ifdef _MSC_VER
    00036     InitializeCriticalSection( &m_mutex );
    00037 #else
    00038     m_count = 0;
    00039     m_threadID = 0;
    00040     //pthread_mutexattr_t attr;
    00041     //pthread_mutexattr_init(&attr);
    00042     //pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    00043     //pthread_mutex_init(&m_mutex, &attr);
    00044     pthread_mutex_init( &m_mutex, 0 );
    00045 #endif
    00046   }
    00047
    00048   ~Mutex()
    00049   {
    00050 #ifdef _MSC_VER
    00051     DeleteCriticalSection( &m_mutex );
    00052 #else
    00053     pthread_mutex_destroy( &m_mutex );
    00054 #endif
    00055   }
    00056
    00057   void lock()
    00058   {
    00059 #ifdef _MSC_VER
    00060     EnterCriticalSection( &m_mutex );
    00061 #else
    00062     if ( m_count && m_threadID == pthread_self() )
    00063     { ++m_count; return ; }
    00064     pthread_mutex_lock( &m_mutex );
    00065     ++m_count;
    00066     m_threadID = pthread_self();
    00067 #endif
    00068   }
    00069
    00070   void unlock()
    00071   {
    00072 #ifdef _MSC_VER
    00073     LeaveCriticalSection( &m_mutex );
    00074 #else
    00075     if ( m_count > 1 )
    00076     { m_count--; return ; }
    00077     --m_count;
    00078     m_threadID = 0;
    00079     pthread_mutex_unlock( &m_mutex );
    00080 #endif
    00081   }
    00082
    00083 private:
    00084
    00085 #ifdef _MSC_VER
    00086   CRITICAL_SECTION m_mutex;
    00087 #else
    00088   pthread_mutex_t m_mutex;
    00089   pthread_t m_threadID;
    00090   int m_count;
    00091 #endif
    00092 };
    00093
    00095 class Locker
    00096 {
    00097 public:
    00098   Locker( Mutex& mutex )
    00099   : m_mutex( mutex )
    00100   {
    00101     m_mutex.lock();
    00102   }
    00103
    00104   ~Locker()
    00105   {
    00106     m_mutex.unlock();
    00107   }
    00108 private:
    00109   Mutex& m_mutex;
    00110 };
    00111
    00113 class ReverseLocker
    00114 {
    00115 public:
    00116   ReverseLocker( Mutex& mutex )
    00117   : m_mutex( mutex )
    00118   {
    00119     m_mutex.unlock();
    00120   }
    00121
    00122   ~ReverseLocker()
    00123   {
    00124     m_mutex.lock();
    00125   }
    00126 private:
    00127   Mutex& m_mutex;
    00128 };
    00129 }
    00130
    00131 #endif