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 
22 class UT_Condition;
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 
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 
47  // tries the lock once, returns true if the lock was obtained.
48  bool tryLock();
49 
50  // blocks until the lock is obtained. lock() is inlined for speed.
51 #if defined(WIN32) || defined(DEBUG_LOCK_CALLERS)
52  void lock();
53 #elif defined(LINUX) || defined(MBSD)
54  void lock()
55  {
56  UT_VERIFY(pthread_mutex_lock(&myLock) == 0);
57 
58  // NOTE: pthread_mutex_lock does a memory fence as appropriate.
59  // See POSIX.1-2008 Section 4.11 Memory Synchronization.
60  // If it didn't, we would need at least a SYSloadFence() here,
61  // after acquiring the lock.
62  }
63 #endif
64 
65  bool safeLock() { lock(); return true; }
66 
67  // tries for N ms to get the lock, returns true if the lock was obtained.
68  bool lock(int ms);
69 
70  // release the lock. unlock() is inlined for speed.
71 #if defined(LINUX) || defined(MBSD)
72  void unlock()
73  {
74  // NOTE: pthread_mutex_unlock does a memory fence as appropriate.
75  // See POSIX.1-2008 Section 4.11 Memory Synchronization.
76  // If it didn't, we would need at least a SYSstoreFence() here,
77  // before releasing the lock.
78  #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
79  int result = pthread_mutex_unlock(&myLock);
80  UT_ASSERT(result == 0);
81  #else
82  (void) pthread_mutex_unlock(&myLock);
83  #endif
84  }
85 #elif defined(WIN32)
86  void unlock();
87 #endif
88 
89  bool isLocked();
90  int getCollisions() { return 0; }
91 
92  int64 getMemoryUsage(bool inclusive) const;
93 
94  /// Class for auto-unlocking
95  typedef UT_LockScopeType<UT_Lock> Scope;
96 
97 private:
98 
99 #ifndef WIN32
100  // UT_ConditionLock uses error check pthread locks, which Windows does not
101  // support (it uses UT_SpinLock on Windows). This allows UT_ConditionLock to
102  // use conditions on platforms that support them without asserting.
103  explicit UT_Lock(UT_ConditionLock &);
104  friend class UT_ConditionLock;
105 #endif
106 
107 private:
108 #if defined(LINUX) || defined(MBSD)
109  pthread_mutex_t myLock;
110  pthread_mutexattr_t myLockAttributes;
111 #elif defined(WIN32)
112  struct Impl // Actually a Windows CRITICAL_SECTION
113  {
114  uint8 myBuffer[40];
115 
116  class CritSec;
117  CritSec *cast();
118  };
119 
120  Impl myLock;
121 #endif
122 
123 #if defined(DEBUG_LOCK_CALLERS)
124  int myLockCount;
125  UT_String myBacktrace;
126 #endif
127 };
128 
129 #else // HAVE_PLATFORM_LOCK
130 
131 #include "UT_SpinLock.h"
133 
134 #endif
135 
140 
141 #endif
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
UT_DebugLockType< UT_Lock > UT_DebugLock
Definition: UT_Lock.h:137
#define UT_API
Definition: UT_API.h:13
long long int64
Definition: SYS_Types.h:107
UT_AutoLockType< UT_Lock > UT_AutoLock
Definition: UT_Lock.h:136
UT_ObjLockType< UT_Lock > UT_ObjLock
Definition: UT_Lock.h:138
#define UT_VERIFY(expr)
Definition: UT_Assert.h:172
unsigned char uint8
Definition: SYS_Types.h:32
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126
UT_RecursiveSpinLock UT_Lock
Definition: UT_Lock.h:23
UT_AutoObjLockType< UT_Lock > UT_AutoObjLock
Definition: UT_Lock.h:139