Skip to content

Commit 7db6f74

Browse files
committed
trylock: Use sync.Mutex in go1.18+
Go 1.18 supplements Mutex with a TryLock method. Use this directly instead of our dumb homebrew trylock mutex when Go 1.18 is in use.
1 parent 611b40a commit 7db6f74

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
working-directory: ./cmd/dcrdata
2525

2626
- name: Install Linters
27-
run: "curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.48.0"
27+
run: "curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.50.1"
2828

2929
- name: Go Tests
3030
env:

trylock/trylock.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
// Copyright (c) 2021, The Decred developers
1+
// Copyright (c) 2021-2022, The Decred developers
22
// See LICENSE for details.
33

4+
//go:build !go1.18
5+
// +build !go1.18
6+
47
package trylock
58

69
import (
@@ -11,7 +14,8 @@ import (
1114

1215
// Mutex is a "try lock" for coordinating multiple accessors, while allowing
1316
// only a single updater. It is not very smart about queueing when there are
14-
// multiple callers of Lock waiting.
17+
// multiple callers of Lock waiting. DEPRECATED: sync.Mutex has TryLock
18+
// functionality built-in as of Go 1.18.
1519
type Mutex struct {
1620
mtx sync.Mutex
1721
c chan struct{}

trylock/trylock_go1.18.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2022, The Decred developers
2+
// See LICENSE for details.
3+
4+
//go:build go1.18
5+
// +build go1.18
6+
7+
package trylock
8+
9+
import "sync"
10+
11+
// Mutex is just an alias for sync.Mutex in Go 1.18 onward since TryLock() was
12+
// added to the standard library.
13+
type Mutex = sync.Mutex

0 commit comments

Comments
 (0)