GL Studio 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) 2015 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 /**
50  Provide a cross platform Mutex class to provide data locking capability in
51  a multithreaded environment. Uses the pimpl idiom to hide details.
52 */
53 class Mutex
54 {
55 public:
56 
57  /// Constructor
58  DISTI_EXPORT Mutex();
59 
60  /// Destructor
61  virtual DISTI_EXPORT ~Mutex();
62 
63  /// Blocks to acquire the mutex
64  DISTI_EXPORT void Lock();
65 
66  /// Attemps to acquire the mutex without blocking
67  /// \return whether the lock succeeded
68  DISTI_EXPORT bool TryLock();
69 
70  /// Releases the mutex, allowing another process to lock it
71  DISTI_EXPORT void Unlock();
72 
73  /// A lock class to automagically un/lock a mutex within its scope
74  class ScopedLock
75  {
76  public:
77  /// Lock the given mutex (blocks on failure to acquire)
78  ScopedLock( Mutex& mut ):
79  _mut( mut )
80  {
81  _mut.Lock();
82  }
83 
84  /// Releases the mutex automatically
86  {
87  _mut.Unlock();
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  ScopedTryLock( Mutex& mut ):
99  _mut( mut ),
100  _locked( _mut.TryLock() )
101  {}
102 
103  /// Returns whether the lock succeeded
104  bool IsLocked() const { return _locked; }
105 
106  /// Releases the mutex automatically, if needed
108  {
109  if( _locked )
110  {
111  _mut.Unlock();
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:104
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:78
bool TryLock()
Definition: gls_mutex.h:53
~ScopedTryLock()
Releases the mutex automatically, if needed.
Definition: gls_mutex.h:107
Mutex()
Constructor.
~ScopedLock()
Releases the mutex automatically.
Definition: gls_mutex.h:85
virtual ~Mutex()
Destructor.
A lock to automagically try to lock and, if applicable, unlock a mutex within its scope...
Definition: gls_mutex.h:94
ScopedTryLock(Mutex &mut)
Tries to lock the given mutex.
Definition: gls_mutex.h:98
A lock class to automagically un/lock a mutex within its scope.
Definition: gls_mutex.h:74
Definition: bmpimage.h:46
void Lock()
Blocks to acquire the mutex.