Panda3D
|
00001 // Filename: mutexHolder.I 00002 // Created by: drose (09Aug02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: MutexHolder::Constructor 00018 // Access: Public 00019 // Description: 00020 //////////////////////////////////////////////////////////////////// 00021 INLINE MutexHolder:: 00022 MutexHolder(const Mutex &mutex) { 00023 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS) 00024 _mutex = &mutex; 00025 _mutex->acquire(); 00026 #endif 00027 } 00028 00029 //////////////////////////////////////////////////////////////////// 00030 // Function: MutexHolder::Constructor 00031 // Access: Public 00032 // Description: This variant on the constructor accepts the current 00033 // thread as a parameter, if it is already known, as an 00034 // optimization. 00035 //////////////////////////////////////////////////////////////////// 00036 INLINE MutexHolder:: 00037 MutexHolder(const Mutex &mutex, Thread *current_thread) { 00038 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS) 00039 _mutex = &mutex; 00040 // Actually, the regular Mutex class doesn't need the current thread 00041 // parameter at the moment. So not actually an optimization. But 00042 // we keep this method because it causes a symmetry with 00043 // ReMutexHolder. 00044 _mutex->acquire(/*current_thread*/); 00045 #endif 00046 } 00047 00048 //////////////////////////////////////////////////////////////////// 00049 // Function: MutexHolder::Constructor 00050 // Access: Public 00051 // Description: If the MutexHolder constructor is given a pointer to 00052 // a Mutex object (instead of an actual object), it will 00053 // first check to see if the pointer is NULL, and 00054 // allocate a new Mutex if it is. This is intended as a 00055 // convenience for functions that may need to reference 00056 // a Mutex at static init time, when it is impossible to 00057 // guarantee ordering of initializers. 00058 //////////////////////////////////////////////////////////////////// 00059 INLINE MutexHolder:: 00060 MutexHolder(Mutex *&mutex) { 00061 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS) 00062 if (mutex == (Mutex *)NULL) { 00063 mutex = new Mutex; 00064 } 00065 _mutex = mutex; 00066 _mutex->acquire(); 00067 #endif 00068 } 00069 00070 //////////////////////////////////////////////////////////////////// 00071 // Function: MutexHolder::Destructor 00072 // Access: Public 00073 // Description: 00074 //////////////////////////////////////////////////////////////////// 00075 INLINE MutexHolder:: 00076 ~MutexHolder() { 00077 #if defined(HAVE_THREADS) || defined(DEBUG_THREADS) 00078 _mutex->release(); 00079 #endif 00080 } 00081 00082 //////////////////////////////////////////////////////////////////// 00083 // Function: MutexHolder::Copy Constructor 00084 // Access: Private 00085 // Description: Do not attempt to copy MutexHolders. 00086 //////////////////////////////////////////////////////////////////// 00087 INLINE MutexHolder:: 00088 MutexHolder(const MutexHolder ©) { 00089 nassertv(false); 00090 } 00091 00092 //////////////////////////////////////////////////////////////////// 00093 // Function: MutexHolder::Copy Assignment Operator 00094 // Access: Private 00095 // Description: Do not attempt to copy MutexHolders. 00096 //////////////////////////////////////////////////////////////////// 00097 INLINE void MutexHolder:: 00098 operator = (const MutexHolder ©) { 00099 nassertv(false); 00100 }