MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
spinlock.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 - 2014 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 _spinlock_h
42 #define _spinlock_h
43 
44 // Parts of this library use the TinyThread++ libary and those parts are also covered by the following license
45 /*
46 Copyright (c) 2010-2012 Marcus Geelnard
47 
48 This software is provided 'as-is', without any express or implied
49 warranty. In no event will the authors be held liable for any damages
50 arising from the use of this software.
51 
52 Permission is granted to anyone to use this software for any purpose,
53 including commercial applications, and to alter it and redistribute it
54 freely, subject to the following restrictions:
55 
56  1. The origin of this software must not be misrepresented; you must not
57  claim that you wrote the original software. If you use this software
58  in a product, an acknowledgment in the product documentation would be
59  appreciated but is not required.
60 
61  2. Altered source versions must be plainly marked as such, and must not be
62  misrepresented as being the original software.
63 
64  3. This notice may not be removed or altered from any source
65  distribution.
66 */
67 
68 #include "datatypes.h"
69 
70 /// @file
71 /// @brief Declares a Mutex, Mutex tools, and at least one MutexLike object.
72 
73 namespace Mezzanine
74 {
75  namespace Threading
76  {
77  /// @brief A mutex like construct that never makes a system call and uses CPU instructions instead.
78  /// @details This should be used when delay is likely to be measured in CPUs cycles and almost
79  /// certainly a short while. For pauses of unknown length use a Mutex so that the OS is informed it
80  /// could schedule another thread.
81  /// @n @n
82  /// This is compatible with the lock_guard class.
84  {
85  private:
86  /// @internal
87  /// 0 if unlocked, any other value if locked.
88  Int32 Locked;
89 
90  public:
91  ///@brief Constructor, creates an unlocked mutex
92  SpinLock();
93 
94  ///@brief Destructor.
95  ~SpinLock();
96 
97  /// @brief Lock the SpinLock.
98  /// @details The method will block the calling thread until a lock on the SpinLock can
99  /// be obtained. The SpinLock remains locked until @c unlock() is called.
100  /// @see lock_guard
101  void Lock();
102 
103  /// @brief Try to lock the spinlock.
104  /// @details The method will try to lock the SpinLock. If it fails, the function will
105  /// return immediately (non-blocking).
106  /// @return @c true if the lock was acquired, or @c false if the lock could
107  /// not be acquired.
108  bool TryLock();
109 
110  /// @brief Unlock the spinlock.
111  /// @details If any threads are waiting for the lock on this mutex, one of them will
112  /// be unblocked.
113  void Unlock();
114  };
115  }//Threading
116 }//Mezzanine
117 
118 #endif