From e0ee037e2d77bb994dcdef7105a1dafd663cf650 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 2 Apr 2005 13:12:28 +0000 Subject: [PATCH] Spinlocks removed. [SVN r27932] --- include/boost/detail/lightweight_mutex.hpp | 62 +---------- include/boost/detail/lwm_gcc.hpp | 77 ------------- include/boost/detail/lwm_irix.hpp | 73 ------------ include/boost/detail/lwm_win32.hpp | 122 --------------------- include/boost/detail/lwm_win32_nt.hpp | 63 ----------- 5 files changed, 5 insertions(+), 392 deletions(-) delete mode 100644 include/boost/detail/lwm_gcc.hpp delete mode 100644 include/boost/detail/lwm_irix.hpp delete mode 100644 include/boost/detail/lwm_win32.hpp delete mode 100644 include/boost/detail/lwm_win32_nt.hpp diff --git a/include/boost/detail/lightweight_mutex.hpp b/include/boost/detail/lightweight_mutex.hpp index 14aad0d..a0936cb 100644 --- a/include/boost/detail/lightweight_mutex.hpp +++ b/include/boost/detail/lightweight_mutex.hpp @@ -18,73 +18,21 @@ // // typedef boost::detail::lightweight_mutex; // -// boost::detail::lightweight_mutex meets a subset of the Mutex concept -// requirements: +// boost::detail::lightweight_mutex is a header-only implementation of +// a subset of the Mutex concept requirements: // // http://www.boost.org/doc/html/threads/concepts.html#threads.concepts.Mutex // -// * Used by the smart pointer library -// * Performance oriented -// * Header-only implementation -// * Small memory footprint -// * Not a general purpose mutex, use boost::mutex, CRITICAL_SECTION or -// pthread_mutex instead. -// * Never spin in a tight lock/do-something/unlock loop, since -// lightweight_mutex does not guarantee fairness. -// * Never keep a lightweight_mutex locked for long periods. +// It maps to a CRITICAL_SECTION on Windows or a pthread_mutex on POSIX. // -// The current implementation can use a pthread_mutex, a CRITICAL_SECTION, -// or a platform-specific spinlock. -// -// You can force a particular implementation by defining BOOST_LWM_USE_PTHREADS, -// BOOST_LWM_USE_CRITICAL_SECTION, or BOOST_LWM_USE_SPINLOCK. -// -// If neither macro has been defined, the default is to use a spinlock on Win32, -// and a pthread_mutex otherwise. -// -// Note that a spinlock is not a general synchronization primitive. In particular, -// it is not guaranteed to be a memory barrier, and it is possible to "livelock" -// if a lower-priority thread has acquired the spinlock but a higher-priority -// thread is spinning trying to acquire the same lock. -// -// For these reasons, spinlocks have been disabled by default except on Windows, -// where a spinlock can be several orders of magnitude faster than a CRITICAL_SECTION. - #include -// Note to implementors: if you write a platform-specific spinlock -// for a platform that supports pthreads, be sure to test its performance -// against the pthreads-based version using shared_ptr_timing_test.cpp and -// shared_ptr_mt_test.cpp. Custom versions are usually not worth the trouble -// _unless_ the performance gains are substantial. -// -// Be sure to compare against a "real" pthreads library; -// shared_ptr_timing_test.cpp will compile succesfully with a stub do-nothing -// pthreads library, since it doesn't create any threads. - -#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) && !defined(BOOST_LWM_USE_CRITICAL_SECTION) && !defined(BOOST_LWM_USE_PTHREADS) -# define BOOST_LWM_WIN32 -#endif - #if !defined(BOOST_HAS_THREADS) -# if defined(BOOST_LWM_WIN32) -# include -# else -# include -# endif -#elif defined(BOOST_LWM_USE_CRITICAL_SECTION) +# include +#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) # include -#elif defined(BOOST_LWM_USE_PTHREADS) -# include -#elif defined(BOOST_LWM_WIN32) -# include -#elif defined(BOOST_LWM_USE_SPINLOCK) && defined(__sgi) -# include -#elif defined(BOOST_LWM_USE_SPINLOCK) && defined(__GLIBCPP__) -# include #elif defined(BOOST_HAS_PTHREADS) -# define BOOST_LWM_USE_PTHREADS # include #else // Use #define BOOST_DISABLE_THREADS to avoid the error diff --git a/include/boost/detail/lwm_gcc.hpp b/include/boost/detail/lwm_gcc.hpp deleted file mode 100644 index e64ef7d..0000000 --- a/include/boost/detail/lwm_gcc.hpp +++ /dev/null @@ -1,77 +0,0 @@ -#ifndef BOOST_DETAIL_LWM_GCC_HPP_INCLUDED -#define BOOST_DETAIL_LWM_GCC_HPP_INCLUDED - -// -// boost/detail/lwm_gcc.hpp -// -// lightweight_mutex for GNU libstdc++ v3 -// -// http://gcc.gnu.org/onlinedocs/porting/Thread-safety.html -// -// Copyright (c) 2002 Peter Dimov and Multi Media Ltd. -// Copyright (c) 2002 Lars Gullik Bjønnes -// -// Distributed under the Boost Software License, Version 1.0. (See -// accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// - -#include -#include - -namespace boost -{ - -namespace detail -{ - -class lightweight_mutex -{ -private: - - _Atomic_word a_; - - lightweight_mutex(lightweight_mutex const &); - lightweight_mutex & operator=(lightweight_mutex const &); - -public: - - lightweight_mutex(): a_(0) - { - } - - class scoped_lock; - friend class scoped_lock; - - class scoped_lock - { - private: - - lightweight_mutex & m_; - - scoped_lock(scoped_lock const &); - scoped_lock & operator=(scoped_lock const &); - - public: - - explicit scoped_lock(lightweight_mutex & m): m_(m) - { - while( __exchange_and_add(&m_.a_, 1) ) - { - __atomic_add(&m_.a_, -1); - sched_yield(); - } - } - - ~scoped_lock() - { - __atomic_add(&m_.a_, -1); - } - }; -}; - -} // namespace detail - -} // namespace boost - -#endif // #ifndef BOOST_DETAIL_LWM_GCC_HPP_INCLUDED diff --git a/include/boost/detail/lwm_irix.hpp b/include/boost/detail/lwm_irix.hpp deleted file mode 100644 index cfc304b..0000000 --- a/include/boost/detail/lwm_irix.hpp +++ /dev/null @@ -1,73 +0,0 @@ -#ifndef BOOST_DETAIL_LWM_IRIX_HPP_INCLUDED -#define BOOST_DETAIL_LWM_IRIX_HPP_INCLUDED - -// -// boost/detail/lwm_irix.hpp -// -// Copyright (c) 2002 Peter Dimov and Multi Media Ltd. -// Copyright (c) 2002 Dan Gohman -// -// Distributed under the Boost Software License, Version 1.0. (See -// accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// - -#include -#include -#include - -namespace boost -{ - -namespace detail -{ - -class lightweight_mutex -{ -private: - - __uint32_t l_; - - lightweight_mutex(lightweight_mutex const &); - lightweight_mutex & operator=(lightweight_mutex const &); - -public: - - lightweight_mutex(): l_(0) - { - } - - class scoped_lock; - friend class scoped_lock; - - class scoped_lock - { - private: - - lightweight_mutex & m_; - - scoped_lock(scoped_lock const &); - scoped_lock & operator=(scoped_lock const &); - - public: - - explicit scoped_lock(lightweight_mutex & m): m_(m) - { - while( test_and_set32(&m_.l_, 1) ) - { - sched_yield(); - } - } - - ~scoped_lock() - { - m_.l_ = 0; - } - }; -}; - -} // namespace detail - -} // namespace boost - -#endif // #ifndef BOOST_DETAIL_LWM_IRIX_HPP_INCLUDED diff --git a/include/boost/detail/lwm_win32.hpp b/include/boost/detail/lwm_win32.hpp deleted file mode 100644 index 8496ed8..0000000 --- a/include/boost/detail/lwm_win32.hpp +++ /dev/null @@ -1,122 +0,0 @@ -#ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED -#define BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED - -// MS compatible compilers support #pragma once - -#if defined(_MSC_VER) && (_MSC_VER >= 1020) -# pragma once -#endif - -// -// boost/detail/lwm_win32.hpp -// -// Copyright (c) 2002, 2003 Peter Dimov -// -// Distributed under the Boost Software License, Version 1.0. (See -// accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// - -#ifdef BOOST_USE_WINDOWS_H -# include -#endif - -#ifdef __BORLANDC__ -# pragma warn -8027 // Functions containing while are not expanded inline -#endif - -namespace boost -{ - -namespace detail -{ - -#ifndef BOOST_USE_WINDOWS_H - -#ifdef _WIN64 - -// Intel 6.0 on Win64 version, posted by Tim Fenders to [boost-users] - -extern "C" long_type __cdecl _InterlockedExchange(long volatile *, long); - -#pragma intrinsic(_InterlockedExchange) - -inline long InterlockedExchange(long volatile* lp, long l) -{ - return _InterlockedExchange(lp, l); -} - -#else // _WIN64 - -extern "C" __declspec(dllimport) long __stdcall InterlockedExchange(long volatile *, long); - -#endif // _WIN64 - -extern "C" __declspec(dllimport) void __stdcall Sleep(unsigned long); - -#endif // #ifndef BOOST_USE_WINDOWS_H - -class lightweight_mutex -{ -private: - - long l_; - - lightweight_mutex(lightweight_mutex const &); - lightweight_mutex & operator=(lightweight_mutex const &); - -public: - - lightweight_mutex(): l_(0) - { - } - - class scoped_lock; - friend class scoped_lock; - - class scoped_lock - { - private: - - lightweight_mutex & m_; - - scoped_lock(scoped_lock const &); - scoped_lock & operator=(scoped_lock const &); - - public: - - explicit scoped_lock(lightweight_mutex & m): m_(m) - { - while( InterlockedExchange(&m_.l_, 1) ) - { - // Note: changed to Sleep(1) from Sleep(0). - // According to MSDN, Sleep(0) will never yield - // to a lower-priority thread, whereas Sleep(1) - // will. Performance seems not to be affected. - - Sleep(1); - } - } - - ~scoped_lock() - { - InterlockedExchange(&m_.l_, 0); - - // Note: adding a yield here will make - // the spinlock more fair and will increase the overall - // performance of some applications substantially in - // high contention situations, but will penalize the - // low contention / single thread case up to 5x - } - }; -}; - -} // namespace detail - -} // namespace boost - -#ifdef __BORLANDC__ -# pragma warn .8027 // Functions containing while are not expanded inline -#endif - -#endif // #ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED diff --git a/include/boost/detail/lwm_win32_nt.hpp b/include/boost/detail/lwm_win32_nt.hpp deleted file mode 100644 index 216e636..0000000 --- a/include/boost/detail/lwm_win32_nt.hpp +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef BOOST_DETAIL_LWM_WIN32_NT_HPP_INCLUDED -#define BOOST_DETAIL_LWM_WIN32_NT_HPP_INCLUDED - -#if defined(_MSC_VER) && (_MSC_VER >= 1020) -# pragma once -#endif - -// -// boost/detail/lwm_win32_nt.hpp -// -// Copyright (c) 2002, 2003 Peter Dimov -// -// Distributed under the Boost Software License, Version 1.0. (See -// accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// "No threads" version of lwm_win32.hpp; binary compatible but no-op. -// - -namespace boost -{ - -namespace detail -{ - -class lightweight_mutex -{ -private: - - long l_; - - lightweight_mutex(lightweight_mutex const &); - lightweight_mutex & operator=(lightweight_mutex const &); - -public: - - lightweight_mutex(): l_(0) - { - } - - class scoped_lock; - friend class scoped_lock; - - class scoped_lock - { - private: - - scoped_lock(scoped_lock const &); - scoped_lock & operator=(scoped_lock const &); - - public: - - explicit scoped_lock(lightweight_mutex &) - { - } - }; -}; - -} // namespace detail - -} // namespace boost - -#endif // #ifndef BOOST_DETAIL_LWM_WIN32_NT_HPP_INCLUDED