Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 597e3189ca | |||
| eebe3d2179 | |||
| 6e902df516 | |||
| b9369570ae | |||
| a7cb9b50dc |
@@ -1,77 +0,0 @@
|
|||||||
package usync
|
|
||||||
|
|
||||||
import "sync"
|
|
||||||
|
|
||||||
// Locker guards access to a value. It must not be copied after first use.
|
|
||||||
type Locker[T any] struct {
|
|
||||||
value T
|
|
||||||
mutex sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewLocker creates a new locker with the specified value.
|
|
||||||
func NewLocker[T any] (value T) Locker[T] {
|
|
||||||
return Locker[T] {
|
|
||||||
value: value,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets the value of the locker.
|
|
||||||
func (this *Locker[T]) Set (value T) {
|
|
||||||
this.mutex.Lock()
|
|
||||||
defer this.mutex.Unlock()
|
|
||||||
this.value = value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Borrow borrows the value from the locker, and returns a function that must
|
|
||||||
// immediately be deferred, like this:
|
|
||||||
//
|
|
||||||
// value, done := locker.Borrow()
|
|
||||||
// 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 *Locker[T]) Borrow () (T, func ()) {
|
|
||||||
this.mutex.Lock()
|
|
||||||
return this.value, this.mutex.Unlock
|
|
||||||
}
|
|
||||||
|
|
||||||
// RWLocker guards separate read/write access to a value.
|
|
||||||
type RWLocker[T any] struct {
|
|
||||||
value T
|
|
||||||
mutex sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRWLocker creates a new locker with the specified value. It must not be
|
|
||||||
// copied after first use.
|
|
||||||
func NewRWLocker[T any] (value T) RWLocker[T] {
|
|
||||||
return RWLocker[T] {
|
|
||||||
value: value,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets the value of the locker.
|
|
||||||
func (this *RWLocker[T]) Set (value T) {
|
|
||||||
this.mutex.Lock()
|
|
||||||
defer this.mutex.Unlock()
|
|
||||||
this.value = value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Borrow borrows the value from the locker for write access, and returns a
|
|
||||||
// function that must immediately be deferred, like this:
|
|
||||||
//
|
|
||||||
// value, done := locker.Borrow()
|
|
||||||
// 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 *RWLocker[T]) Borrow () (T, func ()) {
|
|
||||||
this.mutex.Lock()
|
|
||||||
return this.value, this.mutex.Unlock
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
func (this *RWLocker[T]) RBorrow () (T, func ()) {
|
|
||||||
this.mutex.Lock()
|
|
||||||
return this.value, this.mutex.Unlock
|
|
||||||
}
|
|
||||||
103
sync/monitor.go
Normal file
103
sync/monitor.go
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
package usync
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
// Monitor guards access to a value. It must not be copied after first use.
|
||||||
|
type Monitor[T any] struct {
|
||||||
|
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] {
|
||||||
|
value: value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set sets the value of the Monitor.
|
||||||
|
func (this *Monitor[T]) Set (value T) {
|
||||||
|
this.mutex.Lock()
|
||||||
|
defer this.mutex.Unlock()
|
||||||
|
this.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Borrow borrows the value from the Monitor, and returns a function that must
|
||||||
|
// immediately be deferred, like this:
|
||||||
|
//
|
||||||
|
// value, done := monitor.Borrow()
|
||||||
|
// 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 ()) {
|
||||||
|
this.mutex.Lock()
|
||||||
|
return this.value, this.mutex.Unlock
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
value T
|
||||||
|
mutex sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRWMonitor creates a new Monitor with the specified value. It must not be
|
||||||
|
// copied after first use.
|
||||||
|
func NewRWMonitor[T any] (value T) RWMonitor[T] {
|
||||||
|
return RWMonitor[T] {
|
||||||
|
value: value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set sets the value of the Monitor.
|
||||||
|
func (this *RWMonitor[T]) Set (value T) {
|
||||||
|
this.mutex.Lock()
|
||||||
|
defer this.mutex.Unlock()
|
||||||
|
this.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Borrow borrows the value from the Monitor for write access, and returns a
|
||||||
|
// function that must immediately be deferred, like this:
|
||||||
|
//
|
||||||
|
// value, done := monitor.Borrow()
|
||||||
|
// 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 ()) {
|
||||||
|
this.mutex.Lock()
|
||||||
|
return this.value, this.mutex.Unlock
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 ()) {
|
||||||
|
this.mutex.Lock()
|
||||||
|
return this.value, this.mutex.Unlock
|
||||||
|
}
|
||||||
90
sync/monitor_test.go
Normal file
90
sync/monitor_test.go
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package usync
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
import "math/rand"
|
||||||
|
|
||||||
|
func TestMonitor (test *testing.T) {
|
||||||
|
mon := NewMonitor(9)
|
||||||
|
func () {
|
||||||
|
value, done := mon.Borrow()
|
||||||
|
defer done()
|
||||||
|
test.Log(value)
|
||||||
|
if value != 9 { test.Fatal("not equal") }
|
||||||
|
} ()
|
||||||
|
func () {
|
||||||
|
value, done := mon.BorrowReturn()
|
||||||
|
value += 3
|
||||||
|
defer done(value)
|
||||||
|
} ()
|
||||||
|
func () {
|
||||||
|
value, done := mon.Borrow()
|
||||||
|
defer done()
|
||||||
|
test.Log(value)
|
||||||
|
if value != 12 { test.Fatal("not equal") }
|
||||||
|
} ()
|
||||||
|
mon.Set(11)
|
||||||
|
func () {
|
||||||
|
value, done := mon.Borrow()
|
||||||
|
defer done()
|
||||||
|
test.Log(value)
|
||||||
|
if value != 11 { test.Fatal("not equal") }
|
||||||
|
} ()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMonitorConcurrent (test *testing.T) {
|
||||||
|
mon := NewMonitor(map[int] int { })
|
||||||
|
for index := 0; index < 16; index ++ {
|
||||||
|
go func () {
|
||||||
|
for index := 0; index < 8192; index ++ {
|
||||||
|
func () {
|
||||||
|
value, done := mon.Borrow()
|
||||||
|
defer done()
|
||||||
|
value[rand.Int()] = rand.Int()
|
||||||
|
} ()
|
||||||
|
}
|
||||||
|
} ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRWMonitor (test *testing.T) {
|
||||||
|
mon := NewRWMonitor(9)
|
||||||
|
func () {
|
||||||
|
value, done := mon.Borrow()
|
||||||
|
defer done()
|
||||||
|
test.Log(value)
|
||||||
|
if value != 9 { test.Fatal("not equal") }
|
||||||
|
} ()
|
||||||
|
func () {
|
||||||
|
value, done := mon.BorrowReturn()
|
||||||
|
value += 3
|
||||||
|
defer done(value)
|
||||||
|
} ()
|
||||||
|
func () {
|
||||||
|
value, done := mon.RBorrow()
|
||||||
|
defer done()
|
||||||
|
test.Log(value)
|
||||||
|
if value != 12 { test.Fatal("not equal") }
|
||||||
|
} ()
|
||||||
|
mon.Set(11)
|
||||||
|
func () {
|
||||||
|
value, done := mon.RBorrow()
|
||||||
|
defer done()
|
||||||
|
test.Log(value)
|
||||||
|
if value != 11 { test.Fatal("not equal") }
|
||||||
|
} ()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRWMonitorConcurrent (test *testing.T) {
|
||||||
|
mon := NewRWMonitor(map[int] int { })
|
||||||
|
for index := 0; index < 16; index ++ {
|
||||||
|
go func () {
|
||||||
|
for index := 0; index < 8192; index ++ {
|
||||||
|
func () {
|
||||||
|
value, done := mon.Borrow()
|
||||||
|
defer done()
|
||||||
|
value[rand.Int()] = rand.Int()
|
||||||
|
} ()
|
||||||
|
}
|
||||||
|
} ()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user