sync: Rename Locker to Monitor

Locker is an interface type in the stdlib sync package.
This commit is contained in:
Sasha Koshka 2024-12-22 19:11:50 -05:00
parent 58052e8679
commit a7cb9b50dc

View File

@ -2,76 +2,76 @@ package usync
import "sync" import "sync"
// Locker guards access to a value. It must not be copied after first use. // Monitor guards access to a value. It must not be copied after first use.
type Locker[T any] struct { type Monitor[T any] struct {
value T value T
mutex sync.Mutex mutex sync.Mutex
} }
// NewLocker creates a new locker with the specified value. // NewMonitor creates a new Monitor with the specified value.
func NewLocker[T any] (value T) Locker[T] { func NewMonitor[T any] (value T) Monitor[T] {
return Locker[T] { return Monitor[T] {
value: value, value: value,
} }
} }
// Set sets the value of the locker. // Set sets the value of the Monitor.
func (this *Locker[T]) Set (value T) { func (this *Monitor[T]) Set (value T) {
this.mutex.Lock() this.mutex.Lock()
defer this.mutex.Unlock() defer this.mutex.Unlock()
this.value = value this.value = value
} }
// Borrow borrows the value from the locker, and returns a function that must // Borrow borrows the value from the Monitor, and returns a function that must
// immediately be deferred, like this: // immediately be deferred, like this:
// //
// value, done := locker.Borrow() // value, done := Monitor.Borrow()
// defer done() // defer done()
// //
// From the time Borrow is called to the time the done function is called, it is // 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. // safe to access the locked object from within the current goroutine.
func (this *Locker[T]) Borrow () (T, func ()) { func (this *Monitor[T]) Borrow () (T, func ()) {
this.mutex.Lock() this.mutex.Lock()
return this.value, this.mutex.Unlock return this.value, this.mutex.Unlock
} }
// RWLocker guards separate read/write access to a value. // RWMonitor guards separate read/write access to a value.
type RWLocker[T any] struct { type RWMonitor[T any] struct {
value T value T
mutex sync.RWMutex mutex sync.RWMutex
} }
// NewRWLocker creates a new locker with the specified value. It must not be // NewRWMonitor creates a new Monitor with the specified value. It must not be
// copied after first use. // copied after first use.
func NewRWLocker[T any] (value T) RWLocker[T] { func NewRWMonitor[T any] (value T) RWMonitor[T] {
return RWLocker[T] { return RWMonitor[T] {
value: value, value: value,
} }
} }
// Set sets the value of the locker. // Set sets the value of the Monitor.
func (this *RWLocker[T]) Set (value T) { func (this *RWMonitor[T]) Set (value T) {
this.mutex.Lock() this.mutex.Lock()
defer this.mutex.Unlock() defer this.mutex.Unlock()
this.value = value this.value = value
} }
// Borrow borrows the value from the locker for write access, and returns a // Borrow borrows the value from the Monitor for write access, and returns a
// function that must immediately be deferred, like this: // function that must immediately be deferred, like this:
// //
// value, done := locker.Borrow() // value, done := Monitor.Borrow()
// defer done() // defer done()
// //
// From the time Borrow is called to the time the done function is called, it is // 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. // safe to access the locked object from within the current goroutine.
func (this *RWLocker[T]) Borrow () (T, func ()) { func (this *RWMonitor[T]) Borrow () (T, func ()) {
this.mutex.Lock() this.mutex.Lock()
return this.value, this.mutex.Unlock return this.value, this.mutex.Unlock
} }
// RBorrow is like Borrow, but returns the item for read access only. Do not // RBorrow is like Borrow, but returns the item for read access only. Do not
// under any circumstances write to an item returned from this method. // under any circumstances modify anything returned by this method.
func (this *RWLocker[T]) RBorrow () (T, func ()) { func (this *RWMonitor[T]) RBorrow () (T, func ()) {
this.mutex.Lock() this.mutex.Lock()
return this.value, this.mutex.Unlock return this.value, this.mutex.Unlock
} }