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  bool isLocked();
92  int getCollisions() { return 0; }
93 
94  int64 getMemoryUsage(bool inclusive) const;
95 
96  /// Class for auto-unlocking
98 
99 private:
100 #if defined(LINUX) || defined(MBSD)
101  pthread_mutex_t myLock;
102  pthread_mutexattr_t myLockAttributes;
103 #elif defined(WIN32)
104  struct Impl // Actually a Windows SRWLOCK
105  {
106  uint8 myBuffer[16];
107 
108  struct Data;
109  Data *cast();
110  };
111  void sleep(void *cond_var);
112 
113  Impl myLock;
114 #endif
115 
116 #if defined(DEBUG_LOCK_CALLERS)
117  int myLockCount;
118  UT_String myBacktrace;
119 #endif
120 };
121 
122 #else // HAVE_PLATFORM_LOCK
123 
124 #include "UT_SpinLock.h"
126 
127 #endif
128 
132 
133 SYS_DECLARE_LEGACY_TR(UT_Lock)
134 
135 #endif
void
Definition: png.h:1083
UT_DebugLockType< UT_Lock > UT_DebugLock
Definition: UT_Lock.h:129
#define UT_API
Definition: UT_API.h:14
**But if you need a result
Definition: thread.h:613
Condition synchronization primitive.
Definition: UT_Condition.h:25
unsigned char uint8
Definition: SYS_Types.h:36
#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:130
#define UT_VERIFY(expr)
Definition: UT_Assert.h:202
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
UT_RecursiveSpinLock UT_Lock
Definition: UT_Lock.h:23
UT_AutoObjLockType< UT_Lock > UT_AutoObjLock
Definition: UT_Lock.h:131