From patchwork Sat Dec 4 02:40:08 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: std::try_lock problems Date: Fri, 03 Dec 2010 16:40:08 -0000 From: Jonathan Wakely X-Patchwork-Id: 74234 Message-Id: To: "libstdc++" Cc: gcc-patches This fixes the problems described on the libstdc++ list, tested x86_64-linux and committed to trunk 2010-12-04 Jonathan Wakely * include/std/mutex (try_lock, __try_lock_impl): Fix. (lock): Implement using __try_lock_impl. * testsuite/30_threads/try_lock/2.cc: Fix logic. * testsuite/30_threads/try_lock/4.cc: New. * testsuite/30_threads/lock/1.cc: New. * testsuite/30_threads/lock/2.cc: New. * testsuite/30_threads/lock/3.cc: New. * testsuite/30_threads/lock/4.cc: New. Index: include/std/mutex =================================================================== --- include/std/mutex (revision 167396) +++ include/std/mutex (working copy) @@ -663,16 +663,19 @@ _GLIBCXX_BEGIN_NAMESPACE(std) static int __do_try_lock(tuple<_Lock&...>& __locks) { - if(std::get<_Idx>(__locks).try_lock()) - { - return __try_lock_impl<_Idx + 1, - _Idx + 2 < sizeof...(_Lock)>::__do_try_lock(__locks); - } - else - { - __unlock_impl<_Idx>::__do_unlock(__locks); - return _Idx; - } + __try + { + if(std::get<_Idx>(__locks).try_lock()) + { + return __try_lock_impl<_Idx + 1, + _Idx + 2 < sizeof...(_Lock)>::__do_try_lock(__locks); + } + } + __catch(...) + { + } + __unlock_impl<_Idx - 1>::__do_unlock(__locks); + return _Idx; } }; @@ -683,13 +686,16 @@ _GLIBCXX_BEGIN_NAMESPACE(std) static int __do_try_lock(tuple<_Lock&...>& __locks) { - if(std::get<_Idx>(__locks).try_lock()) - return -1; - else + __try + { + if(std::get<_Idx>(__locks).try_lock()) + return -1; + } + __catch(...) { - __unlock_impl<_Idx>::__do_unlock(__locks); - return _Idx; } + __unlock_impl<_Idx - 1>::__do_unlock(__locks); + return _Idx; } };