From patchwork Wed Nov 2 00:53:19 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 123195 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 1FF64B6F85 for ; Wed, 2 Nov 2011 11:53:44 +1100 (EST) Received: (qmail 15520 invoked by alias); 2 Nov 2011 00:53:40 -0000 Received: (qmail 15504 invoked by uid 22791); 2 Nov 2011 00:53:39 -0000 X-SWARE-Spam-Status: No, hits=-2.4 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW X-Spam-Check-By: sourceware.org Received: from mail-ww0-f51.google.com (HELO mail-ww0-f51.google.com) (74.125.82.51) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 02 Nov 2011 00:53:22 +0000 Received: by wwf27 with SMTP id 27so3373068wwf.8 for ; Tue, 01 Nov 2011 17:53:20 -0700 (PDT) MIME-Version: 1.0 Received: by 10.216.229.14 with SMTP id g14mr609927weq.6.1320195200155; Tue, 01 Nov 2011 17:53:20 -0700 (PDT) Received: by 10.216.90.19 with HTTP; Tue, 1 Nov 2011 17:53:19 -0700 (PDT) Date: Wed, 2 Nov 2011 00:53:19 +0000 Message-ID: Subject: [v3] implement LWG 2067 and new issues with constructors in From: Jonathan Wakely To: "libstdc++" , gcc-patches Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org This patch implements http://lwg.github.com/issues/lwg-active.html#2067 which has Ready status, as well as fixing two new issues I've reported in the past few hours. The first is that packaged_task's template constructors should be restricted to prevent them from being chosen to copy a packaged_task object and the second is that promise and packaged_task should properly support uses-allocator construction so that if "promise(args...)" is well-formed then so is "promise(allocator_arg, alloc, args...)" * include/std/future (promise): Add constructors for uses-allocator construction from rvalue promise. (packaged_task): Implement LWG 2067. Add additional constructors for uses-allocator construction. * testsuite/30_threads/packaged_task/cons/3.cc: New. * testsuite/30_threads/packaged_task/cons/alloc2.cc: New. * testsuite/30_threads/promise/cons/alloc2.cc: New. Tested x86_64-linux, committed to trunk. Index: include/std/future =================================================================== --- include/std/future (revision 180749) +++ include/std/future (working copy) @@ -955,6 +955,12 @@ _M_storage(__future_base::_S_allocate_result<_Res>(__a)) { } + template + promise(allocator_arg_t, const _Allocator&, promise&& __rhs) + : _M_future(std::move(__rhs._M_future)), + _M_storage(std::move(__rhs._M_storage)) + { } + promise(const promise&) = delete; ~promise() @@ -1047,6 +1053,12 @@ _M_storage(__future_base::_S_allocate_result<_Res&>(__a)) { } + template + promise(allocator_arg_t, const _Allocator&, promise&& __rhs) + : _M_future(std::move(__rhs._M_future)), + _M_storage(std::move(__rhs._M_storage)) + { } + promise(const promise&) = delete; ~promise() @@ -1122,6 +1134,12 @@ _M_storage(__future_base::_S_allocate_result(__a)) { } + template + promise(allocator_arg_t, const _Allocator&, promise&& __rhs) + : _M_future(std::move(__rhs._M_future)), + _M_storage(std::move(__rhs._M_storage)) + { } + promise(const promise&) = delete; ~promise() @@ -1270,6 +1288,15 @@ { return std::forward<_Tp>(__t); } }; + template::type>::value> + struct __is_same_pkgdtask + { typedef void __type; }; + + template + struct __is_same_pkgdtask<_Task, _Fn, true> + { }; + /// packaged_task template class packaged_task<_Res(_ArgTypes...)> @@ -1281,13 +1308,20 @@ // Construction and destruction packaged_task() noexcept { } - template + template explicit + packaged_task(allocator_arg_t, const _Allocator& __a) noexcept + { } + + template::__type> + explicit packaged_task(_Fn&& __fn) : _M_state(std::make_shared<_State_type>(std::forward<_Fn>(__fn))) { } - template + template::__type> explicit packaged_task(allocator_arg_t, const _Allocator& __a, _Fn&& __fn) : _M_state(std::allocate_shared<_State_type>(__a, @@ -1301,13 +1335,24 @@ } // No copy - packaged_task(packaged_task&) = delete; - packaged_task& operator=(packaged_task&) = delete; + packaged_task(const packaged_task&) = delete; + packaged_task& operator=(const packaged_task&) = delete; + template + explicit + packaged_task(allocator_arg_t, const _Allocator&, + const packaged_task&) = delete; + // Move support packaged_task(packaged_task&& __other) noexcept { this->swap(__other); } + template + explicit + packaged_task(allocator_arg_t, const _Allocator&, + packaged_task&& __other) noexcept + { this->swap(__other); } + packaged_task& operator=(packaged_task&& __other) noexcept { packaged_task(std::move(__other)).swap(*this); Index: testsuite/30_threads/packaged_task/cons/alloc2.cc =================================================================== --- testsuite/30_threads/packaged_task/cons/alloc2.cc (revision 0) +++ testsuite/30_threads/packaged_task/cons/alloc2.cc (revision 0) @@ -0,0 +1,40 @@ +// { dg-do compile } +// { dg-options "-std=gnu++0x" } +// { dg-require-cstdint "" } +// { dg-require-gthreads "" } +// { dg-require-atomic-builtins "" } + +// Copyright (C) 2011 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 3, 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 COPYING3. If not see +// . + +// Test that packaged_task can be default-constructed or move-constructed +// in a context that does uses-allocator construction. + +#include +#include +#include + +using std::packaged_task; +using std::allocator_arg; +using std::allocator; +using std::tuple; + +typedef packaged_task task; +allocator a; + +tuple t1{ allocator_arg, a }; +tuple t2{ allocator_arg, a, task{} }; Index: testsuite/30_threads/packaged_task/cons/3.cc =================================================================== --- testsuite/30_threads/packaged_task/cons/3.cc (revision 0) +++ testsuite/30_threads/packaged_task/cons/3.cc (revision 0) @@ -0,0 +1,56 @@ +// { 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 "" } +// { dg-require-atomic-builtins "" } + +// Copyright (C) 2011 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 3, 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 COPYING3. If not see +// . + +// LWG 2067. packaged_task should have deleted copy c'tor with const parameter + +#include +#include +#include + +template +std::future::type> spawn_task(F f) +{ + typedef typename std::result_of::type result_type; + std::packaged_task task(std::move(f)); + std::future res(task.get_future()); + std::thread(std::move(task)).detach(); + return res; +} + +int get_res() +{ + return 42; +} + +void test01() +{ + auto f = spawn_task(get_res); + VERIFY( f.get() == get_res() ); +} + +int main() +{ + test01(); +} Index: testsuite/30_threads/promise/cons/alloc2.cc =================================================================== --- testsuite/30_threads/promise/cons/alloc2.cc (revision 0) +++ testsuite/30_threads/promise/cons/alloc2.cc (revision 0) @@ -0,0 +1,42 @@ +// { dg-do compile } +// { dg-options "-std=gnu++0x" } +// { dg-require-cstdint "" } +// { dg-require-gthreads "" } +// { dg-require-atomic-builtins "" } + +// Copyright (C) 2011 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 3, 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 COPYING3. If not see +// . + +// Test that promise can be default-constructed or move-constructed +// in a context that does uses-allocator construction. + +#include +#include +#include + +using std::promise; +using std::allocator_arg; +using std::allocator; +using std::tuple; + +typedef promise p; +typedef promise pr; +typedef promise pv; +allocator

a; + +tuple t1{ allocator_arg, a }; +tuple t2{ allocator_arg, a, p{}, pr{}, pv{} };