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 
23 
24 #if defined(LINUX) || defined(MBSD)
25 #define HAVE_PLATFORM_LOCK
26 #include <pthread.h>
27 #elif defined(WIN32)
28 #define HAVE_PLATFORM_LOCK
29 #endif
30 
31 #ifdef HAVE_PLATFORM_LOCK
32 
33 // Enable a printout of the number of calls to lock() along with a
34 // backtrace showing where the lock was first called. Currently only
35 // available on linux/mac.
36 //#define DEBUG_LOCK_CALLERS
37 
39 {
40  friend class UT_Condition;
41 public:
42  // thread_lock *must* be true
43  explicit UT_Lock(bool initially_locked = false);
44  ~UT_Lock();
45 
46  // tries the lock once, returns true if the lock was obtained.
47  bool tryLock();
48 
49  // blocks until the lock is obtained. lock() is inlined for speed.
50 #if defined(WIN32) || defined(DEBUG_LOCK_CALLERS)
51  void lock();
52 #elif defined(LINUX) || defined(MBSD)
53  void lock()
54  {
55  UT_VERIFY(pthread_mutex_lock(&myLock) == 0);
56 
57  // NOTE: pthread_mutex_lock does a memory fence as appropriate.
58  // See POSIX.1-2008 Section 4.11 Memory Synchronization.
59  // If it didn't, we would need at least a SYSloadFence() here,
60  // after acquiring the lock.
61  }
62 #endif
63 
64  bool safeLock() { lock(); return true; }
65 
66  // tries for N ms to get the lock, returns true if the lock was obtained.
67  bool lock(int ms);
68 
69  // release the lock. unlock() is inlined for speed.
70 #if defined(LINUX) || defined(MBSD)
71  void unlock()
72  {
73  // NOTE: pthread_mutex_unlock does a memory fence as appropriate.
74  // See POSIX.1-2008 Section 4.11 Memory Synchronization.
75  // If it didn't, we would need at least a SYSstoreFence() here,
76  // before releasing the lock.
77  #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
78  int result = pthread_mutex_unlock(&myLock);
79  UT_ASSERT(result == 0);
80  #else
81  (void) pthread_mutex_unlock(&myLock);
82  #endif
83  }
84 #elif defined(WIN32)
85  void unlock();
86 #endif
87 
88  bool isLocked();
89  int getCollisions() { return 0; }
90 
91  int64 getMemoryUsage(bool inclusive) const;
92 
93  /// Class for auto-unlocking
95 
96 private:
97 #if defined(LINUX) || defined(MBSD)
98  pthread_mutex_t myLock;
99  pthread_mutexattr_t myLockAttributes;
100 #elif defined(WIN32)
101  struct Impl // Actually a Windows SRWLOCK
102  {
103  uint8 myBuffer[16];
104 
105  struct Data;
106  Data *cast();
107  };
108  void sleep(void *cond_var);
109 
110  Impl myLock;
111 #endif
112 
113 #if defined(DEBUG_LOCK_CALLERS)
114  int myLockCount;
115  UT_String myBacktrace;
116 #endif
117 };
118 
119 #else // HAVE_PLATFORM_LOCK
120 
121 #include "UT_SpinLock.h"
123 
124 #endif
125 
130 
131 #endif
void
Definition: png.h:1083
UT_DebugLockType< UT_Lock > UT_DebugLock
Definition: UT_Lock.h:127
#define UT_API
Definition: UT_API.h:14
Condition synchronization primitive.
Definition: UT_Condition.h:25
unsigned char uint8
Definition: SYS_Types.h:36
GLuint64EXT * result
Definition: glew.h:14311
UT_AutoLockType< UT_Lock > UT_AutoLock
Definition: UT_Lock.h:126
long long int64
Definition: SYS_Types.h:116
UT_ObjLockType< UT_Lock > UT_ObjLock
Definition: UT_Lock.h:128
#define UT_VERIFY(expr)
Definition: UT_Assert.h:199
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:153
UT_RecursiveSpinLock UT_Lock
Definition: UT_Lock.h:22
UT_AutoObjLockType< UT_Lock > UT_AutoObjLock
Definition: UT_Lock.h:129