go-util/sync/monitor.go

104 lines
2.8 KiB
Go
Raw Normal View History

2024-12-04 18:02:54 -07:00
package usync
import "sync"
// Monitor guards access to a value. It must not be copied after first use.
type Monitor[T any] struct {
2024-12-04 18:02:54 -07:00
value T
mutex sync.Mutex
}
// NewMonitor creates a new Monitor with the specified value.
func NewMonitor[T any] (value T) Monitor[T] {
return Monitor[T] {
2024-12-04 18:02:54 -07:00
value: value,
}
}
// Set sets the value of the Monitor.
func (this *Monitor[T]) Set (value T) {
2024-12-04 18:02:54 -07:00
this.mutex.Lock()
defer this.mutex.Unlock()
this.value = value
}
// Borrow borrows the value from the Monitor, and returns a function that must
2024-12-04 18:02:54 -07:00
// immediately be deferred, like this:
//
2024-12-29 00:32:13 -07:00
// value, done := monitor.Borrow()
2024-12-04 18:02:54 -07:00
// defer done()
//
// From the time Borrow is called to the time the done function is called, it is
// safe to access the locked object from within the current goroutine.
func (this *Monitor[T]) Borrow () (T, func ()) {
2024-12-04 18:02:54 -07:00
this.mutex.Lock()
return this.value, this.mutex.Unlock
}
2024-12-18 08:53:55 -07:00
2024-12-29 00:32:13 -07:00
// BorrowReturn is like borrow, but returns a "done" function that takes in an
// updated value. The intended use of this function is like this:
//
// value, done := monitor.BorrowReturn()
// defer done(value)
func (this *Monitor[T]) BorrowReturn () (T, func (T)) {
this.mutex.Lock()
return this.value, func (value T) {
defer this.mutex.Unlock()
this.value = value
}
}
// RWMonitor guards separate read/write access to a value.
type RWMonitor[T any] struct {
2024-12-18 08:53:55 -07:00
value T
mutex sync.RWMutex
}
// NewRWMonitor creates a new Monitor with the specified value. It must not be
2024-12-18 08:53:55 -07:00
// copied after first use.
func NewRWMonitor[T any] (value T) RWMonitor[T] {
return RWMonitor[T] {
2024-12-18 08:53:55 -07:00
value: value,
}
}
// Set sets the value of the Monitor.
func (this *RWMonitor[T]) Set (value T) {
2024-12-18 08:53:55 -07:00
this.mutex.Lock()
defer this.mutex.Unlock()
this.value = value
}
// Borrow borrows the value from the Monitor for write access, and returns a
2024-12-18 08:53:55 -07:00
// function that must immediately be deferred, like this:
//
2024-12-29 00:32:13 -07:00
// value, done := monitor.Borrow()
2024-12-18 08:53:55 -07:00
// defer done()
//
// From the time Borrow is called to the time the done function is called, it is
// safe to access the locked object from within the current goroutine.
func (this *RWMonitor[T]) Borrow () (T, func ()) {
2024-12-18 08:53:55 -07:00
this.mutex.Lock()
return this.value, this.mutex.Unlock
}
2024-12-29 00:32:13 -07:00
// BorrowReturn is like borrow, but returns a "done" function that takes in an
// updated value. The intended use of this function is like this:
//
// value, done := monitor.BorrowReturn()
// defer done(value)
func (this *RWMonitor[T]) BorrowReturn () (T, func (T)) {
this.mutex.Lock()
return this.value, func (value T) {
defer this.mutex.Unlock()
this.value = value
}
}
2024-12-18 08:53:55 -07:00
// RBorrow is like Borrow, but returns the item for read access only. Do not
// under any circumstances modify anything returned by this method.
func (this *RWMonitor[T]) RBorrow () (T, func ()) {
2024-12-18 08:53:55 -07:00
this.mutex.Lock()
return this.value, this.mutex.Unlock
}