HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_SpinLock.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_SpinLock.h ( UT Library, C++)
7  *
8  * COMMENTS: Cross-platform Lock class for semaphoring.
9  */
10 
11 #ifndef UT_SPINLOCK_H
12 #define UT_SPINLOCK_H
13 
14 #include "UT_API.h"
15 #include "UT_LockUtil.h"
16 #include "UT_NonCopyable.h"
17 // Needed for ut_thread_id_t
18 #include "UT_Thread.h"
19 
20 #include <SYS/SYS_Align.h>
21 #include <SYS/SYS_AtomicInt.h>
22 
23 // Helper class to minimize data noise in the spinlock class
24 namespace ut_internal
25 {
26 template<bool R>
28 {
29 public:
30  void setThreadId(ut_thread_id_t) {}
31  ut_thread_id_t getThreadId() { return UT_INVALID_THREAD_ID; }
32  void setLockCount(int) {}
33  int getLockCount() { return 0; }
34 };
35 
36 template<>
38 {
39  ut_thread_id_t myLockedThread;
40  int myLockCount;
41 public:
43  myLockedThread(UT_INVALID_THREAD_ID),
44  myLockCount(0)
45  {}
46 
47  void setThreadId(ut_thread_id_t thread_id)
48  { myLockedThread = thread_id; }
49 
50  ut_thread_id_t getThreadId()
51  { return myLockedThread; }
52 
53  void setLockCount(int lock_count)
54  { myLockCount = lock_count; }
55 
57  { return myLockCount; }
58 };
59 
60 template<bool R>
62 {
63 public:
64  bool debugThisLock() const { return false; }
65  void setDebugThisLock(bool v) {}
66  bool isActive() const { return true; }
67  int getCollisions() const { return 0; }
68  void addCollision() {}
69 };
70 
71 template <>
72 class ut_SpinLockDebugT<true>
73 {
74 public:
76  : myDebugThisLock(false)
77  , myIsActive(true)
78  , myCollisions(0)
79  {
80  }
82  {
83  myIsActive = false;
84  }
85 
86  bool isActive() const { return myIsActive; }
87  bool debugThisLock() const { return myDebugThisLock; }
88  void setDebugThisLock(bool v) { myDebugThisLock = v; }
89  int getCollisions() const { return myCollisions; }
90  void addCollision() { myCollisions++; }
91 private:
92  uint myIsActive : 1;
93  uint myDebugThisLock : 1;
94  uint myCollisions : 30; // Collision counter
95 };
96 
97 }
98 
99 // If RecursiveLock is true, the locking thread is allowed to re-enter
100 // code areas that it locked. The lock is incremented & decremented.
101 
102 // Align to a cache-line boundary to avoid bus noise when there are
103 // multiple spinlocks declared next to each other.
104 template <bool RecursiveLock = false, bool LockDebug = true>
106 {
107 public:
108  explicit UT_SpinLockT(int lockstate = 0);
109  ~UT_SpinLockT();
110 
111  int64 getMemoryUsage(bool inclusive) const
112  { return inclusive ? sizeof(*this) : 0; }
113 
114  // tries the lock once, returns true if the lock was obtained.
115  bool tryLock();
116 
117  // tries for N ms to get the lock, returns true if the lock was obtained.
118  bool lock(int ms);
119 
120  // blocks until the lock is obtained.
121  void lock();
122 
123  bool safeLock() { lock(); return true; }
124 
125  // release the lock.
126  void unlock();
127 
128  // trigger/wait on trigger methods. waitForTrigger will block until
129  // trigger is called. Cannot be used with thread locks.
130  void waitForTrigger();
131  void trigger();
132 
133  void debugLock(bool debug)
134  { myDebug.setDebugThisLock(debug); }
135 
136  bool isLocked()
137  { return RecursiveLock ?
138  myRecursive.getLockCount() : (int32)mySpinLock;
139  }
140 
141  int getCollisions() { return myDebug.getCollisions(); }
142 
144 
145 private:
146  SYS_AtomicInt32 mySpinLock;
147 
148  // Data for recursive locks. Not present for non-recursive locks.
151 };
152 
156 
157 
158 #endif
159 
bool safeLock()
Definition: UT_SpinLock.h:123
const GLdouble * v
Definition: glcorearb.h:836
void debugLock(bool debug)
Definition: UT_SpinLock.h:133
#define UT_INVALID_THREAD_ID
Definition: UT_Thread.h:44
#define UT_API
Definition: UT_API.h:12
void setThreadId(ut_thread_id_t thread_id)
Definition: UT_SpinLock.h:47
long long int64
Definition: SYS_Types.h:100
int64 getMemoryUsage(bool inclusive) const
Definition: UT_SpinLock.h:111
int int32
Definition: SYS_Types.h:28
unsigned int uint
Definition: SYS_Types.h:33
bool isLocked()
Definition: UT_SpinLock.h:136
void setThreadId(ut_thread_id_t)
Definition: UT_SpinLock.h:30
UT_LockScopeType< UT_SpinLockT< RecursiveLock, LockDebug > > Scope
Definition: UT_SpinLock.h:143
int getCollisions()
Definition: UT_SpinLock.h:141