Panda3D
reMutexDirect.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file reMutexDirect.I
10  * @author drose
11  * @date 2006-02-13
12  */
13 
14 /**
15  *
16  */
17 INLINE ReMutexDirect::
18 ReMutexDirect()
19 #ifndef HAVE_REMUTEXTRUEIMPL
20  : _cvar_impl(_lock_impl)
21 #endif
22 {
23 #ifndef HAVE_REMUTEXTRUEIMPL
24  _locking_thread = nullptr;
25  _lock_count = 0;
26 #endif
27 }
28 
29 /**
30  * Alias for acquire() to match C++11 semantics.
31  * @see acquire()
32  */
33 INLINE void ReMutexDirect::
34 lock() {
35  TAU_PROFILE("void ReMutexDirect::acquire()", " ", TAU_USER);
36 #ifdef HAVE_REMUTEXTRUEIMPL
37  _impl.lock();
38 #else
39  ((ReMutexDirect *)this)->do_lock();
40 #endif // HAVE_REMUTEXTRUEIMPL
41 }
42 
43 /**
44  * Alias for try_acquire() to match C++11 semantics.
45  * @see try_acquire()
46  */
47 INLINE bool ReMutexDirect::
49  TAU_PROFILE("void ReMutexDirect::try_acquire()", " ", TAU_USER);
50 #ifdef HAVE_REMUTEXTRUEIMPL
51  return _impl.try_lock();
52 #else
53  return ((ReMutexDirect *)this)->do_try_lock();
54 #endif // HAVE_REMUTEXTRUEIMPL
55 }
56 
57 /**
58  * Alias for release() to match C++11 semantics.
59  * @see release()
60  */
61 INLINE void ReMutexDirect::
62 unlock() {
63  TAU_PROFILE("void ReMutexDirect::unlock()", " ", TAU_USER);
64 #ifdef HAVE_REMUTEXTRUEIMPL
65  _impl.unlock();
66 #else
67  ((ReMutexDirect *)this)->do_unlock();
68 #endif // HAVE_REMUTEXTRUEIMPL
69 }
70 
71 /**
72  * Grabs the reMutex if it is available. If it is not available, blocks until
73  * it becomes available, then grabs it. In either case, the function does not
74  * return until the reMutex is held; you should then call unlock().
75  *
76  * This method is considered const so that you can lock and unlock const
77  * reMutexes, mainly to allow thread-safe access to otherwise const data.
78  *
79  * Also see ReMutexHolder.
80  */
81 INLINE void ReMutexDirect::
82 acquire() const {
83  TAU_PROFILE("void ReMutexDirect::acquire()", " ", TAU_USER);
84 #ifdef HAVE_REMUTEXTRUEIMPL
85  _impl.lock();
86 #else
87  ((ReMutexDirect *)this)->do_lock();
88 #endif // HAVE_REMUTEXTRUEIMPL
89 }
90 
91 /**
92  * This variant on acquire() accepts the current thread as a parameter, if it
93  * is already known, as an optimization.
94  */
95 INLINE void ReMutexDirect::
96 acquire(Thread *current_thread) const {
97  TAU_PROFILE("void ReMutexDirect::acquire(Thread *)", " ", TAU_USER);
98 #ifdef HAVE_REMUTEXTRUEIMPL
99  _impl.lock();
100 #else
101  ((ReMutexDirect *)this)->do_lock(current_thread);
102 #endif // HAVE_REMUTEXTRUEIMPL
103 }
104 
105 /**
106  * Returns immediately, with a true value indicating the mutex has been
107  * acquired, and false indicating it has not.
108  */
109 INLINE bool ReMutexDirect::
110 try_acquire() const {
111  TAU_PROFILE("void ReMutexDirect::acquire(bool)", " ", TAU_USER);
112 #ifdef HAVE_REMUTEXTRUEIMPL
113  return _impl.try_lock();
114 #else
115  return ((ReMutexDirect *)this)->do_try_lock();
116 #endif // HAVE_REMUTEXTRUEIMPL
117 }
118 
119 /**
120  * Returns immediately, with a true value indicating the mutex has been
121  * acquired, and false indicating it has not.
122  */
123 INLINE bool ReMutexDirect::
124 try_acquire(Thread *current_thread) const {
125  TAU_PROFILE("void ReMutexDirect::acquire(bool)", " ", TAU_USER);
126 #ifdef HAVE_REMUTEXTRUEIMPL
127  return _impl.try_lock();
128 #else
129  return ((ReMutexDirect *)this)->do_try_lock(current_thread);
130 #endif // HAVE_REMUTEXTRUEIMPL
131 }
132 
133 /**
134  * This method increments the lock count, assuming the calling thread already
135  * holds the lock. After this call, release() will need to be called one
136  * additional time to release the lock.
137  *
138  * This method really performs the same function as acquire(), but it offers a
139  * potential (slight) performance benefit when the calling thread knows that
140  * it already holds the lock. It is an error to call this when the calling
141  * thread does not hold the lock.
142  */
143 INLINE void ReMutexDirect::
144 elevate_lock() const {
145  TAU_PROFILE("void ReMutexDirect::elevate_lock()", " ", TAU_USER);
146 #ifdef HAVE_REMUTEXTRUEIMPL
147  _impl.lock();
148 #else
149  ((ReMutexDirect *)this)->do_elevate_lock();
150 #endif // HAVE_REMUTEXTRUEIMPL
151 }
152 
153 /**
154  * Releases the reMutex. It is an error to call this if the reMutex was not
155  * already locked.
156  *
157  * This method is considered const so that you can lock and unlock const
158  * reMutexes, mainly to allow thread-safe access to otherwise const data.
159  */
160 INLINE void ReMutexDirect::
161 release() const {
162  TAU_PROFILE("void ReMutexDirect::release()", " ", TAU_USER);
163 #ifdef HAVE_REMUTEXTRUEIMPL
164  _impl.unlock();
165 #else
166  ((ReMutexDirect *)this)->do_unlock();
167 #endif // HAVE_REMUTEXTRUEIMPL
168 }
169 
170 /**
171  * Returns true if the current thread has locked the ReMutex, false otherwise.
172  * This method is only intended for use in debugging, hence the method name;
173  * in the ReMutexDirect case, it always returns true, since there's not a
174  * reliable way to determine this otherwise.
175  */
176 INLINE bool ReMutexDirect::
178  return true;
179 }
180 
181 /**
182  * The mutex name is only defined when compiling in DEBUG_THREADS mode.
183  */
184 INLINE void ReMutexDirect::
185 set_name(const std::string &) {
186 }
187 
188 /**
189  * The mutex name is only defined when compiling in DEBUG_THREADS mode.
190  */
191 INLINE void ReMutexDirect::
193 }
194 
195 /**
196  * The mutex name is only defined when compiling in DEBUG_THREADS mode.
197  */
198 INLINE bool ReMutexDirect::
199 has_name() const {
200  return false;
201 }
202 
203 /**
204  * The mutex name is only defined when compiling in DEBUG_THREADS mode.
205  */
206 INLINE std::string ReMutexDirect::
207 get_name() const {
208  return std::string();
209 }
210 
211 #ifndef HAVE_REMUTEXTRUEIMPL
212 /**
213  * The private implementation of acquire(), for the case in which the
214  * underlying lock system does not provide a reentrant mutex (and therefore we
215  * have to build this functionality on top of the existing non-reentrant
216  * mutex).
217  */
218 INLINE void ReMutexDirect::
219 do_lock() {
220  do_lock(Thread::get_current_thread());
221 }
222 #endif
223 
224 #ifndef HAVE_REMUTEXTRUEIMPL
225 /**
226  * The private implementation of acquire(false), for the case in which the
227  * underlying lock system does not provide a reentrant mutex (and therefore we
228  * have to build this functionality on top of the existing non-reentrant
229  * mutex).
230  */
231 INLINE bool ReMutexDirect::
232 do_try_lock() {
233  return do_try_lock(Thread::get_current_thread());
234 }
235 #endif
bool try_lock()
Alias for try_acquire() to match C++11 semantics.
Definition: reMutexDirect.I:48
bool has_name() const
The mutex name is only defined when compiling in DEBUG_THREADS mode.
bool try_acquire() const
Returns immediately, with a true value indicating the mutex has been acquired, and false indicating i...
void acquire() const
Grabs the reMutex if it is available.
Definition: reMutexDirect.I:82
void release() const
Releases the reMutex.
void set_name(const std::string &name)
The mutex name is only defined when compiling in DEBUG_THREADS mode.
void lock()
Alias for acquire() to match C++11 semantics.
Definition: reMutexDirect.I:34
std::string get_name() const
The mutex name is only defined when compiling in DEBUG_THREADS mode.
void clear_name()
The mutex name is only defined when compiling in DEBUG_THREADS mode.
This class implements a standard reMutex by making direct calls to the underlying implementation laye...
Definition: reMutexDirect.h:29
bool debug_is_locked() const
Returns true if the current thread has locked the ReMutex, false otherwise.
void unlock()
Alias for release() to match C++11 semantics.
Definition: reMutexDirect.I:62
A thread; that is, a lightweight process.
Definition: thread.h:46
void elevate_lock() const
This method increments the lock count, assuming the calling thread already holds the lock.