forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutexDebug.I
More file actions
151 lines (140 loc) · 4.6 KB
/
mutexDebug.I
File metadata and controls
151 lines (140 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file mutexDebug.I
* @author drose
* @date 2006-02-13
*/
/**
* Alias for acquire() to match C++11 semantics.
* @see acquire()
*/
INLINE void MutexDebug::
lock() {
TAU_PROFILE("void MutexDebug::acquire()", " ", TAU_USER);
_global_lock->lock();
((MutexDebug *)this)->do_lock(Thread::get_current_thread());
_global_lock->unlock();
}
/**
* Alias for try_acquire() to match C++11 semantics.
* @see try_acquire()
*/
INLINE bool MutexDebug::
try_lock() {
TAU_PROFILE("void MutexDebug::try_lock()", " ", TAU_USER);
_global_lock->lock();
bool acquired = ((MutexDebug *)this)->do_try_lock(Thread::get_current_thread());
_global_lock->unlock();
return acquired;
}
/**
* Alias for release() to match C++11 semantics.
* @see release()
*/
INLINE void MutexDebug::
unlock() {
TAU_PROFILE("void MutexDebug::unlock()", " ", TAU_USER);
_global_lock->lock();
((MutexDebug *)this)->do_unlock();
_global_lock->unlock();
}
/**
* Grabs the mutex if it is available. If it is not available, blocks until
* it becomes available, then grabs it. In either case, the function does not
* return until the mutex is held; you should then call unlock().
*
* This method is considered const so that you can lock and unlock const
* mutexes, mainly to allow thread-safe access to otherwise const data.
*
* Also see MutexHolder.
*/
INLINE void MutexDebug::
acquire(Thread *current_thread) const {
TAU_PROFILE("void MutexDebug::acquire(Thread *)", " ", TAU_USER);
nassertv(current_thread == Thread::get_current_thread());
_global_lock->lock();
((MutexDebug *)this)->do_lock(current_thread);
_global_lock->unlock();
}
/**
* Returns immediately, with a true value indicating the mutex has been
* acquired, and false indicating it has not.
*
* @deprecated Python users should use acquire(False), C++ users try_lock()
*/
INLINE bool MutexDebug::
try_acquire(Thread *current_thread) const {
TAU_PROFILE("void MutexDebug::try_acquire(Thread *)", " ", TAU_USER);
nassertr(current_thread == Thread::get_current_thread(), false);
_global_lock->lock();
bool acquired = ((MutexDebug *)this)->do_try_lock(current_thread);
_global_lock->unlock();
return acquired;
}
/**
* This method increments the lock count, assuming the calling thread already
* holds the lock. After this call, release() will need to be called one
* additional time to release the lock.
*
* This method really performs the same function as acquire(), but it offers a
* potential (slight) performance benefit when the calling thread knows that
* it already holds the lock. It is an error to call this when the calling
* thread does not hold the lock.
*/
INLINE void MutexDebug::
elevate_lock() const {
TAU_PROFILE("void MutexDebug::elevate_lock()", " ", TAU_USER);
// You may only pass call elevate_lock() on a ReMutex--that is, to a mutex
// whose _allow_recursion flag is true.
nassertv(_allow_recursion);
// Also, it's an error to call this if the lock is not already held.
nassertv(debug_is_locked());
acquire();
}
/**
* Releases the mutex. It is an error to call this if the mutex was not
* already locked.
*
* This method is considered const so that you can lock and unlock const
* mutexes, mainly to allow thread-safe access to otherwise const data.
*/
INLINE void MutexDebug::
release() const {
TAU_PROFILE("void MutexDebug::release()", " ", TAU_USER);
_global_lock->lock();
((MutexDebug *)this)->do_unlock();
_global_lock->unlock();
}
/**
* Returns true if the current thread has locked the Mutex, false otherwise.
* This method is only intended for use in debugging, hence the method name;
* in the MutexDebug case, it always returns true, since there's not a
* reliable way to determine this otherwise.
*/
INLINE bool MutexDebug::
debug_is_locked() const {
TAU_PROFILE("bool MutexDebug::debug_is_locked()", " ", TAU_USER);
_global_lock->lock();
bool is_locked = do_debug_is_locked();
_global_lock->unlock();
return is_locked;
}
/**
* Ensures the global MutexImpl pointer has been created, and returns its
* pointer. Since this method is called by the MutexDebug constructor, any
* other (non-static) methods of MutexDebug may simply assume that the pointer
* has already been created.
*/
INLINE MutexTrueImpl *MutexDebug::
get_global_lock() {
if (_global_lock == nullptr) {
_global_lock = new MutexTrueImpl;
}
return _global_lock;
}