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
14reproduced, in whole or part, in any form, or by any means of electronic,
15mechanical, or otherwise, without the written permission of DiSTI. Said
16permission may be derived through the purchase of applicable DiSTI product
17licenses which detail the distribution rights of this content and any
18Derivative Works based on this or other copyrighted DiSTI Software.
19
20 NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26
27 LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34EXCEED FIVE DOLLARS (US$5.00).
35
36 The aforementioned terms and restrictions are governed by the laws of the
37State 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
46namespace 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*/
52class Mutex
53{
54public:
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
73 {
74 public:
75 /// Lock the given mutex (blocks on failure to acquire)
76 /// \param mut The mutex to lock.
78 : _mut( mut )
79 {
80 _mut.Lock();
81 }
82
83 /// Releases the mutex automatically
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.
95 {
96 public:
97 /// Tries to lock the given mutex.
98 /// \param mut The mutex to lock.
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
109 {
110 if( _locked )
111 {
112 _mut.Unlock();
113 }
114 }
115
116 private:
117 Mutex& _mut;
118 bool _locked;
119 };
120
121private:
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
A lock class to automagically un/lock a mutex within its scope.
Definition: gls_mutex.h:73
ScopedLock(Mutex &mut)
Definition: gls_mutex.h:77
~ScopedLock()
Releases the mutex automatically.
Definition: gls_mutex.h:84
A lock to automagically try to lock and, if applicable, unlock a mutex within its scope.
Definition: gls_mutex.h:95
bool IsLocked() const
Definition: gls_mutex.h:105
ScopedTryLock(Mutex &mut)
Definition: gls_mutex.h:99
~ScopedTryLock()
Releases the mutex automatically, if needed.
Definition: gls_mutex.h:108
Definition: gls_mutex.h:53
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.
Force inclusion of the DirectShow library.
Definition: bmpimage.h:47