From db9fd080b1c23ad83a0cc9a8567ba9f9c8e99ed8 Mon Sep 17 00:00:00 2001 From: Jonathan Chappelow Date: Tue, 14 Dec 2021 22:51:33 -0600 Subject: [PATCH] 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. --- .github/workflows/build.yml | 2 +- trylock/trylock.go | 8 ++++++-- trylock/trylock_go1.18.go | 13 +++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 trylock/trylock_go1.18.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2df19b9cd..ce63ce42c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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: diff --git a/trylock/trylock.go b/trylock/trylock.go index 1dcaaa1a9..063df6a59 100644 --- a/trylock/trylock.go +++ b/trylock/trylock.go @@ -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 ( @@ -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{} diff --git a/trylock/trylock_go1.18.go b/trylock/trylock_go1.18.go new file mode 100644 index 000000000..e3caf25f8 --- /dev/null +++ b/trylock/trylock_go1.18.go @@ -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