Skip to content

Commit

Permalink
trylock: Use sync.Mutex in go1.18+
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chappjc committed Dec 8, 2022
1 parent 711abc5 commit db9fd08
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
working-directory: ./cmd/dcrdata

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

- name: Go Tests
env:
Expand Down
8 changes: 6 additions & 2 deletions trylock/trylock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) 2021, The Decred developers
// Copyright (c) 2021-2022, The Decred developers
// See LICENSE for details.

//go:build !go1.18
// +build !go1.18

package trylock

import (
Expand All @@ -11,7 +14,8 @@ import (

// Mutex is a "try lock" for coordinating multiple accessors, while allowing
// only a single updater. It is not very smart about queueing when there are
// multiple callers of Lock waiting.
// multiple callers of Lock waiting. DEPRECATED: sync.Mutex has TryLock
// functionality built-in as of Go 1.18.
type Mutex struct {
mtx sync.Mutex
c chan struct{}
Expand Down
13 changes: 13 additions & 0 deletions trylock/trylock_go1.18.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2022, The Decred developers
// See LICENSE for details.

//go:build go1.18
// +build go1.18

package trylock

import "sync"

// Mutex is just an alias for sync.Mutex in Go 1.18 onward since TryLock() was
// added to the standard library.
type Mutex = sync.Mutex

0 comments on commit db9fd08

Please sign in to comment.