From 3f2e70f4eb631c4f1ad217cfafaeefe13cc83ca0 Mon Sep 17 00:00:00 2001 From: David Rose Date: Wed, 8 Oct 2008 01:00:15 +0000 Subject: [PATCH] better simple mutex --- panda/src/pipeline/Sources.pp | 3 - panda/src/pipeline/mutexSimpleImpl.I | 1 - panda/src/pipeline/mutexSimpleImpl.cxx | 4 ++ panda/src/pipeline/mutexTrueImpl.h | 6 +- panda/src/pipeline/pipeline_composite2.cxx | 1 - panda/src/pipeline/reMutexDirect.I | 24 +++---- panda/src/pipeline/reMutexDirect.cxx | 12 ++-- panda/src/pipeline/reMutexDirect.h | 8 +-- panda/src/pipeline/reMutexSimpleImpl.I | 78 ---------------------- panda/src/pipeline/reMutexSimpleImpl.cxx | 53 --------------- panda/src/pipeline/reMutexSimpleImpl.h | 54 --------------- 11 files changed, 30 insertions(+), 214 deletions(-) delete mode 100644 panda/src/pipeline/reMutexSimpleImpl.I delete mode 100644 panda/src/pipeline/reMutexSimpleImpl.cxx delete mode 100644 panda/src/pipeline/reMutexSimpleImpl.h diff --git a/panda/src/pipeline/Sources.pp b/panda/src/pipeline/Sources.pp index b012c8c915..ab3ee08b07 100644 --- a/panda/src/pipeline/Sources.pp +++ b/panda/src/pipeline/Sources.pp @@ -54,7 +54,6 @@ reMutex.I reMutex.h \ reMutexDirect.h reMutexDirect.I \ reMutexHolder.I reMutexHolder.h \ - reMutexSimpleImpl.h reMutexSimpleImpl.I \ thread.h thread.I threadImpl.h \ threadDummyImpl.h threadDummyImpl.I \ threadPosixImpl.h threadPosixImpl.I \ @@ -101,7 +100,6 @@ reMutex.cxx \ reMutexDirect.cxx \ reMutexHolder.cxx \ - reMutexSimpleImpl.cxx \ thread.cxx \ threadDummyImpl.cxx \ threadPosixImpl.cxx \ @@ -154,7 +152,6 @@ reMutex.I reMutex.h \ reMutexDirect.h reMutexDirect.I \ reMutexHolder.I reMutexHolder.h \ - reMutexSimpleImpl.h reMutexSimpleImpl.I \ thread.h thread.I threadImpl.h \ threadDummyImpl.h threadDummyImpl.I \ threadPosixImpl.h threadPosixImpl.I \ diff --git a/panda/src/pipeline/mutexSimpleImpl.I b/panda/src/pipeline/mutexSimpleImpl.I index 41e5b0b2a4..8ec83f7a0d 100644 --- a/panda/src/pipeline/mutexSimpleImpl.I +++ b/panda/src/pipeline/mutexSimpleImpl.I @@ -50,7 +50,6 @@ lock() { //////////////////////////////////////////////////////////////////// INLINE bool MutexSimpleImpl:: try_lock() { - ThreadSimpleImpl::consider_yield(); if ((_flags & F_lock_count) != 0) { return false; } diff --git a/panda/src/pipeline/mutexSimpleImpl.cxx b/panda/src/pipeline/mutexSimpleImpl.cxx index 8a44163877..f822d694ed 100644 --- a/panda/src/pipeline/mutexSimpleImpl.cxx +++ b/panda/src/pipeline/mutexSimpleImpl.cxx @@ -27,6 +27,8 @@ //////////////////////////////////////////////////////////////////// void MutexSimpleImpl:: 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(); ThreadSimpleImpl *thread = manager->get_current_thread(); @@ -45,6 +47,8 @@ do_lock() { //////////////////////////////////////////////////////////////////// void MutexSimpleImpl:: 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(); if (manager->unblock_one(this)) { // There had been a thread waiting on this mutex. Switch contexts diff --git a/panda/src/pipeline/mutexTrueImpl.h b/panda/src/pipeline/mutexTrueImpl.h index 24d2dbee44..c6b620d726 100644 --- a/panda/src/pipeline/mutexTrueImpl.h +++ b/panda/src/pipeline/mutexTrueImpl.h @@ -36,15 +36,17 @@ #if defined(THREAD_SIMPLE_IMPL) && !defined(SIMPLE_THREADS_NO_MUTEX) #include "mutexSimpleImpl.h" -#include "reMutexSimpleImpl.h" typedef MutexSimpleImpl MutexTrueImpl; -typedef ReMutexSimpleImpl ReMutexTrueImpl; +#undef HAVE_REMUTEXTRUEIMPL #else typedef MutexImpl MutexTrueImpl; #if HAVE_REMUTEXIMPL typedef ReMutexImpl ReMutexTrueImpl; +#define HAVE_REMUTEXTRUEIMPL 1 +#else +#undef HAVE_REMUTEXTRUEIMPL #endif // HAVE_REMUTEXIMPL #endif diff --git a/panda/src/pipeline/pipeline_composite2.cxx b/panda/src/pipeline/pipeline_composite2.cxx index 353b70c46d..29d177e2bd 100644 --- a/panda/src/pipeline/pipeline_composite2.cxx +++ b/panda/src/pipeline/pipeline_composite2.cxx @@ -14,7 +14,6 @@ #include "reMutex.cxx" #include "reMutexDirect.cxx" #include "reMutexHolder.cxx" -#include "reMutexSimpleImpl.cxx" #include "thread.cxx" #include "threadDummyImpl.cxx" #include "threadPosixImpl.cxx" diff --git a/panda/src/pipeline/reMutexDirect.I b/panda/src/pipeline/reMutexDirect.I index d5dc240803..5a243bb1b2 100755 --- a/panda/src/pipeline/reMutexDirect.I +++ b/panda/src/pipeline/reMutexDirect.I @@ -20,11 +20,11 @@ //////////////////////////////////////////////////////////////////// INLINE ReMutexDirect:: ReMutexDirect() -#ifndef HAVE_REMUTEXIMPL +#ifndef HAVE_REMUTEXTRUEIMPL : _cvar_impl(_lock_impl) #endif { -#ifndef HAVE_REMUTEXIMPL +#ifndef HAVE_REMUTEXTRUEIMPL _locking_thread = NULL; _lock_count = 0; #endif @@ -46,7 +46,7 @@ INLINE ReMutexDirect:: //////////////////////////////////////////////////////////////////// INLINE ReMutexDirect:: ReMutexDirect(const ReMutexDirect ©) -#ifndef HAVE_REMUTEXIMPL +#ifndef HAVE_REMUTEXTRUEIMPL : _cvar_impl(_lock_impl) #endif { @@ -81,11 +81,11 @@ operator = (const ReMutexDirect ©) { INLINE void ReMutexDirect:: lock() const { TAU_PROFILE("void ReMutexDirect::lock()", " ", TAU_USER); -#ifdef HAVE_REMUTEXIMPL +#ifdef HAVE_REMUTEXTRUEIMPL ((ReMutexDirect *)this)->_impl.lock(); #else ((ReMutexDirect *)this)->do_lock(); -#endif // HAVE_REMUTEXIMPL +#endif // HAVE_REMUTEXTRUEIMPL } //////////////////////////////////////////////////////////////////// @@ -98,11 +98,11 @@ lock() const { INLINE void ReMutexDirect:: lock(Thread *current_thread) const { TAU_PROFILE("void ReMutexDirect::lock(Thread *)", " ", TAU_USER); -#ifdef HAVE_REMUTEXIMPL +#ifdef HAVE_REMUTEXTRUEIMPL ((ReMutexDirect *)this)->_impl.lock(); #else ((ReMutexDirect *)this)->do_lock(current_thread); -#endif // HAVE_REMUTEXIMPL +#endif // HAVE_REMUTEXTRUEIMPL } //////////////////////////////////////////////////////////////////// @@ -123,11 +123,11 @@ lock(Thread *current_thread) const { INLINE void ReMutexDirect:: elevate_lock() const { TAU_PROFILE("void ReMutexDirect::elevate_lock()", " ", TAU_USER); -#ifdef HAVE_REMUTEXIMPL +#ifdef HAVE_REMUTEXTRUEIMPL ((ReMutexDirect *)this)->_impl.lock(); #else ((ReMutexDirect *)this)->do_elevate_lock(); -#endif // HAVE_REMUTEXIMPL +#endif // HAVE_REMUTEXTRUEIMPL } //////////////////////////////////////////////////////////////////// @@ -143,11 +143,11 @@ elevate_lock() const { INLINE void ReMutexDirect:: release() const { TAU_PROFILE("void ReMutexDirect::release()", " ", TAU_USER); -#ifdef HAVE_REMUTEXIMPL +#ifdef HAVE_REMUTEXTRUEIMPL ((ReMutexDirect *)this)->_impl.release(); #else ((ReMutexDirect *)this)->do_release(); -#endif // HAVE_REMUTEXIMPL +#endif // HAVE_REMUTEXTRUEIMPL } //////////////////////////////////////////////////////////////////// @@ -207,7 +207,7 @@ get_name() const { return string(); } -#ifndef HAVE_REMUTEXIMPL +#ifndef HAVE_REMUTEXTRUEIMPL //////////////////////////////////////////////////////////////////// // Function: ReMutexDirect::do_lock // Access: Private diff --git a/panda/src/pipeline/reMutexDirect.cxx b/panda/src/pipeline/reMutexDirect.cxx index d45b86a0be..512c8572c4 100755 --- a/panda/src/pipeline/reMutexDirect.cxx +++ b/panda/src/pipeline/reMutexDirect.cxx @@ -26,7 +26,7 @@ output(ostream &out) const { out << "ReMutex " << (void *)this; } -#ifndef HAVE_REMUTEXIMPL +#ifndef HAVE_REMUTEXTRUEIMPL //////////////////////////////////////////////////////////////////// // Function: ReMutexDirect::do_lock // Access: Private @@ -68,9 +68,9 @@ do_lock(Thread *current_thread) { } _lock_impl.release(); } -#endif // !HAVE_REMUTEXIMPL +#endif // !HAVE_REMUTEXTRUEIMPL -#ifndef HAVE_REMUTEXIMPL +#ifndef HAVE_REMUTEXTRUEIMPL //////////////////////////////////////////////////////////////////// // Function: ReMutexDirect::do_elevate_lock // Access: Private @@ -104,9 +104,9 @@ do_elevate_lock() { _lock_impl.release(); } -#endif // !HAVE_REMUTEXIMPL +#endif // !HAVE_REMUTEXTRUEIMPL -#ifndef HAVE_REMUTEXIMPL +#ifndef HAVE_REMUTEXTRUEIMPL //////////////////////////////////////////////////////////////////// // Function: ReMutexDirect::do_release // Access: Private @@ -142,4 +142,4 @@ do_release() { } _lock_impl.release(); } -#endif // !HAVE_REMUTEXIMPL +#endif // !HAVE_REMUTEXTRUEIMPL diff --git a/panda/src/pipeline/reMutexDirect.h b/panda/src/pipeline/reMutexDirect.h index 6fc07f74b5..4e9b99e2ff 100644 --- a/panda/src/pipeline/reMutexDirect.h +++ b/panda/src/pipeline/reMutexDirect.h @@ -16,7 +16,7 @@ #define REMUTEXDIRECT_H #include "pandabase.h" -#include "mutexImpl.h" +#include "mutexTrueImpl.h" #include "conditionVarImpl.h" class Thread; @@ -51,7 +51,7 @@ PUBLISHED: void output(ostream &out) const; private: -#ifdef HAVE_REMUTEXIMPL +#ifdef HAVE_REMUTEXTRUEIMPL ReMutexImpl _impl; #else @@ -64,9 +64,9 @@ private: Thread *_locking_thread; int _lock_count; - MutexImpl _lock_impl; + MutexTrueImpl _lock_impl; ConditionVarImpl _cvar_impl; -#endif // HAVE_REMUTEXIMPL +#endif // HAVE_REMUTEXTRUEIMPL }; INLINE ostream & diff --git a/panda/src/pipeline/reMutexSimpleImpl.I b/panda/src/pipeline/reMutexSimpleImpl.I deleted file mode 100644 index 2c85d8c732..0000000000 --- a/panda/src/pipeline/reMutexSimpleImpl.I +++ /dev/null @@ -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(); - } -} diff --git a/panda/src/pipeline/reMutexSimpleImpl.cxx b/panda/src/pipeline/reMutexSimpleImpl.cxx deleted file mode 100644 index 71808d7ca6..0000000000 --- a/panda/src/pipeline/reMutexSimpleImpl.cxx +++ /dev/null @@ -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 diff --git a/panda/src/pipeline/reMutexSimpleImpl.h b/panda/src/pipeline/reMutexSimpleImpl.h deleted file mode 100644 index 5a41430d43..0000000000 --- a/panda/src/pipeline/reMutexSimpleImpl.h +++ /dev/null @@ -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