Spinning Topp Logo BlackTopp Studios
inc
mutex.h
Go to the documentation of this file.
1 // The DAGFrameScheduler is a Multi-Threaded lock free and wait free scheduling library.
2 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
3 /* This file is part of The DAGFrameScheduler.
4 
5  The DAGFrameScheduler is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  The DAGFrameScheduler is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with The DAGFrameScheduler. If not, see <http://www.gnu.org/licenses/>.
17 */
18 /* The original authors have included a copy of the license specified above in the
19  'doc' folder. See 'gpl.txt'
20 */
21 /* We welcome the use of the DAGFrameScheduler to anyone, including companies who wish to
22  Build professional software and charge for their product.
23 
24  However there are some practical restrictions, so if your project involves
25  any of the following you should contact us and we will try to work something
26  out:
27  - DRM or Copy Protection of any kind(except Copyrights)
28  - Software Patents You Do Not Wish to Freely License
29  - Any Kind of Linking to Non-GPL licensed Works
30  - Are Currently In Violation of Another Copyright Holder's GPL License
31  - If You want to change our code and not add a few hundred MB of stuff to
32  your distribution
33 
34  These and other limitations could cause serious legal problems if you ignore
35  them, so it is best to simply contact us or the Free Software Foundation, if
36  you have any questions.
37 
38  Joseph Toppi - toppij@gmail.com
39  John Blackwood - makoenergy02@gmail.com
40 */
41 #ifndef _mutex_h
42 #define _mutex_h
43 
44 /// @file
45 /// @brief This file declares and defines a mutex that is a partial implementation
46 
47 // Parts of this library use the TinyThread++ libary and those parts are also covered by the following license
48 /*
49 Copyright (c) 2010-2012 Marcus Geelnard
50 
51 This software is provided 'as-is', without any express or implied
52 warranty. In no event will the authors be held liable for any damages
53 arising from the use of this software.
54 
55 Permission is granted to anyone to use this software for any purpose,
56 including commercial applications, and to alter it and redistribute it
57 freely, subject to the following restrictions:
58 
59  1. The origin of this software must not be misrepresented; you must not
60  claim that you wrote the original software. If you use this software
61  in a product, an acknowledgment in the product documentation would be
62  appreciated but is not required.
63 
64  2. Altered source versions must be plainly marked as such, and must not be
65  misrepresented as being the original software.
66 
67  3. This notice may not be removed or altered from any source
68  distribution.
69 */
70 
71 #if !defined(SWIG) || defined(SWIG_THREADING) // Do not read when in swig and not in the threading module
72 #include "atomicoperations.h"
73 #endif
74 
75 /// @file
76 /// @brief Declares a Mutex, Mutex tools
77 
78 /// @brief Forward declaration
79 #if defined(_MEZZ_THREAD_WIN32_)
80 #if defined(__MINGW64_VERSION_MAJOR)
81 struct _RTL_CRITICAL_SECTION;
82 typedef _RTL_CRITICAL_SECTION CRITICAL_SECTION;
83 #else//__MINGW64_VERSION_MAJOR
84 class _CRITICAL_SECTION;
85 typedef _CRITICAL_SECTION CRITICAL_SECTION;
86 #endif//__MINGW64_VERSION_MAJOR
87 #endif//_MEZZ_THREAD_WIN32_
88 
89 namespace Mezzanine
90 {
91  namespace Threading
92  {
93  /// @brief A cross-platform abstraction of the OS's mutex.
94  /// @details Use this for potentially long delays, like making multiple system calls. This is likely slower
95  /// than a spinlock, but it informs the OS of delay and could allow another thread to be scheduled, making use
96  /// of the time the current thread would otherwise spin.
98  {
99  private:
100 
101  /// @brief Deleted assignment operator
102  #ifdef _MEZZ_CPP11_PARTIAL_
103  Mutex& operator=(const Mutex&) = delete;
104  #else
105  Mutex& operator=(const Mutex&);
106  #endif
107 
108  /// @brief Deleted assignment operator copy constructor
109  #ifdef _MEZZ_CPP11_PARTIAL_
110  Mutex(const Mutex&) = delete;
111  #else
112  Mutex(const Mutex&);
113  #endif
114 
115  /// @var mHandle
116  /// @brief A native handle to the OS specific synchronization object.
117  #if defined(_MEZZ_THREAD_WIN32_)
118  CRITICAL_SECTION* mHandle;
119  /// @brief Is this critical section already locked?
120  bool mAlreadyLocked;
121  #else
122  pthread_mutex_t mHandle;
123  #endif
124 
125 
126  public:
127  ///@brief Constructor, creates an unlocked mutex
128  Mutex();
129 
130  ///@brief Destructor.
131  ~Mutex();
132 
133  /// @brief Lock the mutex.
134  /// @details The method will block the calling thread until a lock on the mutex can
135  /// be obtained. The mutex remains locked until @c unlock() is called.
136  /// @see lock_guard
137  void Lock();
138  /// @brief Try to lock the mutex.
139  /// @details The method will try to lock the mutex. If it fails, the function will
140  /// return immediately (non-blocking).
141  /// @return @c true if the lock was acquired, or @c false if the lock could
142  /// not be acquired.
143  bool TryLock();
144  /// @brief Unlock the mutex.
145  /// @details If any threads are waiting for the lock on this mutex, one of them will
146  /// be unblocked.
147  void Unlock();
148 
149  /// @brief Simply calls Lock() for compatibility with lock_guard and other more standard mutexes
150  void lock()
151  { Lock(); }
152  /// @brief Simply calls Unlock() for compatibility with lock_guard and other more standard mutexes
153  void unlock()
154  { Unlock(); }
155  };//Mutex
156  }//Threading
157 }//Mezzanine
158 
159 #endif
void lock()
Simply calls Lock() for compatibility with lock_guard and other more standard mutexes.
Definition: mutex.h:150
A cross-platform abstraction of the OS's mutex.
Definition: mutex.h:97
void unlock()
Simply calls Unlock() for compatibility with lock_guard and other more standard mutexes.
Definition: mutex.h:153
Simple thread safe ways to check and change a specified variable atomically.
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56