diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4
index 486347b34d9469b4fb085e0a74e66e1d34ed1713..a4a0bb8401815e547ce1ecaecc59c70380550ad2 100644
--- a/libstdc++-v3/acinclude.m4
+++ b/libstdc++-v3/acinclude.m4
@@ -4089,6 +4089,43 @@ AC_DEFUN([GLIBCXX_CHECK_GTHREADS], [
     fi
   fi
 
+  AC_CHECK_HEADER(semaphore.h, [
+    AC_MSG_CHECKING([for POSIX Semaphores and sem_timedwait])
+    AC_TRY_COMPILE([
+	#include <unistd.h>
+	#include <semaphore.h>
+	#include <limits.h>
+      ],
+      [
+	#if !defined _POSIX_TIMEOUTS || _POSIX_TIMEOUTS <= 0
+	# error "POSIX Timeouts option not supported"
+	#elif !defined _POSIX_SEMAPHORES || _POSIX_SEMAPHORES <= 0
+	# error "POSIX Semaphores option not supported"
+	#else
+	#if defined SEM_VALUE_MAX
+	constexpr int sem_value_max = SEM_VALUE_MAX;
+	#elif defined _POSIX_SEM_VALUE_MAX
+	constexpr int sem_value_max = _POSIX_SEM_VALUE_MAX;
+	#else
+	# error "SEM_VALUE_MAX not available"
+	#endif
+	sem_t sem;
+	sem_init(&sem, 0, sem_value_max);
+	struct timespec ts = { 0 };
+	sem_timedwait(&sem, &ts);
+	#endif
+      ],
+      [ac_have_posix_semaphore=yes],
+      [ac_have_posix_semaphore=no])],
+      [ac_have_posix_semaphore=no])
+
+  if test $ac_have_posix_semaphore = yes ; then
+    AC_DEFINE(_GLIBCXX_HAVE_POSIX_SEMAPHORE,
+	      1,
+	      [Define to 1 if POSIX Semaphores with sem_timedwait are available in <semaphore.h>.])
+  fi
+  AC_MSG_RESULT([$ac_have_posix_semaphore])
+
   CXXFLAGS="$ac_save_CXXFLAGS"
   AC_LANG_RESTORE
 ])
diff --git a/libstdc++-v3/config.h.in b/libstdc++-v3/config.h.in
index 8ae3e0fc4bf89143bedcd538ac973104019e540c..72faabfb2c105ff427b43457f86bf796d4143018 100644
--- a/libstdc++-v3/config.h.in
+++ b/libstdc++-v3/config.h.in
@@ -872,6 +872,10 @@
 /* Define if gthreads library is available. */
 #undef _GLIBCXX_HAS_GTHREADS
 
+/* Define to 1 if POSIX Semaphores with sem_timedwait are available in
+   <semaphore.h>. */
+#undef _GLIBCXX_HAVE_POSIX_SEMAPHORE
+
 /* Define to 1 if a full hosted library is built, or 0 if freestanding. */
 #undef _GLIBCXX_HOSTED
 
diff --git a/libstdc++-v3/configure b/libstdc++-v3/configure
index d9ed414cc773bcc5fe3806f545c489948ae0e3a8..a3d08310cf13cf401a66b106fb51caf1057440ba 100755
--- a/libstdc++-v3/configure
+++ b/libstdc++-v3/configure
@@ -76363,6 +76363,64 @@ fi
     fi
   fi
 
+  ac_fn_cxx_check_header_mongrel "$LINENO" "semaphore.h" "ac_cv_header_semaphore_h" "$ac_includes_default"
+if test "x$ac_cv_header_semaphore_h" = xyes; then :
+
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking for POSIX Semaphores and sem_timedwait" >&5
+$as_echo_n "checking for POSIX Semaphores and sem_timedwait... " >&6; }
+    cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+	#include <unistd.h>
+	#include <semaphore.h>
+	#include <limits.h>
+
+int
+main ()
+{
+
+	#if !defined _POSIX_TIMEOUTS || _POSIX_TIMEOUTS <= 0
+	# error "POSIX Timeouts option not supported"
+	#elif !defined _POSIX_SEMAPHORES || _POSIX_SEMAPHORES <= 0
+	# error "POSIX Semaphores option not supported"
+	#else
+	#if defined SEM_VALUE_MAX
+	constexpr int sem_value_max = SEM_VALUE_MAX;
+	#elif defined _POSIX_SEM_VALUE_MAX
+	constexpr int sem_value_max = _POSIX_SEM_VALUE_MAX;
+	#else
+	# error "SEM_VALUE_MAX not available"
+	#endif
+	sem_t sem;
+	sem_init(&sem, 0, sem_value_max);
+	struct timespec ts = { 0 };
+	sem_timedwait(&sem, &ts);
+	#endif
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_cxx_try_compile "$LINENO"; then :
+  ac_have_posix_semaphore=yes
+else
+  ac_have_posix_semaphore=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+else
+  ac_have_posix_semaphore=no
+fi
+
+
+
+  if test $ac_have_posix_semaphore = yes ; then
+
+$as_echo "#define _GLIBCXX_HAVE_POSIX_SEMAPHORE 1" >>confdefs.h
+
+  fi
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_have_posix_semaphore" >&5
+$as_echo "$ac_have_posix_semaphore" >&6; }
+
   CXXFLAGS="$ac_save_CXXFLAGS"
   ac_ext=c
 ac_cpp='$CPP $CPPFLAGS'
diff --git a/libstdc++-v3/include/bits/semaphore_base.h b/libstdc++-v3/include/bits/semaphore_base.h
index 5e29d3783fe1f89172d10d89a60dee8222685673..0692f95f24f21835993ad63e37cdfa02708c56ba 100644
--- a/libstdc++-v3/include/bits/semaphore_base.h
+++ b/libstdc++-v3/include/bits/semaphore_base.h
@@ -39,11 +39,9 @@
 
 #include <ext/numeric_traits.h>
 
-#if __has_include(<semaphore.h>)
+#ifdef _GLIBCXX_HAVE_POSIX_SEMAPHORE
+# include <limits.h>
 # include <semaphore.h>
-# if defined SEM_VALUE_MAX || _POSIX_SEM_VALUE_MAX
-#  define _GLIBCXX_HAVE_POSIX_SEMAPHORE 1
-# endif
 #endif
 
 #include <chrono>