diff --git a/gcc/doc/install.texi b/gcc/doc/install.texi
index 928fc6b6d4b0d24de3bb2d94a0aa7e74b406685f..b30d3691fe6fc7d5bc38357ea67c79f49a982837 100644
--- a/gcc/doc/install.texi
+++ b/gcc/doc/install.texi
@@ -382,6 +382,9 @@ tables.
 
 Used by @command{automake}.
 
+If available, enables parallel testing of @samp{libgomp} in case that
+@command{flock} is not available.
+
 @end table
 
 Several support libraries are necessary to build GCC, some are required,
diff --git a/libgomp/configure b/libgomp/configure
index 489c9a00421711d48a3c0a4bbe0cc9809bb6a7a6..ed18809ff1cef67dcc675fa6a895831cd97c445c 100755
--- a/libgomp/configure
+++ b/libgomp/configure
@@ -16487,6 +16487,8 @@ $as_echo "unable to detect (assuming 1)" >&6; }
 fi
 
 
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for flock implementation" >&5
+$as_echo "$as_me: checking for flock implementation" >&6;}
 for ac_prog in flock
 do
   # Extract the first word of "$ac_prog", so it can be a program name with args.
@@ -16529,6 +16531,46 @@ fi
   test -n "$FLOCK" && break
 done
 
+# Fallback if 'perl' is available.
+if test -z "$FLOCK"; then
+  # Extract the first word of "perl", so it can be a program name with args.
+set dummy perl; ac_word=$2
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
+$as_echo_n "checking for $ac_word... " >&6; }
+if ${ac_cv_prog_FLOCK+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  if test -n "$FLOCK"; then
+  ac_cv_prog_FLOCK="$FLOCK" # Let the user override the test.
+else
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+    for ac_exec_ext in '' $ac_executable_extensions; do
+  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_prog_FLOCK="$srcdir/testsuite/flock"
+    $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+  done
+IFS=$as_save_IFS
+
+fi
+fi
+FLOCK=$ac_cv_prog_FLOCK
+if test -n "$FLOCK"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $FLOCK" >&5
+$as_echo "$FLOCK" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
+
+fi
 
 # Get target configury.
 . ${srcdir}/configure.tgt
diff --git a/libgomp/configure.ac b/libgomp/configure.ac
index 0fab7168cfac632195beef44e8e7b3c5cf16d71a..cc96e5b753b4df4229d42ac6f5ddda916f75faa6 100644
--- a/libgomp/configure.ac
+++ b/libgomp/configure.ac
@@ -339,7 +339,12 @@ fi
 AX_COUNT_CPUS
 AC_SUBST(CPU_COUNT)
 
+AC_MSG_NOTICE([checking for flock implementation])
 AC_CHECK_PROGS(FLOCK, flock)
+# Fallback if 'perl' is available.
+if test -z "$FLOCK"; then
+  AC_CHECK_PROG(FLOCK, perl, $srcdir/testsuite/flock)
+fi
 
 # Get target configury.
 . ${srcdir}/configure.tgt
diff --git a/libgomp/testsuite/flock b/libgomp/testsuite/flock
new file mode 100755
index 0000000000000000000000000000000000000000..71878b104f1dfa4881531e7f321c84997b2c2fb4
--- /dev/null
+++ b/libgomp/testsuite/flock
@@ -0,0 +1,17 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+# Only arguments '--exclusive 1' exactly are supported.
+(@ARGV == 2) or die;
+my $mode = shift;
+($mode eq "--exclusive") or die;
+my $fd = shift;
+($fd eq "1") or die;
+
+use Fcntl ':flock';
+
+open(my $fh, '>&=', 1) or die "open: $!";
+
+flock($fh, LOCK_EX) or die "flock: $!";
diff --git a/libgomp/testsuite/lib/libgomp.exp b/libgomp/testsuite/lib/libgomp.exp
index 8512f2c1e64d58f0fe031192712961eef1c9ac12..70e9b85eb9a69eef5022a95bc54ebf6eafbc987a 100644
--- a/libgomp/testsuite/lib/libgomp.exp
+++ b/libgomp/testsuite/lib/libgomp.exp
@@ -308,12 +308,14 @@ if ![info exists ::env(GCC_RUNTEST_PARALLELIZE_DIR)] {
     rename libgomp_load standard_libgomp_load
     proc libgomp_load { program args } {
 	# ... in order to serialize execution testing via an exclusive lock.
+	# We use stdout, as per <https://perldoc.perl.org/functions/flock>
+	# "[...] FILEHANDLE [...] be open with write intent to use LOCK_EX".
 	set lock_file ../lock
 	set lock_kind --exclusive
 	set lock_fd [open $lock_file a+]
 	set lock_clock_begin [clock seconds]
 	global FLOCK
-	exec $FLOCK $lock_kind 0 <@ $lock_fd
+	exec $FLOCK $lock_kind 1 >@ $lock_fd
 	set lock_clock_end [clock seconds]
 	verbose -log "Got ${FLOCK}('$lock_file', '$lock_kind') at [clock format $lock_clock_end] after [expr $lock_clock_end - $lock_clock_begin] s" 2