Panda3D
 All Classes Functions Variables Enumerations
mutexHolder.I
1 // Filename: mutexHolder.I
2 // Created by: drose (09Aug02)
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: MutexHolder::Constructor
18 // Access: Public
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE MutexHolder::
22 MutexHolder(const Mutex &mutex) {
23 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS)
24  _mutex = &mutex;
25  _mutex->acquire();
26 #endif
27 }
28 
29 ////////////////////////////////////////////////////////////////////
30 // Function: MutexHolder::Constructor
31 // Access: Public
32 // Description: This variant on the constructor accepts the current
33 // thread as a parameter, if it is already known, as an
34 // optimization.
35 ////////////////////////////////////////////////////////////////////
36 INLINE MutexHolder::
37 MutexHolder(const Mutex &mutex, Thread *current_thread) {
38 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS)
39  _mutex = &mutex;
40  // Actually, the regular Mutex class doesn't need the current thread
41  // parameter at the moment. So not actually an optimization. But
42  // we keep this method because it causes a symmetry with
43  // ReMutexHolder.
44  _mutex->acquire(/*current_thread*/);
45 #endif
46 }
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: MutexHolder::Constructor
50 // Access: Public
51 // Description: If the MutexHolder constructor is given a pointer to
52 // a Mutex object (instead of an actual object), it will
53 // first check to see if the pointer is NULL, and
54 // allocate a new Mutex if it is. This is intended as a
55 // convenience for functions that may need to reference
56 // a Mutex at static init time, when it is impossible to
57 // guarantee ordering of initializers.
58 ////////////////////////////////////////////////////////////////////
59 INLINE MutexHolder::
60 MutexHolder(Mutex *&mutex) {
61 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS)
62  if (mutex == (Mutex *)NULL) {
63  mutex = new Mutex;
64  }
65  _mutex = mutex;
66  _mutex->acquire();
67 #endif
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: MutexHolder::Destructor
72 // Access: Public
73 // Description:
74 ////////////////////////////////////////////////////////////////////
75 INLINE MutexHolder::
76 ~MutexHolder() {
77 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS)
78  _mutex->release();
79 #endif
80 }
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function: MutexHolder::Copy Constructor
84 // Access: Private
85 // Description: Do not attempt to copy MutexHolders.
86 ////////////////////////////////////////////////////////////////////
87 INLINE MutexHolder::
88 MutexHolder(const MutexHolder &copy) {
89  nassertv(false);
90 }
91 
92 ////////////////////////////////////////////////////////////////////
93 // Function: MutexHolder::Copy Assignment Operator
94 // Access: Private
95 // Description: Do not attempt to copy MutexHolders.
96 ////////////////////////////////////////////////////////////////////
97 INLINE void MutexHolder::
98 operator = (const MutexHolder &copy) {
99  nassertv(false);
100 }
A standard mutex, or mutual exclusion lock.
Definition: pmutex.h:44
A lightweight C++ object whose constructor calls acquire() and whose destructor calls release() on a ...
Definition: mutexHolder.h:29
void acquire() const
Grabs the mutex if it is available.
Definition: mutexDirect.I:70
A thread; that is, a lightweight process.
Definition: thread.h:51