GL Studio C++ Runtime API
gls_mutex.h
Go to the documentation of this file.
1/// \file gls_mutex.h
2/// \brief A cross platform mutex implementation.
3///
4/// \par Copyright Information
5///
6/// Copyright (c) 2017 by The DiSTI Corporation.<br>
7/// 12249 Science Drive; Suite 300<br>
8/// Orlando, Florida 32826<br>
9/// USA<br>
10/// <br>
11/// All rights reserved.<br>
12///
13/// This Software contains proprietary trade secrets of DiSTI and may not be
14/// reproduced, in whole or part, in any form, or by any means of electronic,
15/// mechanical, or otherwise, without the written permission of DiSTI. Said
16/// permission may be derived through the purchase of applicable DiSTI product
17/// licenses which detail the distribution rights of this content and any
18/// Derivative Works based on this or other copyrighted DiSTI Software.
19///
20/// NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21/// AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22/// PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23/// AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24/// IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25/// PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26///
27/// LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28/// IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29/// INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30/// DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31/// INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32/// INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33/// OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34/// EXCEED FIVE DOLLARS (US$5.00).
35///
36/// The aforementioned terms and restrictions are governed by the laws of the
37/// State of Florida and the United States of America.
38
39#ifndef INCLUDED_DISTI_MUTEX_H
40#define INCLUDED_DISTI_MUTEX_H
41
42#include "disti_include.h"
43
44namespace disti
45{
46/**
47 Provide a cross platform Mutex class to provide data locking capability in
48 a multithreaded environment. Uses the pimpl idiom to hide details.
49*/
50class Mutex
51{
52public:
53 /// Constructor
54 DISTI_EXPORT Mutex();
55
56 /// Destructor
57 virtual DISTI_EXPORT ~Mutex();
58
59 /// Blocks to acquire the mutex
60 DISTI_EXPORT void Lock();
61
62 /// Attemps to acquire the mutex without blocking
63 /// \return whether the lock succeeded
64 DISTI_EXPORT bool TryLock();
65
66 /// Releases the mutex, allowing another process to lock it
67 DISTI_EXPORT void Unlock();
68
69 /// A lock class to automagically un/lock a mutex within its scope
71 {
72 public:
73 /// Lock the given mutex (blocks on failure to acquire)
74 /// \param mut The mutex to lock.
76 : _mut( mut )
77 {
78 _mut.Lock();
79 }
80
81 /// Releases the mutex automatically
83 {
84 _mut.Unlock();
85 }
86
87 private:
88 Mutex& _mut;
89 };
90
91 /// A lock to automagically try to lock and, if applicable, unlock a mutex within its scope.
93 {
94 public:
95 /// Tries to lock the given mutex.
96 /// \param mut The mutex to lock.
98 : _mut( mut )
99 , _locked( _mut.TryLock() )
100 {}
101
102 /// \return Whether or not the lock succeeded.
103 bool IsLocked() const { return _locked; }
104
105 /// Releases the mutex automatically, if needed
107 {
108 if( _locked )
109 {
110 _mut.Unlock();
111 }
112 }
113
114 private:
115 Mutex& _mut;
116 bool _locked;
117 };
118
119private:
120 // The pimpl idiom
121 class Impl;
122 Impl* const _impl;
123
124 // Disable copying
125 Mutex( const Mutex& );
126 void operator=( const Mutex& );
127};
128
129} // namespace disti
130
131#endif
A lock class to automagically un/lock a mutex within its scope.
Definition: gls_mutex.h:71
ScopedLock(Mutex &mut)
Definition: gls_mutex.h:75
~ScopedLock()
Releases the mutex automatically.
Definition: gls_mutex.h:82
A lock to automagically try to lock and, if applicable, unlock a mutex within its scope.
Definition: gls_mutex.h:93
bool IsLocked() const
Definition: gls_mutex.h:103
ScopedTryLock(Mutex &mut)
Definition: gls_mutex.h:97
~ScopedTryLock()
Releases the mutex automatically, if needed.
Definition: gls_mutex.h:106
Definition: gls_mutex.h:51
bool TryLock()
Mutex()
Constructor.
void Lock()
Blocks to acquire the mutex.
virtual ~Mutex()
Destructor.
void Unlock()
Releases the mutex, allowing another process to lock it.
A file for all GL Studio files to include.
Definition: bmpimage.h:46