Skip to content

Commit 288fcb8

Browse files
committed
Use standard library sync.Mutex.TryLock.
The standard library sync.Mutex has included a TryLock method since Go 1.18. There is no longer a need to implement a parallel version with unsafe. Using the standard library version reduces the ongoing maintenance cost of this package. https://pkg.go.dev/sync#Mutex.TryLock https://tip.golang.org/doc/go1.18#minor_library_changes
1 parent bf8062b commit 288fcb8

File tree

4 files changed

+5
-105
lines changed

4 files changed

+5
-105
lines changed

.github/workflows/go.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
fetch-depth: 0
4646
- uses: actions/setup-go@v2
4747
with:
48-
go-version: 1.17
48+
go-version: 1.18
4949
- run: tools/go_branch.sh
5050
- run: git checkout go && git clean -xf . && go build ./...
5151
- if: github.event_name == 'push'

pkg/sync/BUILD

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ go_test(
4242
size = "small",
4343
srcs = [
4444
"gate_test.go",
45-
"mutex_test.go",
4645
"rwmutex_test.go",
4746
"seqcount_test.go",
4847
],

pkg/sync/mutex_test.go

-72
This file was deleted.

pkg/sync/mutex_unsafe.go

+4-31
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,35 @@
33
// Use of this source code is governed by a BSD-style
44
// license that can be found in the LICENSE file.
55

6-
//go:build go1.13 && !go1.21
7-
// +build go1.13,!go1.21
8-
9-
// When updating the build constraint (above), check that syncMutex matches the
10-
// standard library sync.Mutex definition.
11-
126
package sync
137

148
import (
159
"sync"
16-
"sync/atomic"
1710
"unsafe"
1811
)
1912

2013
// CrossGoroutineMutex is equivalent to Mutex, but it need not be unlocked by a
2114
// the same goroutine that locked the mutex.
2215
type CrossGoroutineMutex struct {
23-
sync.Mutex
24-
}
25-
26-
type syncMutex struct {
27-
state int32
28-
sema uint32
29-
}
30-
31-
func (m *CrossGoroutineMutex) state() *int32 {
32-
return &(*syncMutex)(unsafe.Pointer(&m.Mutex)).state
16+
m sync.Mutex
3317
}
3418

3519
// Lock locks the underlying Mutex.
3620
// +checklocksignore
3721
func (m *CrossGoroutineMutex) Lock() {
38-
m.Mutex.Lock()
22+
m.m.Lock()
3923
}
4024

4125
// Unlock unlocks the underlying Mutex.
4226
// +checklocksignore
4327
func (m *CrossGoroutineMutex) Unlock() {
44-
m.Mutex.Unlock()
28+
m.m.Unlock()
4529
}
4630

47-
const (
48-
mutexUnlocked = 0
49-
mutexLocked = 1
50-
)
51-
5231
// TryLock tries to acquire the mutex. It returns true if it succeeds and false
5332
// otherwise. TryLock does not block.
5433
func (m *CrossGoroutineMutex) TryLock() bool {
55-
if atomic.CompareAndSwapInt32(m.state(), mutexUnlocked, mutexLocked) {
56-
if RaceEnabled {
57-
RaceAcquire(unsafe.Pointer(&m.Mutex))
58-
}
59-
return true
60-
}
61-
return false
34+
return m.m.TryLock()
6235
}
6336

6437
// Mutex is a mutual exclusion lock. The zero value for a Mutex is an unlocked

0 commit comments

Comments
 (0)