Panda3D
reMutexDirect.I
1 // Filename: reMutexDirect.I
2 // Created by: drose (13Feb06)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: ReMutexDirect::Constructor
18 // Access: Protected
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE ReMutexDirect::
22 ReMutexDirect()
23 #ifndef HAVE_REMUTEXTRUEIMPL
24  : _cvar_impl(_lock_impl)
25 #endif
26 {
27 #ifndef HAVE_REMUTEXTRUEIMPL
28  _locking_thread = NULL;
29  _lock_count = 0;
30 #endif
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: ReMutexDirect::Destructor
35 // Access: Protected
36 // Description:
37 ////////////////////////////////////////////////////////////////////
38 INLINE ReMutexDirect::
39 ~ReMutexDirect() {
40 }
41 
42 ////////////////////////////////////////////////////////////////////
43 // Function: ReMutexDirect::Copy Constructor
44 // Access: Private
45 // Description: Do not attempt to copy reMutexes.
46 ////////////////////////////////////////////////////////////////////
47 INLINE ReMutexDirect::
48 ReMutexDirect(const ReMutexDirect &copy)
49 #ifndef HAVE_REMUTEXTRUEIMPL
50  : _cvar_impl(_lock_impl)
51 #endif
52 {
53  nassertv(false);
54 }
55 
56 ////////////////////////////////////////////////////////////////////
57 // Function: ReMutexDirect::Copy Assignment Operator
58 // Access: Private
59 // Description: Do not attempt to copy reMutexes.
60 ////////////////////////////////////////////////////////////////////
61 INLINE void ReMutexDirect::
62 operator = (const ReMutexDirect &copy) {
63  nassertv(false);
64 }
65 
66 ////////////////////////////////////////////////////////////////////
67 // Function: ReMutexDirect::acquire
68 // Access: Published
69 // Description: Grabs the reMutex if it is available. If it is not
70 // available, blocks until it becomes available, then
71 // grabs it. In either case, the function does not
72 // return until the reMutex is held; you should then call
73 // unlock().
74 //
75 // This method is considered const so that you can lock
76 // and unlock const reMutexes, mainly to allow thread-safe
77 // 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  ((ReMutexDirect *)this)->_impl.acquire();
86 #else
87  ((ReMutexDirect *)this)->do_acquire();
88 #endif // HAVE_REMUTEXTRUEIMPL
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: ReMutexDirect::acquire
93 // Access: Published
94 // Description: This variant on acquire() accepts the current thread as
95 // a parameter, if it is already known, as an
96 // optimization.
97 ////////////////////////////////////////////////////////////////////
98 INLINE void ReMutexDirect::
99 acquire(Thread *current_thread) const {
100  TAU_PROFILE("void ReMutexDirect::acquire(Thread *)", " ", TAU_USER);
101 #ifdef HAVE_REMUTEXTRUEIMPL
102  ((ReMutexDirect *)this)->_impl.acquire();
103 #else
104  ((ReMutexDirect *)this)->do_acquire(current_thread);
105 #endif // HAVE_REMUTEXTRUEIMPL
106 }
107 
108 ////////////////////////////////////////////////////////////////////
109 // Function: ReMutexDirect::try_acquire
110 // Access: Published
111 // Description: Returns immediately, with a true value indicating the
112 // mutex has been acquired, and false indicating it has
113 // not.
114 ////////////////////////////////////////////////////////////////////
115 INLINE bool ReMutexDirect::
116 try_acquire() const {
117  TAU_PROFILE("void ReMutexDirect::acquire(bool)", " ", TAU_USER);
118 #ifdef HAVE_REMUTEXTRUEIMPL
119  return ((ReMutexDirect *)this)->_impl.try_acquire();
120 #else
121  return ((ReMutexDirect *)this)->do_try_acquire();
122 #endif // HAVE_REMUTEXTRUEIMPL
123 }
124 
125 ////////////////////////////////////////////////////////////////////
126 // Function: ReMutexDirect::try_acquire
127 // Access: Published
128 // Description: Returns immediately, with a true value indicating the
129 // mutex has been acquired, and false indicating it has
130 // not.
131 ////////////////////////////////////////////////////////////////////
132 INLINE bool ReMutexDirect::
133 try_acquire(Thread *current_thread) const {
134  TAU_PROFILE("void ReMutexDirect::acquire(bool)", " ", TAU_USER);
135 #ifdef HAVE_REMUTEXTRUEIMPL
136  return ((ReMutexDirect *)this)->_impl.try_acquire();
137 #else
138  return ((ReMutexDirect *)this)->do_try_acquire(current_thread);
139 #endif // HAVE_REMUTEXTRUEIMPL
140 }
141 
142 ////////////////////////////////////////////////////////////////////
143 // Function: ReMutexDirect::elevate_lock
144 // Access: Published
145 // Description: This method increments the lock count, assuming the
146 // calling thread already holds the lock. After this
147 // call, release() will need to be called one additional
148 // time to release the lock.
149 //
150 // This method really performs the same function as
151 // acquire(), but it offers a potential (slight)
152 // performance benefit when the calling thread knows
153 // that it already holds the lock. It is an error to
154 // call this when the calling thread does not hold the
155 // lock.
156 ////////////////////////////////////////////////////////////////////
157 INLINE void ReMutexDirect::
158 elevate_lock() const {
159  TAU_PROFILE("void ReMutexDirect::elevate_lock()", " ", TAU_USER);
160 #ifdef HAVE_REMUTEXTRUEIMPL
161  ((ReMutexDirect *)this)->_impl.acquire();
162 #else
163  ((ReMutexDirect *)this)->do_elevate_lock();
164 #endif // HAVE_REMUTEXTRUEIMPL
165 }
166 
167 ////////////////////////////////////////////////////////////////////
168 // Function: ReMutexDirect::release
169 // Access: Published
170 // Description: Releases the reMutex. It is an error to call this if
171 // the reMutex was not already locked.
172 //
173 // This method is considered const so that you can lock
174 // and unlock const reMutexes, mainly to allow thread-safe
175 // access to otherwise const data.
176 ////////////////////////////////////////////////////////////////////
177 INLINE void ReMutexDirect::
178 release() const {
179  TAU_PROFILE("void ReMutexDirect::release()", " ", TAU_USER);
180 #ifdef HAVE_REMUTEXTRUEIMPL
181  ((ReMutexDirect *)this)->_impl.release();
182 #else
183  ((ReMutexDirect *)this)->do_release();
184 #endif // HAVE_REMUTEXTRUEIMPL
185 }
186 
187 ////////////////////////////////////////////////////////////////////
188 // Function: ReMutexDirect::debug_is_locked
189 // Access: Published
190 // Description: Returns true if the current thread has locked the
191 // ReMutex, false otherwise. This method is only intended
192 // for use in debugging, hence the method name; in the
193 // ReMutexDirect case, it always returns true, since
194 // there's not a reliable way to determine this
195 // otherwise.
196 ////////////////////////////////////////////////////////////////////
197 INLINE bool ReMutexDirect::
199  return true;
200 }
201 
202 ////////////////////////////////////////////////////////////////////
203 // Function: ReMutexDirect::set_name
204 // Access: Public
205 // Description: The mutex name is only defined when compiling in
206 // DEBUG_THREADS mode.
207 ////////////////////////////////////////////////////////////////////
208 INLINE void ReMutexDirect::
209 set_name(const string &) {
210 }
211 
212 ////////////////////////////////////////////////////////////////////
213 // Function: ReMutexDirect::clear_name
214 // Access: Public
215 // Description: The mutex name is only defined when compiling in
216 // DEBUG_THREADS mode.
217 ////////////////////////////////////////////////////////////////////
218 INLINE void ReMutexDirect::
220 }
221 
222 ////////////////////////////////////////////////////////////////////
223 // Function: ReMutexDirect::has_name
224 // Access: Public
225 // Description: The mutex name is only defined when compiling in
226 // DEBUG_THREADS mode.
227 ////////////////////////////////////////////////////////////////////
228 INLINE bool ReMutexDirect::
229 has_name() const {
230  return false;
231 }
232 
233 ////////////////////////////////////////////////////////////////////
234 // Function: ReMutexDirect::get_name
235 // Access: Public
236 // Description: The mutex name is only defined when compiling in
237 // DEBUG_THREADS mode.
238 ////////////////////////////////////////////////////////////////////
239 INLINE string ReMutexDirect::
240 get_name() const {
241  return string();
242 }
243 
244 #ifndef HAVE_REMUTEXTRUEIMPL
245 ////////////////////////////////////////////////////////////////////
246 // Function: ReMutexDirect::do_acquire
247 // Access: Private
248 // Description: The private implementation of acquire(), for the case in
249 // which the underlying lock system does not provide a
250 // reentrant mutex (and therefore we have to build this
251 // functionality on top of the existing non-reentrant
252 // mutex).
253 ////////////////////////////////////////////////////////////////////
254 INLINE void ReMutexDirect::
255 do_acquire() {
256  do_acquire(Thread::get_current_thread());
257 }
258 #endif
259 
260 #ifndef HAVE_REMUTEXTRUEIMPL
261 ////////////////////////////////////////////////////////////////////
262 // Function: ReMutexDirect::do_try_acquire
263 // Access: Private
264 // Description: The private implementation of acquire(false), for the
265 // case in which the underlying lock system does not
266 // provide a reentrant mutex (and therefore we have to
267 // build this functionality on top of the existing
268 // non-reentrant mutex).
269 ////////////////////////////////////////////////////////////////////
270 INLINE bool ReMutexDirect::
271 do_try_acquire() {
272  return do_try_acquire(Thread::get_current_thread());
273 }
274 #endif
275 
bool has_name() const
The mutex name is only defined when compiling in DEBUG_THREADS mode.
string get_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 set_name(const string &name)
The mutex name is only defined when compiling in DEBUG_THREADS mode.
void release() const
Releases the reMutex.
static Thread * get_current_thread()
Returns a pointer to the currently-executing Thread object.
Definition: thread.I:145
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:32
bool debug_is_locked() const
Returns true if the current thread has locked the ReMutex, false otherwise.
A thread; that is, a lightweight process.
Definition: thread.h:51
void elevate_lock() const
This method increments the lock count, assuming the calling thread already holds the lock...