HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_AbortableLockImpl.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_AbortableLockImpl.h (UT Library, C++)
7  *
8  * COMMENTS: Implementation for UT_AbortableLock
9  *
10  */
11 
12 #ifndef __UT_ABORTABLELOCKIMPL_H_INCLUDED__
13 #define __UT_ABORTABLELOCKIMPL_H_INCLUDED__
14 
15 #ifndef UT_VAR
16 #define UT_VAR(X) (X)
17 #endif
18 
19 template <typename LOCKABLE>
20 bool
22 {
23  int thread_id = SYSgetSTID();
24  int timeout = 1000; // initial time out in ms
25  bool locked = false;
26 
27  // Fast path for low contention case
28  if (UT_VAR(myMutex).tryLock())
29  {
30  myImpl.markAsLocked(thread_id);
31  return true;
32  }
33 
34  // mark us as waiting in this thread
35  myImpl.markAsWaiting(thread_id);
36 
37  // attempt to acquire the lock in a loop with exponential back off
38  while (true)
39  {
40  if (UT_VAR(myMutex).timedLock(timeout))
41  {
42  // We're no longer waiting in this thread
43  // NB: We *must* clear theWaitingLock before marking ourselves as
44  // the thread owner or else some other thread waiting on the
45  // mutex suddenly might think that we're waiting on the lock
46  // while having already acquired it!
47  myImpl.markAsNotWaiting(thread_id);
48  myImpl.markAsLocked(thread_id);
49  locked = true;
50  break;
51  }
52  else if (myImpl.findDeadlock(thread_id))
53  {
54  // We're no longer waiting in this thread
55  myImpl.markAsNotWaiting(thread_id);
56  locked = false;
57  break;
58  }
59  // else retry with double the duration
60  timeout *= 2;
61  }
62 
63  return locked;
64 }
65 
66 template <typename LOCKABLE>
67 bool
69 {
70  if (UT_VAR(myMutex).tryLock())
71  {
72  myImpl.markAsLocked(SYSgetSTID());
73  return true;
74  }
75 
76  return false;
77 }
78 
79 template <typename LOCKABLE>
80 void
82 {
83  UT_VAR(myMutex).lock();
84  myImpl.markAsLocked(SYSgetSTID());
85 }
86 
87 template <typename LOCKABLE>
88 void
90 {
91  myImpl.markAsUnlocked();
92  UT_VAR(myMutex).unlock();
93 }
94 
95 template <typename LOCKABLE>
96 inline bool
98 {
99  return myImpl.hasLock(thread);
100 }
101 
102 template <typename LOCKABLE>
103 bool
105 {
106  return hasLock(SYSgetSTID());
107 }
108 
109 #endif // __UT_ABORTABLELOCKIMPL_H_INCLUDED__
bool safeLock()
Attempt lock on this mutex. Returns true on success, false if deadlock.
void unlock()
Release lock. Undefined if it was not previously locked.
GLbitfield GLuint64 timeout
Definition: glcorearb.h:1598
bool tryLock()
Tries the lock without blocking. Returns true if the lock was obtained.
SYS_API int SYSgetSTID()
#define UT_VAR(X)
void lock()
Locks the underlying mutex without testing for deadlocks.