Panda3D
Loading...
Searching...
No Matches
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 */
17INLINE ReMutexDirect::
18ReMutexDirect()
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 */
33INLINE void ReMutexDirect::
34lock() {
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 */
47INLINE bool ReMutexDirect::
48try_lock() {
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 */
61INLINE void ReMutexDirect::
62unlock() {
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 */
81INLINE void ReMutexDirect::
82acquire() 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 */
95INLINE void ReMutexDirect::
96acquire(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 */
110try_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 */
124try_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 */
144elevate_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 */
161release() 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 */
177debug_is_locked() const {
178 return true;
179}
180
181/**
182 * The mutex name is only defined when compiling in DEBUG_THREADS mode.
183 */
185set_name(const std::string &) {
186}
187
188/**
189 * The mutex name is only defined when compiling in DEBUG_THREADS mode.
190 */
192clear_name() {
193}
194
195/**
196 * The mutex name is only defined when compiling in DEBUG_THREADS mode.
197 */
199has_name() const {
200 return false;
201}
202
203/**
204 * The mutex name is only defined when compiling in DEBUG_THREADS mode.
205 */
206INLINE std::string ReMutexDirect::
207get_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 */
218INLINE void ReMutexDirect::
219do_lock() {
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 */
231INLINE bool ReMutexDirect::
232do_try_lock() {
233 return do_try_lock(Thread::get_current_thread());
234}
235#endif
This class implements a standard reMutex by making direct calls to the underlying implementation laye...
std::string get_name() const
The mutex name is only defined when compiling in DEBUG_THREADS mode.
bool try_lock()
Alias for try_acquire() to match C++11 semantics.
void acquire() const
Grabs the reMutex if it is available.
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.
bool has_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.
void elevate_lock() const
This method increments the lock count, assuming the calling thread already holds the lock.
void release() const
Releases the reMutex.
void lock()
Alias for acquire() to match C++11 semantics.
bool try_acquire() const
Returns immediately, with a true value indicating the mutex has been acquired, and false indicating i...
void set_name(const std::string &name)
The mutex name is only defined when compiling in DEBUG_THREADS mode.
A thread; that is, a lightweight process.
Definition thread.h:46
get_current_thread
Returns a pointer to the currently-executing Thread object.
Definition thread.h:109