This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Add support for C++2a stop_token


On 15/11/19 14:38 +0000, Jonathan Wakely wrote:
On 13/11/19 17:59 -0800, Thomas Rodgers wrote:
+  // TODO verify the standard requires this
+#if 0
+  // register another callback
+  bool cb3called{false};
+  std::stop_callback scb3{stok, [&]
+                                {
+                                  cb3called = true;
+                                }};
+  VERIFY(ssrc.stop_possible());
+  VERIFY(ssrc.stop_requested());
+  VERIFY(stok.stop_possible());
+  VERIFY(stok.stop_requested());
+  VERIFY(!cb1called);
+  VERIFY(cb2called);
+  VERIFY(cb3called);
+#endif

The working draft definitely requires this:

Effects: Initializes callback with std::forward<C>(cb). If st.stop_requested() is true, then
std::forward<Callback>(callback)() is evaluated in the current thread before the constructor
returns.

I've committed this fix to the nostopstate initializer, so it defines
a variable, instead of declaring a function. I've also added a test
that uses the stop_source(nostopstate_t) constructor, and a few other
tweaks to include the new header where it's needed.

Tested powerpc64le-linux, committed to trunk.

commit 293fddacf1d794d92408824c95de24d285bdc273
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Nov 15 13:25:35 2019 +0000

    libstdc++: Fix definition of std::nostopstate object
    
    Also add <stop_token> header to PCH and Doxygen config.
    
            * doc/doxygen/user.cfg.in: Add <stop_token>.
            * include/precompiled/stdc++.h: Likewise.
            * include/std/stop_token: Fix definition of std::nostopstate.
            * testsuite/30_threads/headers/stop_token/synopsis.cc: New test.
            * testsuite/30_threads/headers/thread/types_std_c++20.cc: New test.
            * testsuite/30_threads/stop_token/stop_source.cc: New test.
            * testsuite/30_threads/stop_token/stop_token.cc: Remove unnecessary
            dg-require directives. Remove I/O and inclusion of <iostream>.

diff --git a/libstdc++-v3/doc/doxygen/user.cfg.in b/libstdc++-v3/doc/doxygen/user.cfg.in
index 18994703f0b..19f8ffd8230 100644
--- a/libstdc++-v3/doc/doxygen/user.cfg.in
+++ b/libstdc++-v3/doc/doxygen/user.cfg.in
@@ -840,6 +840,7 @@ INPUT                  = @srcdir@/doc/doxygen/doxygroups.cc \
                          include/sstream \
                          include/stack \
                          include/stdexcept \
+                         include/stop_token \
                          include/streambuf \
                          include/string \
                          include/string_view \
diff --git a/libstdc++-v3/include/precompiled/stdc++.h b/libstdc++-v3/include/precompiled/stdc++.h
index 118fc8f359a..c77136ef441 100644
--- a/libstdc++-v3/include/precompiled/stdc++.h
+++ b/libstdc++-v3/include/precompiled/stdc++.h
@@ -140,6 +140,7 @@
 #include <numbers>
 #include <ranges>
 #include <span>
+#include <stop_token>
 // #include <syncstream>
 #include <version>
 #endif
diff --git a/libstdc++-v3/include/std/stop_token b/libstdc++-v3/include/std/stop_token
index af64018bb0e..f96c267f22e 100644
--- a/libstdc++-v3/include/std/stop_token
+++ b/libstdc++-v3/include/std/stop_token
@@ -47,7 +47,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   class stop_callback;
 
   struct nostopstate_t { explicit nostopstate_t() = default; };
-  inline constexpr nostopstate_t nostopstate();
+  inline constexpr nostopstate_t nostopstate{};
 
   class stop_token
   {
diff --git a/libstdc++-v3/testsuite/30_threads/headers/stop_token/synopsis.cc b/libstdc++-v3/testsuite/30_threads/headers/stop_token/synopsis.cc
new file mode 100644
index 00000000000..b185bc7be39
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/headers/stop_token/synopsis.cc
@@ -0,0 +1,35 @@
+// Copyright (C) 2019 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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <stop_token>
+
+namespace std
+{
+  class stop_token;
+  class stop_source;
+  struct nostopstate_t;
+  template<class>
+    class stop_callback;
+}
+
+namespace gnu
+{
+  constexpr const std::nostopstate_t* tag = &std::nostopstate;
+}
diff --git a/libstdc++-v3/testsuite/30_threads/headers/thread/types_std_c++20.cc b/libstdc++-v3/testsuite/30_threads/headers/thread/types_std_c++20.cc
new file mode 100644
index 00000000000..86f29d8b001
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/headers/thread/types_std_c++20.cc
@@ -0,0 +1,30 @@
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+// { dg-require-gthreads "" }
+
+// Copyright (C) 2019 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
+// <http://www.gnu.org/licenses/>.
+
+#include <thread>
+
+void test01()
+{
+  using t_t = std::thread;
+  using j_t = std::jthread;
+
+  using namespace std::this_thread;
+}
diff --git a/libstdc++-v3/testsuite/30_threads/stop_token/stop_source.cc b/libstdc++-v3/testsuite/30_threads/stop_token/stop_source.cc
new file mode 100644
index 00000000000..9e477b7d7ee
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/stop_token/stop_source.cc
@@ -0,0 +1,75 @@
+// Copyright (C) 2019 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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <stop_token>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  std::stop_source ssrc;
+  VERIFY( ssrc.stop_possible() );
+  VERIFY( !ssrc.stop_requested() );
+
+  std::stop_source copy(ssrc);
+  VERIFY( copy.stop_possible() );
+  VERIFY( !copy.stop_requested() );
+  VERIFY( ssrc.stop_possible() );
+  VERIFY( !ssrc.stop_requested() );
+
+  std::stop_source move(std::move(ssrc));
+  VERIFY( move.stop_possible() );
+  VERIFY( !move.stop_requested() );
+  VERIFY( copy.stop_possible() );
+  VERIFY( !copy.stop_requested() );
+  VERIFY( !ssrc.stop_possible() );
+  VERIFY( !ssrc.stop_requested() );
+}
+
+void
+test02()
+{
+  // stop_source(nostopstate_t) constructor is explicit:
+  static_assert(!std::is_convertible_v<std::nostopstate_t, std::stop_source>);
+
+  std::stop_source ssrc(std::nostopstate);
+  VERIFY( !ssrc.stop_possible() );
+  VERIFY( !ssrc.stop_requested() );
+
+  std::stop_source copy(ssrc);
+  VERIFY( !copy.stop_possible() );
+  VERIFY( !copy.stop_requested() );
+  VERIFY( !ssrc.stop_possible() );
+  VERIFY( !ssrc.stop_requested() );
+
+  std::stop_source move(std::move(ssrc));
+  VERIFY( !move.stop_possible() );
+  VERIFY( !move.stop_requested() );
+  VERIFY( !copy.stop_possible() );
+  VERIFY( !copy.stop_requested() );
+  VERIFY( !ssrc.stop_possible() );
+  VERIFY( !ssrc.stop_requested() );
+}
+
+int main()
+{
+  test01();
+  test02();
+}
diff --git a/libstdc++-v3/testsuite/30_threads/stop_token/stop_token.cc b/libstdc++-v3/testsuite/30_threads/stop_token/stop_token.cc
index 37d79e8a64e..f0772c1d80f 100644
--- a/libstdc++-v3/testsuite/30_threads/stop_token/stop_token.cc
+++ b/libstdc++-v3/testsuite/30_threads/stop_token/stop_token.cc
@@ -15,13 +15,10 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// { dg-options "-std=gnu++2a -pthread" }
-// { dg-do run }
-// { dg-require-effective-target c++2a }
-// { dg-require-effective-target pthread }
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
 
 #include <stop_token>
-#include <iostream>
 #include <testsuite_hooks.h>
 
 int main()
@@ -41,7 +38,6 @@ int main()
   // register callback
   bool cb1called{false};
   auto cb1 = [&]{
-               std::cout << "cb1" << std::endl;
                cb1called = true;
              };
   {

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]