Panda3D
reMutexHolder.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 reMutexHolder.I
10  * @author drose
11  * @date 2006-01-15
12  */
13 
14 /**
15  *
16  */
17 INLINE ReMutexHolder::
18 ReMutexHolder(const ReMutex &mutex) {
19 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS)
20  _mutex = &mutex;
21  _mutex->acquire();
22 #endif
23 }
24 
25 /**
26  * This variant on the constructor accepts the current thread as a parameter,
27  * if it is already known, as an optimization.
28  */
29 INLINE ReMutexHolder::
30 ReMutexHolder(const ReMutex &mutex, Thread *current_thread) {
31 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS)
32  _mutex = &mutex;
33  _mutex->acquire(current_thread);
34 #endif
35 }
36 
37 /**
38  * If the ReMutexHolder constructor is given a pointer to a ReMutex object
39  * (instead of an actual object), it will first check to see if the pointer is
40  * NULL, and allocate a new ReMutex if it is. This is intended as a
41  * convenience for functions that may need to reference a ReMutex at static
42  * init time, when it is impossible to guarantee ordering of initializers.
43  */
44 INLINE ReMutexHolder::
45 ReMutexHolder(ReMutex *&mutex) {
46 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS)
47  if (mutex == nullptr) {
48  mutex = new ReMutex;
49  }
50  _mutex = mutex;
51  _mutex->acquire();
52 #endif
53 }
54 
55 /**
56  *
57  */
58 INLINE ReMutexHolder::
59 ~ReMutexHolder() {
60 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS)
61  _mutex->release();
62 #endif
63 }
void acquire() const
Grabs the reMutex if it is available.
Definition: reMutexDirect.I:82
A thread; that is, a lightweight process.
Definition: thread.h:46
A reentrant mutex.
Definition: reMutex.h:32