Mutex.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 
3 /****************************************************************************
4 ** Copyright (c) 2001-2014
5 **
6 ** This file is part of the QuickFIX FIX Engine
7 **
8 ** This file may be distributed under the terms of the quickfixengine.org
9 ** license as defined by quickfixengine.org and appearing in the file
10 ** LICENSE included in the packaging of this file.
11 **
12 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
13 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 **
15 ** See http://www.quickfixengine.org/LICENSE for licensing information.
16 **
17 ** Contact ask@quickfixengine.org if any conditions of this licensing are
18 ** not clear to you.
19 **
20 ****************************************************************************/
21 
22 #ifndef FIX_MUTEX_H
23 #define FIX_MUTEX_H
24 
25 #include "Utility.h"
26 
27 namespace FIX
28 {
30 class Mutex
31 {
32 public:
33  Mutex()
34  {
35 #ifdef _MSC_VER
36  InitializeCriticalSection( &m_mutex );
37 #else
38  m_count = 0;
39  m_threadID = 0;
40  //pthread_mutexattr_t attr;
41  //pthread_mutexattr_init(&attr);
42  //pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
43  //pthread_mutex_init(&m_mutex, &attr);
44  pthread_mutex_init( &m_mutex, 0 );
45 #endif
46  }
47 
48  ~Mutex()
49  {
50 #ifdef _MSC_VER
51  DeleteCriticalSection( &m_mutex );
52 #else
53  pthread_mutex_destroy( &m_mutex );
54 #endif
55  }
56 
57  void lock()
58  {
59 #ifdef _MSC_VER
60  EnterCriticalSection( &m_mutex );
61 #else
62  if ( m_count && m_threadID == pthread_self() )
63  { ++m_count; return ; }
64  pthread_mutex_lock( &m_mutex );
65  ++m_count;
66  m_threadID = pthread_self();
67 #endif
68  }
69 
70  void unlock()
71  {
72 #ifdef _MSC_VER
73  LeaveCriticalSection( &m_mutex );
74 #else
75  if ( m_count > 1 )
76  { m_count--; return ; }
77  --m_count;
78  m_threadID = 0;
79  pthread_mutex_unlock( &m_mutex );
80 #endif
81  }
82 
83 private:
84 
85 #ifdef _MSC_VER
86  CRITICAL_SECTION m_mutex;
87 #else
88  pthread_mutex_t m_mutex;
89  pthread_t m_threadID;
90  int m_count;
91 #endif
92 };
93 
95 class Locker
96 {
97 public:
98  Locker( Mutex& mutex )
99  : m_mutex( mutex )
100  {
101  m_mutex.lock();
102  }
103 
105  {
106  m_mutex.unlock();
107  }
108 private:
109  Mutex& m_mutex;
110 };
111 
113 class ReverseLocker
114 {
115 public:
116  ReverseLocker( Mutex& mutex )
117  : m_mutex( mutex )
118  {
119  m_mutex.unlock();
120  }
121 
123  {
125  }
126 private:
127  Mutex& m_mutex;
128 };
129 }
130 
131 #endif
FIX::Mutex::lock
void lock()
Definition: Mutex.h:91
FIX::ReverseLocker::~ReverseLocker
~ReverseLocker()
Definition: Mutex.h:139
FIX::Mutex::Mutex
Mutex()
Definition: Mutex.h:67
FIX::Locker::~Locker
~Locker()
Definition: Mutex.h:121
FIX::Mutex
Portable implementation of a mutex.
Definition: Mutex.h:47
FIX::Mutex::m_threadID
pthread_t m_threadID
Definition: Mutex.h:123
FIX::Mutex::unlock
void unlock()
Definition: Mutex.h:104
FIX::ReverseLocker::ReverseLocker
ReverseLocker(Mutex &mutex)
Definition: Mutex.h:133
FIX::Mutex::m_mutex
pthread_mutex_t m_mutex
Definition: Mutex.h:122
FIX
Definition: Acceptor.cpp:34
FIX::Locker::m_mutex
Mutex & m_mutex
Definition: Mutex.h:126
FIX::Mutex::m_count
int m_count
Definition: Mutex.h:124
FIX::Locker
Locks/Unlocks a mutex using RAII.
Definition: Mutex.h:112
FIX::ReverseLocker::m_mutex
Mutex & m_mutex
Definition: Mutex.h:144
FIX::Mutex::~Mutex
~Mutex()
Definition: Mutex.h:82
Utility.h
FIX::Locker::Locker
Locker(Mutex &mutex)
Definition: Mutex.h:115

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001