diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:23:18 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:23:18 +0000 |
commit | 43a123c1ae6613b3efeed291fa552ecd909d3acf (patch) | |
tree | fd92518b7024bc74031f78a1cf9e454b65e73665 /src/os/file_mutex_plan9.go | |
parent | Initial commit. (diff) | |
download | golang-1.20-43a123c1ae6613b3efeed291fa552ecd909d3acf.tar.xz golang-1.20-43a123c1ae6613b3efeed291fa552ecd909d3acf.zip |
Adding upstream version 1.20.14.upstream/1.20.14upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/os/file_mutex_plan9.go')
-rw-r--r-- | src/os/file_mutex_plan9.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/os/file_mutex_plan9.go b/src/os/file_mutex_plan9.go new file mode 100644 index 0000000..26bf5a7 --- /dev/null +++ b/src/os/file_mutex_plan9.go @@ -0,0 +1,70 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package os + +// File locking support for Plan 9. This uses fdMutex from the +// internal/poll package. + +// incref adds a reference to the file. It returns an error if the file +// is already closed. This method is on File so that we can incorporate +// a nil test. +func (f *File) incref(op string) (err error) { + if f == nil { + return ErrInvalid + } + if !f.fdmu.Incref() { + err = ErrClosed + if op != "" { + err = &PathError{Op: op, Path: f.name, Err: err} + } + } + return err +} + +// decref removes a reference to the file. If this is the last +// remaining reference, and the file has been marked to be closed, +// then actually close it. +func (file *file) decref() error { + if file.fdmu.Decref() { + return file.destroy() + } + return nil +} + +// readLock adds a reference to the file and locks it for reading. +// It returns an error if the file is already closed. +func (file *file) readLock() error { + if !file.fdmu.ReadLock() { + return ErrClosed + } + return nil +} + +// readUnlock removes a reference from the file and unlocks it for reading. +// It also closes the file if it marked as closed and there is no remaining +// reference. +func (file *file) readUnlock() { + if file.fdmu.ReadUnlock() { + file.destroy() + } +} + +// writeLock adds a reference to the file and locks it for writing. +// It returns an error if the file is already closed. +func (file *file) writeLock() error { + if !file.fdmu.WriteLock() { + return ErrClosed + } + return nil +} + +// writeUnlock removes a reference from the file and unlocks it for writing. +// It also closes the file if it is marked as closed and there is no remaining +// reference. +func (file *file) writeUnlock() { + if file.fdmu.WriteUnlock() { + file.destroy() + } +} |