HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_RWLock.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_RWLock.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  * Synchronization class to support multiple readers and one writer.
10  */
11 
12 #ifndef __UT_RWLock_h__
13 #define __UT_RWLock_h__
14 
15 #include "UT_API.h"
16 #include "UT_Assert.h"
17 #include "UT_NonCopyable.h"
18 
19 /// A simple class to hold a rwlock and release it when it goes out of scope.
20 template <typename Lock, bool READMODE>
22 {
23 public:
25  : myLock(lock)
26  {
27  if (READMODE)
28  myLock.readLock();
29  else
30  myLock.writeLock();
31  }
32 
34  {
35  if (READMODE)
36  myLock.readUnlock();
37  else
38  myLock.writeUnlock();
39  }
40 
41 private:
42  Lock &myLock;
43 };
44 
45 
46 /// Reader/Writer mutex class
48 {
49 public:
50  UT_RWLock();
51  ~UT_RWLock();
52 
53  /// Attempt to acquire a shared read lock, returning false if the shared
54  /// read lock cannot be acquired immediately.
55  ///
56  /// A thread holding the exclusive write lock may acquire a shared read
57  /// lock as well, thereby holding both.
58  bool tryReadLock(int ntrials=1);
59 
60  /// Attempt to acquire an exclusive write lock, returning false if the
61  /// exclusive write lock cannot be acquired immediately.
62  ///
63  /// It is an error to attempt to acquire an exclusive write lock while
64  /// holding a shared read lock.
65  bool tryWriteLock(int ntrials=1);
66 
67  /// Attempt to acquire a shared read lock, blocking until the lock is
68  /// acquired.
69  ///
70  /// A thread holding the exclusive write lock may acquire a shared read
71  /// lock as well, thereby holding both.
72  void readLock();
73 
74  /// Attempt to acquire an exclusive write lock, blocking until the lock is
75  /// acquired.
76  ///
77  /// It is an error to attempt to acquire an exclusive write lock while
78  /// holding a shared read lock. If you're not sure whether you're holding
79  /// the read lock or not, use @c tryWriteLock instead.
80  void writeLock();
81 
82  /// Release a shared read lock.
83  void readUnlock();
84 
85  /// Release an exclusive write lock.
86  void writeUnlock();
87 
90 
91 private:
92  struct ut_RWLockData;
93 
94  ut_RWLockData *myData;
95 };
96 
97 
100 
101 #endif
#define UT_API
Definition: UT_API.h:12
UT_AutoRWLockType(Lock &lock)
Definition: UT_RWLock.h:24
UT_AutoRWLockType< UT_RWLock, true > UT_AutoReadLock
Definition: UT_RWLock.h:98
A simple class to hold a rwlock and release it when it goes out of scope.
Definition: UT_RWLock.h:21
UT_AutoRWLockType< UT_RWLock, false > UT_AutoWriteLock
Definition: UT_RWLock.h:99
Reader/Writer mutex class.
Definition: UT_RWLock.h:47
UT_AutoRWLockType< UT_RWLock, false > WriteScope
Definition: UT_RWLock.h:89
UT_AutoRWLockType< UT_RWLock, true > ReadScope
Definition: UT_RWLock.h:88