github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/lockedfile/mutex.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package lockedfile 6 7 import ( 8 "github.com/shogo82148/std/sync" 9 ) 10 11 // A Mutex provides mutual exclusion within and across processes by locking a 12 // well-known file. Such a file generally guards some other part of the 13 // filesystem: for example, a Mutex file in a directory might guard access to 14 // the entire tree rooted in that directory. 15 // 16 // Mutex does not implement sync.Locker: unlike a sync.Mutex, a lockedfile.Mutex 17 // can fail to lock (e.g. if there is a permission error in the filesystem). 18 // 19 // Like a sync.Mutex, a Mutex may be included as a field of a larger struct but 20 // must not be copied after first use. The Path field must be set before first 21 // use and must not be change thereafter. 22 type Mutex struct { 23 Path string 24 mu sync.Mutex 25 } 26 27 // MutexAt returns a new Mutex with Path set to the given non-empty path. 28 func MutexAt(path string) *Mutex 29 30 func (mu *Mutex) String() string 31 32 // Lock attempts to lock the Mutex. 33 // 34 // If successful, Lock returns a non-nil unlock function: it is provided as a 35 // return-value instead of a separate method to remind the caller to check the 36 // accompanying error. (See https://golang.org/issue/20803.) 37 func (mu *Mutex) Lock() (unlock func(), err error)