re PR libstdc++/24704 (__gnu_cxx::__exchange_and_add is called even for single threaded applications)

2006-05-24  Paolo Carlini  <pcarlini@suse.de>

	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
This commit is contained in:
Paolo Carlini 2006-05-24 16:37:42 +00:00 committed by Paolo Carlini
parent cc07b2dbeb
commit b7ee72de2d
11 changed files with 98 additions and 27 deletions

View File

@ -1,3 +1,22 @@
2006-05-24 Paolo Carlini <pcarlini@suse.de>
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 <pcarlini@suse.de>
* testsuite/testsuite_shared.cc: Fix --enable-threads=single build.

View File

@ -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 <bits/c++config.h>
#include <bits/gthr.h>
#include <bits/atomic_word.h>
_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

View File

@ -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

View File

@ -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;

View File

@ -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; }

View File

@ -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);

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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 <iostream> static object, ie just using <ios> 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

View File

@ -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;
}