better simple mutex

This commit is contained in:
David Rose 2008-10-08 01:00:15 +00:00
parent 323d4a0a19
commit 3f2e70f4eb
11 changed files with 30 additions and 214 deletions

View File

@ -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 \

View File

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

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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 &copy)
#ifndef HAVE_REMUTEXIMPL
#ifndef HAVE_REMUTEXTRUEIMPL
: _cvar_impl(_lock_impl)
#endif
{
@ -81,11 +81,11 @@ operator = (const ReMutexDirect &copy) {
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

View File

@ -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

View File

@ -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 &

View File

@ -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();
}
}

View File

@ -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

View File

@ -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