From b7ee72de2db0a89db568b94650e06ead6ed1feaf Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Wed, 24 May 2006 16:37:42 +0000 Subject: [PATCH] re PR libstdc++/24704 (__gnu_cxx::__exchange_and_add is called even for single threaded applications) 2006-05-24 Paolo Carlini PR libstdc++/24704 * include/bits/atomicity.h (__exchange_and_add_single, __atomic_add_single): New, single thread versions of the atomic functions. (__exchange_and_add_dispatch, __atomic_add_dispatch): New, depending on __GTHREADS and __gthread_active_p() dispatch either to the above or to the existing atomic functions. * include/ext/pool_allocator.h: Update callers. * include/ext/rc_string_base.h: Likewise. * include/bits/locale_classes.h: Likewise. * include/bits/basic_string.h: Likewise. * include/bits/ios_base.h: Likewise. * include/tr1/boost_shared_ptr.h: Likewise. * src/ios.cc: Likewise. * src/locale.cc: Likewise. * src/ios_init.cc: Likewise. From-SVN: r114044 --- libstdc++-v3/ChangeLog | 19 ++++++++ libstdc++-v3/include/bits/atomicity.h | 53 ++++++++++++++++++++- libstdc++-v3/include/bits/basic_string.h | 7 +-- libstdc++-v3/include/bits/ios_base.h | 4 +- libstdc++-v3/include/bits/locale_classes.h | 8 ++-- libstdc++-v3/include/ext/pool_allocator.h | 4 +- libstdc++-v3/include/ext/rc_string_base.h | 7 +-- libstdc++-v3/include/tr1/boost_shared_ptr.h | 12 ++--- libstdc++-v3/src/ios.cc | 2 +- libstdc++-v3/src/ios_init.cc | 6 +-- libstdc++-v3/src/locale.cc | 3 +- 11 files changed, 98 insertions(+), 27 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 7d008bf8919..21e4f00c03a 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,22 @@ +2006-05-24 Paolo Carlini + + PR libstdc++/24704 + * include/bits/atomicity.h (__exchange_and_add_single, + __atomic_add_single): New, single thread versions of the atomic + functions. + (__exchange_and_add_dispatch, __atomic_add_dispatch): New, + depending on __GTHREADS and __gthread_active_p() dispatch either + to the above or to the existing atomic functions. + * include/ext/pool_allocator.h: Update callers. + * include/ext/rc_string_base.h: Likewise. + * include/bits/locale_classes.h: Likewise. + * include/bits/basic_string.h: Likewise. + * include/bits/ios_base.h: Likewise. + * include/tr1/boost_shared_ptr.h: Likewise. + * src/ios.cc: Likewise. + * src/locale.cc: Likewise. + * src/ios_init.cc: Likewise. + 2006-05-23 Paolo Carlini * testsuite/testsuite_shared.cc: Fix --enable-threads=single build. diff --git a/libstdc++-v3/include/bits/atomicity.h b/libstdc++-v3/include/bits/atomicity.h index f436e0bdc00..53ab6fad703 100644 --- a/libstdc++-v3/include/bits/atomicity.h +++ b/libstdc++-v3/include/bits/atomicity.h @@ -1,6 +1,6 @@ // Low-level functions for atomic operations -*- C++ -*- -// Copyright (C) 2004, 2005 Free Software Foundation, Inc. +// Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -36,11 +36,12 @@ #define _GLIBCXX_ATOMICITY_H 1 #include +#include #include _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) - _Atomic_word + _Atomic_word __attribute__ ((__unused__)) __exchange_and_add(volatile _Atomic_word* __mem, int __val); @@ -48,6 +49,54 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) __attribute__ ((__unused__)) __atomic_add(volatile _Atomic_word* __mem, int __val); + static inline _Atomic_word + __exchange_and_add_single(volatile _Atomic_word* __mem, int __val) + { + _Atomic_word __result = *__mem; + *__mem += __val; + return __result; + } + + static inline void + __atomic_add_single(volatile _Atomic_word* __mem, int __val) + { *__mem += __val; } + + static inline _Atomic_word + __attribute__ ((__unused__)) + __exchange_and_add_dispatch(volatile _Atomic_word* __mem, int __val) + { +#ifdef __GTHREADS + + if (__gthread_active_p()) + return __exchange_and_add(__mem, __val); + else + return __exchange_and_add_single(__mem, __val); + +#else + + return __exchange_and_add_single(__mem, __val); + +#endif + } + + static inline void + __attribute__ ((__unused__)) + __atomic_add_dispatch(volatile _Atomic_word* __mem, int __val) + { +#ifdef __GTHREADS + + if (__gthread_active_p()) + __atomic_add(__mem, __val); + else + __atomic_add_single(__mem, __val); + +#else + + __atomic_add_single(__mem, __val); + +#endif + } + _GLIBCXX_END_NAMESPACE /* Even if the CPU doesn't need a memory barrier, we need to ensure that diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index acd2f144e81..4a703ce2b10 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -1,6 +1,6 @@ // Components for manipulating sequences of characters -*- C++ -*- -// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 +// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 // Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free @@ -232,7 +232,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std) #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING if (__builtin_expect(this != &_S_empty_rep(), false)) #endif - if (__gnu_cxx::__exchange_and_add(&this->_M_refcount, -1) <= 0) + if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount, + -1) <= 0) _M_destroy(__a); } // XXX MT @@ -245,7 +246,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std) #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING if (__builtin_expect(this != &_S_empty_rep(), false)) #endif - __gnu_cxx::__atomic_add(&this->_M_refcount, 1); + __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1); return _M_refdata(); } // XXX MT diff --git a/libstdc++-v3/include/bits/ios_base.h b/libstdc++-v3/include/bits/ios_base.h index 9b695079d7d..dbaa74d87c0 100644 --- a/libstdc++-v3/include/bits/ios_base.h +++ b/libstdc++-v3/include/bits/ios_base.h @@ -476,12 +476,12 @@ _GLIBCXX_BEGIN_NAMESPACE(std) : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { } void - _M_add_reference() { __gnu_cxx::__atomic_add(&_M_refcount, 1); } + _M_add_reference() { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); } // 0 => OK to delete. int _M_remove_reference() - { return __gnu_cxx::__exchange_and_add(&_M_refcount, -1); } + { return __gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1); } }; _Callback_list* _M_callbacks; diff --git a/libstdc++-v3/include/bits/locale_classes.h b/libstdc++-v3/include/bits/locale_classes.h index f72b72ab669..ab5efb7466f 100644 --- a/libstdc++-v3/include/bits/locale_classes.h +++ b/libstdc++-v3/include/bits/locale_classes.h @@ -400,12 +400,12 @@ _GLIBCXX_BEGIN_NAMESPACE(std) private: inline void _M_add_reference() const throw() - { __gnu_cxx::__atomic_add(&_M_refcount, 1); } + { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); } inline void _M_remove_reference() const throw() { - if (__gnu_cxx::__exchange_and_add(&_M_refcount, -1) == 1) + if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1) { try { delete this; } @@ -505,12 +505,12 @@ _GLIBCXX_BEGIN_NAMESPACE(std) inline void _M_add_reference() throw() - { __gnu_cxx::__atomic_add(&_M_refcount, 1); } + { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); } inline void _M_remove_reference() throw() { - if (__gnu_cxx::__exchange_and_add(&_M_refcount, -1) == 1) + if (__gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1) == 1) { try { delete this; } diff --git a/libstdc++-v3/include/ext/pool_allocator.h b/libstdc++-v3/include/ext/pool_allocator.h index 0d633366003..d12831018ce 100644 --- a/libstdc++-v3/include/ext/pool_allocator.h +++ b/libstdc++-v3/include/ext/pool_allocator.h @@ -206,9 +206,9 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) if (_S_force_new == 0) { if (std::getenv("GLIBCXX_FORCE_NEW")) - __atomic_add(&_S_force_new, 1); + __atomic_add_dispatch(&_S_force_new, 1); else - __atomic_add(&_S_force_new, -1); + __atomic_add_dispatch(&_S_force_new, -1); } const size_t __bytes = __n * sizeof(_Tp); diff --git a/libstdc++-v3/include/ext/rc_string_base.h b/libstdc++-v3/include/ext/rc_string_base.h index dece887d199..c8bb9f3edca 100644 --- a/libstdc++-v3/include/ext/rc_string_base.h +++ b/libstdc++-v3/include/ext/rc_string_base.h @@ -1,6 +1,6 @@ // Reference-counted versatile string base -*- C++ -*- -// Copyright (C) 2005 Free Software Foundation, Inc. +// Copyright (C) 2005, 2006 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -134,7 +134,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) _CharT* _M_refcopy() throw() { - __atomic_add(&_M_info._M_refcount, 1); + __atomic_add_dispatch(&_M_info._M_refcount, 1); return _M_refdata(); } // XXX MT @@ -202,7 +202,8 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) void _M_dispose() { - if (__exchange_and_add(&_M_rep()->_M_info._M_refcount, -1) <= 0) + if (__exchange_and_add_dispatch(&_M_rep()->_M_info._M_refcount, + -1) <= 0) _M_rep()->_M_destroy(_M_get_allocator()); } // XXX MT diff --git a/libstdc++-v3/include/tr1/boost_shared_ptr.h b/libstdc++-v3/include/tr1/boost_shared_ptr.h index e47c570c881..27f0e7a101e 100644 --- a/libstdc++-v3/include/tr1/boost_shared_ptr.h +++ b/libstdc++-v3/include/tr1/boost_shared_ptr.h @@ -130,14 +130,14 @@ public: void add_ref_copy() { - __gnu_cxx::__atomic_add(&_M_use_count, 1); + __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); } void add_ref_lock() { __gnu_cxx::lock lock(_M_mutex); - if (__gnu_cxx::__exchange_and_add(&_M_use_count, 1) == 0) + if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) { _M_use_count = 0; __throw_bad_weak_ptr(); @@ -147,14 +147,14 @@ public: void release() // nothrow { - if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) == 1) + if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1) { dispose(); #ifdef __GTHREADS _GLIBCXX_READ_MEM_BARRIER; _GLIBCXX_WRITE_MEM_BARRIER; #endif - if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) == 1) + if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1) destroy(); } } @@ -162,13 +162,13 @@ public: void weak_add_ref() // nothrow { - __gnu_cxx::__atomic_add(&_M_weak_count, 1); + __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); } void weak_release() // nothrow { - if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) == 1) + if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1) { #ifdef __GTHREADS _GLIBCXX_READ_MEM_BARRIER; diff --git a/libstdc++-v3/src/ios.cc b/libstdc++-v3/src/ios.cc index 5b05f283a85..e22c681e905 100644 --- a/libstdc++-v3/src/ios.cc +++ b/libstdc++-v3/src/ios.cc @@ -107,7 +107,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std) // Implementation note: Initialize top to zero to ensure that // initialization occurs before main() is started. static _Atomic_word _S_top = 0; - return __gnu_cxx::__exchange_and_add(&_S_top, 1) + 4; + return __gnu_cxx::__exchange_and_add_dispatch(&_S_top, 1) + 4; } void diff --git a/libstdc++-v3/src/ios_init.cc b/libstdc++-v3/src/ios_init.cc index 93a39857bc7..680efddaabf 100644 --- a/libstdc++-v3/src/ios_init.cc +++ b/libstdc++-v3/src/ios_init.cc @@ -82,7 +82,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std) ios_base::Init::Init() { - if (__gnu_cxx::__exchange_and_add(&_S_refcount, 1) == 0) + if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0) { // Standard streams default to synced with "C" operations. _S_synced_with_stdio = true; @@ -121,13 +121,13 @@ _GLIBCXX_BEGIN_NAMESPACE(std) // streams are not re-initialized with uses of ios_base::Init // besides static object, ie just using with // ios_base::Init objects. - __gnu_cxx::__atomic_add(&_S_refcount, 1); + __gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1); } } ios_base::Init::~Init() { - if (__gnu_cxx::__exchange_and_add(&_S_refcount, -1) == 2) + if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, -1) == 2) { // Catch any exceptions thrown by basic_ostream::flush() try diff --git a/libstdc++-v3/src/locale.cc b/libstdc++-v3/src/locale.cc index c56ec51909b..22d8ab0a675 100644 --- a/libstdc++-v3/src/locale.cc +++ b/libstdc++-v3/src/locale.cc @@ -433,7 +433,8 @@ _GLIBCXX_BEGIN_NAMESPACE(std) _M_index = 1 + f->_M_id(); else #endif - _M_index = 1 + __gnu_cxx::__exchange_and_add(&_S_refcount, 1); + _M_index = 1 + __gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, + 1); } return _M_index - 1; }