diff --git a/include/boost/detail/atomic_count.hpp b/include/boost/detail/atomic_count.hpp index 2adb57e..b34dc85 100644 --- a/include/boost/detail/atomic_count.hpp +++ b/include/boost/detail/atomic_count.hpp @@ -97,6 +97,8 @@ typedef long atomic_count; # include #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) # include +#elif defined(__GLIBCPP__) +# include #elif defined(BOOST_HAS_PTHREADS) # define BOOST_AC_USE_PTHREADS # include diff --git a/include/boost/detail/atomic_count_gcc.hpp b/include/boost/detail/atomic_count_gcc.hpp new file mode 100644 index 0000000..c3d29a8 --- /dev/null +++ b/include/boost/detail/atomic_count_gcc.hpp @@ -0,0 +1,61 @@ +#ifndef BOOST_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED +#define BOOST_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED + +// +// boost/detail/atomic_count_gcc.hpp +// +// atomic_count for GNU libstdc++ v3 +// +// http://gcc.gnu.org/onlinedocs/porting/Thread-safety.html +// +// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. +// Copyright (c) 2002 Lars Gullik Bjønnes +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// + +#include + +namespace boost +{ + +namespace detail +{ + +class atomic_count +{ +public: + + explicit atomic_count(long v) : value_(v) {} + + void operator++() + { + __atomic_add(&value_, 1); + } + + long operator--() + { + return !__exchange_and_add(&value_, -1); + } + + operator long() const + { + return __exchange_and_add(&value_, 0); + } + +private: + + atomic_count(atomic_count const &); + atomic_count & operator=(atomic_count const &); + + _Atomic_word value_; +}; + +} // namespace detail + +} // namespace boost + +#endif // #ifndef BOOST_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED diff --git a/include/boost/detail/lightweight_mutex.hpp b/include/boost/detail/lightweight_mutex.hpp index 6a74397..b92b576 100644 --- a/include/boost/detail/lightweight_mutex.hpp +++ b/include/boost/detail/lightweight_mutex.hpp @@ -40,10 +40,14 @@ // // Note to implementors: if you write a platform-specific lightweight_mutex // for a platform that supports pthreads, be sure to test its performance -// against the pthreads-based version using smart_ptr_timing_test.cpp and -// smart_ptr_mt_test.cpp. Custom versions are usually not worth the trouble +// 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. +// #ifndef BOOST_HAS_THREADS # include @@ -57,6 +61,8 @@ # include #elif defined(__sgi) # include +#elif defined(__GLIBCPP__) +# include #elif defined(BOOST_HAS_PTHREADS) # define BOOST_LWM_USE_PTHREADS # include diff --git a/include/boost/detail/lwm_gcc.hpp b/include/boost/detail/lwm_gcc.hpp new file mode 100644 index 0000000..bf8a09a --- /dev/null +++ b/include/boost/detail/lwm_gcc.hpp @@ -0,0 +1,78 @@ +#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 +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// + +#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_(1) + { + } + + 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_linux.hpp b/include/boost/detail/lwm_linux.hpp index c4b3b1d..62d8e64 100644 --- a/include/boost/detail/lwm_linux.hpp +++ b/include/boost/detail/lwm_linux.hpp @@ -86,4 +86,4 @@ public: } // namespace boost -#endif // #ifndef BOOST_DETAIL_LWM_WIN32_HPP_INCLUDED +#endif // #ifndef BOOST_DETAIL_LWM_LINUX_HPP_INCLUDED