Selaa lähdekoodia

define CHECK_REENTRANT_MUTEX

David Rose 20 vuotta sitten
vanhempi
sitoutus
01a2a7c13c

+ 4 - 0
dtool/Config.pp

@@ -566,6 +566,10 @@
 // define this true unless you have NSPR installed.
 #define HAVE_THREADS
 
+// Even if threading is not defined, you might want to double-check
+// that ordinary mutexes are not locked reentrantly:
+#define CHECK_REENTRANT_MUTEX
+
 // Do you want to build the network interface?  What additional libraries
 // are required?  Currently, this requires NSPR.
 #define NET_IPATH

+ 3 - 0
dtool/LocalSetup.pp

@@ -232,6 +232,9 @@ $[cdefine HAVE_CHROMIUM]
 /* Define if we want to compile the threading code.  */
 $[cdefine HAVE_THREADS]
 
+/* Define to check that ordinary (non-reentrant) Mutexes are not reentrantly locked. */
+$[cdefine CHECK_REENTRANT_MUTEX]
+
 /* Define if we want to compile the net code.  */
 $[cdefine HAVE_NET]
 

+ 1 - 0
dtool/Package.pp

@@ -213,6 +213,7 @@
 #set HAVE_OPENCV $[HAVE_OPENCV]
 
 #set HAVE_THREADS $[HAVE_THREADS]
+#set CHECK_REENTRANT_MUTEX $[CHECK_REENTRANT_MUTEX]
 
 #set NET_IPATH $[unixfilename $[NET_IPATH]]
 #set NET_LPATH $[unixfilename $[NET_LPATH]]

+ 4 - 4
panda/src/express/conditionVar.I

@@ -104,10 +104,10 @@ get_mutex() {
 ////////////////////////////////////////////////////////////////////
 INLINE void ConditionVar::
 wait() {
-  nassertv(_mutex._locking_thread == Thread::get_current_thread());
+  nassertv(_mutex.debug_is_locked());
   _impl.wait();
 
-#ifndef NDEBUG
+#ifdef CHECK_REENTRANT_MUTEX
   _mutex._locking_thread = Thread::get_current_thread();
 #endif
 }
@@ -130,7 +130,7 @@ wait() {
 ////////////////////////////////////////////////////////////////////
 INLINE void ConditionVar::
 signal() {
-  nassertv(_mutex._locking_thread == Thread::get_current_thread());
+  nassertv(_mutex.debug_is_locked());
   _impl.signal();
 }
 
@@ -146,6 +146,6 @@ signal() {
 ////////////////////////////////////////////////////////////////////
 INLINE void ConditionVar::
 signal_all() {
-  nassertv(_mutex._locking_thread == Thread::get_current_thread());
+  nassertv(_mutex.debug_is_locked());
   _impl.signal_all();
 }

+ 21 - 6
panda/src/express/pmutex.I

@@ -72,8 +72,8 @@ operator = (const Mutex &copy) {
 ////////////////////////////////////////////////////////////////////
 INLINE void Mutex::
 lock() const {
-#ifdef NDEBUG
-  // In the NDEBUG case, just lock the thing immediately.  Don't
+#ifndef CHECK_REENTRANT_MUTEX
+  // In the production case, just lock the thing immediately.  Don't
   // bother with the out-of-line do_lock() method, since we won't be
   // performing any checks anyway.
   ((MutexImpl &)_impl).lock();
@@ -97,10 +97,10 @@ lock() const {
 ////////////////////////////////////////////////////////////////////
 INLINE void Mutex::
 release() const {
-#ifdef NDEBUG
-  // In the NDEBUG case, just release the thing immediately.  Don't
-  // bother with the out-of-line do_release() method, since we won't
-  // be performing any checks anyway.
+#ifndef CHECK_REENTRANT_MUTEX
+  // In the production case, just release the thing immediately.
+  // Don't bother with the out-of-line do_release() method, since we
+  // won't be performing any checks anyway.
   ((MutexImpl &)_impl).release();
 
 #else 
@@ -109,3 +109,18 @@ release() const {
   ((Mutex *)this)->do_release();
 #endif
 }
+
+#ifndef CHECK_REENTRANT_MUTEX
+////////////////////////////////////////////////////////////////////
+//     Function: Mutex::debug_is_locked
+//       Access: Public
+//  Description: Returns true if the current thread has locked the
+//               Mutex, false otherwise.  This method is only
+//               meaningful if CHECK_REENTRANT_MUTEX is defined;
+//               otherwise, it always returns true.
+////////////////////////////////////////////////////////////////////
+INLINE bool Mutex::
+debug_is_locked() const {
+  return true;
+}
+#endif  // CHECK_REENTRANT_MUTEX

+ 9 - 7
panda/src/express/pmutex.cxx

@@ -19,20 +19,20 @@
 #include "pmutex.h"
 #include "thread.h"
 
-#ifndef NDEBUG
+#ifdef CHECK_REENTRANT_MUTEX
 ////////////////////////////////////////////////////////////////////
 //     Function: Mutex::debug_is_locked
 //       Access: Public
 //  Description: Returns true if the current thread has locked the
-//               Mutex, false otherwise.  This method only exists in
-//               !NDEBUG mode, so it's only appropriate to call it
-//               from within an assert().
+//               Mutex, false otherwise.  This method is only
+//               meaningful if CHECK_REENTRANT_MUTEX is defined;
+//               otherwise, it always returns true.
 ////////////////////////////////////////////////////////////////////
 bool Mutex::
 debug_is_locked() const {
   return (_locking_thread == Thread::get_current_thread());
 }
-#endif  // NDEBUG
+#endif  // CHECK_REENTRANT_MUTEX
 
 ////////////////////////////////////////////////////////////////////
 //     Function: Mutex::do_lock
@@ -41,10 +41,12 @@ debug_is_locked() const {
 ////////////////////////////////////////////////////////////////////
 void Mutex::
 do_lock() {
+#ifdef CHECK_REENTRANT_MUTEX
   nassertv(_locking_thread != Thread::get_current_thread());
+#endif
   _impl.lock();
 
-#ifndef NDEBUG
+#ifdef CHECK_REENTRANT_MUTEX
   _locking_thread = Thread::get_current_thread();
 #endif
 }
@@ -56,8 +58,8 @@ do_lock() {
 ////////////////////////////////////////////////////////////////////
 void Mutex::
 do_release() {
+#ifdef CHECK_REENTRANT_MUTEX
   nassertv(_locking_thread == Thread::get_current_thread());
-#ifndef NDEBUG
   _locking_thread = (Thread *)NULL;
 #endif
 

+ 4 - 2
panda/src/express/pmutex.h

@@ -43,8 +43,10 @@ public:
   INLINE void lock() const;
   INLINE void release() const;
 
-#ifndef NDEBUG
+#ifdef CHECK_REENTRANT_MUTEX
   bool debug_is_locked() const;
+#else
+  INLINE bool debug_is_locked() const;
 #endif
 
 private:
@@ -54,7 +56,7 @@ private:
 private:
   MutexImpl _impl;
 
-#ifndef NDEBUG
+#ifdef CHECK_REENTRANT_MUTEX
   // Make sure that ordinary mutexes are not locked reentrantly
   // (that's what a ReMutex is for).
   Thread *_locking_thread;