HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_RecursiveTimedLock.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_RecursiveTimedLock.h (UT Library, C++)
7  *
8  * COMMENTS: Recursive mutex that supports timedLock(), required
9  * by UT_AbortableLock.
10  *
11  */
12 
13 #ifndef __UT_RECURSIVETIMEDLOCK_H_INCLUDED__
14 #define __UT_RECURSIVETIMEDLOCK_H_INCLUDED__
15 
16 #include "UT_API.h"
17 #include "UT_Assert.h"
18 #include "UT_Lockable.h"
19 #include "UT_NonCopyable.h"
20 #include <SYS/SYS_Types.h>
21 
22 #if defined(USE_PTHREADS) && !defined(MBSD)
23 //
24 // pthread implementation
25 //
26 
27 #include <pthread.h>
28 #include <sys/time.h>
29 #include <errno.h>
30 
31 #if defined(MBSD)
32  #define UT_PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE
33 #else
34  #define UT_PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP
35 #endif
36 
38 {
39 public:
41  {
42  pthread_mutexattr_t attr;
43  UT_VERIFY(pthread_mutexattr_init(&attr) == 0);
44  UT_VERIFY(pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE)==0);
45  UT_VERIFY(pthread_mutex_init(&myMutex, &attr) == 0);
46  UT_VERIFY(pthread_mutexattr_destroy(&attr) == 0);
47  }
49  {
50  pthread_mutex_destroy(&myMutex);
51  }
52 
54 
55  bool tryLock()
56  {
57  int res = pthread_mutex_trylock(&myMutex);
58  UT_ASSERT(res == 0 || res == EBUSY);
59  return (res == 0);
60  }
61 
62  void lock()
63  {
64  UT_VERIFY(pthread_mutex_lock(&myMutex) == 0);
65  }
66 
67  bool timedLock(int ms)
68  {
69  const uint64 THOUSAND = 1000;
70  const uint64 MILLION = THOUSAND * THOUSAND;
71  struct timeval now;
72  struct timespec timeout;
73  uint64 usec;
74 
75  gettimeofday(&now, NULL);
76  usec = now.tv_usec + THOUSAND*(ms + now.tv_sec*THOUSAND);
77  timeout.tv_sec = (time_t)(usec / MILLION);
78  timeout.tv_nsec = (long)((usec % MILLION) * THOUSAND);
79 
80  int res = pthread_mutex_timedlock(&myMutex, &timeout);
81  UT_ASSERT(res == 0|| res == ETIMEDOUT);
82  return (res == 0);
83  }
84 
85  void unlock()
86  {
87  UT_VERIFY(pthread_mutex_unlock(&myMutex) == 0);
88  }
89 
90  bool isLocked()
91  {
92  if (tryLock())
93  {
94  unlock();
95  return false;
96  }
97  return true;
98  }
99 
100  /// Class for auto-unlocking
102 
103 private:
104  pthread_mutex_t myMutex;
105 };
106 
107 #elif defined(_WIN32) || defined(MBSD)
108 //
109 // hboost::recursive_timed_mutex is fastest on Windows and OSX,
110 // beats std::recursive_timed_mutex on those platforms.
111 //
112 
113 #include <SYS/SYS_BoostThread.h>
114 
115 /// Lock adapter for boost mutexes
116 template <class HBOOST_MUTEX>
117 class UT_BoostLockable : public UT_Lockable<HBOOST_MUTEX>
118 {
119 public:
120  bool timedLock(int ms)
121  {
123  .timed_lock(hboost::posix_time::milliseconds(ms));
124  }
125 };
126 
127 // Use hboost::recursive_timed_mutex
128 typedef UT_BoostLockable<hboost::recursive_timed_mutex> UT_RecursiveTimedLock;
129 
130 #else
131 //
132 // C++11 standard implementation
133 //
134 // NOTE: Currently unused because:
135 // - It slower than hboost::recursive_timed_mutex on Windows/OSX
136 // (see testut -it TESTUT_BoostThread)
137 // - On gcc 4.8, it's completely broken, only fixed in g++ >= 4.9
138 //
139 
140 #include <chrono>
141 #include <mutex>
142 
143 /// Lock adapter for std mutexes
144 template <class STD_MUTEX>
145 class UT_StdLockable : public UT_Lockable<STD_MUTEX>
146 {
147 public:
148  bool timedLock(int ms)
149  {
151  .try_lock_for(std::chrono::milliseconds(ms));
152  }
153 };
155 
156 #endif
157 
158 #endif // __UT_RECURSIVETIMEDLOCK_H_INCLUDED__
UT_Lock interface adapter for C++11 mutexes.
Definition: UT_Lockable.h:22
unsigned long long uint64
Definition: SYS_Types.h:117
MUTEX & mutex()
Definition: UT_Lockable.h:53
Lock adapter for std mutexes.
GLbitfield GLuint64 timeout
Definition: glcorearb.h:1599
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
UT_UniqueLock< UT_Lockable< STD_MUTEX > > Scope
Definition: UT_Lockable.h:30
#define UT_VERIFY(expr)
Definition: UT_Assert.h:202
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
UT_StdLockable< std::recursive_timed_mutex > UT_RecursiveTimedLock