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  ScopedLock( Mutex& mut )
77  : _mut( mut )
78  {
79  _mut.Lock();
80  }
81 
82  /// Releases the mutex automatically
84  {
85  _mut.Unlock();
86  }
87 
88  private:
89  Mutex& _mut;
90  };
91 
92  /// A lock to automagically try to lock and, if applicable, unlock a mutex within its scope.
94  {
95  public:
96  /// Tries to lock the given mutex
97  ScopedTryLock( Mutex& mut )
98  : _mut( mut )
99  , _locked( _mut.TryLock() )
100  {}
101 
102  /// Returns whether 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 
119 private:
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
bool IsLocked() const
Returns whether the lock succeeded.
Definition: gls_mutex.h:103
void Unlock()
Releases the mutex, allowing another process to lock it.
A file for all GL Studio files to include.
ScopedLock(Mutex &mut)
Lock the given mutex (blocks on failure to acquire)
Definition: gls_mutex.h:76
bool TryLock()
Definition: gls_mutex.h:52
~ScopedTryLock()
Releases the mutex automatically, if needed.
Definition: gls_mutex.h:106
Mutex()
Constructor.
~ScopedLock()
Releases the mutex automatically.
Definition: gls_mutex.h:83
virtual ~Mutex()
Destructor.
A lock to automagically try to lock and, if applicable, unlock a mutex within its scope...
Definition: gls_mutex.h:93
ScopedTryLock(Mutex &mut)
Tries to lock the given mutex.
Definition: gls_mutex.h:97
A lock class to automagically un/lock a mutex within its scope.
Definition: gls_mutex.h:72
Definition: bmpimage.h:46
void Lock()
Blocks to acquire the mutex.