EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Mutex.h
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24 */
25 
26 #ifndef __THREADING__MUTEX_H__INCL__
27 #define __THREADING__MUTEX_H__INCL__
28 
29 #include "utils/Lock.h"
30 
36 class Mutex
37 : public Lockable
38 {
39 public:
43  Mutex();
47  ~Mutex();
48 
52  void Lock();
59  bool TryLock();
60 
64  void Unlock();
65 
66 protected:
67 #ifdef HAVE_WINDOWS_H
68  CRITICAL_SECTION mCriticalSection;
70 #else /* !HAVE_WINDOWS_H */
71  pthread_mutex_t mMutex;
73 #endif /* !HAVE_WINDOWS_H */
74 };
75 
78 
79 // Somewhat untested...
80 // Multi-read, single write Mutex
81 class MRMutex {
82 public:
83  MRMutex();
84  ~MRMutex();
85 
86  void ReadLock();
87  bool TryReadLock();
88  void UnReadLock();
89 
90  void WriteLock();
91  bool TryWriteLock();
92  void UnWriteLock();
93 
96 
97 private:
98  int32 rl; // read locks in effect
99  int32 wr; // write lock requests pending
100  int32 wl; // write locks in effect (should never be more than 1)
102 };
103 
104 #endif /* !__THREADING__MUTEX_H__INCL__ */
Mutex()
Primary contructor.
Definition: Mutex.cpp:33
bool TryLock()
Attempts to lock the mutex.
Definition: Mutex.cpp:66
bool TryReadLock()
Definition: Mutex.cpp:107
int32 ReadLockCount()
Definition: Mutex.cpp:178
Basic interface of a lockable object.
Definition: Lock.h:34
A lock for a Lockable object.
Definition: Lock.h:70
Common wrapper for platform-specific mutexes.
Definition: Mutex.h:36
void Lock()
Locks the mutex.
Definition: Mutex.cpp:57
void WriteLock()
Definition: Mutex.cpp:131
bool TryWriteLock()
Definition: Mutex.cpp:154
signed __int32 int32
Definition: eve-compat.h:49
~Mutex()
Destructor, releases allocated resources.
Definition: Mutex.cpp:48
int32 WriteLockCount()
Definition: Mutex.cpp:185
pthread_mutex_t mMutex
A pthread mutex used for mutex implementation using pthread library.
Definition: Mutex.h:72
Definition: Mutex.h:81
int32 wl
Definition: Mutex.h:100
void UnReadLock()
Definition: Mutex.cpp:120
MRMutex()
Definition: Mutex.cpp:87
Mutex MCounters
Definition: Mutex.h:101
void Unlock()
Unlocks the mutex.
Definition: Mutex.cpp:75
Lock< Mutex > MutexLock
Convenience typedef for Mutex's lock.
Definition: Mutex.h:77
~MRMutex()
Definition: Mutex.cpp:93
void ReadLock()
Definition: Mutex.cpp:101
void UnWriteLock()
Definition: Mutex.cpp:167
int32 wr
Definition: Mutex.h:99
int32 rl
Definition: Mutex.h:98