Browse Source

better simple mutex

David Rose 17 years ago
parent
commit
3f2e70f4eb

+ 0 - 3
panda/src/pipeline/Sources.pp

@@ -54,7 +54,6 @@
     reMutex.I reMutex.h \
     reMutex.I reMutex.h \
     reMutexDirect.h reMutexDirect.I \
     reMutexDirect.h reMutexDirect.I \
     reMutexHolder.I reMutexHolder.h \
     reMutexHolder.I reMutexHolder.h \
-    reMutexSimpleImpl.h reMutexSimpleImpl.I \
     thread.h thread.I threadImpl.h \
     thread.h thread.I threadImpl.h \
     threadDummyImpl.h threadDummyImpl.I \
     threadDummyImpl.h threadDummyImpl.I \
     threadPosixImpl.h threadPosixImpl.I \
     threadPosixImpl.h threadPosixImpl.I \
@@ -101,7 +100,6 @@
     reMutex.cxx \
     reMutex.cxx \
     reMutexDirect.cxx \
     reMutexDirect.cxx \
     reMutexHolder.cxx \
     reMutexHolder.cxx \
-    reMutexSimpleImpl.cxx \
     thread.cxx \
     thread.cxx \
     threadDummyImpl.cxx \
     threadDummyImpl.cxx \
     threadPosixImpl.cxx \
     threadPosixImpl.cxx \
@@ -154,7 +152,6 @@
     reMutex.I reMutex.h \
     reMutex.I reMutex.h \
     reMutexDirect.h reMutexDirect.I \
     reMutexDirect.h reMutexDirect.I \
     reMutexHolder.I reMutexHolder.h \
     reMutexHolder.I reMutexHolder.h \
-    reMutexSimpleImpl.h reMutexSimpleImpl.I \
     thread.h thread.I threadImpl.h \
     thread.h thread.I threadImpl.h \
     threadDummyImpl.h threadDummyImpl.I \
     threadDummyImpl.h threadDummyImpl.I \
     threadPosixImpl.h threadPosixImpl.I \
     threadPosixImpl.h threadPosixImpl.I \

+ 0 - 1
panda/src/pipeline/mutexSimpleImpl.I

@@ -50,7 +50,6 @@ lock() {
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 INLINE bool MutexSimpleImpl::
 INLINE bool MutexSimpleImpl::
 try_lock() {
 try_lock() {
-  ThreadSimpleImpl::consider_yield();
   if ((_flags & F_lock_count) != 0) {
   if ((_flags & F_lock_count) != 0) {
     return false;
     return false;
   }
   }

+ 4 - 0
panda/src/pipeline/mutexSimpleImpl.cxx

@@ -27,6 +27,8 @@
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void MutexSimpleImpl::
 void MutexSimpleImpl::
 do_lock() {
 do_lock() {
+  // By the time we get here, we already know that someone else is
+  // holding the lock: (_flags & F_lock_count) != 0.
   ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
   ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
   ThreadSimpleImpl *thread = manager->get_current_thread();
   ThreadSimpleImpl *thread = manager->get_current_thread();
 
 
@@ -45,6 +47,8 @@ do_lock() {
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void MutexSimpleImpl::
 void MutexSimpleImpl::
 do_release() {
 do_release() {
+  // By the time we get here, we already know that someone else is
+  // blocked on this mutex: (_flags & F_waiters) != 0.
   ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
   ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
   if (manager->unblock_one(this)) {
   if (manager->unblock_one(this)) {
     // There had been a thread waiting on this mutex.  Switch contexts
     // There had been a thread waiting on this mutex.  Switch contexts

+ 4 - 2
panda/src/pipeline/mutexTrueImpl.h

@@ -36,15 +36,17 @@
 #if defined(THREAD_SIMPLE_IMPL) && !defined(SIMPLE_THREADS_NO_MUTEX)
 #if defined(THREAD_SIMPLE_IMPL) && !defined(SIMPLE_THREADS_NO_MUTEX)
 
 
 #include "mutexSimpleImpl.h"
 #include "mutexSimpleImpl.h"
-#include "reMutexSimpleImpl.h"
 typedef MutexSimpleImpl MutexTrueImpl;
 typedef MutexSimpleImpl MutexTrueImpl;
-typedef ReMutexSimpleImpl ReMutexTrueImpl;
+#undef HAVE_REMUTEXTRUEIMPL
 
 
 #else
 #else
 
 
 typedef MutexImpl MutexTrueImpl;
 typedef MutexImpl MutexTrueImpl;
 #if HAVE_REMUTEXIMPL
 #if HAVE_REMUTEXIMPL
 typedef ReMutexImpl ReMutexTrueImpl;
 typedef ReMutexImpl ReMutexTrueImpl;
+#define HAVE_REMUTEXTRUEIMPL 1
+#else
+#undef HAVE_REMUTEXTRUEIMPL
 #endif // HAVE_REMUTEXIMPL
 #endif // HAVE_REMUTEXIMPL
 
 
 #endif
 #endif

+ 0 - 1
panda/src/pipeline/pipeline_composite2.cxx

@@ -14,7 +14,6 @@
 #include "reMutex.cxx"
 #include "reMutex.cxx"
 #include "reMutexDirect.cxx"
 #include "reMutexDirect.cxx"
 #include "reMutexHolder.cxx"
 #include "reMutexHolder.cxx"
-#include "reMutexSimpleImpl.cxx"
 #include "thread.cxx"
 #include "thread.cxx"
 #include "threadDummyImpl.cxx"
 #include "threadDummyImpl.cxx"
 #include "threadPosixImpl.cxx"
 #include "threadPosixImpl.cxx"

+ 12 - 12
panda/src/pipeline/reMutexDirect.I

@@ -20,11 +20,11 @@
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 INLINE ReMutexDirect::
 INLINE ReMutexDirect::
 ReMutexDirect() 
 ReMutexDirect() 
-#ifndef HAVE_REMUTEXIMPL
+#ifndef HAVE_REMUTEXTRUEIMPL
   : _cvar_impl(_lock_impl)
   : _cvar_impl(_lock_impl)
 #endif
 #endif
 {
 {
-#ifndef HAVE_REMUTEXIMPL
+#ifndef HAVE_REMUTEXTRUEIMPL
   _locking_thread = NULL;
   _locking_thread = NULL;
   _lock_count = 0;
   _lock_count = 0;
 #endif
 #endif
@@ -46,7 +46,7 @@ INLINE ReMutexDirect::
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 INLINE ReMutexDirect::
 INLINE ReMutexDirect::
 ReMutexDirect(const ReMutexDirect &copy)
 ReMutexDirect(const ReMutexDirect &copy)
-#ifndef HAVE_REMUTEXIMPL
+#ifndef HAVE_REMUTEXTRUEIMPL
   : _cvar_impl(_lock_impl)
   : _cvar_impl(_lock_impl)
 #endif
 #endif
 {
 {
@@ -81,11 +81,11 @@ operator = (const ReMutexDirect &copy) {
 INLINE void ReMutexDirect::
 INLINE void ReMutexDirect::
 lock() const {
 lock() const {
   TAU_PROFILE("void ReMutexDirect::lock()", " ", TAU_USER);
   TAU_PROFILE("void ReMutexDirect::lock()", " ", TAU_USER);
-#ifdef HAVE_REMUTEXIMPL
+#ifdef HAVE_REMUTEXTRUEIMPL
   ((ReMutexDirect *)this)->_impl.lock();
   ((ReMutexDirect *)this)->_impl.lock();
 #else
 #else
   ((ReMutexDirect *)this)->do_lock();
   ((ReMutexDirect *)this)->do_lock();
-#endif  // HAVE_REMUTEXIMPL
+#endif  // HAVE_REMUTEXTRUEIMPL
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -98,11 +98,11 @@ lock() const {
 INLINE void ReMutexDirect::
 INLINE void ReMutexDirect::
 lock(Thread *current_thread) const {
 lock(Thread *current_thread) const {
   TAU_PROFILE("void ReMutexDirect::lock(Thread *)", " ", TAU_USER);
   TAU_PROFILE("void ReMutexDirect::lock(Thread *)", " ", TAU_USER);
-#ifdef HAVE_REMUTEXIMPL
+#ifdef HAVE_REMUTEXTRUEIMPL
   ((ReMutexDirect *)this)->_impl.lock();
   ((ReMutexDirect *)this)->_impl.lock();
 #else
 #else
   ((ReMutexDirect *)this)->do_lock(current_thread);
   ((ReMutexDirect *)this)->do_lock(current_thread);
-#endif  // HAVE_REMUTEXIMPL
+#endif  // HAVE_REMUTEXTRUEIMPL
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -123,11 +123,11 @@ lock(Thread *current_thread) const {
 INLINE void ReMutexDirect::
 INLINE void ReMutexDirect::
 elevate_lock() const {
 elevate_lock() const {
   TAU_PROFILE("void ReMutexDirect::elevate_lock()", " ", TAU_USER);
   TAU_PROFILE("void ReMutexDirect::elevate_lock()", " ", TAU_USER);
-#ifdef HAVE_REMUTEXIMPL
+#ifdef HAVE_REMUTEXTRUEIMPL
   ((ReMutexDirect *)this)->_impl.lock();
   ((ReMutexDirect *)this)->_impl.lock();
 #else
 #else
   ((ReMutexDirect *)this)->do_elevate_lock();
   ((ReMutexDirect *)this)->do_elevate_lock();
-#endif  // HAVE_REMUTEXIMPL
+#endif  // HAVE_REMUTEXTRUEIMPL
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -143,11 +143,11 @@ elevate_lock() const {
 INLINE void ReMutexDirect::
 INLINE void ReMutexDirect::
 release() const {
 release() const {
   TAU_PROFILE("void ReMutexDirect::release()", " ", TAU_USER);
   TAU_PROFILE("void ReMutexDirect::release()", " ", TAU_USER);
-#ifdef HAVE_REMUTEXIMPL
+#ifdef HAVE_REMUTEXTRUEIMPL
   ((ReMutexDirect *)this)->_impl.release();
   ((ReMutexDirect *)this)->_impl.release();
 #else
 #else
   ((ReMutexDirect *)this)->do_release();
   ((ReMutexDirect *)this)->do_release();
-#endif  // HAVE_REMUTEXIMPL
+#endif  // HAVE_REMUTEXTRUEIMPL
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -207,7 +207,7 @@ get_name() const {
   return string();
   return string();
 }
 }
 
 
-#ifndef HAVE_REMUTEXIMPL
+#ifndef HAVE_REMUTEXTRUEIMPL
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: ReMutexDirect::do_lock
 //     Function: ReMutexDirect::do_lock
 //       Access: Private
 //       Access: Private

+ 6 - 6
panda/src/pipeline/reMutexDirect.cxx

@@ -26,7 +26,7 @@ output(ostream &out) const {
   out << "ReMutex " << (void *)this;
   out << "ReMutex " << (void *)this;
 }
 }
 
 
-#ifndef HAVE_REMUTEXIMPL
+#ifndef HAVE_REMUTEXTRUEIMPL
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: ReMutexDirect::do_lock
 //     Function: ReMutexDirect::do_lock
 //       Access: Private
 //       Access: Private
@@ -68,9 +68,9 @@ do_lock(Thread *current_thread) {
   }
   }
   _lock_impl.release();
   _lock_impl.release();
 }
 }
-#endif  // !HAVE_REMUTEXIMPL
+#endif  // !HAVE_REMUTEXTRUEIMPL
 
 
-#ifndef HAVE_REMUTEXIMPL
+#ifndef HAVE_REMUTEXTRUEIMPL
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: ReMutexDirect::do_elevate_lock
 //     Function: ReMutexDirect::do_elevate_lock
 //       Access: Private
 //       Access: Private
@@ -104,9 +104,9 @@ do_elevate_lock() {
 
 
   _lock_impl.release();
   _lock_impl.release();
 }
 }
-#endif  // !HAVE_REMUTEXIMPL
+#endif  // !HAVE_REMUTEXTRUEIMPL
 
 
-#ifndef HAVE_REMUTEXIMPL
+#ifndef HAVE_REMUTEXTRUEIMPL
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: ReMutexDirect::do_release
 //     Function: ReMutexDirect::do_release
 //       Access: Private
 //       Access: Private
@@ -142,4 +142,4 @@ do_release() {
   }
   }
   _lock_impl.release();
   _lock_impl.release();
 }
 }
-#endif  // !HAVE_REMUTEXIMPL
+#endif  // !HAVE_REMUTEXTRUEIMPL

+ 4 - 4
panda/src/pipeline/reMutexDirect.h

@@ -16,7 +16,7 @@
 #define REMUTEXDIRECT_H
 #define REMUTEXDIRECT_H
 
 
 #include "pandabase.h"
 #include "pandabase.h"
-#include "mutexImpl.h"
+#include "mutexTrueImpl.h"
 #include "conditionVarImpl.h"
 #include "conditionVarImpl.h"
 
 
 class Thread;
 class Thread;
@@ -51,7 +51,7 @@ PUBLISHED:
   void output(ostream &out) const;
   void output(ostream &out) const;
 
 
 private:
 private:
-#ifdef HAVE_REMUTEXIMPL
+#ifdef HAVE_REMUTEXTRUEIMPL
   ReMutexImpl _impl;
   ReMutexImpl _impl;
 
 
 #else
 #else
@@ -64,9 +64,9 @@ private:
   Thread *_locking_thread;
   Thread *_locking_thread;
   int _lock_count;
   int _lock_count;
 
 
-  MutexImpl _lock_impl;
+  MutexTrueImpl _lock_impl;
   ConditionVarImpl _cvar_impl;
   ConditionVarImpl _cvar_impl;
-#endif  // HAVE_REMUTEXIMPL
+#endif  // HAVE_REMUTEXTRUEIMPL
 };
 };
 
 
 INLINE ostream &
 INLINE ostream &

+ 0 - 78
panda/src/pipeline/reMutexSimpleImpl.I

@@ -1,78 +0,0 @@
-// Filename: reMutexSimpleImpl.I
-// Created by:  drose (20Jun07)
-//
-////////////////////////////////////////////////////////////////////
-//
-// PANDA 3D SOFTWARE
-// Copyright (c) Carnegie Mellon University.  All rights reserved.
-//
-// All use of this software is subject to the terms of the revised BSD
-// license.  You should have received a copy of this license along
-// with this source code in a file named "LICENSE."
-//
-////////////////////////////////////////////////////////////////////
-
-
-////////////////////////////////////////////////////////////////////
-//     Function: ReMutexSimpleImpl::Constructor
-//       Access: Public
-//  Description:
-////////////////////////////////////////////////////////////////////
-INLINE ReMutexSimpleImpl::
-ReMutexSimpleImpl() {
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: ReMutexSimpleImpl::Destructor
-//       Access: Public
-//  Description:
-////////////////////////////////////////////////////////////////////
-INLINE ReMutexSimpleImpl::
-~ReMutexSimpleImpl() {
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: ReMutexSimpleImpl::lock
-//       Access: Public
-//  Description: 
-////////////////////////////////////////////////////////////////////
-INLINE void ReMutexSimpleImpl::
-lock() {
-  if (!try_lock()) {
-    do_lock();
-  }
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: ReMutexSimpleImpl::try_lock
-//       Access: Public
-//  Description: 
-////////////////////////////////////////////////////////////////////
-INLINE bool ReMutexSimpleImpl::
-try_lock() {
-  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
-  ThreadSimpleImpl *thread = manager->get_current_thread();
-
-  thread->consider_yield_this();
-  if ((_flags & F_lock_count) != 0) {
-    return false;
-  }
-  ++_flags;
-  _locking_thread = thread;
-  return true;
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: ReMutexSimpleImpl::release
-//       Access: Public
-//  Description: 
-////////////////////////////////////////////////////////////////////
-INLINE void ReMutexSimpleImpl::
-release() {
-  nassertv((_flags & F_lock_count) != 0);
-
-  --_flags;
-  if ((_flags & F_lock_count) == 0 && (_flags & F_has_waiters)) {
-    do_release();
-  }
-}

+ 0 - 53
panda/src/pipeline/reMutexSimpleImpl.cxx

@@ -1,53 +0,0 @@
-// Filename: reMutexSimpleImpl.cxx
-// Created by:  drose (20Jun07)
-//
-////////////////////////////////////////////////////////////////////
-//
-// PANDA 3D SOFTWARE
-// Copyright (c) Carnegie Mellon University.  All rights reserved.
-//
-// All use of this software is subject to the terms of the revised BSD
-// license.  You should have received a copy of this license along
-// with this source code in a file named "LICENSE."
-//
-////////////////////////////////////////////////////////////////////
-
-#include "selectThreadImpl.h"
-
-#ifdef THREAD_SIMPLE_IMPL
-
-#include "reMutexSimpleImpl.h"
-#include "threadSimpleImpl.h"
-#include "threadSimpleManager.h"
-
-////////////////////////////////////////////////////////////////////
-//     Function: ReMutexSimpleImpl::do_lock
-//       Access: Private
-//  Description: 
-////////////////////////////////////////////////////////////////////
-void ReMutexSimpleImpl::
-do_lock() {
-  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
-  ThreadSimpleImpl *thread = manager->get_current_thread();
-
-  while ((_flags & F_lock_count) != 0) {
-    manager->enqueue_block(thread, this);
-    manager->next_context();
-  }
-  
-  ++_flags;
-  _locking_thread = thread;
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: ReMutexSimpleImpl::do_release
-//       Access: Private
-//  Description: 
-////////////////////////////////////////////////////////////////////
-void ReMutexSimpleImpl::
-do_release() {
-  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
-  manager->unblock_one(this);
-}
-
-#endif  // THREAD_SIMPLE_IMPL

+ 0 - 54
panda/src/pipeline/reMutexSimpleImpl.h

@@ -1,54 +0,0 @@
-// Filename: reMutexSimpleImpl.h
-// Created by:  drose (20Jun07)
-//
-////////////////////////////////////////////////////////////////////
-//
-// PANDA 3D SOFTWARE
-// Copyright (c) Carnegie Mellon University.  All rights reserved.
-//
-// All use of this software is subject to the terms of the revised BSD
-// license.  You should have received a copy of this license along
-// with this source code in a file named "LICENSE."
-//
-////////////////////////////////////////////////////////////////////
-
-#ifndef REMUTEXSIMPLEIMPL_H
-#define REMUTEXSIMPLEIMPL_H
-
-#include "pandabase.h"
-#include "selectThreadImpl.h"
-
-#ifdef THREAD_SIMPLE_IMPL
-
-#include "blockerSimple.h"
-#include "threadSimpleImpl.h"
-
-////////////////////////////////////////////////////////////////////
-//       Class : ReMutexSimpleImpl
-// Description : This is the mutex implementation for the simple,
-//               simulated threads case, for recursive mutexes.  See
-//               MutexSimpleImple.
-////////////////////////////////////////////////////////////////////
-class EXPCL_PANDA_PIPELINE ReMutexSimpleImpl : public BlockerSimple {
-public:
-  INLINE ReMutexSimpleImpl();
-  INLINE ~ReMutexSimpleImpl();
-
-  INLINE void lock();
-  INLINE bool try_lock();
-  INLINE void release();
-
-private:
-  void do_lock();
-  void do_release();
-
-  ThreadSimpleImpl *_locking_thread;
-
-  friend class ThreadSimpleManager;
-};
-
-#include "reMutexSimpleImpl.I"
-
-#endif  // THREAD_SIMPLE_IMPL
-
-#endif