Spinning Topp Logo BlackTopp Studios
inc
readwritespinlock.cpp
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 _readwritespinlock_cpp
42 #define _readwritespinlock_cpp
43 
44 #include "atomicoperations.h"
45 #include "readwritespinlock.h"
46 #include "crossplatformincludes.h"
47 #include <limits>
48 #include <cassert>
49 
50 /// @file
51 /// @brief Contains the implementation for the @ref Mezzanine::Threading::Mutex Mutex synchronization object.
52 
53 namespace Mezzanine
54 {
55  namespace Threading
56  {
58  {}
59 
61  {}
62 
63 
65  { while(!TryLockForRead()){} }
66 
68  {
69  if(CountGaurd.TryLock())
70  {
71  if(0<=Locked)
72  {
73  assert(0<=Locked); // fail because of timing bug in this lock
74  Locked++;
75  CountGaurd.Unlock();
76  return true;
77  }else{
78  CountGaurd.Unlock();
79  return false;
80  }
81  }else{
82  return false;
83  }
84  }
85 
87  {
88  CountGaurd.Lock();
89  assert(!(0>Locked)); // fail because of writelock
90  assert(!(0==Locked)); // fail because not locked
91  Locked--;
92  CountGaurd.Unlock();
93  }
94 
96  { while(!TryLockForWrite()){} }
97 
99  {
100  if(CountGaurd.TryLock())
101  {
102  if(0==Locked)
103  {
104  Locked=std::numeric_limits<Int32>::min();
105  CountGaurd.Unlock();
106  return true;
107  }else{
108  CountGaurd.Unlock();
109  return false;
110  }
111  }else{
112  return false;
113  }
114  }
115 
117  {
118  CountGaurd.Lock();
119  assert(!(0<Locked)); // fail because of Readlock
120  assert(!(0==Locked)); // fail because not locked
121  assert(!(std::numeric_limits<Int32>::min()!=Locked)); // failed because cannot unlocked if already unlocked
122  Locked=0;
123  CountGaurd.Unlock();
124  }
125 
126 
127 
128  } // \Threading namespace
129 } // \Mezzanine namespace
130 #endif
Declares a Mutex, Mutex tools, and at least one MutexLike object.
This file determines what features are offered by the compiler that this library can take advantage o...
ReadWriteSpinLock()
Constructor, creates an unlocked mutex.
void LockForRead()
Lock the ReadWriteSpinLock for Reading.
bool TryLockForRead()
Try to lock this for reading.
void UnlockWrite()
Unlock this for writing.
void Lock()
Lock the SpinLock.
Definition: spinlock.cpp:61
bool TryLockForWrite()
Try to lock this for writing.
bool TryLock()
Try to lock the spinlock.
Definition: spinlock.cpp:67
Simple thread safe ways to check and change a specified variable atomically.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
void Unlock()
Unlock the spinlock.
Definition: spinlock.cpp:70
void LockForWrite()
Lock the ReadWriteSpinLock for Writing.