Skip to content

Commit 8c896aa

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/lockedfile: add a sync.Mutex to lockedfile.Mutex
The compiler (and race detector) don't interpret locking a file as a synchronization operation, so we add an explicit (and redundant) sync.Mutex to make that property clear. The additional synchronization makes it safe to parallelize the tests in cmd/go/internal/modfetch/coderepo_test.go, which cuts the wall time of that test by around 50%. Updates #30550 Change-Id: Ief3479020ebf9e0fee524a4aae5568697727c683 Reviewed-on: https://go-review.googlesource.com/c/go/+/170597 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent b0bcd7a commit 8c896aa

File tree

2 files changed

+180
-152
lines changed

2 files changed

+180
-152
lines changed

src/cmd/go/internal/lockedfile/mutex.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package lockedfile
77
import (
88
"fmt"
99
"os"
10+
"sync"
1011
)
1112

1213
// A Mutex provides mutual exclusion within and across processes by locking a
@@ -21,7 +22,8 @@ import (
2122
// must not be copied after first use. The Path field must be set before first
2223
// use and must not be change thereafter.
2324
type Mutex struct {
24-
Path string // The path to the well-known lock file. Must be non-empty.
25+
Path string // The path to the well-known lock file. Must be non-empty.
26+
mu sync.Mutex // A redundant mutex. The race detector doesn't know about file locking, so in tests we may need to lock something that it understands.
2527
}
2628

2729
// MutexAt returns a new Mutex with Path set to the given non-empty path.
@@ -56,5 +58,10 @@ func (mu *Mutex) Lock() (unlock func(), err error) {
5658
if err != nil {
5759
return nil, err
5860
}
59-
return func() { f.Close() }, nil
61+
mu.mu.Lock()
62+
63+
return func() {
64+
mu.mu.Unlock()
65+
f.Close()
66+
}, nil
6067
}

0 commit comments

Comments
 (0)