HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
112 
113  int64 getMemoryUsage(bool inclusive) const
114  { return inclusive ? sizeof(*this) : 0; }
115 
116  // tries the lock once, returns true if the lock was obtained.
117  bool tryLock();
118 
119  // tries for N ms to get the lock, returns true if the lock was obtained.
120  bool lock(int ms);
121 
122  // blocks until the lock is obtained.
123  void lock();
124 
125  bool safeLock() { lock(); return true; }
126 
127  // release the lock.
128  void unlock();
129 
130  // trigger/wait on trigger methods. waitForTrigger will block until
131  // trigger is called. Cannot be used with thread locks.
132  void waitForTrigger();
133  void trigger();
134 
135  void debugLock(bool debug)
136  { myDebug.setDebugThisLock(debug); }
137 
138  bool isLocked()
139  { return RecursiveLock ?
140  myRecursive.getLockCount() : (int32)mySpinLock.load(SYS_MEMORY_ORDER_LOAD);
141  }
142 
143  int getCollisions() { return myDebug.getCollisions(); }
144 
146 
147 private:
148  SYS_AtomicInt32 mySpinLock;
149 
150  // Data for recursive locks. Not present for non-recursive locks.
153 };
154 
158 
159 
160 #endif
161 
bool safeLock()
Definition: UT_SpinLock.h:125
int int32
Definition: SYS_Types.h:39
const GLdouble * v
Definition: glcorearb.h:837
void debugLock(bool debug)
Definition: UT_SpinLock.h:135
#define UT_INVALID_THREAD_ID
Definition: UT_Thread.h:47
UT_UniqueLock< UT_SpinLockT< RecursiveLock, LockDebug >> Scope
Definition: UT_SpinLock.h:145
#define UT_API
Definition: UT_API.h:14
void setThreadId(ut_thread_id_t thread_id)
Definition: UT_SpinLock.h:47
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
OIIO_API void debug(string_view str)
bool isLocked()
Definition: UT_SpinLock.h:138
void setThreadId(ut_thread_id_t)
Definition: UT_SpinLock.h:30
int getCollisions()
Definition: UT_SpinLock.h:143
**Note that the tasks the thread_id
Definition: thread.h:637
unsigned int uint
Definition: SYS_Types.h:45