| GL Studio C++ Runtime API

gls_mutex.h
Go to the documentation of this file.
1/*! \file
2 \brief A cross platform mutex implementation.
3
4 \par Copyright Information
5
6 Copyright (c) 2017 by The DiSTI Corporation.<br>
7 11301 Corporate Blvd; Suite 100<br>
8 Orlando, Florida 32817<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*/
40
41#ifndef INCLUDED_DISTI_MUTEX_H
42#define INCLUDED_DISTI_MUTEX_H
43
44#include "disti_include.h"
45
46 namespace disti
47{
48/**
49 Provide a cross platform Mutex class to provide data locking capability in
50 a multithreaded environment. Uses the pimpl idiom to hide details.
51*/
52 class Mutex
53{
54 public:
55 /// Constructor
56 DISTI_EXPORT Mutex();
57
58 /// Destructor
59 virtual DISTI_EXPORT ~Mutex();
60
61 /// Blocks to acquire the mutex
62 DISTI_EXPORT void Lock();
63
64 /// Attemps to acquire the mutex without blocking
65 /// \return whether the lock succeeded
66 DISTI_EXPORT bool TryLock();
67
68 /// Releases the mutex, allowing another process to lock it
69 DISTI_EXPORT void Unlock();
70
71 /// A lock class to automagically un/lock a mutex within its scope
72 class ScopedLock
73 {
74 public:
75 /// Lock the given mutex (blocks on failure to acquire)
76 /// \param mut The mutex to lock.
77 ScopedLock( Mutex& mut )
78 : _mut( mut )
79 {
80 _mut.Lock();
81 }
82
83 /// Releases the mutex automatically
84 ~ScopedLock()
85 {
86 _mut.Unlock();
87 }
88
89 private:
90 Mutex& _mut;
91 };
92
93 /// A lock to automagically try to lock and, if applicable, unlock a mutex within its scope.
94 class ScopedTryLock
95 {
96 public:
97 /// Tries to lock the given mutex.
98 /// \param mut The mutex to lock.
99 ScopedTryLock( Mutex& mut )
100 : _mut( mut )
101 , _locked( _mut.TryLock() )
102 {}
103
104 /// \return Whether or not the lock succeeded.
105 bool IsLocked() const { return _locked; }
106
107 /// Releases the mutex automatically, if needed
108 ~ScopedTryLock()
109 {
110 if( _locked )
111 {
112 _mut.Unlock();
113 }
114 }
115
116 private:
117 Mutex& _mut;
118 bool _locked;
119 };
120
121 private:
122 // The pimpl idiom
123 class Impl;
124 Impl* const _impl;
125
126 // Disable copying
127 Mutex( const Mutex& );
128 void operator=( const Mutex& );
129};
130
131} // namespace disti
132
133#endif
disti::Mutex::ScopedLock
A lock class to automagically un/lock a mutex within its scope.
Definition: gls_mutex.h:73
disti::Mutex::ScopedLock::ScopedLock
ScopedLock(Mutex &mut)
Definition: gls_mutex.h:77
disti::Mutex::ScopedLock::~ScopedLock
~ScopedLock()
Releases the mutex automatically.
Definition: gls_mutex.h:84
disti::Mutex::ScopedTryLock
A lock to automagically try to lock and, if applicable, unlock a mutex within its scope.
Definition: gls_mutex.h:95
disti::Mutex::ScopedTryLock::IsLocked
bool IsLocked() const
Definition: gls_mutex.h:105
disti::Mutex::ScopedTryLock::ScopedTryLock
ScopedTryLock(Mutex &mut)
Definition: gls_mutex.h:99
disti::Mutex::ScopedTryLock::~ScopedTryLock
~ScopedTryLock()
Releases the mutex automatically, if needed.
Definition: gls_mutex.h:108
disti::Mutex
Definition: gls_mutex.h:53
disti::Mutex::TryLock
bool TryLock()
disti::Mutex::Mutex
Mutex()
Constructor.
disti::Mutex::Lock
void Lock()
Blocks to acquire the mutex.
disti::Mutex::~Mutex
virtual ~Mutex()
Destructor.
disti::Mutex::Unlock
void Unlock()
Releases the mutex, allowing another process to lock it.
disti_include.h
A file for all GL Studio files to include.
disti
Definition: bmpimage.h:47