Commit 4ef89725 by Jimmy Guo Committed by Paolo Carlini

re PR libstdc++/40088 (Creating a std::ostringstream object locks a global mutex)

2009-12-18  Jimmy Guo  <jguo@yahoo-inc.com>

	PR libstdc++/40088
	* src/locale_init.cc (locale::locale()): Optimize the common case
	where _S_global still points to _S_classic.

From-SVN: r155342
parent 24ea727a
2009-12-18 Jimmy Guo <jguo@yahoo-inc.com>
PR libstdc++/40088
* src/locale_init.cc (locale::locale()): Optimize the common case
where _S_global still points to _S_classic.
2009-12-17 Dave Korn <dave.korn.cygwin@gmail.com> 2009-12-17 Dave Korn <dave.korn.cygwin@gmail.com>
PR target/42377 PR target/42377
......
...@@ -50,6 +50,9 @@ namespace std ...@@ -50,6 +50,9 @@ namespace std
* @{ * @{
*/ */
/// cv_status
enum class cv_status { no_timeout, timeout };
/// condition_variable /// condition_variable
class condition_variable class condition_variable
{ {
...@@ -84,13 +87,13 @@ namespace std ...@@ -84,13 +87,13 @@ namespace std
} }
template<typename _Duration> template<typename _Duration>
bool cv_status
wait_until(unique_lock<mutex>& __lock, wait_until(unique_lock<mutex>& __lock,
const chrono::time_point<__clock_t, _Duration>& __atime) const chrono::time_point<__clock_t, _Duration>& __atime)
{ return __wait_until_impl(__lock, __atime); } { return __wait_until_impl(__lock, __atime); }
template<typename _Clock, typename _Duration> template<typename _Clock, typename _Duration>
bool cv_status
wait_until(unique_lock<mutex>& __lock, wait_until(unique_lock<mutex>& __lock,
const chrono::time_point<_Clock, _Duration>& __atime) const chrono::time_point<_Clock, _Duration>& __atime)
{ {
...@@ -110,14 +113,14 @@ namespace std ...@@ -110,14 +113,14 @@ namespace std
_Predicate __p) _Predicate __p)
{ {
while (!__p()) while (!__p())
if (!wait_until(__lock, __atime)) if (wait_until(__lock, __atime) == cv_status::timeout)
return __p(); return __p();
return true; return true;
} }
template<typename _Rep, typename _Period> template<typename _Rep, typename _Period>
bool cv_status
wait_for(unique_lock<mutex>& __lock, wait_for(unique_lock<mutex>& __lock,
const chrono::duration<_Rep, _Period>& __rtime) const chrono::duration<_Rep, _Period>& __rtime)
{ return wait_until(__lock, __clock_t::now() + __rtime); } { return wait_until(__lock, __clock_t::now() + __rtime); }
...@@ -135,7 +138,7 @@ namespace std ...@@ -135,7 +138,7 @@ namespace std
private: private:
template<typename _Clock, typename _Duration> template<typename _Clock, typename _Duration>
bool cv_status
__wait_until_impl(unique_lock<mutex>& __lock, __wait_until_impl(unique_lock<mutex>& __lock,
const chrono::time_point<_Clock, _Duration>& __atime) const chrono::time_point<_Clock, _Duration>& __atime)
{ {
...@@ -154,7 +157,8 @@ namespace std ...@@ -154,7 +157,8 @@ namespace std
__gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(), __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
&__ts); &__ts);
return _Clock::now() < __atime; return (_Clock::now() < __atime
? cv_status::no_timeout : cv_status::timeout);
} }
}; };
...@@ -189,7 +193,7 @@ namespace std ...@@ -189,7 +193,7 @@ namespace std
wait(_Lock& __lock, _Predicate __p); wait(_Lock& __lock, _Predicate __p);
template<typename _Lock, typename _Clock, typename _Duration> template<typename _Lock, typename _Clock, typename _Duration>
bool cv_status
wait_until(_Lock& __lock, wait_until(_Lock& __lock,
const chrono::time_point<_Clock, _Duration>& __atime); const chrono::time_point<_Clock, _Duration>& __atime);
...@@ -201,7 +205,7 @@ namespace std ...@@ -201,7 +205,7 @@ namespace std
_Predicate __p); _Predicate __p);
template<typename _Lock, typename _Rep, typename _Period> template<typename _Lock, typename _Rep, typename _Period>
bool cv_status
wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime); wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime);
template<typename _Lock, typename _Rep, template<typename _Lock, typename _Rep,
......
...@@ -208,10 +208,26 @@ _GLIBCXX_BEGIN_NAMESPACE(std) ...@@ -208,10 +208,26 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
locale::locale() throw() : _M_impl(0) locale::locale() throw() : _M_impl(0)
{ {
_S_initialize(); _S_initialize();
// Checked locking to optimize the common case where _S_global
// still points to _S_classic (locale::_S_initialize_once()):
// - If they are the same, just increment the reference count and
// we are done. This effectively constructs a C locale object
// identical to the static c_locale.
// - Otherwise, _S_global can and may be destroyed due to
// locale::global() call on another thread, in which case we
// fall back to lock protected access to both _S_global and
// its reference count.
_M_impl = _S_global;
if (_M_impl == _S_classic)
_M_impl->_M_add_reference();
else
{
__gnu_cxx::__scoped_lock sentry(get_locale_mutex()); __gnu_cxx::__scoped_lock sentry(get_locale_mutex());
_S_global->_M_add_reference(); _S_global->_M_add_reference();
_M_impl = _S_global; _M_impl = _S_global;
} }
}
locale locale
locale::global(const locale& __other) locale::global(const locale& __other)
......
...@@ -32,4 +32,4 @@ void test01() ...@@ -32,4 +32,4 @@ void test01()
} }
// { dg-error "used here" "" { target *-*-* } 31 } // { dg-error "used here" "" { target *-*-* } 31 }
// { dg-error "deleted function" "" { target *-*-* } 67 } // { dg-error "deleted function" "" { target *-*-* } 70 }
...@@ -31,4 +31,4 @@ void test01() ...@@ -31,4 +31,4 @@ void test01()
} }
// { dg-error "used here" "" { target *-*-* } 30 } // { dg-error "used here" "" { target *-*-* } 30 }
// { dg-error "deleted function" "" { target *-*-* } 66 } // { dg-error "deleted function" "" { target *-*-* } 69 }
...@@ -40,8 +40,8 @@ int main() ...@@ -40,8 +40,8 @@ int main()
std::unique_lock<std::mutex> l(m); std::unique_lock<std::mutex> l(m);
auto then = std::chrono::system_clock::now(); auto then = std::chrono::system_clock::now();
bool result = c1.wait_for(l, ms); std::cv_status result = c1.wait_for(l, ms);
VERIFY( !result ); VERIFY( result == std::cv_status::timeout );
VERIFY( (std::chrono::system_clock::now() - then) >= ms ); VERIFY( (std::chrono::system_clock::now() - then) >= ms );
VERIFY( l.owns_lock() ); VERIFY( l.owns_lock() );
} }
......
...@@ -40,8 +40,8 @@ int main() ...@@ -40,8 +40,8 @@ int main()
std::unique_lock<std::mutex> l(m); std::unique_lock<std::mutex> l(m);
auto then = std::chrono::monotonic_clock::now(); auto then = std::chrono::monotonic_clock::now();
bool result = c1.wait_until(l, then + ms); std::cv_status result = c1.wait_until(l, then + ms);
VERIFY( !result ); VERIFY( result == std::cv_status::timeout );
VERIFY( (std::chrono::monotonic_clock::now() - then) >= ms ); VERIFY( (std::chrono::monotonic_clock::now() - then) >= ms );
VERIFY( l.owns_lock() ); VERIFY( l.owns_lock() );
} }
......
...@@ -32,4 +32,4 @@ void test01() ...@@ -32,4 +32,4 @@ void test01()
} }
// { dg-error "used here" "" { target *-*-* } 31 } // { dg-error "used here" "" { target *-*-* } 31 }
// { dg-error "deleted function" "" { target *-*-* } 175 } // { dg-error "deleted function" "" { target *-*-* } 179 }
...@@ -31,4 +31,4 @@ void test01() ...@@ -31,4 +31,4 @@ void test01()
} }
// { dg-error "used here" "" { target *-*-* } 30 } // { dg-error "used here" "" { target *-*-* } 30 }
// { dg-error "deleted function" "" { target *-*-* } 174 } // { dg-error "deleted function" "" { target *-*-* } 178 }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment