From 8ab0b63fd0ca17961d0eed3395bdf7848f47482e Mon Sep 17 00:00:00 2001 From: David Rose Date: Tue, 21 Feb 2006 23:11:05 +0000 Subject: [PATCH] move mutexImpl etc. from pandaexpress --- dtool/src/interrogatedb/Sources.pp | 19 ++- .../interrogatedb_composite1.cxx | 3 +- .../interrogatedb_composite2.cxx | 7 +- dtool/src/interrogatedb/mutexDummyImpl.I | 54 +++++++ dtool/src/interrogatedb/mutexDummyImpl.cxx | 25 ++++ dtool/src/interrogatedb/mutexDummyImpl.h | 48 ++++++ dtool/src/interrogatedb/mutexImpl.h | 57 ++++++++ dtool/src/interrogatedb/mutexNsprImpl.I | 72 +++++++++ dtool/src/interrogatedb/mutexNsprImpl.cxx | 25 ++++ dtool/src/interrogatedb/mutexNsprImpl.h | 55 +++++++ dtool/src/interrogatedb/mutexPosixImpl.I | 138 ++++++++++++++++++ dtool/src/interrogatedb/mutexPosixImpl.cxx | 25 ++++ dtool/src/interrogatedb/mutexPosixImpl.h | 73 +++++++++ dtool/src/interrogatedb/mutexWin32Impl.I | 68 +++++++++ dtool/src/interrogatedb/mutexWin32Impl.cxx | 25 ++++ dtool/src/interrogatedb/mutexWin32Impl.h | 55 +++++++ dtool/src/interrogatedb/py_panda.cxx | 1 + dtool/src/interrogatedb/selectThreadImpl.h | 71 +++++++++ dtool/src/interrogatedb/typeRegistry.cxx | 109 +++++++++++--- dtool/src/interrogatedb/typeRegistry.h | 3 +- 20 files changed, 905 insertions(+), 28 deletions(-) create mode 100644 dtool/src/interrogatedb/mutexDummyImpl.I create mode 100644 dtool/src/interrogatedb/mutexDummyImpl.cxx create mode 100644 dtool/src/interrogatedb/mutexDummyImpl.h create mode 100644 dtool/src/interrogatedb/mutexImpl.h create mode 100644 dtool/src/interrogatedb/mutexNsprImpl.I create mode 100644 dtool/src/interrogatedb/mutexNsprImpl.cxx create mode 100644 dtool/src/interrogatedb/mutexNsprImpl.h create mode 100644 dtool/src/interrogatedb/mutexPosixImpl.I create mode 100644 dtool/src/interrogatedb/mutexPosixImpl.cxx create mode 100644 dtool/src/interrogatedb/mutexPosixImpl.h create mode 100644 dtool/src/interrogatedb/mutexWin32Impl.I create mode 100644 dtool/src/interrogatedb/mutexWin32Impl.cxx create mode 100644 dtool/src/interrogatedb/mutexWin32Impl.h create mode 100644 dtool/src/interrogatedb/selectThreadImpl.h diff --git a/dtool/src/interrogatedb/Sources.pp b/dtool/src/interrogatedb/Sources.pp index d8a3da534c..36bbe3ec2d 100644 --- a/dtool/src/interrogatedb/Sources.pp +++ b/dtool/src/interrogatedb/Sources.pp @@ -15,8 +15,14 @@ interrogateManifest.h interrogateType.I interrogateType.h \ interrogate_datafile.I interrogate_datafile.h \ interrogate_interface.h interrogate_request.h \ + mutexImpl.h \ + mutexDummyImpl.h mutexDummyImpl.I \ + mutexNsprImpl.h mutexNsprImpl.I \ + mutexPosixImpl.h mutexPosixImpl.I \ + mutexWin32Impl.h mutexWin32Impl.I \ py_panda.h \ register_type.I register_type.h \ + selectThreadImpl.h \ typedObject.I typedObject.h \ typeHandle.I typeHandle.h \ typeRegistry.I typeRegistry.h \ @@ -32,6 +38,10 @@ interrogateFunctionWrapper.cxx interrogateManifest.cxx \ interrogateType.cxx interrogate_datafile.cxx \ interrogate_interface.cxx interrogate_request.cxx \ + mutexDummyImpl.cxx \ + mutexNsprImpl.cxx \ + mutexPosixImpl.cxx \ + mutexWin32Impl.cxx \ py_panda.cxx \ register_type.cxx \ typedObject.cxx \ @@ -41,8 +51,15 @@ #define INSTALL_HEADERS \ interrogate_interface.h interrogate_request.h vector_int.h \ - config_interrogatedb.h py_panda.h \ + config_interrogatedb.h \ + mutexImpl.h \ + mutexDummyImpl.h mutexDummyImpl.I \ + mutexNsprImpl.h mutexNsprImpl.I \ + mutexPosixImpl.h mutexPosixImpl.I \ + mutexWin32Impl.h mutexWin32Impl.I \ + py_panda.h \ register_type.I register_type.h \ + selectThreadImpl.h \ typedObject.I typedObject.h \ typeHandle.I typeHandle.h \ typeRegistry.I typeRegistry.h \ diff --git a/dtool/src/interrogatedb/interrogatedb_composite1.cxx b/dtool/src/interrogatedb/interrogatedb_composite1.cxx index 2c18b5e400..b438ff333b 100644 --- a/dtool/src/interrogatedb/interrogatedb_composite1.cxx +++ b/dtool/src/interrogatedb/interrogatedb_composite1.cxx @@ -1,4 +1,3 @@ - #include "vector_int.cxx" #include "dtool_super_base.cxx" #include "config_interrogatedb.cxx" @@ -9,5 +8,7 @@ #include "interrogateFunctionWrapper.cxx" #include "interrogate_datafile.cxx" #include "interrogate_interface.cxx" +#include "interrogateDatabase.cxx" +#include "interrogateManifest.cxx" diff --git a/dtool/src/interrogatedb/interrogatedb_composite2.cxx b/dtool/src/interrogatedb/interrogatedb_composite2.cxx index 8b396d8546..68e2e45e10 100644 --- a/dtool/src/interrogatedb/interrogatedb_composite2.cxx +++ b/dtool/src/interrogatedb/interrogatedb_composite2.cxx @@ -1,8 +1,9 @@ - -#include "interrogateDatabase.cxx" -#include "interrogateManifest.cxx" #include "interrogateType.cxx" #include "interrogate_request.cxx" +#include "mutexDummyImpl.cxx" +#include "mutexNsprImpl.cxx" +#include "mutexPosixImpl.cxx" +#include "mutexWin32Impl.cxx" #include "py_panda.cxx" #include "register_type.cxx" #include "typedObject.cxx" diff --git a/dtool/src/interrogatedb/mutexDummyImpl.I b/dtool/src/interrogatedb/mutexDummyImpl.I new file mode 100644 index 0000000000..3711d6eaa3 --- /dev/null +++ b/dtool/src/interrogatedb/mutexDummyImpl.I @@ -0,0 +1,54 @@ +// Filename: mutexDummyImpl.I +// Created by: drose (08Aug02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: MutexDummyImpl::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE MutexDummyImpl:: +MutexDummyImpl() { +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexDummyImpl::Destructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE MutexDummyImpl:: +~MutexDummyImpl() { +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexDummyImpl::lock +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void MutexDummyImpl:: +lock() { +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexDummyImpl::release +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void MutexDummyImpl:: +release() { +} diff --git a/dtool/src/interrogatedb/mutexDummyImpl.cxx b/dtool/src/interrogatedb/mutexDummyImpl.cxx new file mode 100644 index 0000000000..1597988c21 --- /dev/null +++ b/dtool/src/interrogatedb/mutexDummyImpl.cxx @@ -0,0 +1,25 @@ +// Filename: mutexDummyImpl.cxx +// Created by: drose (08Aug02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#include "selectThreadImpl.h" + +#ifdef THREAD_DUMMY_IMPL + +#include "mutexDummyImpl.h" + +#endif // THREAD_DUMMY_IMPL diff --git a/dtool/src/interrogatedb/mutexDummyImpl.h b/dtool/src/interrogatedb/mutexDummyImpl.h new file mode 100644 index 0000000000..b03ee4482f --- /dev/null +++ b/dtool/src/interrogatedb/mutexDummyImpl.h @@ -0,0 +1,48 @@ +// Filename: mutexDummyImpl.h +// Created by: drose (08Aug02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#ifndef MUTEXDUMMYIMPL_H +#define MUTEXDUMMYIMPL_H + +#include "dtoolbase.h" +#include "selectThreadImpl.h" + +#ifdef THREAD_DUMMY_IMPL + +#include "notify.h" + +//////////////////////////////////////////////////////////////////// +// Class : MutexDummyImpl +// Description : A fake mutex implementation for single-threaded +// applications that don't need any synchronization +// control. This does nothing at all. +//////////////////////////////////////////////////////////////////// +class EXPCL_DTOOLCONFIG MutexDummyImpl { +public: + INLINE MutexDummyImpl(); + INLINE ~MutexDummyImpl(); + + INLINE void lock(); + INLINE void release(); +}; + +#include "mutexDummyImpl.I" + +#endif // THREAD_DUMMY_IMPL + +#endif diff --git a/dtool/src/interrogatedb/mutexImpl.h b/dtool/src/interrogatedb/mutexImpl.h new file mode 100644 index 0000000000..e874194c09 --- /dev/null +++ b/dtool/src/interrogatedb/mutexImpl.h @@ -0,0 +1,57 @@ +// Filename: mutexImpl.h +// Created by: drose (08Aug02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#ifndef MUTEXIMPL_H +#define MUTEXIMPL_H + +#include "dtoolbase.h" +#include "selectThreadImpl.h" + +#if defined(THREAD_DUMMY_IMPL) + +#include "mutexDummyImpl.h" +typedef MutexDummyImpl MutexImpl; +typedef MutexDummyImpl ReMutexImpl; +#define HAVE_REMUTEXIMPL 1 + +#elif defined(THREAD_WIN32_IMPL) + +#include "mutexWin32Impl.h" +typedef MutexWin32Impl MutexImpl; +typedef MutexWin32Impl ReMutexImpl; // Win32 Mutexes are always reentrant. +#define HAVE_REMUTEXIMPL 1 + +#elif defined(THREAD_POSIX_IMPL) + +#include "mutexPosixImpl.h" +typedef MutexPosixImpl MutexImpl; +typedef ReMutexPosixImpl ReMutexImpl; +#define HAVE_REMUTEXIMPL 1 + +#elif defined(THREAD_NSPR_IMPL) + +#include "mutexNsprImpl.h" +typedef MutexNsprImpl MutexImpl; +#undef HAVE_REMUTEXIMPL // NSPR doesn't provide a reentrant mutex. + +#endif + +#endif + + + diff --git a/dtool/src/interrogatedb/mutexNsprImpl.I b/dtool/src/interrogatedb/mutexNsprImpl.I new file mode 100644 index 0000000000..ffe3ac2b76 --- /dev/null +++ b/dtool/src/interrogatedb/mutexNsprImpl.I @@ -0,0 +1,72 @@ +// Filename: mutexNsprImpl.I +// Created by: drose (08Aug02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: MutexNsprImpl::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE MutexNsprImpl:: +MutexNsprImpl() { + _lock = PR_NewLock(); + nassertv(_lock != (PRLock *)NULL); +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexNsprImpl::Destructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE MutexNsprImpl:: +~MutexNsprImpl() { + PR_DestroyLock(_lock); +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexNsprImpl::lock +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void MutexNsprImpl:: +lock() { + PR_Lock(_lock); +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexNsprImpl::try_lock +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool MutexNsprImpl:: +try_lock() { + // NSPR doesn't define a try_lock function. Too bad. We just + // report that it would always block (since we don't know). + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexNsprImpl::release +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void MutexNsprImpl:: +release() { + int status = PR_Unlock(_lock); + nassertv(status == PR_SUCCESS); +} diff --git a/dtool/src/interrogatedb/mutexNsprImpl.cxx b/dtool/src/interrogatedb/mutexNsprImpl.cxx new file mode 100644 index 0000000000..61b1d58aec --- /dev/null +++ b/dtool/src/interrogatedb/mutexNsprImpl.cxx @@ -0,0 +1,25 @@ +// Filename: mutexNsprImpl.cxx +// Created by: drose (09Aug02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#include "selectThreadImpl.h" + +#ifdef THREAD_NSPR_IMPL + +#include "mutexNsprImpl.h" + +#endif // THREAD_NSPR_IMPL diff --git a/dtool/src/interrogatedb/mutexNsprImpl.h b/dtool/src/interrogatedb/mutexNsprImpl.h new file mode 100644 index 0000000000..d71597206f --- /dev/null +++ b/dtool/src/interrogatedb/mutexNsprImpl.h @@ -0,0 +1,55 @@ +// Filename: mutexNsprImpl.h +// Created by: drose (08Aug02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#ifndef MUTEXNSPRIMPL_H +#define MUTEXNSPRIMPL_H + +#include "dtoolbase.h" +#include "selectThreadImpl.h" + +#ifdef THREAD_NSPR_IMPL + +#include "notify.h" + +#include + +#undef MUTEX_DEFINES_TRYLOCK + +//////////////////////////////////////////////////////////////////// +// Class : MutexNsprImpl +// Description : Uses NSPR to implement a mutex. +//////////////////////////////////////////////////////////////////// +class EXPCL_DTOOLCONFIG MutexNsprImpl { +public: + INLINE MutexNsprImpl(); + INLINE ~MutexNsprImpl(); + + INLINE void lock(); + INLINE bool try_lock(); + INLINE void release(); + +private: + PRLock *_lock; + friend class ConditionVarNsprImpl; +}; + +#include "mutexNsprImpl.I" + +#endif // THREAD_NSPR_IMPL + +#endif diff --git a/dtool/src/interrogatedb/mutexPosixImpl.I b/dtool/src/interrogatedb/mutexPosixImpl.I new file mode 100644 index 0000000000..5a7595f954 --- /dev/null +++ b/dtool/src/interrogatedb/mutexPosixImpl.I @@ -0,0 +1,138 @@ +// Filename: mutexPosixImpl.I +// Created by: drose (10Feb06) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: MutexPosixImpl::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE MutexPosixImpl:: +MutexPosixImpl() { + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); + int result = pthread_mutex_init(&_lock, &attr); + pthread_mutexattr_destroy(&attr); + nassertv(result == 0); +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexPosixImpl::Destructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE MutexPosixImpl:: +~MutexPosixImpl() { + int result = pthread_mutex_destroy(&_lock); + nassertv(result == 0); +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexPosixImpl::lock +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void MutexPosixImpl:: +lock() { + int result = pthread_mutex_lock(&_lock); + nassertv(result == 0); +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexPosixImpl::try_lock +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool MutexPosixImpl:: +try_lock() { + int result = pthread_mutex_trylock(&_lock); + nassertr(result == 0 || result == EBUSY, false); + return (result == 0); +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexPosixImpl::release +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void MutexPosixImpl:: +release() { + int result = pthread_mutex_unlock(&_lock); + nassertv(result == 0); +} + +//////////////////////////////////////////////////////////////////// +// Function: ReMutexPosixImpl::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE ReMutexPosixImpl:: +ReMutexPosixImpl() { + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + int result = pthread_mutex_init(&_lock, &attr); + pthread_mutexattr_destroy(&attr); + nassertv(result == 0); +} + +//////////////////////////////////////////////////////////////////// +// Function: ReMutexPosixImpl::Destructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE ReMutexPosixImpl:: +~ReMutexPosixImpl() { + int result = pthread_mutex_destroy(&_lock); + nassertv(result == 0); +} + +//////////////////////////////////////////////////////////////////// +// Function: ReMutexPosixImpl::lock +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void ReMutexPosixImpl:: +lock() { + int result = pthread_mutex_lock(&_lock); + nassertv(result == 0); +} + +//////////////////////////////////////////////////////////////////// +// Function: ReMutexPosixImpl::try_lock +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool ReMutexPosixImpl:: +try_lock() { + int result = pthread_mutex_trylock(&_lock); + nassertr(result == 0 || result == EBUSY, false); + return (result == 0); +} + +//////////////////////////////////////////////////////////////////// +// Function: ReMutexPosixImpl::release +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void ReMutexPosixImpl:: +release() { + int result = pthread_mutex_unlock(&_lock); + nassertv(result == 0); +} diff --git a/dtool/src/interrogatedb/mutexPosixImpl.cxx b/dtool/src/interrogatedb/mutexPosixImpl.cxx new file mode 100644 index 0000000000..ce93f2160d --- /dev/null +++ b/dtool/src/interrogatedb/mutexPosixImpl.cxx @@ -0,0 +1,25 @@ +// Filename: mutexPosixImpl.cxx +// Created by: drose (10Feb06) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#include "selectThreadImpl.h" + +#ifdef THREAD_POSIX_IMPL + +#include "mutexPosixImpl.h" + +#endif // THREAD_POSIX_IMPL diff --git a/dtool/src/interrogatedb/mutexPosixImpl.h b/dtool/src/interrogatedb/mutexPosixImpl.h new file mode 100644 index 0000000000..7520e299be --- /dev/null +++ b/dtool/src/interrogatedb/mutexPosixImpl.h @@ -0,0 +1,73 @@ +// Filename: mutexPosixImpl.h +// Created by: drose (10Feb06) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#ifndef MUTEXPOSIXIMPL_H +#define MUTEXPOSIXIMPL_H + +#include "dtoolbase.h" +#include "selectThreadImpl.h" + +#ifdef THREAD_POSIX_IMPL + +#include "notify.h" + +#include +#include + +#define MUTEX_DEFINES_TRYLOCK 1 + +//////////////////////////////////////////////////////////////////// +// Class : MutexPosixImpl +// Description : Uses Posix threads to implement a mutex. +//////////////////////////////////////////////////////////////////// +class EXPCL_DTOOLCONFIG MutexPosixImpl { +public: + INLINE MutexPosixImpl(); + INLINE ~MutexPosixImpl(); + + INLINE void lock(); + INLINE bool try_lock(); + INLINE void release(); + +private: + pthread_mutex_t _lock; + friend class ConditionVarPosixImpl; +}; + +//////////////////////////////////////////////////////////////////// +// Class : ReMutexPosixImpl +// Description : Uses Posix threads to implement a reentrant mutex. +//////////////////////////////////////////////////////////////////// +class EXPCL_DTOOLCONFIG ReMutexPosixImpl { +public: + INLINE ReMutexPosixImpl(); + INLINE ~ReMutexPosixImpl(); + + INLINE void lock(); + INLINE bool try_lock(); + INLINE void release(); + +private: + pthread_mutex_t _lock; +}; + +#include "mutexPosixImpl.I" + +#endif // THREAD_POSIX_IMPL + +#endif diff --git a/dtool/src/interrogatedb/mutexWin32Impl.I b/dtool/src/interrogatedb/mutexWin32Impl.I new file mode 100644 index 0000000000..6b321f458d --- /dev/null +++ b/dtool/src/interrogatedb/mutexWin32Impl.I @@ -0,0 +1,68 @@ +// Filename: mutexWin32Impl.I +// Created by: drose (07Feb06) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: MutexWin32Impl::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE MutexWin32Impl:: +MutexWin32Impl() { + InitializeCriticalSection(&_lock); +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexWin32Impl::Destructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE MutexWin32Impl:: +~MutexWin32Impl() { + DeleteCriticalSection(&_lock); +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexWin32Impl::lock +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void MutexWin32Impl:: +lock() { + EnterCriticalSection(&_lock); +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexWin32Impl::try_lock +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool MutexWin32Impl:: +try_lock() { + return (TryEnterCriticalSection(&_lock) != 0); +} + +//////////////////////////////////////////////////////////////////// +// Function: MutexWin32Impl::release +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void MutexWin32Impl:: +release() { + LeaveCriticalSection(&_lock); +} diff --git a/dtool/src/interrogatedb/mutexWin32Impl.cxx b/dtool/src/interrogatedb/mutexWin32Impl.cxx new file mode 100644 index 0000000000..18b7a856bc --- /dev/null +++ b/dtool/src/interrogatedb/mutexWin32Impl.cxx @@ -0,0 +1,25 @@ +// Filename: mutexWin32Impl.cxx +// Created by: drose (07Feb06) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#include "selectThreadImpl.h" + +#ifdef THREAD_WIN32_IMPL + +#include "mutexWin32Impl.h" + +#endif // THREAD_WIN32_IMPL diff --git a/dtool/src/interrogatedb/mutexWin32Impl.h b/dtool/src/interrogatedb/mutexWin32Impl.h new file mode 100644 index 0000000000..c7c84a8703 --- /dev/null +++ b/dtool/src/interrogatedb/mutexWin32Impl.h @@ -0,0 +1,55 @@ +// Filename: mutexWin32Impl.h +// Created by: drose (07Feb06) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#ifndef MUTEXWIN32IMPL_H +#define MUTEXWIN32IMPL_H + +#include "dtoolbase.h" +#include "selectThreadImpl.h" + +#ifdef THREAD_WIN32_IMPL + +#include "notify.h" + +#include + +#define MUTEX_DEFINES_TRYLOCK 1 + +//////////////////////////////////////////////////////////////////// +// Class : MutexWin32Impl +// Description : Uses Windows native calls to implement a mutex. +//////////////////////////////////////////////////////////////////// +class EXPCL_DTOOLCONFIG MutexWin32Impl { +public: + INLINE MutexWin32Impl(); + INLINE ~MutexWin32Impl(); + + INLINE void lock(); + INLINE bool try_lock(); + INLINE void release(); + +private: + CRITICAL_SECTION _lock; + friend class ConditionVarWin32Impl; +}; + +#include "mutexWin32Impl.I" + +#endif // THREAD_WIN32_IMPL + +#endif diff --git a/dtool/src/interrogatedb/py_panda.cxx b/dtool/src/interrogatedb/py_panda.cxx index 6ee1b5fb27..c31a2d64dc 100644 --- a/dtool/src/interrogatedb/py_panda.cxx +++ b/dtool/src/interrogatedb/py_panda.cxx @@ -17,6 +17,7 @@ //////////////////////////////////////////////////////////////////// #include "py_panda.h" +#include "config_interrogatedb.h" #ifdef HAVE_PYTHON diff --git a/dtool/src/interrogatedb/selectThreadImpl.h b/dtool/src/interrogatedb/selectThreadImpl.h new file mode 100644 index 0000000000..f630a6a0e2 --- /dev/null +++ b/dtool/src/interrogatedb/selectThreadImpl.h @@ -0,0 +1,71 @@ +// Filename: selectThreadImpl.h +// Created by: drose (09Aug02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#ifndef SELECTTHREADIMPL_H +#define SELECTTHREADIMPL_H + +#include "dtoolbase.h" + +//////////////////////////////////////////////////////////////////// +// This file decides which of the core implementations of the various +// threading and locking implementations we should use, based on +// platform and/or available libraries. +// +// This file, along with mutexImpl.h and the various Mutex +// implementation classes, are defined in dtool so that some form of +// critical-section protection will be available to view low-level +// classes like TypeRegistry. Most of the rest of the threading and +// synchronization classes are defined in panda/src/express. +//////////////////////////////////////////////////////////////////// + +#if !defined(HAVE_THREADS) + +// With threading disabled, use the do-nothing implementation. +#define THREAD_DUMMY_IMPL 1 + +#elif defined(WIN32_VC) + +// In Windows, use the native threading library. +#define THREAD_WIN32_IMPL 1 + +#elif defined(HAVE_POSIX_THREADS) + +// Posix threads are nice. +#define THREAD_POSIX_IMPL 1 + +#elif defined(HAVE_NSPR) + +// If NSPR is available, use that. +#define THREAD_NSPR_IMPL 1 + +#else + +// This is a configuration error. For some reason, HAVE_THREADS is +// defined but we don't have any way to implement it. +#error No thread implementation defined for platform. + +#endif + +// Let's also factor out some of the other configuration variables. +#if defined(DO_PIPELINING) && defined(HAVE_THREADS) +#define THREADED_PIPELINE 1 +#else +#undef THREADED_PIPELINE +#endif + +#endif diff --git a/dtool/src/interrogatedb/typeRegistry.cxx b/dtool/src/interrogatedb/typeRegistry.cxx index 63f3558360..306cc291ea 100644 --- a/dtool/src/interrogatedb/typeRegistry.cxx +++ b/dtool/src/interrogatedb/typeRegistry.cxx @@ -31,6 +31,7 @@ // static init time, and we must use the arrow syntax to force // initialization of the interrogatedb_cat category. +MutexImpl TypeRegistry::_lock; TypeRegistry *TypeRegistry::_global_pointer = NULL; @@ -47,12 +48,15 @@ TypeRegistry *TypeRegistry::_global_pointer = NULL; //////////////////////////////////////////////////////////////////// bool TypeRegistry:: register_type(TypeHandle &type_handle, const string &name) { + _lock.lock(); + if (type_handle != TypeHandle::none()) { // Here's a type that was already registered. Just make sure // everything's still kosher. TypeRegistryNode *rnode = look_up(type_handle, NULL); if (&type_handle == &rnode->_ref) { // No problem. + _lock.release(); nassertr(rnode->_name == name, false); return false; } @@ -82,6 +86,7 @@ register_type(TypeHandle &type_handle, const string &name) { _derivations_fresh = false; type_handle = new_handle; + _lock.release(); return true; } TypeRegistryNode *rnode = (*ri).second; @@ -97,6 +102,7 @@ register_type(TypeHandle &type_handle, const string &name) { if (type_handle == rnode->_handle) { // No problem. + _lock.release(); return false; } // But wait--the type_handle has changed! We kept a reference to @@ -107,6 +113,7 @@ register_type(TypeHandle &type_handle, const string &name) { interrogatedb_cat->error() << "Reregistering " << name << "\n"; type_handle == rnode->_handle; + _lock.release(); return false; } @@ -122,6 +129,7 @@ register_type(TypeHandle &type_handle, const string &name) { type_handle = rnode->_handle; } + _lock.release(); return false; } @@ -136,6 +144,8 @@ register_type(TypeHandle &type_handle, const string &name) { //////////////////////////////////////////////////////////////////// TypeHandle TypeRegistry:: register_dynamic_type(const string &name) { + _lock.lock(); + NameRegistry::iterator ri; ri = _name_registry.find(name); @@ -162,12 +172,15 @@ register_dynamic_type(const string &name) { _name_registry[name] = rnode; _derivations_fresh = false; + _lock.release(); return *new_handle; } // Return the TypeHandle previously obtained. TypeRegistryNode *rnode = (*ri).second; - return rnode->_handle; + TypeHandle handle = rnode->_handle; + _lock.release(); + return handle; } //////////////////////////////////////////////////////////////////// @@ -180,6 +193,8 @@ register_dynamic_type(const string &name) { //////////////////////////////////////////////////////////////////// void TypeRegistry:: record_derivation(TypeHandle child, TypeHandle parent) { + _lock.lock(); + TypeRegistryNode *cnode = look_up(child, NULL); nassertv(cnode != (TypeRegistryNode *)NULL); TypeRegistryNode *pnode = look_up(parent, NULL); @@ -196,6 +211,8 @@ record_derivation(TypeHandle child, TypeHandle parent) { pnode->_child_classes.push_back(cnode); _derivations_fresh = false; } + + _lock.release(); } //////////////////////////////////////////////////////////////////// @@ -209,6 +226,8 @@ record_derivation(TypeHandle child, TypeHandle parent) { //////////////////////////////////////////////////////////////////// void TypeRegistry:: record_alternate_name(TypeHandle type, const string &name) { + _lock.lock(); + TypeRegistryNode *rnode = look_up(type, (TypedObject *)NULL); if (rnode != (TypeRegistryNode *)NULL) { NameRegistry::iterator ri = @@ -219,6 +238,8 @@ record_alternate_name(TypeHandle type, const string &name) { << rnode->_name << "; cannot reassign to " << type << "\n"; } } + + _lock.release(); } //////////////////////////////////////////////////////////////////// @@ -230,13 +251,17 @@ record_alternate_name(TypeHandle type, const string &name) { //////////////////////////////////////////////////////////////////// TypeHandle TypeRegistry:: find_type(const string &name) const { + _lock.lock(); + + TypeHandle handle; NameRegistry::const_iterator ri; ri = _name_registry.find(name); - if (ri == _name_registry.end()) { - return TypeHandle::none(); - } else { - return (*ri).second->_handle; + if (ri != _name_registry.end()) { + handle = (*ri).second->_handle; } + _lock.release(); + + return handle; } @@ -252,9 +277,13 @@ find_type(const string &name) const { //////////////////////////////////////////////////////////////////// string TypeRegistry:: get_name(TypeHandle type, TypedObject *object) const { + _lock.lock(); TypeRegistryNode *rnode = look_up(type, object); nassertr(rnode != (TypeRegistryNode *)NULL, ""); - return rnode->_name; + string name = rnode->_name; + _lock.release(); + + return name; } //////////////////////////////////////////////////////////////////// @@ -278,12 +307,17 @@ get_name(TypeHandle type, TypedObject *object) const { bool TypeRegistry:: is_derived_from(TypeHandle child, TypeHandle base, TypedObject *child_object) { + _lock.lock(); + const TypeRegistryNode *child_node = look_up(child, child_object); const TypeRegistryNode *base_node = look_up(base, (TypedObject *)NULL); nassertr(child_node != (TypeRegistryNode *)NULL && base_node != (TypeRegistryNode *)NULL, false); freshen_derivations(); - return TypeRegistryNode::is_derived_from(child_node, base_node); + + bool result = TypeRegistryNode::is_derived_from(child_node, base_node); + _lock.release(); + return result; } //////////////////////////////////////////////////////////////////// @@ -295,8 +329,11 @@ is_derived_from(TypeHandle child, TypeHandle base, //////////////////////////////////////////////////////////////////// int TypeRegistry:: get_num_root_classes() { + _lock.lock(); freshen_derivations(); - return _root_classes.size(); + int num_roots = _root_classes.size(); + _lock.release(); + return num_roots; } //////////////////////////////////////////////////////////////////// @@ -307,9 +344,15 @@ get_num_root_classes() { //////////////////////////////////////////////////////////////////// TypeHandle TypeRegistry:: get_root_class(int n) { + _lock.lock(); freshen_derivations(); - nassertr(n >= 0 && n < get_num_root_classes(), TypeHandle::none()); - return _root_classes[n]->_handle; + TypeHandle handle; + if (n >= 0 && n < get_num_root_classes()) { + handle = _root_classes[n]->_handle; + } + _lock.release(); + + return handle; } //////////////////////////////////////////////////////////////////// @@ -330,9 +373,12 @@ get_root_class(int n) { //////////////////////////////////////////////////////////////////// int TypeRegistry:: get_num_parent_classes(TypeHandle child, TypedObject *child_object) const { + _lock.lock(); TypeRegistryNode *rnode = look_up(child, child_object); nassertr(rnode != (TypeRegistryNode *)NULL, 0); - return rnode->_parent_classes.size(); + int num_parents = rnode->_parent_classes.size(); + _lock.release(); + return num_parents; } //////////////////////////////////////////////////////////////////// @@ -344,11 +390,15 @@ get_num_parent_classes(TypeHandle child, TypedObject *child_object) const { //////////////////////////////////////////////////////////////////// TypeHandle TypeRegistry:: get_parent_class(TypeHandle child, int index) const { + _lock.lock(); + TypeHandle handle; TypeRegistryNode *rnode = look_up(child, (TypedObject *)NULL); nassertr(rnode != (TypeRegistryNode *)NULL, TypeHandle::none()); - nassertr(index >= 0 && index < (int)rnode->_parent_classes.size(), - TypeHandle::none()); - return rnode->_parent_classes[index]->_handle; + if (index >= 0 && index < (int)rnode->_parent_classes.size()) { + handle = rnode->_parent_classes[index]->_handle; + } + _lock.release(); + return handle; } //////////////////////////////////////////////////////////////////// @@ -365,9 +415,12 @@ get_parent_class(TypeHandle child, int index) const { //////////////////////////////////////////////////////////////////// int TypeRegistry:: get_num_child_classes(TypeHandle child, TypedObject *child_object) const { + _lock.lock(); TypeRegistryNode *rnode = look_up(child, child_object); nassertr(rnode != (TypeRegistryNode *)NULL, 0); - return rnode->_child_classes.size(); + int num_children = rnode->_child_classes.size(); + _lock.release(); + return num_children; } //////////////////////////////////////////////////////////////////// @@ -379,11 +432,15 @@ get_num_child_classes(TypeHandle child, TypedObject *child_object) const { //////////////////////////////////////////////////////////////////// TypeHandle TypeRegistry:: get_child_class(TypeHandle child, int index) const { + _lock.lock(); + TypeHandle handle; TypeRegistryNode *rnode = look_up(child, (TypedObject *)NULL); nassertr(rnode != (TypeRegistryNode *)NULL, TypeHandle::none()); - nassertr(index >= 0 && index < (int)rnode->_child_classes.size(), - TypeHandle::none()); - return rnode->_child_classes[index]->_handle; + if (index >= 0 && index < (int)rnode->_child_classes.size()) { + handle = rnode->_child_classes[index]->_handle; + } + _lock.release(); + return handle; } //////////////////////////////////////////////////////////////////// @@ -403,12 +460,16 @@ get_child_class(TypeHandle child, int index) const { TypeHandle TypeRegistry:: get_parent_towards(TypeHandle child, TypeHandle base, TypedObject *child_object) { + _lock.lock(); + TypeHandle handle; const TypeRegistryNode *child_node = look_up(child, child_object); const TypeRegistryNode *base_node = look_up(base, NULL); nassertr(child_node != (TypeRegistryNode *)NULL && base_node != (TypeRegistryNode *)NULL, TypeHandle::none()); freshen_derivations(); - return TypeRegistryNode::get_parent_towards(child_node, base_node); + handle = TypeRegistryNode::get_parent_towards(child_node, base_node); + _lock.release(); + return handle; } @@ -425,6 +486,7 @@ get_parent_towards(TypeHandle child, TypeHandle base, //////////////////////////////////////////////////////////////////// void TypeRegistry:: reregister_types() { + _lock.lock(); HandleRegistry::iterator ri; TypeRegistry *reg = ptr(); for (ri = reg->_handle_registry.begin(); @@ -436,6 +498,7 @@ reregister_types() { << "Reregistering " << rnode->_name << "\n"; } } + _lock.release(); } @@ -448,6 +511,7 @@ reregister_types() { //////////////////////////////////////////////////////////////////// void TypeRegistry:: write(ostream &out) const { + _lock.lock(); // Recursively write out the tree, starting from each node that has // no parent. HandleRegistry::const_iterator hi; @@ -459,6 +523,7 @@ write(ostream &out) const { write_node(out, 2, root); } } + _lock.release(); } //////////////////////////////////////////////////////////////////// @@ -469,6 +534,7 @@ write(ostream &out) const { //////////////////////////////////////////////////////////////////// TypeRegistry *TypeRegistry:: ptr() { + _lock.lock(); if (_global_pointer == NULL) { #ifdef NOTIFY_DEBUG if (interrogatedb_cat->is_spam()) { @@ -478,6 +544,7 @@ ptr() { #endif init_global_pointer(); } + _lock.release(); return _global_pointer; } @@ -500,9 +567,7 @@ TypeRegistry() { // Function: TypeRegistry::init_global_pointer // Access: Private, Static // Description: Constructs the TypeRegistry object for the first -// time. It is initially created on the local heap, -// then as soon as shared memory becomes available, it -// should be moved into shared memory. +// time. //////////////////////////////////////////////////////////////////// void TypeRegistry:: init_global_pointer() { diff --git a/dtool/src/interrogatedb/typeRegistry.h b/dtool/src/interrogatedb/typeRegistry.h index da2bf07f7f..49324e2905 100644 --- a/dtool/src/interrogatedb/typeRegistry.h +++ b/dtool/src/interrogatedb/typeRegistry.h @@ -20,7 +20,7 @@ #define TYPEREGISTRY_H #include "dtoolbase.h" - +#include "mutexImpl.h" #include "notify.h" #include "pvector.h" #include "pmap.h" @@ -106,6 +106,7 @@ private: bool _derivations_fresh; + static MutexImpl _lock; static TypeRegistry *_global_pointer; };