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
27namespace FIX
28{
30class Mutex
31{
32public:
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
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
83private:
84
85#ifdef _MSC_VER
86 CRITICAL_SECTION m_mutex;
87#else
88 pthread_mutex_t m_mutex;
89 pthread_t m_threadID;
91#endif
92};
93
95class Locker
96{
97public:
98 Locker( Mutex& mutex )
99 : m_mutex( mutex )
100 {
101 m_mutex.lock();
102 }
103
105 {
106 m_mutex.unlock();
107 }
108private:
110};
111
114{
115public:
117 : m_mutex( mutex )
118 {
119 m_mutex.unlock();
120 }
121
123 {
124 m_mutex.lock();
125 }
126private:
128};
129}
130
131#endif
Locks/Unlocks a mutex using RAII.
Definition Mutex.h:96
Locker(Mutex &mutex)
Definition Mutex.h:98
Mutex & m_mutex
Definition Mutex.h:109
Portable implementation of a mutex.
Definition Mutex.h:31
~Mutex()
Definition Mutex.h:48
pthread_mutex_t m_mutex
Definition Mutex.h:88
void unlock()
Definition Mutex.h:70
void lock()
Definition Mutex.h:57
int m_count
Definition Mutex.h:90
Mutex()
Definition Mutex.h:33
pthread_t m_threadID
Definition Mutex.h:89
Does the opposite of the Locker to ensure mutex ends up in a locked state.
Definition Mutex.h:114
Mutex & m_mutex
Definition Mutex.h:127
ReverseLocker(Mutex &mutex)
Definition Mutex.h:116

Generated on Sat Feb 3 2024 04:23:15 for QuickFIX by doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2001