From Mathias Froechlich, "Attached is a change to that atomic stuff to move the win32, msvc
implementation of the atomic increment and decrement into a implementation file. This way inlining and compiler optimization can no longer happen for these implementations, but it fixes compilation on win32 msvc targets. I expect that this is still faster than with with mutexes. Also the i386 gcc target gets atomic operations with this patch. By using an implementation file we can guarantee that we have the right compiler flags available."
This commit is contained in:
101
src/OpenThreads/common/Atomic.cpp
Normal file
101
src/OpenThreads/common/Atomic.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
/* -*-c++-*- OpenThreads library, Copyright (C) 2008 The Open Thread Group
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
#include <OpenThreads/Atomic>
|
||||
|
||||
#if defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
|
||||
# include <windows.h>
|
||||
#endif
|
||||
|
||||
namespace OpenThreads {
|
||||
|
||||
#if defined(_OPENTHREADS_ATOMIC_USE_LIBRARY_ROUTINES)
|
||||
|
||||
// Non inline implementations for two special cases:
|
||||
// * win32
|
||||
// * i386 gcc
|
||||
//
|
||||
// On win32 we do not want to pull windows.h in the Atomic header.
|
||||
// windows.h just pulls too much stuff that disturbs sources.
|
||||
//
|
||||
// On i386 gcc, we have that nice builtins available but only with some compiler
|
||||
// architectural compiler flags enabled. This way we can make sure that the
|
||||
// compiler flags are like we had them in the configure test. If the functions
|
||||
// are inline, we do not need what compiler flags the Atomic header will see.
|
||||
|
||||
unsigned
|
||||
Atomic::operator++()
|
||||
{
|
||||
#if defined(_OPENTHREADS_ATOMIC_USE_GCC_BUILTINS)
|
||||
return __sync_add_and_fetch(&_value, 1);
|
||||
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
|
||||
return InterlockedIncrement(&_value);
|
||||
#else
|
||||
# error This implementation should happen inline in the include file
|
||||
#endif
|
||||
}
|
||||
|
||||
unsigned
|
||||
Atomic::operator--()
|
||||
{
|
||||
#if defined(_OPENTHREADS_ATOMIC_USE_GCC_BUILTINS)
|
||||
return __sync_sub_and_fetch(&_value, 1);
|
||||
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
|
||||
return InterlockedDecrement(&_value);
|
||||
#else
|
||||
# error This implementation should happen inline in the include file
|
||||
#endif
|
||||
}
|
||||
|
||||
Atomic::operator unsigned() const
|
||||
{
|
||||
#if defined(_OPENTHREADS_ATOMIC_USE_GCC_BUILTINS)
|
||||
__sync_synchronize();
|
||||
return _value;
|
||||
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
|
||||
MemoryBarrier();
|
||||
return static_cast<unsigned const volatile &>(_value);
|
||||
#else
|
||||
# error This implementation should happen inline in the include file
|
||||
#endif
|
||||
}
|
||||
|
||||
bool
|
||||
AtomicPtr::assign(void* ptrNew, const void* const ptrOld)
|
||||
{
|
||||
#if defined(_OPENTHREADS_ATOMIC_USE_GCC_BUILTINS)
|
||||
return __sync_bool_compare_and_swap(&_ptr, ptrOld, ptrNew);
|
||||
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
|
||||
return ptrOld == InterlockedCompareExchangePointer((PVOID volatile*)&_ptr, (PVOID)ptrNew, (PVOID)ptrOld);
|
||||
#else
|
||||
# error This implementation should happen inline in the include file
|
||||
#endif
|
||||
}
|
||||
|
||||
void*
|
||||
AtomicPtr::get() const
|
||||
{
|
||||
#if defined(_OPENTHREADS_ATOMIC_USE_GCC_BUILTINS)
|
||||
__sync_synchronize();
|
||||
return _ptr;
|
||||
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
|
||||
MemoryBarrier();
|
||||
return _ptr;
|
||||
#else
|
||||
# error This implementation should happen inline in the include file
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // defined(_OPENTHREADS_ATOMIC_USE_LIBRARY_ROUTINES)
|
||||
|
||||
}
|
||||
@@ -20,6 +20,7 @@ ADD_LIBRARY(${LIB_NAME}
|
||||
PThreadMutexPrivateData.h
|
||||
PThreadPrivateData.h
|
||||
../common/Version.cpp
|
||||
../common/Atomic.cpp
|
||||
)
|
||||
|
||||
IF(OPENTHREADS_SONAMES)
|
||||
|
||||
@@ -247,7 +247,7 @@ Referenced::~Referenced()
|
||||
delete tmpMutexPtr;
|
||||
}
|
||||
#else
|
||||
ObserverSetData* observerSetData = _observerSetDataPtr.get();
|
||||
ObserverSetData* observerSetData = static_cast<ObserverSetData*>(_observerSetDataPtr.get());
|
||||
if (observerSetData)
|
||||
{
|
||||
for(ObserverSet::iterator itr = observerSetData->_observers.begin();
|
||||
@@ -307,12 +307,12 @@ void Referenced::unref_nodelete() const
|
||||
void Referenced::addObserver(Observer* observer)
|
||||
{
|
||||
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
|
||||
ObserverSetData* observerSetData = _observerSetDataPtr.get();
|
||||
ObserverSetData* observerSetData = static_cast<ObserverSetData*>(_observerSetDataPtr.get());
|
||||
while (0 == observerSetData) {
|
||||
ObserverSetData* newObserverSetData = new ObserverSetData;
|
||||
if (!_observerSetDataPtr.assign(newObserverSetData, 0))
|
||||
delete newObserverSetData;
|
||||
observerSetData = _observerSetDataPtr.get();
|
||||
observerSetData = static_cast<ObserverSetData*>(_observerSetDataPtr.get());
|
||||
}
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(observerSetData->_mutex);
|
||||
observerSetData->_observers.insert(observer);
|
||||
@@ -335,7 +335,7 @@ void Referenced::addObserver(Observer* observer)
|
||||
void Referenced::removeObserver(Observer* observer)
|
||||
{
|
||||
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
|
||||
ObserverSetData* observerSetData = _observerSetDataPtr.get();
|
||||
ObserverSetData* observerSetData = static_cast<ObserverSetData*>(_observerSetDataPtr.get());
|
||||
if (observerSetData)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(observerSetData->_mutex);
|
||||
|
||||
Reference in New Issue
Block a user