HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 #include <SYS/SYS_Compiler.h>
19 
20 /// A simple class to hold a rwlock and release it when it goes out of scope.
21 template <typename Lock, bool READMODE>
23 {
24 public:
26  : myLock(lock)
27  {
28  if (READMODE)
29  myLock.readLock();
30  else
31  myLock.writeLock();
32  }
33 
35  {
36  if (READMODE)
37  myLock.readUnlock();
38  else
39  myLock.writeUnlock();
40  }
41 
42 private:
43  Lock &myLock;
44 };
45 
46 
47 /// Reader/Writer mutex class
49 {
50 public:
51  UT_RWLock();
52  ~UT_RWLock();
53 
55 
56  /// Attempt to acquire a shared read lock, returning false if the shared
57  /// read lock cannot be acquired immediately.
58  ///
59  /// A thread holding the exclusive write lock may acquire a shared read
60  /// lock as well, thereby holding both.
61  bool tryReadLock(int ntrials=1);
62 
63  /// Attempt to acquire an exclusive write lock, returning false if the
64  /// exclusive write lock cannot be acquired immediately.
65  ///
66  /// It is an error to attempt to acquire an exclusive write lock while
67  /// holding a shared read lock.
68  bool tryWriteLock(int ntrials=1);
69 
70  /// Attempt to acquire a shared read lock, blocking until the lock is
71  /// acquired.
72  ///
73  /// A thread holding the exclusive write lock may acquire a shared read
74  /// lock as well, thereby holding both.
75  void readLock();
76 
77  /// Attempt to acquire an exclusive write lock, blocking until the lock is
78  /// acquired.
79  ///
80  /// It is an error to attempt to acquire an exclusive write lock while
81  /// holding a shared read lock. If you're not sure whether you're holding
82  /// the read lock or not, use @c tryWriteLock instead.
83  void writeLock();
84 
85  /// Release a shared read lock.
86  void readUnlock();
87 
88  /// Release an exclusive write lock.
89  void writeUnlock();
90 
93 
94 private:
95  struct ut_RWLockData;
96 
97  ut_RWLockData *myData;
98 };
99 
100 
103 
104 #endif
#define UT_API
Definition: UT_API.h:14
UT_AutoRWLockType(Lock &lock)
Definition: UT_RWLock.h:25
UT_AutoRWLockType< UT_RWLock, true > UT_AutoReadLock
Definition: UT_RWLock.h:101
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
#define SYS_NO_DISCARD_RESULT
Definition: SYS_Compiler.h:93
A simple class to hold a rwlock and release it when it goes out of scope.
Definition: UT_RWLock.h:22
UT_AutoRWLockType< UT_RWLock, false > UT_AutoWriteLock
Definition: UT_RWLock.h:102
Reader/Writer mutex class.
Definition: UT_RWLock.h:48
UT_AutoRWLockType< UT_RWLock, false > WriteScope
Definition: UT_RWLock.h:92
UT_AutoRWLockType< UT_RWLock, true > ReadScope
Definition: UT_RWLock.h:91