From Blasius Czink, "Among other things I added support for atomic operations on BSD-like systems and additional methods (for "and", "or", "xor").

"

and a later post the same osg-submissions thread:

"it's been a while since I have made the changes but I think it was due to problems with static builds of OpenThreads on windows. I was using
OpenThreads in a communication/synchronisation library (without
OpenSceneGraph). It seems I forgot to post a small change in the CMakeLists file of OpenThreads. If a user turns DYNAMIC_OPENTHREADS to OFF (static build) OT_LIBRARY_STATIC will be defined in the Config.
Without these changes a windows user will always end up with a "__declspec(dllexport)" or "__declspec(dllimport)" which is a problem for static builds."

And another post from Blasius on this topic:

"I tested with VS2005 and VS2008. For 32 bit everything works as expected. For x64 and VS2008 I could successfully do the cmake-configure and then the compilation but I had occasional crashes of cmTryCompileExec.exe (during the cmake-configure phase) which seems to be a cmake bug. With VS2005 and 64bit cmake does not set _OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED although the interlocked functionality should be there. If I place the source snippet from the CHECK_CXX_SOURCE_RUNS macro to a separate sourcefile I can compile and run the resulting executable successfully. Forcing OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED (on VS2005/x64) reveals a bug in "intrin.h" which seems to be fixed in VS2008 but not in VS2005.

In case anyone is interested the lines:
__MACHINEI(unsigned char _interlockedbittestandset(long *a, long b))
__MACHINEI(unsigned char _interlockedbittestandreset(long *a, long b))
__MACHINEX64(unsigned char _interlockedbittestandset64(__int64 *a, __int64 b))
__MACHINEX64(unsigned char _interlockedbittestandreset64(__int64 *a, __int64 b))

should be changed to:
__MACHINEI(unsigned char _interlockedbittestandset(long volatile *a, long b))
__MACHINEI(unsigned char _interlockedbittestandreset(long volatile *a, long b))
__MACHINEX64(unsigned char _interlockedbittestandset64(__int64 volatile *a, __int64 b))
__MACHINEX64(unsigned char _interlockedbittestandreset64(__int64 volatile *a, __int64 b))

The worst thing that can happen is that interlocked funtionality is not detected during cmake-configure and the mutex fallback is used.
Which reminds me another small glitch in the Atomic header so I attached a corrected version.



    Why is the OT_LIBRARY_STATIC added to the config file? It is not needed anywhere.

OT_LIBRARY_STATIC is needed if you are doing static-builds on Windows. See my previous post on that.
"
This commit is contained in:
Robert Osfield
2008-10-27 10:42:58 +00:00
parent b1c858d8f8
commit d703c58936
8 changed files with 212 additions and 27 deletions

View File

@@ -14,7 +14,11 @@
#include <OpenThreads/Atomic>
#if defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
# include <windows.h>
#include <windows.h>
#include <intrin.h>
#pragma intrinsic(_InterlockedAnd)
#pragma intrinsic(_InterlockedOr)
#pragma intrinsic(_InterlockedXor)
#endif
namespace OpenThreads {
@@ -40,6 +44,8 @@ Atomic::operator++()
return __sync_add_and_fetch(&_value, 1);
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
return InterlockedIncrement(&_value);
#elif defined(_OPENTHREADS_ATOMIC_USE_BSD_ATOMIC)
return OSAtomicIncrement32(&_value);
#else
# error This implementation should happen inline in the include file
#endif
@@ -52,11 +58,71 @@ Atomic::operator--()
return __sync_sub_and_fetch(&_value, 1);
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
return InterlockedDecrement(&_value);
#elif defined(_OPENTHREADS_ATOMIC_USE_BSD_ATOMIC)
return OSAtomicDecrement32(&_value);
#else
# error This implementation should happen inline in the include file
#endif
}
unsigned
Atomic::AND(unsigned value)
{
#if defined(_OPENTHREADS_ATOMIC_USE_GCC_BUILTINS)
return __sync_fetch_and_and(&_value, value);
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
return _InterlockedAnd(&_value, value);
#elif defined(_OPENTHREADS_ATOMIC_USE_BSD_ATOMIC)
return OSAtomicAnd32((uint32_t)value, (uint32_t *)&_value);
#else
# error This implementation should happen inline in the include file
#endif
}
unsigned
Atomic::OR(unsigned value)
{
#if defined(_OPENTHREADS_ATOMIC_USE_GCC_BUILTINS)
return __sync_fetch_and_or(&_value, value);
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
return _InterlockedOr(&_value, value);
#elif defined(_OPENTHREADS_ATOMIC_USE_BSD_ATOMIC)
return OSAtomicOr32((uint32_t)value, (uint32_t *)&_value);
#else
# error This implementation should happen inline in the include file
#endif
}
unsigned
Atomic::XOR(unsigned value)
{
#if defined(_OPENTHREADS_ATOMIC_USE_GCC_BUILTINS)
return __sync_fetch_and_xor(&_value, value);
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
return _InterlockedXor(&_value, value);
#elif defined(_OPENTHREADS_ATOMIC_USE_BSD_ATOMIC)
return OSAtomicXor32((uint32_t)value, (uint32_t *)&_value);
#else
# error This implementation should happen inline in the include file
#endif
}
unsigned
Atomic::exchange(unsigned value)
{
#if defined(_OPENTHREADS_ATOMIC_USE_GCC_BUILTINS)
return __sync_lock_test_and_set(&_value, value);
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
return InterlockedExchange(&_value, value);
#elif defined(_OPENTHREADS_ATOMIC_USE_BSD_ATOMIC)
return OSAtomicCompareAndSwap32(_value, value, &_value);
#else
# error This implementation should happen inline in the include file
#endif
}
Atomic::operator unsigned() const
{
#if defined(_OPENTHREADS_ATOMIC_USE_GCC_BUILTINS)
@@ -65,6 +131,9 @@ Atomic::operator unsigned() const
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
MemoryBarrier();
return static_cast<unsigned const volatile &>(_value);
#elif defined(_OPENTHREADS_ATOMIC_USE_BSD_ATOMIC)
OSMemoryBarrier();
return static_cast<unsigned const volatile>(_value);
#else
# error This implementation should happen inline in the include file
#endif
@@ -77,6 +146,8 @@ AtomicPtr::assign(void* ptrNew, const void* const ptrOld)
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);
#elif defined(_OPENTHREADS_ATOMIC_USE_BSD_ATOMIC)
return OSAtomicCompareAndSwapPtr((void *)ptrOld, (void *)ptrNew, (void* volatile *)&_ptr);
#else
# error This implementation should happen inline in the include file
#endif
@@ -91,6 +162,9 @@ AtomicPtr::get() const
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
MemoryBarrier();
return _ptr;
#elif defined(_OPENTHREADS_ATOMIC_USE_BSD_ATOMIC)
OSMemoryBarrier();
return _ptr;
#else
# error This implementation should happen inline in the include file
#endif

View File

@@ -27,6 +27,8 @@
#cmakedefine _OPENTHREADS_ATOMIC_USE_MIPOSPRO_BUILTINS
#cmakedefine _OPENTHREADS_ATOMIC_USE_SUN
#cmakedefine _OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED
#cmakedefine _OPENTHREADS_ATOMIC_USE_BSD_ATOMIC
#cmakedefine _OPENTHREADS_ATOMIC_USE_MUTEX
#cmakedefine OT_LIBRARY_STATIC
#endif