HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_AbortableLock.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_AbortableLock.h (UT Library, C++)
7  *
8  * COMMENTS: A mutex class that supports dynamic deadlock detection amongst
9  * all instances of UT_AbortableLock.
10  */
11 
12 #ifndef __UT_ABORTABLELOCK_H_INCLUDED__
13 #define __UT_ABORTABLELOCK_H_INCLUDED__
14 
15 #include "UT_API.h"
16 #include "UT_Assert.h"
17 #include "UT_IntArray.h"
18 #include "UT_LockUtil.h"
19 #include "UT_NonCopyable.h"
20 #include "UT_RecursiveTimedLock.h"
21 #include "UT_ThreadSpecificValue.h"
22 #include <SYS/SYS_AtomicInt.h>
23 #include <SYS/SYS_AtomicPtr.h>
24 
25 namespace UT_AbortableLockImpl
26 {
27 
29 {
30 public:
31  LockState();
32 
33  bool hasLock(int thread_id);
34 
35  void markAsLocked(int thread_id);
36  void markAsUnlocked();
37 
38  void markAsWaiting(int thread_id);
39  void markAsNotWaiting(int thread_id);
40 
41  bool findDeadlock(int thread_id);
42  bool verifyDeadlock(
43  int start, const UT_IntArray &threads,
44  const UT_ValArray<LockState *> &locks);
45  void printDeadlock(
46  int start, const UT_IntArray &threads,
47  const UT_ValArray<LockState *> &locks);
48 
49  void markDeadlock(int thread_id);
50 
51  static bool getPrintDeadlocks();
52  static void setPrintDeadlocks(bool flag);
53 
54  static bool runUnitTest(bool print_progress,
55  bool for_performance);
56 
57 private:
58  SYS_AtomicInt32 myThreadOwner;
59  SYS_AtomicInt32 myLockCount;
60  SYS_AtomicInt32 myUnlockCount;
61 
62  // theWaitingLock keeps track of the current UT_AbortableLock waiting to
63  // acquire ownership for a specific thread.
65  theWaitingLock;
66 
67  // Keep track if the current thread hit a deadlock
69  theDeadlockFlag;
70 
71  // If true, we will print our cycle when we hit a deadlock
72  static bool thePrintDeadlocks;
73 };
74 
75 } // namespace UT_AbortableLockImpl
76 
77 
78 /// A mutex class that supports dynamic deadlock detection amongst all
79 /// instances of UT_AbortableLock.
80 template <typename LOCKABLE>
82 {
83 public:
86 
87  /// Attempt lock on this mutex. Returns true on success, false if deadlock
88  bool safeLock();
89 
90  /// Tries the lock without blocking. Returns true if the lock was obtained.
91  bool tryLock();
92 
93  /// Locks the underlying mutex without testing for deadlocks
94  void lock();
95 
96  /// Release lock. Undefined if it was not previously locked.
97  void unlock();
98 
99  /// Check if a thread has acquired this lock
100  bool hasLock(int thread);
101 
102  /// Check if the current thread has acquired this lock.
103  /// It is equivalent to hasLock(SYSgetSTID()).
104  bool hasLock();
105 
106  /// Use UT_AbortableLock::Scope for scope/exception safety
108 
109 private:
110  LOCKABLE myMutex;
112 };
113 
114 
115 /// A form of UT_Lock with deadlock detection
117 
118 // UT_AbortableTaskLock does not work because UT_AbortableLock assumes that
119 // it's underlying mutex only ever has 1 owner thread
120 #if 0
121 // A form of UT_TaskLock with deadlock detection
122 typedef UT_AbortableLock<UT_TaskLock> UT_AbortableTaskLock;
123 #endif
124 
125 
126 // Implementation
127 #include "UT_AbortableLockImpl.h"
128 
129 #endif // __UT_ABORTABLELOCK_H_INCLUDED__
GLuint start
Definition: glcorearb.h:474
UT_LockScopeType< UT_AbortableLock > Scope
Use UT_AbortableLock::Scope for scope/exception safety.
#define UT_API
Definition: UT_API.h:12
bool safeLock()
Attempt lock on this mutex. Returns true on success, false if deadlock.
void unlock()
Release lock. Undefined if it was not previously locked.
bool tryLock()
Tries the lock without blocking. Returns true if the lock was obtained.
png_infop png_uint_32 flag
Definition: png.h:2242
void lock()
Locks the underlying mutex without testing for deadlocks.
UT_AbortableLock< UT_RecursiveTimedLock > UT_AbortableRecursiveLock
A form of UT_Lock with deadlock detection.