HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Lock.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_Lock.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  * Locking class
10  */
11 
12 #ifndef __UT_Lock_h__
13 #define __UT_Lock_h__
14 
15 #include "UT_API.h"
16 #include "UT_Assert.h"
17 #include "UT_LockUtil.h"
18 #include "UT_NonCopyable.h"
19 #include "UT_String.h"
20 #include <SYS/SYS_Types.h>
21 #include <SYS/SYS_TypeDecorate.h>
22 
24 
25 #if defined(LINUX) || defined(MBSD)
26 #define HAVE_PLATFORM_LOCK
27 #include <pthread.h>
28 #elif defined(WIN32)
29 #define HAVE_PLATFORM_LOCK
30 #endif
31 
32 #ifdef HAVE_PLATFORM_LOCK
33 
34 // Enable a printout of the number of calls to lock() along with a
35 // backtrace showing where the lock was first called. Currently only
36 // available on linux/mac.
37 //#define DEBUG_LOCK_CALLERS
38 
39 class UT_API UT_Lock
40 {
41  friend class UT_Condition;
42 public:
43  // thread_lock *must* be true
44  explicit UT_Lock(bool initially_locked = false);
45  ~UT_Lock();
46 
48 
49  // tries the lock once, returns true if the lock was obtained.
50  bool tryLock();
51 
52  // blocks until the lock is obtained. lock() is inlined for speed.
53 #if defined(WIN32) || defined(DEBUG_LOCK_CALLERS)
54  void lock();
55 #elif defined(LINUX) || defined(MBSD)
56  void lock()
57  {
58  UT_VERIFY(pthread_mutex_lock(&myLock) == 0);
59 
60  // NOTE: pthread_mutex_lock does a memory fence as appropriate.
61  // See POSIX.1-2008 Section 4.11 Memory Synchronization.
62  // If it didn't, we would need at least a SYSloadFence() here,
63  // after acquiring the lock.
64  }
65 #endif
66 
67  bool safeLock() { lock(); return true; }
68 
69  // tries for N ms to get the lock, returns true if the lock was obtained.
70  bool lock(int ms);
71 
72  // release the lock. unlock() is inlined for speed.
73 #if defined(LINUX) || defined(MBSD)
74  void unlock()
75  {
76  // NOTE: pthread_mutex_unlock does a memory fence as appropriate.
77  // See POSIX.1-2008 Section 4.11 Memory Synchronization.
78  // If it didn't, we would need at least a SYSstoreFence() here,
79  // before releasing the lock.
80  #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
81  int result = pthread_mutex_unlock(&myLock);
82  UT_ASSERT(result == 0);
83  #else
84  (void) pthread_mutex_unlock(&myLock);
85  #endif
86  }
87 #elif defined(WIN32)
88  void unlock();
89 #endif
90 
91  // There's no way to portably determine whether a recursive mutex is locked
92  // in PThreads.
93 #if 0
94  bool isLocked();
95 #endif
96  int getCollisions() { return 0; }
97 
98  int64 getMemoryUsage(bool inclusive) const;
99 
100  /// Class for auto-unlocking
102 
103 private:
104 #if defined(LINUX) || defined(MBSD)
105  pthread_mutex_t myLock;
106  pthread_mutexattr_t myLockAttributes;
107 #elif defined(WIN32)
108  struct Impl // Actually a Windows SRWLOCK
109  {
110  alignas(void*)
111  uint8 myBuffer[16];
112 
113  struct Data;
114  Data *cast();
115  };
116  void sleep(void *cond_var);
117  /// sleep until timeout - return true if condition triggered,
118  /// false if timeout.
119  bool sleepMS(void *cond_var, int64 timeout_ms);
120 
121  Impl myLock;
122 #endif
123 
124 #if defined(DEBUG_LOCK_CALLERS)
125  int myLockCount;
126  UT_String myBacktrace;
127 #endif
128 };
129 
130 #else // HAVE_PLATFORM_LOCK
131 
132 #include "UT_SpinLock.h"
134 
135 #endif
136 
140 
141 SYS_DECLARE_LEGACY_TR(UT_Lock)
142 
143 #endif
void
Definition: png.h:1083
UT_DebugLockType< UT_Lock > UT_DebugLock
Definition: UT_Lock.h:137
#define UT_API
Definition: UT_API.h:14
**But if you need a result
Definition: thread.h:622
Condition synchronization primitive.
Definition: UT_Condition.h:25
OutGridT const XformOp bool bool
unsigned char uint8
Definition: SYS_Types.h:36
SYS_FORCE_INLINE const X * cast(const InstancablePtr *o)
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
SYS_DECLARE_LEGACY_TR(GU_Detail)
UT_ObjLockType< UT_Lock > UT_ObjLock
Definition: UT_Lock.h:138
#define UT_VERIFY(expr)
Definition: UT_Assert.h:213
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:165
UT_RecursiveSpinLock UT_Lock
Definition: UT_Lock.h:23
UT_AutoObjLockType< UT_Lock > UT_AutoObjLock
Definition: UT_Lock.h:139