[v2,3/6,gdb/testsuite] Simplify lock_file_acquire

Message ID 20260929110921.1910094-3-tdevries@suse.de
State New
Headers
Series [v2,1/6,gdb/testsuite] Add gdb.testsuite/lock.exp |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed

Commit Message

Tom de Vries Sept. 29, 2026, 11:09 a.m. UTC
  Currently the entire functionality of lock_file_acquire is contained in a
loop, which makes control flow harder to follow, especially so given that the
loop also contains exception handling.

Instead, limit the scope of the loop to the part that actually needs
repeating: getting a valid file handle:
...
    while {![info exists fh]} {
	try {
	    set fh [open $lockfile {WRONLY CREAT EXCL}]
	} on error {} {
	    # Ignore and try again.
	    after 10
	}
    }
...

I also liked this version:
...
    while {true} {
	try {
	    set fh [open $lockfile {WRONLY CREAT EXCL}]
	    break
	} on error {} {
	    # Ignore and try again.
	    after 10
	    continue
	}
    }
...
but ended up choosing for the first because the loop condition states the goal
of the loop.
---
 gdb/testsuite/lib/gdb-utils.exp | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
  

Patch

diff --git a/gdb/testsuite/lib/gdb-utils.exp b/gdb/testsuite/lib/gdb-utils.exp
index 56d8e80f152..854496e9bb1 100644
--- a/gdb/testsuite/lib/gdb-utils.exp
+++ b/gdb/testsuite/lib/gdb-utils.exp
@@ -185,22 +185,24 @@  proc version_compare { l1 op l2 } {
 
 proc lock_file_acquire {lockfile} {
     verbose -log "acquiring lock file: $::subdir/${::gdb_test_file_name}.exp"
-    while {true} {
+
+    while {![info exists fh]} {
 	try {
-	    open $lockfile {WRONLY CREAT EXCL}
-	} on ok {fh} {
-	    set msg "locked by $::subdir/${::gdb_test_file_name}.exp"
-	    verbose -log "lock file: $msg"
-	    # For debugging, put info in the lockfile about who owns
-	    # it.
-	    puts $fh $msg
-	    flush $fh
-	    return [list $fh $lockfile]
+	    set fh [open $lockfile {WRONLY CREAT EXCL}]
 	} on error {} {
 	    # Ignore and try again.
+	    after 10
 	}
-	after 10
     }
+
+    set msg "locked by $::subdir/${::gdb_test_file_name}.exp"
+    verbose -log "lock file: $msg"
+
+    # For debugging, put info in the lockfile about who owns
+    # it.
+    puts $fh $msg
+    flush $fh
+    return [list $fh $lockfile]
 }
 
 # Release a lock file.