HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  ~LockState() = default;
33 
35 
36  bool hasLock(int thread_id);
37 
38  void markAsLocked(int thread_id);
39  void markAsUnlocked();
40 
41  void markAsWaiting(int thread_id);
42  void markAsNotWaiting(int thread_id);
43 
44  bool findDeadlock(int thread_id);
45  bool verifyDeadlock(
46  int start, const UT_IntArray &threads,
47  const UT_ValArray<LockState *> &locks);
48  void printDeadlock(
49  int start, const UT_IntArray &threads,
50  const UT_ValArray<LockState *> &locks);
51 
52  void markDeadlock(int thread_id);
53 
54  static bool getPrintDeadlocks();
55  static void setPrintDeadlocks(bool flag);
56 
57  static bool runUnitTest(bool print_progress,
58  bool for_performance);
59 
60 private:
61  SYS_AtomicInt32 myThreadOwner;
62  SYS_AtomicInt32 myLockCount;
63  SYS_AtomicInt32 myUnlockCount;
64 
65  // theWaitingLock keeps track of the current UT_AbortableLock waiting to
66  // acquire ownership for a specific thread.
68  theWaitingLock;
69 
70  // Keep track if the current thread hit a deadlock
72  theDeadlockFlag;
73 
74  // If true, we will print our cycle when we hit a deadlock
75  static bool thePrintDeadlocks;
76 };
77 
78 } // namespace UT_AbortableLockImpl
79 
80 
81 /// A mutex class that supports dynamic deadlock detection amongst all
82 /// instances of UT_AbortableLock.
83 template <typename LOCKABLE>
85 {
86 public:
89 
91 
92  /// Attempt lock on this mutex. Returns true on success, false if deadlock
93  bool safeLock();
94 
95  /// Tries the lock without blocking. Returns true if the lock was obtained.
96  bool tryLock();
97 
98  /// Locks the underlying mutex without testing for deadlocks
99  void lock();
100 
101  /// Release lock. Undefined if it was not previously locked.
102  void unlock();
103 
104  /// Check if a thread has acquired this lock
105  bool hasLock(int thread);
106 
107  /// Check if the current thread has acquired this lock.
108  /// It is equivalent to hasLock(SYSgetSTID()).
109  bool hasLock();
110 
111  /// Use UT_AbortableLock::Scope for scope/exception safety
113 
114 private:
115  LOCKABLE myMutex;
116  UT_AbortableLockImpl::LockState myImpl;
117 };
118 
119 
120 /// A form of UT_Lock with deadlock detection
122 
123 // UT_AbortableTaskLock does not work because UT_AbortableLock assumes that
124 // it's underlying mutex only ever has 1 owner thread
125 #if 0
126 // A form of UT_TaskLock with deadlock detection
127 typedef UT_AbortableLock<UT_TaskLock> UT_AbortableTaskLock;
128 #endif
129 
130 
131 // Implementation
132 #include "UT_AbortableLockImpl.h"
133 
134 #endif // __UT_ABORTABLELOCK_H_INCLUDED__
GLuint start
Definition: glcorearb.h:475
#define UT_API
Definition: UT_API.h:14
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.
Lock adapter for std mutexes.
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
**Note that the tasks the is the thread number *for the or if it s being executed by a non pool thread(this *can happen in cases where the whole pool is occupied and the calling *thread contributes to running the work load).**Thread pool.Have fun
bool tryLock()
Tries the lock without blocking. Returns true if the lock was obtained.
**Note that the tasks the thread_id
Definition: thread.h:637
void lock()
Locks the underlying mutex without testing for deadlocks.