[PATCH v2 1/3] libstdc++: Add infrastructure for std::hazard_pointer (C++26)

Paul Xi Cao <[email protected]>
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.libstdc++.devel
Message-ID <[email protected]>
Prepare libstdc++ for the std::hazard_pointer implementation (P2530R3,
C++26 [32.11.3]):

  * <hazard_pointer> top-level header with the conventional
    feature-test gate around the internal include.
  * bits/hazard_ptr.h empty stub so the symlink farm picks it up
    before the implementation commit lands.
  * version.def entry for __cpp_lib_hazard_pointer == 202306L gated
    on gthread = yes and hosted = yes -- hazard_pointer is a hosted
    concurrency facility and must not advertise the feature-test
    macro on no-thread or freestanding builds, matching the pattern
    used by jthread, barrier, and latch.
  * Register bits/hazard_ptr.h in include/Makefile.am so the build
    creates a symlink under build/include/bits/.
  * Smoke tests verifying __cpp_lib_hazard_pointer is exposed at
    202306L through both <hazard_pointer> (1.cc) and <version>
    (version.cc), matching the jthread/expected convention.

Changed since v1: version.h is now the autogen output rather than a
hand-edit, so the gthread and hosted gating declared in version.def is
actually emitted.  v1 shipped a version.h whose condition was only
__cplusplus, which advertised the feature-test macro on no-thread and
freestanding builds -- exactly what the gating exists to prevent.

No behavior change; the public class definitions arrive in the next
commit.

libstdc++-v3/ChangeLog:

	* include/Makefile.am: Add hazard_ptr.h.
	* include/Makefile.in: Regenerate.
	* include/bits/version.def: New entry, gated on gthread and hosted.
	* include/bits/version.h: Regenerate.
	* include/bits/hazard_ptr.h: New file (stub).
	* include/std/hazard_pointer: New file.
	* testsuite/30_threads/hazard_pointer/1.cc: New test.
	* testsuite/30_threads/hazard_pointer/version.cc: New test.
---
 libstdc++-v3/include/Makefile.am              |  2 +
 libstdc++-v3/include/Makefile.in              |  2 +
 libstdc++-v3/include/bits/hazard_ptr.h        | 33 ++++++++++++++
 libstdc++-v3/include/bits/version.def         | 10 +++++
 libstdc++-v3/include/bits/version.h           | 10 +++++
 libstdc++-v3/include/std/hazard_pointer       | 44 +++++++++++++++++++
 .../testsuite/30_threads/hazard_pointer/1.cc  | 28 ++++++++++++
 .../30_threads/hazard_pointer/version.cc      | 29 ++++++++++++
 8 files changed, 158 insertions(+)
 create mode 100644 libstdc++-v3/include/bits/hazard_ptr.h
 create mode 100644 libstdc++-v3/include/std/hazard_pointer
 create mode 100644 libstdc++-v3/testsuite/30_threads/hazard_pointer/1.cc
 create mode 100644 libstdc++-v3/testsuite/30_threads/hazard_pointer/version.cc

diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am
index 3c30aa21d6b..1f9c1566674 100644
--- a/libstdc++-v3/include/Makefile.am
+++ b/libstdc++-v3/include/Makefile.am
@@ -79,6 +79,7 @@ std_headers = \
 	${std_srcdir}/forward_list \
 	${std_srcdir}/fstream \
 	${std_srcdir}/future \
+	${std_srcdir}/hazard_pointer \
 	${std_srcdir}/inplace_vector \
 	${std_srcdir}/iomanip \
 	${std_srcdir}/ios \
@@ -231,6 +232,7 @@ bits_headers = \
 	${bits_srcdir}/gslice_array.h \
 	${bits_srcdir}/hashtable.h \
 	${bits_srcdir}/hashtable_policy.h \
+	${bits_srcdir}/hazard_ptr.h \
 	${bits_srcdir}/indirect.h \
 	${bits_srcdir}/indirect_array.h \
 	${bits_srcdir}/ios_base.h \
diff --git a/libstdc++-v3/include/Makefile.in b/libstdc++-v3/include/Makefile.in
index d081481293f..cc2fb87631f 100644
--- a/libstdc++-v3/include/Makefile.in
+++ b/libstdc++-v3/include/Makefile.in
@@ -438,6 +438,7 @@ std_freestanding = \
 @GLIBCXX_HOSTED_TRUE@	${std_srcdir}/forward_list \
 @GLIBCXX_HOSTED_TRUE@	${std_srcdir}/fstream \
 @GLIBCXX_HOSTED_TRUE@	${std_srcdir}/future \
+@GLIBCXX_HOSTED_TRUE@	${std_srcdir}/hazard_pointer \
 @GLIBCXX_HOSTED_TRUE@	${std_srcdir}/inplace_vector \
 @GLIBCXX_HOSTED_TRUE@	${std_srcdir}/iomanip \
 @GLIBCXX_HOSTED_TRUE@	${std_srcdir}/ios \
@@ -587,6 +588,7 @@ bits_freestanding = \
 @GLIBCXX_HOSTED_TRUE@	${bits_srcdir}/gslice_array.h \
 @GLIBCXX_HOSTED_TRUE@	${bits_srcdir}/hashtable.h \
 @GLIBCXX_HOSTED_TRUE@	${bits_srcdir}/hashtable_policy.h \
+@GLIBCXX_HOSTED_TRUE@	${bits_srcdir}/hazard_ptr.h \
 @GLIBCXX_HOSTED_TRUE@	${bits_srcdir}/indirect.h \
 @GLIBCXX_HOSTED_TRUE@	${bits_srcdir}/indirect_array.h \
 @GLIBCXX_HOSTED_TRUE@	${bits_srcdir}/ios_base.h \
diff --git a/libstdc++-v3/include/bits/hazard_ptr.h b/libstdc++-v3/include/bits/hazard_ptr.h
new file mode 100644
index 00000000000..3ffccfd2d19
--- /dev/null
+++ b/libstdc++-v3/include/bits/hazard_ptr.h
@@ -0,0 +1,33 @@
+// Implementation of std::hazard_pointer -*- C++ -*-
+
+// Copyright (C) 2026 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.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file bits/hazard_ptr.h
+ *  This is an internal header file, included by other library headers.
+ *  Do not attempt to use it directly. @headername{hazard_pointer}
+ */
+
+#ifndef _GLIBCXX_HAZARD_PTR_H
+#define _GLIBCXX_HAZARD_PTR_H 1
+
+#endif // _GLIBCXX_HAZARD_PTR_H
diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def
index d14d4f4ef25..fe107a8f2fe 100644
--- a/libstdc++-v3/include/bits/version.def
+++ b/libstdc++-v3/include/bits/version.def
@@ -2625,6 +2625,16 @@ ftms = {
   };
 };
 
+ftms = {
+  name = hazard_pointer;
+  values = {
+    v = 202306;
+    cxxmin = 26;
+    gthread = yes;
+    hosted = yes;
+  };
+};
+
 // Standard test specifications.
 stds[97] = ">= 199711L";
 stds[03] = ">= 199711L";
diff --git a/libstdc++-v3/include/bits/version.h b/libstdc++-v3/include/bits/version.h
index 9a73f771e72..abcbf3e3020 100644
--- a/libstdc++-v3/include/bits/version.h
+++ b/libstdc++-v3/include/bits/version.h
@@ -2909,4 +2909,14 @@
 #endif /* !defined(__cpp_lib_hardened_view_interface) */
 #undef __glibcxx_want_hardened_view_interface
 
+#if !defined(__cpp_lib_hazard_pointer)
+# if (__cplusplus >  202302L) && defined(_GLIBCXX_HAS_GTHREADS) && _GLIBCXX_HOSTED
+#  define __glibcxx_hazard_pointer 202306L
+#  if defined(__glibcxx_want_all) || defined(__glibcxx_want_hazard_pointer)
+#   define __cpp_lib_hazard_pointer 202306L
+#  endif
+# endif
+#endif /* !defined(__cpp_lib_hazard_pointer) */
+#undef __glibcxx_want_hazard_pointer
+
 #undef __glibcxx_want_all
diff --git a/libstdc++-v3/include/std/hazard_pointer b/libstdc++-v3/include/std/hazard_pointer
new file mode 100644
index 00000000000..c231f69ff34
--- /dev/null
+++ b/libstdc++-v3/include/std/hazard_pointer
@@ -0,0 +1,44 @@
+// <hazard_pointer> -*- C++ -*-
+
+// Copyright (C) 2026 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.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file include/hazard_pointer
+ *  This is a Standard C++ Library header.
+ */
+
+#ifndef _GLIBCXX_HAZARD_POINTER
+#define _GLIBCXX_HAZARD_POINTER 1
+
+#ifdef _GLIBCXX_SYSHDR
+#pragma GCC system_header
+#endif
+
+#include <bits/requires_hosted.h> // concurrency
+
+#define __glibcxx_want_hazard_pointer
+#include <bits/version.h>
+
+#ifdef __cpp_lib_hazard_pointer // C++ >= 26 && hosted
+#include <bits/hazard_ptr.h>
+#endif // __cpp_lib_hazard_pointer
+#endif // _GLIBCXX_HAZARD_POINTER
diff --git a/libstdc++-v3/testsuite/30_threads/hazard_pointer/1.cc b/libstdc++-v3/testsuite/30_threads/hazard_pointer/1.cc
new file mode 100644
index 00000000000..0d425b90450
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/hazard_pointer/1.cc
@@ -0,0 +1,28 @@
+// Copyright (C) 2026 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-do compile { target c++26 } }
+// { dg-require-effective-target hosted }
+// { dg-add-options no_pch }
+
+#include <hazard_pointer>
+
+#ifndef __cpp_lib_hazard_pointer
+# error "Feature-test macro for hazard_pointer missing in <hazard_pointer>"
+#elif __cpp_lib_hazard_pointer != 202306L
+# error "Feature-test macro for hazard_pointer has wrong value in <hazard_pointer>"
+#endif
\ No newline at end of file
diff --git a/libstdc++-v3/testsuite/30_threads/hazard_pointer/version.cc b/libstdc++-v3/testsuite/30_threads/hazard_pointer/version.cc
new file mode 100644
index 00000000000..8ac0fde320f
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/hazard_pointer/version.cc
@@ -0,0 +1,29 @@
+// Copyright (C) 2026 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-do compile { target c++26 } }
+// { dg-require-gthreads "" }
+// { dg-require-effective-target hosted }
+// { dg-add-options no_pch }
+
+#include <version>
+
+#ifndef __cpp_lib_hazard_pointer
+# error "Feature-test macro for hazard_pointer missing in <version>"
+#elif __cpp_lib_hazard_pointer != 202306L
+# error "Feature-test macro for hazard_pointer has wrong value in <version>"
+#endif
-- 
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.