From: cfairles Date: Sun, 28 Sep 2008 09:05:07 +0000 (+0000) Subject: 2008-09-28 Chris Fairles X-Git-Url: http://git.sourceforge.jp/view?p=pf3gnuchains%2Fgcc-fork.git;a=commitdiff_plain;h=c643ba1a340de0fa8a5fffa1993b5d90c5243a50;hp=cdfcd28789dbd886520e846de511207da2171e55 2008-09-28 Chris Fairles * include/std/mutex (try_lock): Implement generic try_lock. * testsuite/30_threads/try_lock/1.cc: New. * testsuite/30_threads/try_lock/2.cc: Likewise. * testsuite/30_threads/try_lock/3.cc: Likewise. * testsuite/30_threads/mutex/cons/assign_neg.cc: Adjust line numbers. * testsuite/30_threads/mutex/cons/copy_neg.cc: Likewise. * testsuite/30_threads/timed_mutex/cons/assign_neg.cc: Likewise. * testsuite/30_threads/timed_mutex/cons/copy_neg.cc: Likewise. * testsuite/30_threads/recursive_mutex/cons/assign_neg.cc: Likewise. * testsuite/30_threads/recursive_mutex/cons/copy_neg.cc: Likewise. * testsuite/30_threads/recursive_timed_mutex/cons/assign_neg.cc: Likewise. * testsuite/30_threads/recursive_timed_mutex/cons/copy_neg.cc: Likewise. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@140729 138bc75d-0d04-0410-961f-82ee72b054a4 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 66bd48a3a52..74ec2261272 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,19 @@ +2008-09-28 Chris Fairles + + * include/std/mutex (try_lock): Implement generic try_lock. + * testsuite/30_threads/try_lock/1.cc: New. + * testsuite/30_threads/try_lock/2.cc: Likewise. + * testsuite/30_threads/try_lock/3.cc: Likewise. + * testsuite/30_threads/mutex/cons/assign_neg.cc: Adjust line numbers. + * testsuite/30_threads/mutex/cons/copy_neg.cc: Likewise. + * testsuite/30_threads/timed_mutex/cons/assign_neg.cc: Likewise. + * testsuite/30_threads/timed_mutex/cons/copy_neg.cc: Likewise. + * testsuite/30_threads/recursive_mutex/cons/assign_neg.cc: Likewise. + * testsuite/30_threads/recursive_mutex/cons/copy_neg.cc: Likewise. + * testsuite/30_threads/recursive_timed_mutex/cons/assign_neg.cc: + Likewise. + * testsuite/30_threads/recursive_timed_mutex/cons/copy_neg.cc: Likewise. + 2008-09-26 Peter O'Gorman Steve Ellcey diff --git a/libstdc++-v3/include/std/mutex b/libstdc++-v3/include/std/mutex index e4ceaf2aac8..f3848d09c02 100644 --- a/libstdc++-v3/include/std/mutex +++ b/libstdc++-v3/include/std/mutex @@ -41,6 +41,7 @@ # include #else +#include #include #include #include @@ -601,9 +602,81 @@ namespace std swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>&& __y) { __x.swap(__y); } - template + template + struct __unlock_impl + { + template + static void + __do_unlock(tuple<_Lock&...>& __locks) + { + std::get<_Idx>(__locks).unlock(); + __unlock_impl<_Idx - 1>::__do_unlock(__locks); + } + }; + + template<> + struct __unlock_impl<-1> + { + template + static void + __do_unlock(tuple<_Lock&...>&) + { } + }; + + template + struct __try_lock_impl + { + template + 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; + } + } + }; + + template + struct __try_lock_impl<_Idx, false> + { + template + static int + __do_try_lock(tuple<_Lock&...>& __locks) + { + if(std::get<_Idx>(__locks).try_lock()) + return -1; + else + { + __unlock_impl<_Idx>::__do_unlock(__locks); + return _Idx; + } + } + }; + + /** @brief Generic try_lock. + * @param __l1 Meets Mutex requirements (try_lock() may throw). + * @param __l2 Meets Mutex requirements (try_lock() may throw). + * @param __l3 Meets Mutex requirements (try_lock() may throw). + * @return Returns -1 if all try_lock() calls return true. Otherwise returns + * a 0-based index corresponding to the argument that returned false. + * @post Either all arguments are locked, or none will be. + * + * Sequentially calls try_lock() on each argument. + */ + template int - try_lock(_L1& __l1, _L2& __l2, _L3&... __l3); + try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3) + { + tuple<_Lock1&, _Lock2&, _Lock3&...> __locks(__l1, __l2, __l3...); + return __try_lock_impl<0>::__do_try_lock(__locks); + } template void diff --git a/libstdc++-v3/testsuite/30_threads/mutex/cons/assign_neg.cc b/libstdc++-v3/testsuite/30_threads/mutex/cons/assign_neg.cc index 388743dfb67..ba7a53f2f16 100644 --- a/libstdc++-v3/testsuite/30_threads/mutex/cons/assign_neg.cc +++ b/libstdc++-v3/testsuite/30_threads/mutex/cons/assign_neg.cc @@ -42,4 +42,4 @@ void test01() } // { dg-error "used here" "" { target *-*-* } 41 } -// { dg-error "deleted function" "" { target *-*-* } 76 } +// { dg-error "deleted function" "" { target *-*-* } 77 } diff --git a/libstdc++-v3/testsuite/30_threads/mutex/cons/copy_neg.cc b/libstdc++-v3/testsuite/30_threads/mutex/cons/copy_neg.cc index 37daed3611d..b1c97fe6b26 100644 --- a/libstdc++-v3/testsuite/30_threads/mutex/cons/copy_neg.cc +++ b/libstdc++-v3/testsuite/30_threads/mutex/cons/copy_neg.cc @@ -41,4 +41,4 @@ void test01() } // { dg-error "used here" "" { target *-*-* } 40 } -// { dg-error "deleted function" "" { target *-*-* } 75 } +// { dg-error "deleted function" "" { target *-*-* } 76 } diff --git a/libstdc++-v3/testsuite/30_threads/recursive_mutex/cons/assign_neg.cc b/libstdc++-v3/testsuite/30_threads/recursive_mutex/cons/assign_neg.cc index 1ca7f1c6c5d..b1b6c15b481 100644 --- a/libstdc++-v3/testsuite/30_threads/recursive_mutex/cons/assign_neg.cc +++ b/libstdc++-v3/testsuite/30_threads/recursive_mutex/cons/assign_neg.cc @@ -42,4 +42,4 @@ void test01() } // { dg-error "used here" "" { target *-*-* } 41 } -// { dg-error "deleted function" "" { target *-*-* } 128 } +// { dg-error "deleted function" "" { target *-*-* } 129 } diff --git a/libstdc++-v3/testsuite/30_threads/recursive_mutex/cons/copy_neg.cc b/libstdc++-v3/testsuite/30_threads/recursive_mutex/cons/copy_neg.cc index d7322453c5c..2fa153be981 100644 --- a/libstdc++-v3/testsuite/30_threads/recursive_mutex/cons/copy_neg.cc +++ b/libstdc++-v3/testsuite/30_threads/recursive_mutex/cons/copy_neg.cc @@ -41,4 +41,4 @@ void test01() } // { dg-error "used here" "" { target *-*-* } 40 } -// { dg-error "deleted function" "" { target *-*-* } 127 } +// { dg-error "deleted function" "" { target *-*-* } 128 } diff --git a/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/cons/assign_neg.cc b/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/cons/assign_neg.cc index 28320937ec8..4d93beef812 100644 --- a/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/cons/assign_neg.cc +++ b/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/cons/assign_neg.cc @@ -42,4 +42,4 @@ void test01() } // { dg-error "used here" "" { target *-*-* } 41 } -// { dg-error "deleted function" "" { target *-*-* } 282 } +// { dg-error "deleted function" "" { target *-*-* } 283 } diff --git a/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/cons/copy_neg.cc b/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/cons/copy_neg.cc index 95eda1f3939..e1b63c28a5a 100644 --- a/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/cons/copy_neg.cc +++ b/libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/cons/copy_neg.cc @@ -41,4 +41,4 @@ void test01() } // { dg-error "used here" "" { target *-*-* } 40 } -// { dg-error "deleted function" "" { target *-*-* } 281 } +// { dg-error "deleted function" "" { target *-*-* } 282 } diff --git a/libstdc++-v3/testsuite/30_threads/timed_mutex/cons/assign_neg.cc b/libstdc++-v3/testsuite/30_threads/timed_mutex/cons/assign_neg.cc index 719ef0cb24a..715a5793a25 100644 --- a/libstdc++-v3/testsuite/30_threads/timed_mutex/cons/assign_neg.cc +++ b/libstdc++-v3/testsuite/30_threads/timed_mutex/cons/assign_neg.cc @@ -42,4 +42,4 @@ void test01() } // { dg-error "used here" "" { target *-*-* } 41 } -// { dg-error "deleted function" "" { target *-*-* } 179 } +// { dg-error "deleted function" "" { target *-*-* } 180 } diff --git a/libstdc++-v3/testsuite/30_threads/timed_mutex/cons/copy_neg.cc b/libstdc++-v3/testsuite/30_threads/timed_mutex/cons/copy_neg.cc index 20f309e9bb5..3c22c1eba95 100644 --- a/libstdc++-v3/testsuite/30_threads/timed_mutex/cons/copy_neg.cc +++ b/libstdc++-v3/testsuite/30_threads/timed_mutex/cons/copy_neg.cc @@ -41,4 +41,4 @@ void test01() } // { dg-error "used here" "" { target *-*-* } 40 } -// { dg-error "deleted function" "" { target *-*-* } 178 } +// { dg-error "deleted function" "" { target *-*-* } 179 } diff --git a/libstdc++-v3/testsuite/30_threads/try_lock/1.cc b/libstdc++-v3/testsuite/30_threads/try_lock/1.cc new file mode 100644 index 00000000000..4f86190a047 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/try_lock/1.cc @@ -0,0 +1,72 @@ +// { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } } +// { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } } +// { dg-options " -std=gnu++0x -pthreads" { target *-*-solaris* } } +// { dg-options " -std=gnu++0x " { target *-*-cygwin *-*-darwin* } } +// { dg-require-cstdint "" } +// { dg-require-gthreads "" } + +// Copyright (C) 2008 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 +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + +#include +#include +#include + +int main() +{ + bool test __attribute__((unused)) = true; + typedef std::mutex mutex_type; + typedef std::unique_lock lock_type; + + try + { + mutex_type m1, m2, m3; + lock_type l1(m1, std::defer_lock), + l2(m2, std::defer_lock), + l3(m3, std::defer_lock); + + try + { + int result = std::try_lock(l1, l2, l3); + VERIFY( result == -1 ); + } + catch (const std::system_error& e) + { + VERIFY( false ); + } + } + catch (const std::system_error& e) + { + VERIFY( false ); + } + catch (...) + { + VERIFY( false ); + } + + return 0; +} diff --git a/libstdc++-v3/testsuite/30_threads/try_lock/2.cc b/libstdc++-v3/testsuite/30_threads/try_lock/2.cc new file mode 100644 index 00000000000..b2046f012c3 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/try_lock/2.cc @@ -0,0 +1,117 @@ +// { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } } +// { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } } +// { dg-options " -std=gnu++0x -pthreads" { target *-*-solaris* } } +// { dg-options " -std=gnu++0x " { target *-*-cygwin *-*-darwin* } } +// { dg-require-cstdint "" } +// { dg-require-gthreads "" } + +// Copyright (C) 2008 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 +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + +#include +#include +#include + +void test01() +{ + bool test __attribute__((unused)) = true; + + try + { + std::mutex m1, m2, m3; + m1.lock(); + int result = std::try_lock(m1, m2, m3); + VERIFY( result == 0 ); + m1.lock(); + m2.lock(); + m3.lock(); + } + catch (const std::system_error& e) + { + VERIFY( false ); + } + catch (...) + { + VERIFY( false ); + } +} + +void test02() +{ + bool test __attribute__((unused)) = true; + + try + { + std::mutex m1, m2, m3; + m2.lock(); + int result = std::try_lock(m1, m2, m3); + VERIFY( result == 1 ); + m1.lock(); + m2.lock(); + m3.lock(); + } + catch (const std::system_error& e) + { + VERIFY( false ); + } + catch (...) + { + VERIFY( false ); + } +} + +void test03() +{ + bool test __attribute__((unused)) = true; + + try + { + std::mutex m1, m2, m3; + m3.lock(); + int result = std::try_lock(m1, m2, m3); + VERIFY( result == 2 ); + m1.lock(); + m2.lock(); + m3.lock(); + } + catch (const std::system_error& e) + { + VERIFY( false ); + } + catch (...) + { + VERIFY( false ); + } +} + +int main() +{ + test01(); + test02(); + test03(); + return 0; +} diff --git a/libstdc++-v3/testsuite/30_threads/try_lock/3.cc b/libstdc++-v3/testsuite/30_threads/try_lock/3.cc new file mode 100644 index 00000000000..a8bd41369eb --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/try_lock/3.cc @@ -0,0 +1,100 @@ +// { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } } +// { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } } +// { dg-options " -std=gnu++0x -pthreads" { target *-*-solaris* } } +// { dg-options " -std=gnu++0x " { target *-*-cygwin *-*-darwin* } } +// { dg-require-cstdint "" } +// { dg-require-gthreads "" } + +// Copyright (C) 2008 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 +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + +#include +#include +#include + +struct user_lock +{ + user_lock() : is_locked(false) { } + ~user_lock() = default; + user_lock(const user_lock&) = default; + + void lock() + { + bool test __attribute__((unused)) = true; + VERIFY( !is_locked ); + is_locked = true; + } + + bool try_lock() + { return is_locked ? false : (is_locked = true); } + + void unlock() + { + bool test __attribute__((unused)) = true; + VERIFY( is_locked ); + is_locked = false; + } + +private: + bool is_locked; +}; + +int main() +{ + bool test __attribute__((unused)) = true; + + try + { + std::mutex m1; + std::recursive_mutex m2; + user_lock m3; + + try + { + //heterogeneous types + int result = std::try_lock(m1, m2, m3); + VERIFY( result == -1 ); + m1.unlock(); + m2.unlock(); + m3.unlock(); + } + catch (const std::system_error& e) + { + VERIFY( false ); + } + } + catch (const std::system_error& e) + { + VERIFY( false ); + } + catch (...) + { + VERIFY( false ); + } + + return 0; +}